fg电池监控平台的达梦数据库版本
whycxzp
2024-11-12 92ba7a968c67e8c8a64efea1759929649189f390
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.whyc.service;
 
import com.whyc.dto.Response;
import com.whyc.dto.weather.Weather;
import com.whyc.dto.weather.WeatherInfo;
import com.whyc.dto.weather.WeatherInfo2;
import com.whyc.mapper.WeatherCityMapper;
import com.whyc.pojo.WeatherCity;
import com.whyc.util.ActionUtil;
import com.whyc.util.HttpUtil;
import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
 
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.*;
 
import static com.whyc.util.HttpUtil.urlencode;
 
@Service
public class WeatherService {
 
    /**属于第三方-聚合的天气接口*/
    // 天气情况查询接口地址
    private static String API_URL = "http://apis.juhe.cn/simpleWeather/query";
    // 接口请求Key
    private static String API_KEY = "d88bb2c73f4fa5d1c0fee1df0b5d108a";
 
    /**属于第三方-百度的天气接口*/
    private static String API_URL_BAIDU = "https://api.map.baidu.com/weather/v1/";
    private static String API_KEY_BAIDU = "tcnlFtekhszWP65H14Mzvhd10uwZVW6G";
 
    @Resource
    private WeatherCityMapper weatherCityMapper;
 
