jinpengyong
2020-10-23 2c42977a1a38e4f079c5c44fe51a46c27b865f80
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
package com.moral.service.impl;
 
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import com.alibaba.fastjson.JSONObject;
import com.moral.mapper.AQIMapper;
import com.moral.mapper.AreaMapper;
import com.moral.mapper.CityMapper;
import com.moral.mapper.DeviceMapper;
import com.moral.mapper.WeatherMapper;
import com.moral.mapper.HistoryHourlyMapper;
import com.moral.mapper.MonitorPointMapper;
import com.moral.mapper.ProvinceMapper;
import com.moral.service.WeatherService;
 
@Service
public class WeatherServiceImpl implements WeatherService {
 
    @Resource
    private WeatherMapper weatherMapper;
 
    @Resource
    private MonitorPointMapper monitorPointMapper;
 
    @Resource
    private AQIMapper aqiMapper;
 
    @Resource
    private DeviceMapper deviceMapper;
 
    @Resource
    private HistoryHourlyMapper historyHourlyMapper;
 
    @Override
    public int insertForecastWeatherNew() throws ParseException {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String today = sdf.format(c.getTime());
        DecimalFormat df = new DecimalFormat("0.0");
        c.add(Calendar.DAY_OF_MONTH, 1);
        String nextDay = sdf.format(c.getTime());
        c.setTime(c.getTime());
        c.add(Calendar.DAY_OF_MONTH, 1);
        String nextNextDay = sdf.format(c.getTime()) + " 00";
        RestTemplate restTemplate = new RestTemplate();
        List<Map<String, Object>> cityList = weatherMapper.getCityWeatherConfig();
        cityList.clear();
        Map<String, Object> cz = new HashMap<>();
        cz.put("cityCode", 130900);
        cz.put("cityId", 101090701);
        cityList.add(cz);
        Map<String, Object> ks = new HashMap<>();
        ks.put("cityCode", 320583);
        ks.put("cityId", 101190404);
        cityList.add(ks);
        int count = 0;
        for (Map<String, Object> map : cityList) {
            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 fxTime = hourlyMap.get("fxTime").toString().split("\\+")[0].replace("T", " ") + ":00";
                hourlyMap.remove("fxTime");
                //icon为不需要字段
                hourlyMap.remove("icon");
                hourlyMap.put("time", fxTime);
                //天气
                String text = hourlyMap.get("text").toString();
                //风速km/h->m/s
                Double windSpeed = Double.valueOf(hourlyMap.get("windSpeed").toString());
                windSpeed = windSpeed * 1000 / 3600;
                hourlyMap.put("windSpeed", df.format(windSpeed));
                String condition = "";
                if ("晴".equals(text)) {
                    condition = "100";
                } else if ("雾".equals(text)) {
                    condition = "90";
                } else if ("多云".equals(text)) {
                    condition = "80";
                } else if ("霾".equals(text)) {
                    condition = "70";
                } else if ("阴".equals(text) || "扬沙".equals(text) || "浮尘".equals(text) || "".equals(text)) {
                    condition = "60";
                } else if ("阵雨".equals(text)) {
                    condition = "45";
                } else if ("雷阵雨".equals(text)) {
                    condition = "40";
                } else if ("雷阵雨转小雨".equals(text) || "阵雨转小雨".equals(text)) {
                    condition = "30";
                } else if ("小雨".equals(text)) {
                    condition = "20";
                } else if ("雨".equals(text)) {
                    condition = "10";
                } else if ("雷阵雨转中雨".equals(text)) {
                    condition = "5";
                } else if ("雷阵雨转大雨".equals(text)) {
                    condition = "4";
                } else if ("中雨".equals(text) || "大雨".equals(text) || "暴雨".equals(text)
                        || "小雪".equals(text) || "中雪".equals(text) || "大雪".equals(text)
                        || "雨夹雪".equals(text) || "雨夹雪(伴有冰雹)".equals(text) || "暴雪".equals(text)
                ) {
                    condition = "0";
                } else {
                    condition = "50";
                }
                hourlyMap.put("condition", condition);
                String hour = fxTime.split(" ")[0];
                String hour1 = fxTime.split(":")[0];
                if (hour.equals(nextDay)) {
                    nextDayList.add(hourlyMap);
                }
                if (hour1.equals(nextNextDay)) {
                    nextDayList.add(hourlyMap);
                }
            }
            nextDayList.remove(0);
            map.put("data", nextDayList);
        }
 
