From c60311f48e7ec07c56a4df31536f795ba62751ce Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Fri, 09 Oct 2020 16:44:42 +0800
Subject: [PATCH] 昆山,沧州大数据预测O3

---
 src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java |  761 +++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 691 insertions(+), 70 deletions(-)

diff --git a/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java b/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java
index 5f4b537..7be449b 100644
--- a/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java
@@ -1,13 +1,14 @@
 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.HashSet;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -18,16 +19,20 @@
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 
-import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.moral.entity.Area;
 import com.moral.entity.City;
 import com.moral.entity.MonitorPoint;
+import com.moral.entity.Province;
+import com.moral.mapper.AQIMapper;
 import com.moral.mapper.AreaMapper;
 import com.moral.mapper.CityMapper;
+import com.moral.mapper.DeviceMapper;
 import com.moral.mapper.ForecastWeatherMapper;
+import com.moral.mapper.HistoryHourlyMapper;
 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;
 
@@ -49,136 +54,752 @@
     @Resource
     private ProvinceMapper provinceMapper;
 
+    @Resource
+    private RealWeatherMapper realWeatherMapper;
+
+    @Resource
+    private AQIMapper aqiMapper;
+
+    @Resource
+    private DeviceMapper deviceMapper;
+
+    @Resource
+    private HistoryHourlyMapper historyHourlyMapper;
+
     @Override
-    public void insertForecastWeather() throws ParseException {
+    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 now = sdf.format(c.getTime());
+        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());
-
+        String nextNextDay = sdf.format(c.getTime()) + " 00";
         RestTemplate restTemplate = new RestTemplate();
         List<MonitorPoint> monitorPointList = monitorPointMapper.getMonitorPointList();
-
         Set<Map<String, Object>> hashSet = new HashSet<>();