    /**百度天气查询使用*/
    public Response get2(WeatherCity city) {
        Response model = new Response();
 
        //调试用
        //city.setId(1);
        List<WeatherInfo2> futureList = new LinkedList<>();
        WeatherInfo realtime = new WeatherInfo();
 
        //根据传过来的省市区,定位到区码cityId
        Integer cityId = getId2(city);
        if(cityId==0){
            model.setII(0,"暂不支持当前城市天气查询");
        }else {
            //首先从ApplicationContext中获取,如果不存在,则请求第三方接口,同时将值存入
            ServletContext application = ActionUtil.getApplication();
            List<Weather> weatherList = (List<Weather>) application.getAttribute("weather");
 
 
            if (weatherList == null) {
                weatherList = new ArrayList<>();
                /*//调试
                Weather weather = new Weather();
                weather.setId(2);
                weather.setRealtime(new WeatherInfo());
                weatherList.add(weather);*/
            }
            //List<Weather> weatherList = new ArrayList<>();
            //weatherList.addAll(weatherList2);
 
            if (weatherList.size() == 0) {
                Map<String, Object> params = new HashMap<>();//组合参数
                params.put("district_id", cityId);
                params.put("data_type", "all");
                params.put("ak", API_KEY_BAIDU);
                String queryParams = urlencode(params);
                String response = HttpUtil.doGet(API_URL_BAIDU, queryParams);
                try {
                    JSONObject jsonObject = JSONObject.fromObject(response);
                    int error_code = jsonObject.getInt("status");
                    if (error_code == 0) {
 
                        JSONObject result = jsonObject.getJSONObject("result");
                        JSONObject realtimeJson = result.getJSONObject("now");
                        JSONArray futureJsonArr = result.getJSONArray("forecasts");
                        JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[]{"yyyy-MM-dd", "yyyy-MM-dd"}));
                        realtime.setTemperature(String.valueOf(realtimeJson.getInt("temp")));
                        realtime.setPower(realtimeJson.getString("wind_class"));
                        realtime.setDirect(realtimeJson.getString("wind_dir"));
                        realtime.setInfo(realtimeJson.getString("text"));
                        for (int i = 0; i < futureJsonArr.size(); i++) {
 
                            WeatherInfo2 futureInfo = new WeatherInfo2();
 
                            JSONObject futureJson = futureJsonArr.getJSONObject(i);
                            futureInfo.setTemperatureHigh(futureJson.getInt("high"));
                            futureInfo.setTemperatureLow(futureJson.getInt("low"));
                            futureInfo.setPowerDay(futureJson.getString("wc_day"));
                            futureInfo.setPowerNight(futureJson.getString("wc_night"));
                            futureInfo.setDirectDay(futureJson.getString("wd_day"));
                            futureInfo.setDirectNight(futureJson.getString("wd_night"));
                            futureInfo.setInfoDay(futureJson.getString("text_day"));
                            futureInfo.setInfoNight(futureJson.getString("text_night"));
                            futureInfo.setWeek(futureJson.getString("week"));
                            futureInfo.setDate(futureJson.getString("date"));
 
                            futureList.add(futureInfo);
                        }
                        model.setCode(1);
                        model.setData(realtime);
                        model.setData2(futureList);
                        System.out.println("调用第三方接口取,并存入内存");
                        //将值存入到application中
                        Weather weatherTemp = new Weather();
                        weatherTemp.setId(cityId);
                        weatherTemp.setRealtime(realtime);
                        weatherTemp.setFuture2(futureList);
                        weatherList.add(weatherTemp);
                        application.setAttribute("weather", weatherList);
                    } else {
                        model.setMsg("天气查询接口失败:" + jsonObject.getString("message"));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    model.setMsg("接口异常");
                }
            } else {
                for (int i = 0; i < weatherList.size(); i++) {
                    if(weatherList.get(i).getId().intValue()==cityId){
                        model.setCode(1);
                        model.setData(weatherList.get(i).getRealtime());
                        model.setData2(weatherList.get(i).getFuture2());
                        System.out.println("从内存中取");
                        return model;
                    }
 
                }
 
                Map<String, Object> params = new HashMap<>();//组合参数
                params.put("district_id", cityId);
                params.put("data_type", "all");
                params.put("ak", API_KEY_BAIDU);
                String queryParams = urlencode(params);
                String response = HttpUtil.doGet(API_URL_BAIDU, queryParams);
                try {
                    JSONObject jsonObject = JSONObject.fromObject(response);
                    int error_code = jsonObject.getInt("status");
                    if (error_code == 0) {
                        JSONObject result = jsonObject.getJSONObject("result");
                        JSONObject realtimeJson = result.getJSONObject("now");
                        JSONArray futureJsonArr = result.getJSONArray("forecasts");
                        JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[]{"yyyy-MM-dd", "yyyy-MM-dd"}));
                        realtime.setTemperature(String.valueOf(realtimeJson.getInt("temp")));
                        realtime.setPower(realtimeJson.getString("wind_class"));
                        realtime.setDirect(realtimeJson.getString("wind_dir"));
                        realtime.setInfo(realtimeJson.getString("text"));
                        for (int i = 0; i < futureJsonArr.size(); i++) {
 
                            WeatherInfo2 futureInfo = new WeatherInfo2();
 
                            JSONObject futureJson = futureJsonArr.getJSONObject(i);
                            futureInfo.setTemperatureHigh(futureJson.getInt("high"));
                            futureInfo.setTemperatureLow(futureJson.getInt("low"));
                            futureInfo.setPowerDay(futureJson.getString("wc_day"));
                            futureInfo.setPowerNight(futureJson.getString("wc_night"));
                            futureInfo.setDirectDay(futureJson.getString("wd_day"));
                            futureInfo.setDirectNight(futureJson.getString("wd_night"));
                            futureInfo.setInfoDay(futureJson.getString("text_day"));
                            futureInfo.setInfoNight(futureJson.getString("text_night"));
                            futureInfo.setWeek(futureJson.getString("week"));
                            futureInfo.setDate(futureJson.getString("date"));
 
                            futureList.add(futureInfo);
                        }
                        model.setCode(1);
                        model.setData(realtime);
                        model.setData2(futureList);
                        System.out.println("调用第三方接口取,并存入内存");
                        //将值存入到application中
                        Weather weatherTemp = new Weather();
                        weatherTemp.setId(cityId);
                        weatherTemp.setRealtime(realtime);
                        weatherTemp.setFuture2(futureList);
                        weatherList.add(weatherTemp);
                        application.setAttribute("weather", weatherList);
                    } else {
                        model.setMsg("天气查询接口失败:" + jsonObject.getString("message"));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    model.setMsg("接口异常");
                }
            }
        }
 
        return model;
    }
    
 
    public Integer getId2(WeatherCity city){
        WeatherCity weatherCity = weatherCityMapper.getWeatherCity2(city);
        //当前区不存在,则查询上一级市的
        if (weatherCity==null){
            city.setDistrict(city.getCity().replace("市",""));
            WeatherCity weatherCity1 = weatherCityMapper.getWeatherCity2(city);
            if (weatherCity1==null){
                return 0;
            }else {
                return Integer.valueOf(weatherCity1.getDistrictCode());
            }
        }else{
            return Integer.valueOf(weatherCity.getDistrictCode());
        }
    }
 
}