        //today:今天
        c.setTime(sdf.parse(today));
        c.add(Calendar.YEAR, -3);
        Date start = c.getTime();
        Date end = sdf.parse(today);
        int timeUnits1 = c.get(Calendar.YEAR);
        int timeUnits2 = timeUnits1 + 1;
        int timeUnits3 = timeUnits1 + 2;
        int timeUnits4 = timeUnits1 + 3;
        //当天前15天 70%,当天前45天15%,其他 15%
        c.setTime(end);
        c.add(Calendar.DAY_OF_MONTH, -15);
        //前15天分割时间
        Date slicedTime1 = c.getTime();
        //前45天分割时间
        c.add(Calendar.DAY_OF_MONTH, -30);
        Date slicedTime2 = c.getTime();
        for (Map<String, Object> map : cityList) {
            String cityCode = map.get("cityCode").toString();
            ArrayList<Map<String, Object>> dataList = (ArrayList<Map<String, Object>>) map.get("data");
            for (Map<String, Object> dataMap : dataList) {
                Map<String, Object> hashMap = new HashMap<>();
                List<String> hours = new ArrayList<>();
                hashMap.put("cityCode", cityCode);
                hashMap.put("start", start);
                hashMap.put("end", end);
                hashMap.put("typeFormat", "%Y-%m-%d %H:%i:%s");
                Integer condition = Integer.valueOf(dataMap.get("condition").toString());
                Double temp = Double.valueOf(dataMap.get("temp").toString());
                Integer pressure = Integer.valueOf(dataMap.get("pressure").toString());
                String time = dataMap.get("time").toString();
                //当前小时
                hours.add(time.substring(11, 13));
                c.setTime(sdf1.parse(time));
                //前1小时
                c.add(Calendar.HOUR_OF_DAY, -1);
                hours.add(sdf1.format(c.getTime()).substring(11, 13));
                //前2小时
                c.add(Calendar.HOUR_OF_DAY, -1);
                String sHour = sdf1.format(c.getTime()).substring(11, 13);
                //后1小时
                c.add(Calendar.HOUR_OF_DAY, 3);
                hours.add(sdf1.format(c.getTime()).substring(11, 13));
                //后2小时
                c.add(Calendar.HOUR_OF_DAY, 1);
                String eHour = sdf1.format(c.getTime()).substring(11, 13);
                hashMap.put("hours", hours);
                hashMap.put("score", 50);
                if (condition > 50) {
                    hashMap.put("condition", 0);
                } else {
                    hashMap.put("condition", 1);
                }
                List<Integer> years = new ArrayList<>();
                Collections.addAll(years, timeUnits1, timeUnits2, timeUnits3, timeUnits4);
                List<String> samples = getSample(hashMap, years);
                if (samples.size() > 100) {
                    hashMap.put("startTemp", temp - 1.0);
                    hashMap.put("endTemp", temp + 1.0);
                    samples = getSample(hashMap, years);
                    if (samples.size() > 100) {
                        hashMap.put("startPressure", pressure - 5);
                        hashMap.put("endPressure", pressure + 5);
                        samples = getSample(hashMap, years);
                    }
                }
                hashMap.put("slicedTime1", slicedTime1);
                hashMap.put("slicedTime2", slicedTime2);
                hashMap.put("nowTemp", temp);
                hashMap.put("sHour", sHour);
                hashMap.put("eHour", eHour);
                Map<String, List<String>> sectionTimesMap = getSectionTimes(hashMap, samples, years);
                Map<String, Object> params = new HashMap<>();
                params.put("cityCode", cityCode);
                params.put("typeFormat", "%Y-%m-%d %H:%i:%s");
                List<String> times1 = sectionTimesMap.get("times1");
                List<String> times2 = sectionTimesMap.get("times2");
                List<String> times3 = sectionTimesMap.get("times3");
                Double avg1 = getAvgO3(params, times1);
                Double avg2 = getAvgO3(params, times2);
                Double avg3 = getAvgO3(params, times3);
                Double resultO3 = avg1 * 0.7 + avg2 * 0.15 + avg3 * 0.15;
                dataMap.put("O3C", String.valueOf(Math.round(resultO3)));
            }
 
            List<Map<String, Object>> resultList = new ArrayList<>();
            for (Map<String, Object> dataMap : dataList) {
                Map<String, Object> resultMap = new HashMap<>();
                String time = dataMap.remove("time").toString();
                Map<String, Object> params = new HashMap<>();
                params.put("cityCode", cityCode);
                params.put("time", sdf1.parse(time));
                String beam = weatherMapper.getBeam(params);
                if (beam != null) {
                    dataMap.put("beam", beam.replace("\"", ""));
                }
                resultMap.put("cityCode", cityCode);
                resultMap.put("time", time);
                resultMap.put("json", JSONObject.toJSONString(dataMap));
                resultList.add(resultMap);
            }
            Map<String, Object> parameters = new HashMap<>();
            parameters.put("start", sdf1.parse(nextDay + " 01:00:00"));
            c.setTime(sdf1.parse(nextDay + " 01:00:00"));
            c.add(Calendar.DAY_OF_MONTH, 1);
            parameters.put("end", c.getTime());
            parameters.put("cityCode", cityCode);
            weatherMapper.deleteByTime(parameters);
            count = count + weatherMapper.insertForecastWeather(resultList);
        }
        return count;
    }
 
    private List<String> getSample(Map<String, Object> params, List<Integer> years) {
        List<String> samples = new ArrayList<>();
        if (years.size() > 0) {
            for (Integer year : years) {
                params.put("timeUnits", year);
                samples.addAll(weatherMapper.getSampleFromHistoryWeather(params));
            }
        }
        samples.addAll(weatherMapper.getSampleFromRealWeather(params));
        return samples;
    }
 
    private Double getAvgO3(Map<String, Object> params, List<String> times) {
        List<String> values = new ArrayList<>();
        for (String time : times) {
            String s = time.substring(0, 4);
            params.put("time", time);
            String O3 = aqiMapper.getO3ByTime(params);
            if (O3 == null) {
                params.put("timeUnits", s);
                O3 = aqiMapper.getO3FromHistory(params);
            }
            if (O3 != null) {
                values.add(O3.replace("\"", ""));
            }
        }
        Double sum = 0.0;
        for (String value : values) {
            sum += Double.valueOf(value);
        }
        return sum / values.size();
    }
 
    private Map<String, List<String>> getSectionTimes(Map<String, Object> params, List<String> samples, List<Integer> years) throws ParseException {
        Map<String, List<String>> hashMap = new HashMap<>();
        //当天前15天
        List<String> times1 = new ArrayList<>();
        //45-15
        List<String> times2 = new ArrayList<>();
        //其他
        List<String> times3 = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date slicedTime1 = (Date) (params.get("slicedTime1"));
        Date slicedTime2 = (Date) (params.get("slicedTime2"));
        Double nowTemp = Double.valueOf(params.get("nowTemp").toString());
        ArrayList<String> hours = (ArrayList<String>) params.get("hours");
        for (String sample : samples) {
            Date samTime = sdf.parse(sample);
            if (samTime.getTime() >= slicedTime1.getTime()) {
                times1.add(sample);
            } else if (samTime.getTime() >= slicedTime2.getTime()) {
                times2.add(sample);
            } else {
                times3.add(sample);
            }
        }
        hashMap.put("times1", times1);
        hashMap.put("times2", times2);
        hashMap.put("times3", times3);
        for (String key : hashMap.keySet()) {
            List<String> values = hashMap.get(key);
            if (values.size() == 0) {
                params.put("startTemp", nowTemp - 2.0);
                params.put("endTemp", nowTemp + 2.0);
                if ("times1".equals(key) || "times2".equals(key)) {
                    years = new ArrayList<>();
                    values = getSample(params, years);
                } else {
                    values = getSample(params, years);
                }
                if (values.size() == 0) {
                    hours.add(params.get("sHour").toString());
                    hours.add(params.get("eHour").toString());
                    params.put("hours", hours);
                    if ("times1".equals(key) || "times2".equals(key)) {
                        years = new ArrayList<>();
                        values = getSample(params, years);
                    } else {
                        values = getSample(params, years);
                    }
                }
                hashMap.put(key, values);
            }
        }
        return hashMap;
    }
 
    @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");
        DecimalFormat df = new DecimalFormat("0.0");
        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();
        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()));
        int count = 0;
        List<Map<String, Object>> cityList = weatherMapper.getCityWeatherConfig();
        for (Map<String, Object> map : cityList) {
            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);
                }
            }
            nextDayList.remove(0);
            for (Map<String, Object> nextDayMap : nextDayList) {
                //风速km/h->m/s
                Double windSpeed = Double.valueOf(nextDayMap.get("windSpeed").toString());
                windSpeed = windSpeed * 1000 / 3600;
                nextDayMap.put("windSpeed", df.format(windSpeed));
                String text = nextDayMap.get("text").toString();
                String condition = "";
                if ("晴".equals(text)) {
                    condition = "100";
                } else if ("雾".equals(text)) {
                    condition = "90";
                } else if ("多云".equals(text)) {
                    condition = "80";
                } else if ("霾".equals(text)) {
                    condition = "70";
                } else if ("阴".equals(text) || "扬沙".equals(text) || "浮尘".equals(text) || "".equals(text)) {
                    condition = "60";
                } else if ("阵雨".equals(text)) {
                    condition = "45";
                } else if ("雷阵雨".equals(text)) {
                    condition = "40";
                } else if ("雷阵雨转小雨".equals(text) || "阵雨转小雨".equals(text)) {
                    condition = "30";
                } else if ("小雨".equals(text)) {
                    condition = "20";
                } else if ("雨".equals(text)) {
                    condition = "10";
                } else if ("雷阵雨转中雨".equals(text)) {
                    condition = "5";
                } else if ("雷阵雨转大雨".equals(text)) {
                    condition = "4";
                } else if ("中雨".equals(text) || "大雨".equals(text) || "暴雨".equals(text)
                        || "小雪".equals(text) || "中雪".equals(text) || "大雪".equals(text)
                        || "雨夹雪".equals(text) || "雨夹雪(伴有冰雹)".equals(text) || "暴雪".equals(text)
                ) {
                    condition = "0";
                } else {
                    condition = "50";
                }
                nextDayMap.put("condition", condition);
            }
            map.put("data", nextDayList);
        }
 
        for (Map<String, Object> map : cityList) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            String cityCode = map.get("cityCode").toString();
            String parentCode = "";
            if (cityCode.endsWith("0000")) {
                parentCode = cityCode;
            } else if (cityCode.endsWith("00")) {
                parentCode = cityCode.substring(0, cityCode.length() - 4) + "0000";
            } else {
                parentCode = cityCode.substring(0, cityCode.length() - 2) + "00";
            }
            map.put("parentCode", parentCode);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("typeFormat", "%Y-%m-%d %H:%i:%s");
            Map<String, Object> map1 = new HashMap<>();
            map1.put("typeFormat", "%Y-%m-%d %H:%i:%s");
            map1.put("cityCode", Integer.valueOf(cityCode));
            //根据city_code获取所有monitorPoint
            Map<String, Object> params = new HashMap<>();
            if (cityCode.endsWith("0000")) {
                params.put("provinceCode", Integer.valueOf(cityCode));
            } else if (cityCode.endsWith("00")) {
                params.put("cityCode", Integer.valueOf(cityCode));
            } else {
                params.put("areaCode", Integer.valueOf(cityCode));
            }
            List<Integer> pointList = new ArrayList<>();
            if (cityCode.equals("130900")) {
                pointList.add(48);
            } else {
                pointList = monitorPointMapper.getMonitorList(params);
            }
            List<String> macList = new ArrayList<>();
            if (pointList.size() != 0) {
                macList = deviceMapper.getMacsByMonitorPointIds(pointList);
            }
            if (macList.size() != 0) {
                map.put("macs", macList);
            }
            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";
                int hour = Integer.valueOf(time.substring(11, 13));
                map.put("time", hour);
                hashMap.put("time", sdf1.parse(time));
                String nextDayTemp = dataMap.get("temp").toString();
                String nextDayCloud = dataMap.get("cloud").toString();
                Integer condition = Integer.valueOf(dataMap.get("condition").toString());
                Integer value = 0;
                if (condition >= 80) {
                    value = 80;
                } else if (condition >= 40) {
                    value = 40;
                } else if (condition >= 0) {
                    value = 0;
                }
                map.put("condition", value);
                List<Map<String, Object>> tempAndCloudList = weatherMapper.getTempAndCloud(map);
                List<String> times = new ArrayList<>();
                for (Map<String, Object> tempAndCloudMap : tempAndCloudList) {
                    times.add(tempAndCloudMap.get("time").toString());
                }
                map.put("times", times);
                map1.put("times", times);
                map1.put("time", hour);
                List<Map<String, Object>> O3List = new ArrayList<>();
                List<Map<String, Object>> beamList = new ArrayList<>();
                if (times.size() != 0) {
                    O3List = aqiMapper.getO3(map1);
                    if (O3List.size() == 0) {
                        map1.put("cityCode", Integer.valueOf(map.get("parentCode").toString()));
                        O3List = aqiMapper.getO3(map1);
                    }
                    if (map.get("macs") != null) {
                        beamList = historyHourlyMapper.getBeamByMacs(map);
                    }
                }
 
                for (Map<String, Object> tempAndCloudMap : tempAndCloudList) {
                    String time1 = tempAndCloudMap.get("time").toString();
                    for (Map<String, Object> beamMap : beamList) {
                        if (beamList.size() != 0) {
                            String time2 = beamMap.get("time").toString();
                            if (time1.equals(time2)) {
                                tempAndCloudMap.putAll(beamMap);
                            }
                        }
                    }
                }
 
                for (Map<String, Object> tempAndCloudMap : tempAndCloudList) {
                    String time1 = tempAndCloudMap.get("time").toString();
                    for (Map<String, Object> O3Map : O3List) {
                        if (O3List.size() != 0) {
                            String time3 = O3Map.get("time").toString();
                            if (time1.equals(time3)) {
                                tempAndCloudMap.putAll(O3Map);
                            }
                        }
                    }
                }
 
                double tempSum = 0.0;//温度和
                double O3Sum = 0.0;//O3和
                double sum1 = 0.0;//
                double sum2 = 0.0;
 
                double cloudSum = 0.0;//总云量和
                double beamSum = 0.0;//光照和
                double sum3 = 0.0;//
                double sum4 = 0.0;
                int size = tempAndCloudList.size();
                Map<String, Object> cmap = new HashMap<>();
                cmap.put("time", hour);
                for (Map<String, Object> listMap : tempAndCloudList) {
                    //O3
                    double temp = Double.valueOf(listMap.get("temp").toString().replace("\"", "").split("\\.")[0]);
                    if (listMap.get("O3") != null) {
                        double O3 = Double.valueOf(listMap.get("O3").toString().replace("\"", "").split("\\.")[0]);
                        tempSum = tempSum + temp;
                        O3Sum = O3Sum + O3;
                        sum1 = sum1 + temp * O3;
                        sum2 = sum2 + temp * temp;
                    }
                    //光照
                    double cloud = Double.valueOf(listMap.get("cloud").toString().replace("\"", "").split("\\.")[0]);
                    if (listMap.get("beam") != null) {
                        double beam = Double.valueOf(listMap.get("beam").toString().replace("\"", "").split("\\.")[0]);
                        cloudSum = cloudSum + cloud;
                        beamSum = beamSum + beam;
                        sum3 = sum3 + cloud * beam;
                        sum4 = sum4 + cloud * cloud;
                    }
                }
 
                if (tempSum != 0.0) {
                    double tempAvg = tempSum / size;
                    double O3Avg = O3Sum / size;
                    double b = (sum1 - size * tempAvg * O3Avg) / (sum2 - size * tempAvg * tempAvg);
                    double a = O3Avg - (b * tempAvg);
                    cmap.put("a", a);
                    cmap.put("b", b);
                }
 
                if (cloudSum != 0.0) {
                    double cloudAvg = cloudSum / size;
                    double beamAvg = beamSum / size;
                    double n = (sum3 - size * cloudAvg * beamAvg) / (sum4 - size * cloudAvg * cloudAvg);
                    double m = beamAvg - (n * cloudAvg);
                    cmap.put("m", m);
                    cmap.put("n", n);
                }
                if (cmap.get("a") != null) {
                    Double a = Double.valueOf(cmap.get("a").toString());
                    Double b = Double.valueOf(cmap.get("b").toString());
                    double O3C = b * Double.valueOf(nextDayTemp) + a;
                    if (O3C < 1.0) {
                        O3C = 1.0;
                    }
                    dataMap.put("O3C", String.valueOf(Math.round(O3C)));
                }
                if (cmap.get("m") != null) {
                    Double m = Double.valueOf(cmap.get("m").toString());
                    Double n = Double.valueOf(cmap.get("n").toString());
                    double beam = n * Double.valueOf(nextDayCloud) + m;
                    if (beam < 1.0) {
                        beam = 0.0;
                    }
                    dataMap.put("beam", String.valueOf(Math.round(beam)));
                }
                dataMap.remove("icon");
                dataMap.remove("fxTime");
                hashMap.put("json", JSONObject.toJSONString(dataMap));
                resultList.add(hashMap);
            }
            Map<String, Object> parameters = new HashMap<>();
            parameters.put("start", start);
            parameters.put("end", end);
            parameters.put("cityCode", cityCode);
            weatherMapper.deleteByTime(parameters);
            count = count + weatherMapper.insertForecastWeather(resultList);
        }
        return count;
    }
 
    @Override
    public int insertRealWeather() throws ParseException {
        DecimalFormat df = new DecimalFormat("0.0");
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = sdf1.parse(sdf.format(c.getTime()) + ":00:00");
        RestTemplate restTemplate = new RestTemplate();
        List<Map<String, Object>> cityList = weatherMapper.getCityWeatherConfig();
        int count = 0;
        for (Map<String, Object> map : cityList) {
            String id = map.get("cityId").toString();
            Map<String, Object> dataMap = restTemplate.getForObject("https://api.heweather.net/v7/weather/now?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);
            Map<String, Object> nowMap = (Map<String, Object>) dataMap.get("now");
            map.put("data", nowMap);
        }
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Map<String, Object> map : cityList) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("cityCode", map.get("cityCode").toString());
            resultMap.put("time", now);
            Map<String, Object> jsonMap = (Map<String, Object>) map.get("data");
 
            //风速km/h->m/s
            Double windSpeed = Double.valueOf(jsonMap.get("windSpeed").toString());
            windSpeed = windSpeed * 1000 / 3600;
            jsonMap.put("windSpeed", df.format(windSpeed));
            jsonMap.remove("obsTime");
            jsonMap.remove("icon");
            String text = jsonMap.get("text").toString();
            String condition;
            if ("晴".equals(text)) {
                condition = "100";
            } else if ("雾".equals(text)) {
                condition = "90";
            } else if ("多云".equals(text)) {
                condition = "80";
            } else if ("霾".equals(text)) {
                condition = "70";
            } else if ("阴".equals(text) || "扬沙".equals(text) || "浮尘".equals(text) || "".equals(text)) {
                condition = "60";
            } else if ("阵雨".equals(text)) {
                condition = "45";
            } else if ("雷阵雨".equals(text)) {
                condition = "40";
            } else if ("雷阵雨转小雨".equals(text) || "阵雨转小雨".equals(text)) {
                condition = "30";
            } else if ("小雨".equals(text)) {
                condition = "20";
            } else if ("雨".equals(text)) {
                condition = "10";
            } else if ("雷阵雨转中雨".equals(text)) {
                condition = "5";
            } else if ("雷阵雨转大雨".equals(text)) {
                condition = "4";
            } else if ("中雨".equals(text) || "大雨".equals(text) || "暴雨".equals(text)
                    || "小雪".equals(text) || "中雪".equals(text) || "大雪".equals(text)
                    || "雨夹雪".equals(text) || "雨夹雪(伴有冰雹)".equals(text) || "暴雪".equals(text)
            ) {
                condition = "0";
            } else {
                condition = "50";
            }
            jsonMap.put("condition", condition);
            resultMap.put("json", JSONObject.toJSONString(jsonMap));
            resultList.add(resultMap);
        }
        weatherMapper.deleteRealWeather(sdf1.format(now));
        return weatherMapper.insertRealWeather(resultList);
    }
}