From 2ef66767cc7c0eca9cff04893fc735b0435086bd Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Wed, 02 Dec 2020 13:09:16 +0800
Subject: [PATCH] update

---
 src/main/resources/mapper/WeatherMapper.xml                  |   88 ++++++++--------------
 src/main/java/com/moral/mapper/WeatherMapper.java            |    4 
 src/main/java/com/moral/service/impl/WeatherServiceImpl.java |   97 ++++++++---------------
 3 files changed, 69 insertions(+), 120 deletions(-)

diff --git a/src/main/java/com/moral/mapper/WeatherMapper.java b/src/main/java/com/moral/mapper/WeatherMapper.java
index 65f94f4..7a357c1 100644
--- a/src/main/java/com/moral/mapper/WeatherMapper.java
+++ b/src/main/java/com/moral/mapper/WeatherMapper.java
@@ -27,7 +27,7 @@
 
     List<Map<String, Object>> getWeatherByHour(Map<String, Object> params);
 
-    List<String> getSampleFromHistoryWeather(Map<String, Object> params);
+    List<Map<String,Object>> getSampleFromHistoryWeather(Map<String, Object> params);
 
-    List<String> getSampleFromRealWeather(Map<String, Object> params);
+    List<Map<String,Object>> getSampleFromRealWeather(Map<String, Object> params);
 }
diff --git a/src/main/java/com/moral/service/impl/WeatherServiceImpl.java b/src/main/java/com/moral/service/impl/WeatherServiceImpl.java
index 9fdd356..2c9a0ed 100644
--- a/src/main/java/com/moral/service/impl/WeatherServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/WeatherServiceImpl.java
@@ -10,6 +10,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 import javax.annotation.Resource;
 
@@ -138,7 +139,7 @@
                 //���������������������
                 String windDir = nextDayMap.get("windDir").toString();
                 //������
-                String windScale = nextDayMap.get("windScale").toString();
+                int windScale = Integer.parseInt(nextDayMap.get("windScale").toString());
                 String time = nextDayMap.remove("time").toString();
                 //������������
                 hours.add(time.substring(11, 13));
@@ -164,22 +165,21 @@
                 }
                 List<Integer> years = new ArrayList<>();
                 Collections.addAll(years, timeUnits1, timeUnits2, timeUnits3, timeUnits4);
-                List<String> O3Samples = getSample(hashMap, years);
+                //������������������
+                List<Map<String, Object>> O3Samples = getSample(hashMap, years);
                 if (O3Samples.size() > 100) {
-                    hashMap.put("startTemp", temp - 1);
-                    hashMap.put("endTemp", temp + 1);
-                    O3Samples = getSample(hashMap, years);
+                    O3Samples = O3Samples.parallelStream()
+                            .filter(sample -> Integer.parseInt(sample.get("temp").toString()) >= temp - 1 && Integer.parseInt(sample.get("temp").toString()) <= temp + 1)
+                            .collect(Collectors.toList());
                     if (O3Samples.size() > 100) {
-                        hashMap.put("startPressure", pressure - 5);
-                        hashMap.put("endPressure", pressure + 5);
-                        O3Samples = getSample(hashMap, years);
+                        O3Samples = O3Samples.parallelStream()
+                                .filter(sample -> Integer.parseInt(sample.get("pressure").toString()) >= pressure - 5 && Integer.parseInt(sample.get("pressure").toString()) <= pressure + 5)
+                                .collect(Collectors.toList());
                     }
                 }
+
                 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, O3Samples, years);
                 Map<String, Object> params = new HashMap<>();
                 params.put("cityCode", cityCode);
@@ -192,7 +192,7 @@
                 Double O3Avg1 = getAvg(params, times1);
                 Double O3Avg2 = getAvg(params, times2);
                 Double O3Avg3 = getAvg(params, times3);
-                Double resultO3 = O3Avg1 * 0.7 + O3Avg2 * 0.15 + O3Avg3 * 0.15;
+                double resultO3 = O3Avg1 * 0.7 + O3Avg2 * 0.15 + O3Avg3 * 0.15;
                 nextDayMap.put("O3C", String.valueOf(Math.round(resultO3)));
                 //pm2.5,pm10���������������,������������������������������
                 Map<String, Object> hashMap1 = new HashMap<>();
@@ -202,17 +202,19 @@
                 hashMap1.put("typeFormat", "%Y-%m-%d %H:%i:%s");
                 hashMap1.put("startTemp", temp - 1);
                 hashMap1.put("endTemp", temp + 1);