-        List<Map<String, Object>> resultList = new ArrayList<>();
         for (MonitorPoint monitorPoint : monitorPointList) {
-            Map<String, Object> hashMap1 = new HashMap<>();
+            Map<String, Object> hashMap = new HashMap<>();
+            Integer areaCode = monitorPoint.getAreaCode();
+            Integer cityCode = monitorPoint.getCityCode();
+            Integer provinceCode = monitorPoint.getProvinceCode();
+            String parentName;
+            String name1;
+            Integer code;
+            Integer parentCode;
+            if (areaCode != null) {
+                Area area = areaMapper.getAreaByAreaCode(areaCode);
+                City city = cityMapper.getCityByCityCode(cityCode);
+                name1 = area.getAreaName();
+                parentName = city.getCityName();
+                parentCode = cityCode;
+                code = areaCode;
+                if ("���������".equals(name1)) {
+                    name1 = parentName;
+                    Province province = provinceMapper.getProvinceByProvinceCode(provinceCode);
+                    parentName = province.getProvinceName();
+                    parentCode = provinceCode;
+                    code = cityCode;
+                }
+            } else {
+                if (cityCode != null) {
+                    City city = cityMapper.getCityByCityCode(cityCode);
+                    name1 = city.getCityName();
+                    code = cityCode;
+                    Province province = provinceMapper.getProvinceByProvinceCode(provinceCode);
+                    parentName = province.getProvinceName();
+                    parentCode = provinceCode;
+                    if ("���������".equals(name1)) {
+                        name1 = parentName;
+                        code = provinceCode;
+                    }
+                } else {
+                    code = provinceCode;
+                    Province province = provinceMapper.getProvinceByProvinceCode(provinceCode);
+                    name1 = province.getProvinceName();
+                    parentCode = provinceCode;
+                    parentName = name1;
+                }
+            }
+            hashMap.put("name1", name1);
+            hashMap.put("parentName", parentName);
+            hashMap.put("cityCode", code);
+            hashMap.put("parentCode", parentCode);
+            hashSet.add(hashMap);
+        }
+
+        Map<String, Object> smap = new HashMap<>();
+        smap.put("name1", "���������");
+        smap.put("parentName", "���������");
+        smap.put("cityCode", 320500);
+        smap.put("parentCode", 320000);
+        hashSet.add(smap);
+
+        Map<String, Object> bmap = new HashMap<>();
+        bmap.put("name1", "���������");
+        bmap.put("parentName", "���������");
+        bmap.put("cityCode", 110000);
+        bmap.put("parentCode", 110000);
+        hashSet.add(bmap);
+
+        Map<String, Object> cz = new HashMap<>();
+        Set<Map<String, Object>> set = new HashSet<>();
+        cz.put("name1", "���������");
+        cz.put("parentName", "���������");
+        cz.put("cityCode", 130900);
+        cz.put("parentCode", 130000);
+        set.add(cz);
+
+        Map<String, Object> ks = new HashMap<>();
+        ks.put("name1", "���������");
+        ks.put("parentName", "���������");
+        ks.put("cityCode", 320583);
+        ks.put("parentCode", 320500);
+        set.add(ks);
+
+        List<Element> elements = Dom4jUtils.readDocument();
+        String cityID;
+        for (Map<String, Object> map : set) {
+            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;
+                    }
+                }
+            }
+        }
+        int count = 0;
+        for (Map<String, Object> map : set) {
+            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 : set) {
+            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 = forecastWeatherMapper.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);
+            forecastWeatherMapper.deleteByTime(parameters);
+            count = count + forecastWeatherMapper.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(realWeatherMapper.getSampleFromHistoryWeather(params));
+            }
+        }
+        samples.addAll(realWeatherMapper.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();
+        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;
+            Integer parentCode = 0;
             if (areaCode != null) {
                 Area area = areaMapper.getAreaByAreaCode(areaCode);
                 City city = cityMapper.getCityByCityCode(cityCode);
                 name1 = area.getAreaName();
                 parentName = city.getCityName();
+                parentCode = city.getCityCode();
+                code = areaCode;
                 if ("���������".equals(name1)) {
                     name1 = parentName;
-                    parentName = provinceMapper.getProvinceByProvinceCode(provinceCode).getProvinceName();
+                    Province province = provinceMapper.getProvinceByProvinceCode(provinceCode);
+                    parentName = province.getProvinceName();
+                    parentCode = province.getProvinceCode();
+                    code = cityCode;
                 }
             } else {
                 City city = cityMapper.getCityByCityCode(cityCode);
                 name1 = city.getCityName();
-                parentName = provinceMapper.getProvinceByProvinceCode(provinceCode).getProvinceName();
+                Province province = provinceMapper.getProvinceByProvinceCode(provinceCode);
+                parentName = province.getProvinceName();
+                code = cityCode;
+                parentCode = province.getProvinceCode();
+                if ("���������".equals(name1)) {
+                    name1 = parentName;
+                    parentName = "";
+                    code = provinceCode;
+                }
             }
             hashMap.put("name1", name1);
             hashMap.put("parentName", parentName);
+            hashMap.put("cityCode", code);
+            hashMap.put("parentCode", parentCode);
             hashSet.add(hashMap);
-            hashMap1.put("monitorPointId", monitorPoint.getId());
-            hashMap1.put("name", name1);
-            resultList.add(hashMap1);
         }
+
+        Map<String, Object> bmap = new HashMap<>();
+        bmap.put("name1", "���������");
+        bmap.put("parentName", "���������");
+        bmap.put("cityCode", 110000);
+        bmap.put("parentCode", 110000);
+        hashSet.add(bmap);
+        Map<String, Object> smap = new HashMap<>();
+        smap.put("name1", "���������");
+        smap.put("parentName", "���������");
+        smap.put("cityCode", 320500);
+        smap.put("parentCode", 320000);
+        hashSet.add(smap);
+
         List<Element> elements = Dom4jUtils.readDocument();
         String cityID = "101190404";
