jinpengyong
2020-08-13 c75b4210328b98913de7eeef507e6d88069bcecc
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package com.moral.service.impl;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.dom4j.Element;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import com.alibaba.fastjson.JSONObject;
import com.moral.entity.Area;
import com.moral.entity.City;
import com.moral.entity.MonitorPoint;
import com.moral.mapper.AQIMapper;
import com.moral.mapper.AreaMapper;
import com.moral.mapper.CityMapper;
import com.moral.mapper.ForecastWeatherMapper;
import com.moral.mapper.MonitorPointMapper;
import com.moral.mapper.ProvinceMapper;
import com.moral.mapper.RealWeatherMapper;
import com.moral.service.ForecastWeatherService;
import com.moral.util.Dom4jUtils;
 
@Service
public class ForecastWeatherServiceImpl implements ForecastWeatherService {
 
    @Resource
    private ForecastWeatherMapper forecastWeatherMapper;
 
    @Resource
    private MonitorPointMapper monitorPointMapper;
 
    @Resource
    private AreaMapper areaMapper;
 
    @Resource
    private CityMapper cityMapper;
 
    @Resource
    private ProvinceMapper provinceMapper;
 
    @Resource
    private RealWeatherMapper realWeatherMapper;
 
    @Resource
    private AQIMapper aqiMapper;
 
    @Override
    public int insertForecastWeather() throws ParseException {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        c.add(Calendar.DAY_OF_MONTH, 1);
        String nextDay = sdf.format(c.getTime());
        c.setTime(c.getTime());
        Date start = sdf1.parse(nextDay + " 01:00:00");
        c.add(Calendar.DAY_OF_MONTH, 1);
        String nextNextDay = sdf.format(c.getTime()) + " 00";
        Date end = sdf1.parse(sdf.format(c.getTime()) + " 01:00:00");
        RestTemplate restTemplate = new RestTemplate();
        List<MonitorPoint> monitorPointList = monitorPointMapper.getMonitorPointList();
        Set<Map<String, Object>> hashSet = new HashSet<>();
        for (MonitorPoint monitorPoint : monitorPointList) {
            Map<String, Object> hashMap = new HashMap<>();
            Integer areaCode = monitorPoint.getAreaCode();
            Integer cityCode = monitorPoint.getCityCode();
            Integer provinceCode = monitorPoint.getProvinceCode();
            String parentName = "";
            String name1 = "";
            Integer code = 0;
            if (areaCode != null) {
                Area area = areaMapper.getAreaByAreaCode(areaCode);
                City city = cityMapper.getCityByCityCode(cityCode);
                name1 = area.getAreaName();
                parentName = city.getCityName();
                code = areaCode;
                if ("市辖区".equals(name1)) {
                    name1 = parentName;
                    parentName = provinceMapper.getProvinceByProvinceCode(provinceCode).getProvinceName();
                    code = cityCode;
                }
            } else {
                City city = cityMapper.getCityByCityCode(cityCode);
                name1 = city.getCityName();
                parentName = provinceMapper.getProvinceByProvinceCode(provinceCode).getProvinceName();
                code = cityCode;
                if ("市辖区".equals(name1)) {
                    name1 = parentName;
                    parentName = "";
                    code = provinceCode;
                }
            }
            hashMap.put("name1", name1);
            hashMap.put("parentName", parentName);
            hashMap.put("cityCode", code);
            hashSet.add(hashMap);
        }
        List<Element> elements = Dom4jUtils.readDocument();
        String cityID = "101190404";
        for (Map<String, Object> map : hashSet) {
            String name1 = map.get("name1").toString();
            for (Element element : elements) {
                String name2 = element.element("name").getText();
                String parentName = map.get("parentName").toString();
                if (name2.equals("大丰市")) {
                    name2 = "大丰区";
                }
                if (name1.equals(name2)) {
                    cityID = element.element("Fweathercn").getText();
                    map.put("cityID", cityID);
                    break;
                }
                if (name2.endsWith(name1)) {
                    if (name2.startsWith(parentName)) {
                        cityID = element.element("Fweathercn").getText();
                        map.put("cityID", cityID);
                        break;
                    }
                }
            }
        }
 
        c.setTime(new Date());
        Date endTime = sdf.parse(sdf.format(c.getTime()));
        c.add(Calendar.MONTH, -2);
        Date startTime = sdf.parse(sdf.format(c.getTime()));
 
        for (Map<String, Object> hashmap : hashSet) {
            hashmap.put("typeFormat", "%Y-%m-%d %H:%i:%s");
            hashmap.put("startTime", startTime);
            hashmap.put("endTime", endTime);
            List<List<Map<String, Object>>> list1 = new ArrayList<>();
            List<List<Map<String, Object>>> list2 = new ArrayList<>();
 
            for (int i = 0; i < 24; i++) {
                List<Map<String, Object>> list3 = new ArrayList<>();
                List<Map<String, Object>> list4 = new ArrayList<>();
                if (i < 10) {
                    hashmap.put("time", "0" + i + ":00:00");
                } else {
                    hashmap.put("time", i + ":00:00");
                }
                List<Map<String, Object>> tempList = realWeatherMapper.getTemp(hashmap);
                List<Map<String, Object>> O3List = aqiMapper.getO3(hashmap);
                list3.addAll(tempList);
                list4.addAll(O3List);
                list1.add(list3);
                list2.add(list4);
            }
 
            List<List<Map<String, Object>>> list = new ArrayList<>();
            if (list2.get(0).size() == 0) {
                list = list1;
            } else {
                for (List<Map<String, Object>> tempList : list1) {
                    List<Map<String, Object>> list3 = new ArrayList<>();
                    for (List<Map<String, Object>> O3List : list2) {
                        for (Map<String, Object> tempMap : tempList) {
                            for (Map<String, Object> O3Map : O3List) {
                                if (O3Map.get("O3") != null) {
                                    if (tempMap.get("time").equals(O3Map.get("time"))) {
                                        Map<String, Object> map = new HashMap<>();
                                        map.put("time", tempMap.get("time"));
                                        map.put("temp", tempMap.get("temp").toString());
                                        map.put("O3", O3Map.get("O3").toString());
                                        list3.add(map);
                                    }
                                }
                            }
                        }
                    }
                    list.add(list3);
                }
            }
 
            if (list.get(0).get(0).size() > 2) {
                List<Map<String, Object>> constantList = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    List<Map<String, Object>> mapList = list.get(i);
                    double tempSum = 0;//温度和
                    double O3Sum = 0;//O3和
                    double sum = 0;//
                    double sum1 = 0;
                    int length = mapList.size();
                    Map<String, Object> cmap = new HashMap<>();
                    cmap.put("time", i);
                    for (Map<String, Object> map : mapList) {
                        double temp = Double.valueOf(map.get("temp").toString().replace("\"", "").split("\\.")[0]);
                        double O3 = Double.valueOf(map.get("O3").toString().replace("\"", "").split("\\.")[0]);
                        tempSum = tempSum + temp;
                        O3Sum = O3Sum + O3;
                        sum = sum + temp * O3;
                        sum1 = sum1 + temp * temp;
                    }
                    double tempAvg = tempSum / length;
                    double O3Avg = O3Sum / length;
                    double b = (sum - length * tempAvg * O3Avg) / (sum1 - length * tempAvg * tempAvg);
                    double a = O3Avg - (b * tempAvg);
                    cmap.put("a", a);
                    cmap.put("b", b);
                    constantList.add(cmap);
                }
                hashmap.put("constants", constantList);
            }
        }
 