-                List<String> pmSamples = getSample(hashMap1, years);
+                List<Map<String, Object>> pmSamples = getSample(hashMap1, years);
                 if (pmSamples.size() > 100) {
-                    hashMap1.put("windScale", windScale);
-                    pmSamples = getSample(hashMap1, years);
+                    pmSamples = pmSamples.parallelStream()
+                            .filter(sample -> Integer.parseInt(sample.get("windScale").toString()) == windScale)
+                            .collect(Collectors.toList());
                     if (pmSamples.size() > 100) {
-                        hashMap1.put("windDir", windDir);
-                        pmSamples = getSample(hashMap1, years);
+                        pmSamples = pmSamples.parallelStream()
+                                .filter(sample -> sample.get("windDir").toString().replace("\"", "").equals(windDir))
+                                .collect(Collectors.toList());
                         if (pmSamples.size() > 100) {
-                            hashMap1.put("startPressure", pressure - 5);
-                            hashMap1.put("endPressure", pressure + 5);
-                            pmSamples = getSample(hashMap1, years);
+                            pmSamples = pmSamples.parallelStream()
+                                    .filter(sample -> Integer.parseInt(sample.get("pressure").toString()) >= pressure - 5 && Integer.parseInt(sample.get("pressure").toString()) <= pressure + 5)
+                                    .collect(Collectors.toList());
                         }
                     }
                 }
@@ -236,9 +238,9 @@
                 Double PM10Avg1 = getAvg(params1, times4);
                 Double PM10Avg2 = getAvg(params1, times5);
                 Double PM10Avg3 = getAvg(params1, times6);
-                Double resultPM25 = PM25Avg1 * 0.7 + PM25Avg2 * 0.15 + PM25Avg3 * 0.15;
+                double resultPM25 = PM25Avg1 * 0.7 + PM25Avg2 * 0.15 + PM25Avg3 * 0.15;
                 nextDayMap.put("PM25C", String.valueOf(Math.round(resultPM25)));
-                Double resultPM10 = PM10Avg1 * 0.7 + PM10Avg2 * 0.15 + PM10Avg3 * 0.15;
+                double resultPM10 = PM10Avg1 * 0.7 + PM10Avg2 * 0.15 + PM10Avg3 * 0.15;
                 nextDayMap.put("PM10C", String.valueOf(Math.round(resultPM10)));
                 params.put("time", time);
                 String beam = weatherMapper.getBeam(params);
@@ -264,8 +266,8 @@
         return count;
     }
 
