From 32983a583c99c190ecb4361d72b6f13cd06fefc7 Mon Sep 17 00:00:00 2001 From: jinpengyong <jpy123456> Date: Thu, 27 Aug 2020 10:22:57 +0800 Subject: [PATCH] 根据小时不同天气预测o3,光照 --- src/main/java/com/moral/mapper/RealWeatherMapper.java | 3 src/main/java/com/moral/service/impl/RealWeatherServiceImpl.java | 9 + src/main/resources/mapper/DeviceMapper.xml | 18 +- src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java | 290 +++++++++++++++++++++++++---------------- src/main/resources/mapper/MonitorPointMapper.xml | 17 ++ src/main/resources/mapper/RealWeatherMapper.xml | 8 src/main/java/com/moral/mapper/HistoryHourlyMapper.java | 2 src/main/java/com/moral/mapper/DeviceMapper.java | 3 src/main/resources/mapper/AQIMapper.xml | 7 src/main/resources/mapper/HistoryHourlyMapper.xml | 17 ++ src/main/java/com/moral/mapper/MonitorPointMapper.java | 3 11 files changed, 248 insertions(+), 129 deletions(-) diff --git a/src/main/java/com/moral/mapper/DeviceMapper.java b/src/main/java/com/moral/mapper/DeviceMapper.java index 87f34bc..f435dc0 100644 --- a/src/main/java/com/moral/mapper/DeviceMapper.java +++ b/src/main/java/com/moral/mapper/DeviceMapper.java @@ -35,4 +35,7 @@ List<String> getMacByOrganizationid(@Param("organizationIdList") List<Object> organizationIdList); List<Map<String, Object>> macAndOrganizationIdMap(@Param("macs") List<String> macs); + + List<String> getMacsByMonitorPointIds(List<Integer> list); + } \ No newline at end of file diff --git a/src/main/java/com/moral/mapper/HistoryHourlyMapper.java b/src/main/java/com/moral/mapper/HistoryHourlyMapper.java index 128d1a6..e63ce6e 100644 --- a/src/main/java/com/moral/mapper/HistoryHourlyMapper.java +++ b/src/main/java/com/moral/mapper/HistoryHourlyMapper.java @@ -15,4 +15,6 @@ int insertHistoryO3Hourly(@Param("list") List<Map<String, Object>> list); + List<Map<String,Object>> getBeamByMacs(Map<String,Object> params); + } diff --git a/src/main/java/com/moral/mapper/MonitorPointMapper.java b/src/main/java/com/moral/mapper/MonitorPointMapper.java index f55396b..6758440 100644 --- a/src/main/java/com/moral/mapper/MonitorPointMapper.java +++ b/src/main/java/com/moral/mapper/MonitorPointMapper.java @@ -1,10 +1,13 @@ package com.moral.mapper; import java.util.List; +import java.util.Map; import com.moral.entity.MonitorPoint; public interface MonitorPointMapper { List<MonitorPoint> getMonitorPointList(); + + List<Integer> getMonitorList(Map<String, Object> params); } diff --git a/src/main/java/com/moral/mapper/RealWeatherMapper.java b/src/main/java/com/moral/mapper/RealWeatherMapper.java index 7e718ee..71e5d07 100644 --- a/src/main/java/com/moral/mapper/RealWeatherMapper.java +++ b/src/main/java/com/moral/mapper/RealWeatherMapper.java @@ -7,5 +7,6 @@ int insertRealWeather(List<Map<String,Object>> list); - List<Map<String,Object>> getTemp(Map<String,Object> params); + List<Map<String,Object>> getTempAndCloud(Map<String,Object> params); + } diff --git a/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java b/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java index ee83dee..ae0bec1 100644 --- a/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java +++ b/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java @@ -25,7 +25,9 @@ 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; @@ -55,6 +57,12 @@ @Resource private AQIMapper aqiMapper; + + @Resource + private DeviceMapper deviceMapper; + + @Resource + private HistoryHourlyMapper historyHourlyMapper; @Override public int insertForecastWeather() throws ParseException { @@ -142,98 +150,6 @@ Date endTime = sdf.parse(sdf.format(c.getTime())); c.add(Calendar.MONTH, -2); Date startTime = sdf.parse(sdf.format(c.getTime())); - - for (Map<String, Object> hashmap : hashSet) { - Map<String, Object> map1 = new HashMap<>(); - map1.put("typeFormat", "%Y-%m-%d %H:%i:%s"); - map1.put("startTime", startTime); - map1.put("endTime", endTime); - hashmap.put("typeFormat", "%Y-%m-%d %H:%i:%s"); - hashmap.put("startTime", startTime); - hashmap.put("endTime", endTime); - map1.put("cityCode", Integer.valueOf(hashmap.get("cityCode").toString())); - List<List<Map<String, Object>>> list1 = new ArrayList<>(); - List<List<Map<String, Object>>> list2 = new ArrayList<>(); - - for (int i = 0; i < 24; i++) { - List<Map<String, Object>> list3 = new ArrayList<>(); - List<Map<String, Object>> list4 = new ArrayList<>(); - if (i < 10) { - map1.put("time", "0" + i + ":00:00"); - hashmap.put("time", "0" + i + ":00:00"); - } else { - map1.put("time", i + ":00:00"); - hashmap.put("time", i + ":00:00"); - } - List<Map<String, Object>> tempList = realWeatherMapper.getTemp(hashmap); - List<Map<String, Object>> O3List = aqiMapper.getO3(map1); - if (O3List.size() == 0) { - map1.put("cityCode", Integer.valueOf(hashmap.get("parentCode").toString())); - O3List = aqiMapper.getO3(map1); - } - list3.addAll(tempList); - list4.addAll(O3List); - list1.add(list3); - list2.add(list4); - } - List<List<Map<String, Object>>> list = new ArrayList<>(); - if (list2.get(0).size() == 0) { - list = list1; - } else { - for (List<Map<String, Object>> tempList : list1) { - List<Map<String, Object>> list3 = new ArrayList<>(); - for (List<Map<String, Object>> O3List : list2) { - for (Map<String, Object> tempMap : tempList) { - for (Map<String, Object> O3Map : O3List) { - if (O3Map.get("O3") != null) { - if (tempMap.get("time").equals(O3Map.get("time"))) { - Map<String, Object> map = new HashMap<>(); - map.put("time", tempMap.get("time")); - map.put("temp", tempMap.get("temp").toString()); - map.put("O3", O3Map.get("O3").toString()); - list3.add(map); - } - } - } - } - } - list.add(list3); - } - } - - if (list.get(0).size()!=0) { - if (list.get(0).get(0).size() > 2) { - List<Map<String, Object>> constantList = new ArrayList<>(); - for (int i = 0; i < list.size(); i++) { - List<Map<String, Object>> mapList = list.get(i); - double tempSum = 0;//��������� - double O3Sum = 0;//O3��� - double sum = 0;// - double sum1 = 0; - int length = mapList.size(); - Map<String, Object> cmap = new HashMap<>(); - cmap.put("time", i); - for (Map<String, Object> map : mapList) { - double temp = Double.valueOf(map.get("temp").toString().replace("\"", "").split("\\.")[0]); - double O3 = Double.valueOf(map.get("O3").toString().replace("\"", "").split("\\.")[0]); - tempSum = tempSum + temp; - O3Sum = O3Sum + O3; - sum = sum + temp * O3; - sum1 = sum1 + temp * temp; - } - double tempAvg = tempSum / length; - double O3Avg = O3Sum / length; - double b = (sum - length * tempAvg * O3Avg) / (sum1 - length * tempAvg * tempAvg); - double a = O3Avg - (b * tempAvg); - cmap.put("a", a); - cmap.put("b", b); - constantList.add(cmap); - } - hashmap.put("constants", constantList); - } - } - } - int count = 0; for (Map<String, Object> map : hashSet) { String id = map.get("cityID").toString(); @@ -251,48 +167,194 @@ String hour1 = hourlyMap.get("fxTime").toString().split("\\+")[0].split(":")[0].replace("T", " "); if (hour.equals(nextDay)) { nextDayList.add(hourlyMap); - } if (hour1.equals(nextNextDay)) { nextDayList.add(hourlyMap); } - map.put("data", nextDayList); } + nextDayList.remove(0); + for (Map<String, Object> nextDayMap : nextDayList) { + String text = nextDayMap.get("text").toString(); + String condition = ""; + if ("���".equals(text)) { + condition = "100"; + } else if ("������".equals(text)) { + condition = "80"; + } else if ("���".equals(text)) { + condition = "60"; + } else if ("���������".equals(text) || "������".equals(text)) { + condition = "40"; + } else if ("������".equals(text)) { + condition = "20"; + } else if ("���".equals(text)) { + condition = "0"; + } + nextDayMap.put("condition", condition); + } + map.put("data", nextDayList); } for (Map<String, Object> map : hashSet) { - String cityCode = map.get("cityCode").toString(); 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", cityCode); String time = dataMap.get("fxTime").toString().split("\\+")[0].replace("T", " ") + ":00"; - hashMap.put("time", sdf1.parse(time)); - - String temp = dataMap.get("temp").toString(); int hour = Integer.valueOf(time.substring(11, 13)); - if (map.get("constants") != null) { - List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("constants"); - for (Map<String, Object> listMap : list) { - int value = Integer.valueOf(listMap.get("time").toString()); - Double a = Double.valueOf(listMap.get("a").toString()); - Double b = Double.valueOf(listMap.get("b").toString()); - if (hour == value) { - double O3C = b * Double.valueOf(temp) + a; - dataMap.put("O3C", String.valueOf(Math.round(O3C))); + 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()); + String value=""; + 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 = aqiMapper.getO3(map1); + if (O3List.size() == 0) { + map1.put("cityCode", Integer.valueOf(map.get("parentCode").toString())); + O3List = aqiMapper.getO3(map1); + } + List<Map<String, Object>> beamList = new ArrayList<>(); + 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=1.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); diff --git a/src/main/java/com/moral/service/impl/RealWeatherServiceImpl.java b/src/main/java/com/moral/service/impl/RealWeatherServiceImpl.java index e483ba2..8615506 100644 --- a/src/main/java/com/moral/service/impl/RealWeatherServiceImpl.java +++ b/src/main/java/com/moral/service/impl/RealWeatherServiceImpl.java @@ -93,6 +93,13 @@ hashMap.put("cityCode", code); hashSet.add(hashMap); } + + Map<String,Object> kunShanMap=new HashMap<>(); + kunShanMap.put("name1","���������"); + kunShanMap.put("parentName","���������"); + kunShanMap.put("cityCode",320500); + hashSet.add(kunShanMap); + List<Element> elements = Dom4jUtils.readDocument(); String cityID = "101190404"; for (Map<String, Object> map : hashSet) { @@ -145,7 +152,7 @@ jsonMap.put("condition","80"); }else if(text.equals("���")){ jsonMap.put("condition","60"); - }else if(text.equals("���������")){ + }else if(text.equals("���������")||text.equals("������")){ jsonMap.put("condition","40"); }else if(text.equals("������")){ jsonMap.put("condition","20"); diff --git a/src/main/resources/mapper/AQIMapper.xml b/src/main/resources/mapper/AQIMapper.xml index 3771b2c..54d35f5 100644 --- a/src/main/resources/mapper/AQIMapper.xml +++ b/src/main/resources/mapper/AQIMapper.xml @@ -18,8 +18,9 @@ ifnull(aqi_json->'$.O3C',aqi_json->'$.O3') 'O3' from hangzhou_aqi where city_code=#{cityCode} - and time >= #{startTime} - AND time <![CDATA[<]]> #{endTime} - and DATE_FORMAT(time, #{typeFormat}) like '%${time}' + and DATE_FORMAT(time, #{typeFormat}) in + <foreach collection="times" open="(" separator="," close=")" item="time"> + #{time} + </foreach> </select> </mapper> \ No newline at end of file diff --git a/src/main/resources/mapper/DeviceMapper.xml b/src/main/resources/mapper/DeviceMapper.xml index f490465..eca27c3 100644 --- a/src/main/resources/mapper/DeviceMapper.xml +++ b/src/main/resources/mapper/DeviceMapper.xml @@ -58,8 +58,8 @@ <select id="selectWithOrgIdsByMac" parameterType="java.lang.String" resultMap="BaseResultWithOrgIdsMap"> select <include refid="simple_Column_List" /> , - dvn.version as device_version_value, - den.limit_val, + dvn.version as device_version_value, + den.limit_val, mp.province_code, mp.city_code, mp.area_code, @@ -74,7 +74,7 @@ where mac = #{mac,jdbcType=VARCHAR} </select> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> - select + select <include refid="Base_Column_List" /> from device where id = #{id,jdbcType=INTEGER} @@ -254,8 +254,8 @@ FROM monitor_point mp, device d WHERE mp.id = d.monitor_point_id AND mp.organization_id IN - <foreach collection="organizationIdList" index="index" item="organizationId" open="(" separator="," close=")"> - #{organizationId} + <foreach collection="organizationIdList" index="index" item="organizationId" open="(" separator="," close=")"> + #{organizationId} </foreach> AND d.is_delete = '0'; </select> @@ -278,7 +278,7 @@ </foreach> AND d.is_delete = '0' </select> - + <select id="macAndOrganizationIdMap" resultType="java.util.LinkedHashMap"> select mac,organization_id as organizationId FROM monitor_point m,device d @@ -290,5 +290,9 @@ </foreach> </select> - + <select id="getMacsByMonitorPointIds" resultType="java.lang.String"> + select mac from device where monitor_point_id in + <foreach item="item" collection="list" index="index" open="(" separator="," close=")">#{item}</foreach> + and is_delete=0; + </select> </mapper> \ No newline at end of file diff --git a/src/main/resources/mapper/HistoryHourlyMapper.xml b/src/main/resources/mapper/HistoryHourlyMapper.xml index d380d68..cf4fc42 100644 --- a/src/main/resources/mapper/HistoryHourlyMapper.xml +++ b/src/main/resources/mapper/HistoryHourlyMapper.xml @@ -63,4 +63,21 @@ </foreach> </insert> + <select id="getBeamByMacs" resultType="java.util.Map"> + SELECT + DATE_FORMAT(h.time,#{typeFormat}) time, + avg(h.json->'$.e12[0]') 'beam' + FROM history_hourly h + WHERE h.mac in + <foreach collection="macs" open="(" separator="," close=")" item="mac"> + #{mac} + </foreach> + and h.time in + <foreach collection="times" open="(" separator="," close=")" item="time"> + #{time} + </foreach> + and h.json->'$.e12[0]' is not null + group by h.time + </select> + </mapper> \ No newline at end of file diff --git a/src/main/resources/mapper/MonitorPointMapper.xml b/src/main/resources/mapper/MonitorPointMapper.xml index 3914466..479dd35 100644 --- a/src/main/resources/mapper/MonitorPointMapper.xml +++ b/src/main/resources/mapper/MonitorPointMapper.xml @@ -18,4 +18,21 @@ <select id="getMonitorPointList" resultType="com.moral.entity.MonitorPoint" resultMap="BaseResultMap"> select * from monitor_point </select> + + <select id="getMonitorList" resultType="java.lang.Integer"> + select id + from monitor_point + where + <if test="areaCode != null"> + area_code = #{areaCode} + </if> + + <if test="cityCode != null"> + city_code = #{cityCode} + </if> + + <if test="provinceCode != null"> + province_code = #{provinceCode} + </if> + </select> </mapper> \ No newline at end of file diff --git a/src/main/resources/mapper/RealWeatherMapper.xml b/src/main/resources/mapper/RealWeatherMapper.xml index 70b0ce7..b4ee00e 100644 --- a/src/main/resources/mapper/RealWeatherMapper.xml +++ b/src/main/resources/mapper/RealWeatherMapper.xml @@ -13,13 +13,15 @@ </foreach> </insert> - <select id="getTemp" resultType="java.util.Map"> + <select id="getTempAndCloud" resultType="java.util.Map"> select DATE_FORMAT(time, #{typeFormat}) time, - json->'$.temp' 'temp' + json->'$.temp' 'temp', + json->'$.cloud' 'cloud' from real_weather where city_code=#{cityCode} and time >= #{startTime} AND time <![CDATA[<]]> #{endTime} - and DATE_FORMAT(time, #{typeFormat}) like '%${time}' + and DATE_FORMAT(time,'%H:%i:%s')=#{time} + and json->'$.condition'>=#{condition} </select> </mapper> \ No newline at end of file -- Gitblit v1.8.0