        int count = 0;
        for (Map<String, Object> map : hashSet) {
            String id = map.get("cityID").toString();
            Map<String, Object> dataMap = restTemplate.getForObject("https://api.heweather.net/v7/weather/72h?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, id);
            if (dataMap == null) {
                return count;
            }
            String json = JSONObject.toJSONString(dataMap);
            dataMap = (Map<String, Object>) JSONObject.parse(json);
            List<Map<String, Object>> nextDayList = new ArrayList<>();
            String string = dataMap.get("hourly").toString();
            List<Map<String, Object>> hourlyList = (List<Map<String, Object>>) JSONObject.parse(string);
            for (Map<String, Object> hourlyMap : hourlyList) {
                String hour = hourlyMap.get("fxTime").toString().split("T")[0];
                String hour1 = hourlyMap.get("fxTime").toString().split("\\+")[0].split(":")[0].replace("T", " ");
                if (hour.equals(nextDay)) {
                    nextDayList.add(hourlyMap);
 
                }
                if (hour1.equals(nextNextDay)) {
                    nextDayList.add(hourlyMap);
                }
 
                map.put("data", nextDayList);
            }
        }
 
        for (Map<String, Object> map : hashSet) {
            String cityCode = map.get("cityCode").toString();
            List<Map<String, Object>> resultList = new ArrayList<>();
            ArrayList<Map<String, Object>> dataList = (ArrayList<Map<String, Object>>) map.get("data");
            for (Map<String, Object> dataMap : dataList) {
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("cityCode", cityCode);
                String time = dataMap.get("fxTime").toString().split("\\+")[0].replace("T", " ") + ":00";
                hashMap.put("time", sdf1.parse(time));
 
                String temp = dataMap.get("temp").toString();
                int hour = Integer.valueOf(time.substring(11, 13));
                if (map.get("constants") != null) {
                    List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("constants");
                    for (Map<String, Object> listMap : list) {
                        int value = Integer.valueOf(listMap.get("time").toString());
                        Double a = Double.valueOf(listMap.get("a").toString());
                        Double b = Double.valueOf(listMap.get("b").toString());
                        if (hour == value) {
                            double O3C = b * Double.valueOf(temp) + a;
                            dataMap.put("O3C", String.valueOf(Math.round(O3C)));
                        }
                    }
                }
                hashMap.put("json", JSONObject.toJSONString(dataMap));
                resultList.add(hashMap);
            }
            resultList.remove(0);
            for (Map<String, Object> resultMap : resultList) {
                Map<String, Object> jsonMap = (Map<String, Object>) JSONObject.parse(resultMap.get("json").toString());
                jsonMap.remove("fxTime");
                jsonMap.remove("icon");
                resultMap.put("json", JSONObject.toJSONString(jsonMap));
            }
            Map<String, Object> parameters = new HashMap<>();
            parameters.put("start", start);
            parameters.put("end", end);
            parameters.put("cityCode", cityCode);
            forecastWeatherMapper.deleteByTime(parameters);
            count = count + forecastWeatherMapper.insertForecastWeather(resultList);
        }
        return count;
    }
}