-    private List<String> getSample(Map<String, Object> params, List<Integer> years) {
-        List<String> samples = new ArrayList<>();
+    private List<Map<String, Object>> getSample(Map<String, Object> params, List<Integer> years) {
+        List<Map<String, Object>> samples = new ArrayList<>();
         if (years.size() > 0) {
             for (Integer year : years) {
                 params.put("timeUnits", year);
@@ -294,14 +296,14 @@
                 values.add(value.replace("\"", ""));
             }
         }
-        Double sum = 0.0;
+        double sum = 0.0;
         for (String v : values) {
-            sum += Double.valueOf(v);
+            sum += Double.parseDouble(v);
         }
         return sum / values.size();
     }
 
-    private Map<String, List<String>> getSectionTimes(Map<String, Object> params, List<String> samples, List<Integer> years) throws ParseException {
+    private Map<String, List<String>> getSectionTimes(Map<String, Object> params, List<Map<String, Object>> samples, List<Integer> years) throws ParseException {
         Map<String, List<String>> hashMap = new HashMap<>();
         //���������15���
         List<String> times1 = new ArrayList<>();
@@ -312,50 +314,21 @@
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         Date slicedTime1 = (Date) (params.get("slicedTime1"));
         Date slicedTime2 = (Date) (params.get("slicedTime2"));
-        for (String sample : samples) {
-            Date samTime = sdf.parse(sample);
+        for (Map<String, Object> sample : samples) {
+            String time = sample.get("time").toString();
+            Date samTime = sdf.parse(time);
             if (samTime.getTime() >= slicedTime1.getTime()) {
-                times1.add(sample);
+                times1.add(time);
             } else if (samTime.getTime() >= slicedTime2.getTime()) {
-                times2.add(sample);
+                times2.add(time);
             } else {
-                times3.add(sample);
+                times3.add(time);
             }
         }
 
         hashMap.put("times1", times1);
         hashMap.put("times2", times2);
         hashMap.put("times3", times3);
-
-        if (params.get("nowTemp") != null && params.get("hours") != null) {
-            Double nowTemp = Double.valueOf(params.get("nowTemp").toString());
-            ArrayList<String> hours = (ArrayList<String>) params.get("hours");
-            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;
     }
 
diff --git a/src/main/resources/mapper/WeatherMapper.xml b/src/main/resources/mapper/WeatherMapper.xml
index 9dda41b..aec50d0 100644
--- a/src/main/resources/mapper/WeatherMapper.xml
+++ b/src/main/resources/mapper/WeatherMapper.xml
@@ -115,42 +115,22 @@
         )
     </select>
 
-    <select id="getSampleFromHistoryWeather" resultType="java.lang.String">
-        select DATE_FORMAT(time, #{typeFormat}) as time
+    <select id="getSampleFromHistoryWeather" resultType="java.util.Map">
+        select DATE_FORMAT(time, #{typeFormat}) as `time`,
+        cast(value->'$.temp' as SIGNED) as 'temp',
+        cast(value->'$.pressure' as SIGNED) as 'pressure',
+        value->'$.windDir'  as 'windDir',
+        cast(value->'$.windScale' as SIGNED)  as 'windScale'
         from history_weather_${timeUnits}
         <where>
             city_code=#{cityCode}
 
-            <if test="windDir != null">
-                and value->'$.windDir' = #{windDir}
-            </if>
-
-            <if test="windScale != null">
-                and value->'$.windScale' = #{windScale}
-            </if>
-
             <if test="condition == 0">
-                and cast(value->'$.condition' as UNSIGNED integer) > #{score}
+                and cast(value->'$.condition' as SIGNED) > #{score}
             </if>
 
             <if test="condition == 1">
-                and cast(value->'$.condition' as UNSIGNED integer) <![CDATA[<=]]> #{score}
-            </if>
-
-            <if test="startTemp != null">
-                and cast(value->'$.temp' as decimal(10, 0)) >= #{startTemp}
-            </if>
-
-            <if test="endTemp != null">
-                and cast(value->'$.temp' as decimal(10, 0)) <![CDATA[<=]]> #{endTemp}
-            </if>
-
-            <if test="startPressure != null">
-                and cast(value->'$.pressure' as UNSIGNED integer) >= #{startPressure}
-            </if>
-
-            <if test="endPressure != null">
-                and cast(value->'$.pressure' as UNSIGNED integer) <![CDATA[<=]]> #{endPressure}
+                and cast(value->'$.condition' as SIGNED) <![CDATA[<=]]> #{score}
             </if>
 
             <if test="start != null">
@@ -159,6 +139,14 @@
 
             <if test="end != null">
                 and time <![CDATA[<]]> #{end}
+            </if>
+
+            <if test="startTemp != null">
+                and cast(value->'$.temp' as SIGNED) >= #{startTemp}
+            </if>
+
+            <if test="endTemp != null">
+                and cast(value->'$.temp' as SIGNED) <![CDATA[<=]]> #{endTemp}
             </if>
 
             <if test="hours != null">
@@ -170,42 +158,22 @@
         </where>
     </select>
 
-    <select id="getSampleFromRealWeather" resultType="java.lang.String">
-        select DATE_FORMAT(time, #{typeFormat}) as time
+    <select id="getSampleFromRealWeather" resultType="java.util.Map">
+        select DATE_FORMAT(time, #{typeFormat}) as `time`,
+        cast(json->'$.temp' as SIGNED) as 'temp',
+        cast(json->'$.pressure' as SIGNED) as 'pressure',
+        json->'$.windDir' as 'windDir',
+        cast(json->'$.windScale' as SIGNED)  as 'windScale'
         from real_weather
         <where>
             city_code=#{cityCode}
 
-            <if test="windDir != null">
-                and json->'$.windDir' = #{windDir}
-            </if>
-
-            <if test="windScale != null">
-                and json->'$.windScale' = #{windScale}
-            </if>
-
             <if test="condition == 0">
-                and cast(json->'$.condition' as UNSIGNED integer) > #{score}
+                and cast(json->'$.condition' as SIGNED) > #{score}
             </if>
 
             <if test="condition == 1">
-                and cast(json->'$.condition' as UNSIGNED integer) <![CDATA[<=]]> #{score}
-            </if>
-
-            <if test="startTemp != null">
-                and cast(json->'$.temp' as decimal(10, 0)) >= #{startTemp}
-            </if>
-
-            <if test="endTemp != null">
-                and cast(json->'$.temp' as decimal(10, 0)) <![CDATA[<=]]> #{endTemp}
-            </if>
-
-            <if test="startPressure != null">
-                and cast(json->'$.pressure' as UNSIGNED integer) >= #{startPressure}
-            </if>
-
-            <if test="endPressure != null">
-                and cast(json->'$.pressure' as UNSIGNED integer) <![CDATA[<=]]> #{endPressure}
+                and cast(json->'$.condition' as SIGNED) <![CDATA[<=]]> #{score}
             </if>
 
             <if test="start != null">
@@ -216,6 +184,14 @@
                 and time <![CDATA[<]]> #{end}
             </if>
 
+            <if test="startTemp != null">
+                and cast(json->'$.temp' as SIGNED) >= #{startTemp}
+            </if>
+
+            <if test="endTemp != null">
+                and cast(json->'$.temp' as SIGNED) <![CDATA[<=]]> #{endTemp}
+            </if>
+
             <if test="hours != null">
                 and DATE_FORMAT(time, '%H') in
                 <foreach collection="hours" open="(" separator="," close=")" item="hour">

--
Gitblit v1.8.0