From 48bb075f91c5daf35cff0a16abe1136f132aafb4 Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Mon, 28 Sep 2020 15:40:26 +0800
Subject: [PATCH] 不同天气给定不同分数

---
 src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java |  284 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 258 insertions(+), 26 deletions(-)

diff --git a/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java b/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java
index f23339e..4e3457b 100644
--- a/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java
@@ -1,5 +1,6 @@
 package com.moral.service.impl;
 
+import java.text.DecimalFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -17,17 +18,20 @@
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONArray;
 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,11 +53,24 @@
     @Resource
     private ProvinceMapper provinceMapper;
 
+    @Resource
+    private RealWeatherMapper realWeatherMapper;
+
+    @Resource
+    private AQIMapper aqiMapper;
+
+    @Resource
+    private DeviceMapper deviceMapper;
+
+    @Resource
+    private HistoryHourlyMapper historyHourlyMapper;
+
     @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());
@@ -71,34 +88,55 @@
             Integer provinceCode = monitorPoint.getProvinceCode();
             String parentName = "";
             String name1 = "";
-            Integer code=0;
+            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();
-                code=areaCode;
+                parentCode = city.getCityCode();
+                code = areaCode;
                 if ("���������".equals(name1)) {
                     name1 = parentName;
-                    parentName = provinceMapper.getProvinceByProvinceCode(provinceCode).getProvinceName();
-                    code=cityCode;
+                    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();
-                code=cityCode;
+                Province province = provinceMapper.getProvinceByProvinceCode(provinceCode);
+                parentName = province.getProvinceName();
+                code = cityCode;
+                parentCode = province.getProvinceCode();
                 if ("���������".equals(name1)) {
                     name1 = parentName;
                     parentName = "";
-                    code=provinceCode;
+                    code = provinceCode;
                 }
             }
             hashMap.put("name1", name1);
             hashMap.put("parentName", parentName);
-            hashMap.put("cityCode",code);
+            hashMap.put("cityCode", code);
+            hashMap.put("parentCode", parentCode);
             hashSet.add(hashMap);
         }
+
+        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";
         for (Map<String, Object> map : hashSet) {
@@ -124,7 +162,11 @@
             }
         }
 
-        int count=0;
+        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);
@@ -145,34 +187,224 @@
                 if (hour1.equals(nextNextDay)) {
                     nextDayList.add(hourlyMap);
                 }
-                map.put("data", nextDayList);
             }
+            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 : hashSet) {
-            List<Map<String,Object>> resultList=new ArrayList<>();
+            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) {
-                Map<String,Object> hashMap=new HashMap<>();
-                hashMap.put("cityCode",map.get("cityCode").toString());
+                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));
-                hashMap.put("json",JSONObject.toJSONString(dataMap));
+                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);
+                    }
+                }
+
+                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;//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);
-            }
-            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", map.get("cityCode").toString());
+            parameters.put("cityCode", cityCode);
             forecastWeatherMapper.deleteByTime(parameters);
-            count = count+forecastWeatherMapper.insertForecastWeather(resultList);
+            count = count + forecastWeatherMapper.insertForecastWeather(resultList);
         }
         return count;
     }

--
Gitblit v1.8.0