-        List<Map<String, Object>> insertList = new ArrayList<>();
         for (Map<String, Object> map : hashSet) {
-            Map<String, Object> hashMap = new HashMap<>();
             String name1 = map.get("name1").toString();
-            hashMap.put("name", name1);
             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("Fid").getText();
+                    cityID = element.element("Fweathercn").getText();
+                    map.put("cityID", cityID);
                     break;
                 }
                 if (name2.endsWith(name1)) {
                     if (name2.startsWith(parentName)) {
-                        cityID = element.element("Fid").getText();
+                        cityID = element.element("Fweathercn").getText();
+                        map.put("cityID", cityID);
                         break;
                     }
                 }
             }
-            Map<String, Object> map1 = restTemplate.getForObject("https://yiketianqi.com/api?version=v9&appid=68884517&appsecret=kqUYTBk3&cityid={1}", Map.class, cityID);
-            if (map1 == null) {
-                map1=new HashMap<>();
+        }
+
+        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;
+        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);
+                }
+            }
+            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);
+        }
 
-            List<Map<String, Object>> dataList = (ArrayList<Map<String, Object>>) map1.get("data");
-            List<Map<String, Object>> arrayList = new ArrayList<>();
-
+        for (Map<String, Object> map : hashSet) {
+            List<Map<String, Object>> resultList = new ArrayList<>();
+            String cityCode = map.get("cityCode").toString();
+            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 = 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) {
-                String date = dataMap.get("date").toString();
-                ArrayList<Map<String, Object>> list = (ArrayList<Map<String, Object>>) dataMap.get("hours");
-                if (date.equals(now)) {
-                    for (int i = list.size() - 7; i < list.size(); i++) {
-                        Map<String, Object> resultMap = list.get(i);
-                        arrayList.add(resultMap);
+                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 = realWeatherMapper.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);
                     }
                 }
-                if (date.equals(nextDay)) {
-                    for (int i = 0; i < 17; i++) {
-                        Map<String, Object> resultMap = list.get(i);
-                        arrayList.add(resultMap);
-                    }
-                }
-            }
-            hashMap.put("data", arrayList);
-            insertList.add(hashMap);
-        }
 
-        for (Map<String, Object> resultMap : resultList) {
-            List<Map<String, Object>> list = new ArrayList<>();
-            String monitorPointId = resultMap.get("monitorPointId").toString();
-            for (Map<String, Object> insertMap : insertList) {
-                if (resultMap.get("name").equals(insertMap.get("name"))) {
-                    ArrayList<Map<String, Object>> dataList = (ArrayList<Map<String, Object>>) insertMap.get("data");
-                    for (Map<String, Object> dataMap : dataList) {
-                        dataMap.remove("wea_img");
-                        Map<String, Object> map = new HashMap<>();
-                        map.put("monitorPointId", Integer.valueOf(monitorPointId));
-                        String hour = dataMap.get("hours").toString().replaceAll("[\u4e00-\u9fa5]+", "");
-                        Date date = sdf1.parse(nextDay + " " + hour + ":00:00");
-                        if (hour.equals("00")) {
-                            date = sdf1.parse(nextNextDay + " 00:00:00");
+                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);
+                            }
                         }
-                        map.put("time", date);
-                        String json = JSONObject.toJSONString(dataMap);
-                        map.put("json", json);
-                        list.add(map);
                     }
                 }
-            }
 
-            for (Map<String, Object> map : list) {
-                String json = map.get("json").toString();
-                Map<String, Object> jsonMap = JSON.parseObject(json, Map.class);
-                Iterator<String> iterator = jsonMap.keySet().iterator();
-                while (iterator.hasNext()) {
-                    if (iterator.next().equals("hours")) {
-                        iterator.remove();
-                        json = JSONObject.toJSONString(jsonMap);
-                        map.put("json",json);
+                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;//O3���
+                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);
             }
-            forecastWeatherMapper.insertForecastWeather(list);
+            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;
     }
 }

--
Gitblit v1.8.0