kaiyu
2020-11-24 3152a4e4dadcd6ebfb8e882aa356d063416d49cc
Merge branch 'master' of http://blit.7drlb.com:8888/r/screen_job
4 files modified
178 ■■■■■ changed files
src/main/java/com/moral/mapper/AQIMapper.java 8 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/WeatherServiceImpl.java 137 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/AQIMapper.xml 10 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/WeatherMapper.xml 23 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/mapper/AQIMapper.java
@@ -9,12 +9,12 @@
    int insertAQIData(Map<String, Object> parameters);
    List<Map<String,Object>> getO3(Map<String,Object> params);
    List<Map<String, Object>> getO3(Map<String, Object> params);
    String getO3ByTime(Map<String,Object> params);
    String getAqiFromHangzhou(Map<String, Object> params);
    List<Map<String,Object>> getAqiByHour(Map<String,Object> params);
    List<Map<String, Object>> getAqiByHour(Map<String, Object> params);
    String getO3FromHistory(Map<String,Object> params);
    String getAqiFromHistory(Map<String, Object> params);
}
src/main/java/com/moral/service/impl/WeatherServiceImpl.java
@@ -125,6 +125,7 @@
            //每个城市结果存放list
            List<Map<String, Object>> resultList = new ArrayList<>();
            for (Map<String, Object> nextDayMap : nextDayList) {
                //O3预测
                Map<String, Object> hashMap = new HashMap<>();
                List<String> hours = new ArrayList<>();
                hashMap.put("cityCode", cityCode);
@@ -135,11 +136,6 @@
                double temp = Double.parseDouble(nextDayMap.get("temp").toString());
                int pressure = Integer.parseInt(nextDayMap.get("pressure").toString());
                String time = nextDayMap.remove("time").toString();
                //月份,pm2.5,pm10去往年同月取
                String month = time.substring(5, 7);
                //明日此小时风向
                String windDir = nextDayMap.get("windDir").toString();
                double windSpeed = Double.parseDouble(nextDayMap.get("windSpeed").toString());
                //当前小时
                hours.add(time.substring(11, 13));
                c.setTime(sdf1.parse(time));
@@ -164,15 +160,15 @@
                }
                List<Integer> years = new ArrayList<>();
                Collections.addAll(years, timeUnits1, timeUnits2, timeUnits3, timeUnits4);
                List<String> samples = getSample(hashMap, years);
                if (samples.size() > 100) {
                List<String> O3Samples = getSample(hashMap, years);
                if (O3Samples.size() > 100) {
                    hashMap.put("startTemp", temp - 1.0);
                    hashMap.put("endTemp", temp + 1.0);
                    samples = getSample(hashMap, years);
                    if (samples.size() > 100) {
                    O3Samples = getSample(hashMap, years);
                    if (O3Samples.size() > 100) {
                        hashMap.put("startPressure", pressure - 5);
                        hashMap.put("endPressure", pressure + 5);
                        samples = getSample(hashMap, years);
                        O3Samples = getSample(hashMap, years);
                    }
                }
                hashMap.put("slicedTime1", slicedTime1);
@@ -180,19 +176,68 @@
                hashMap.put("nowTemp", temp);
                hashMap.put("sHour", sHour);
                hashMap.put("eHour", eHour);
                Map<String, List<String>> sectionTimesMap = getSectionTimes(hashMap, samples, years);
                Map<String, List<String>> sectionTimesMap = getSectionTimes(hashMap, O3Samples, years);
                Map<String, Object> params = new HashMap<>();
                params.put("cityCode", cityCode);
                params.put("typeFormat", "%Y-%m-%d %H:%i:%s");
                params.put("sensorKey", "O3");
                params.put("sensorKey1", "O3C");
                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;
                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;
                nextDayMap.put("O3C", String.valueOf(Math.round(resultO3)));
                //pm2.5,pm10预测
                //pm2.5,pm10颗粒物预测
                //月份,pm2.5,pm10去往年同月取
                String month = time.substring(5, 7);
                //明日此小时风向
                String windDir = nextDayMap.get("windDir").toString();
                //风级
                String windScale = nextDayMap.get("windScale").toString();
                Map<String, Object> hashMap1 = new HashMap<>();
                hashMap1.put("cityCode", cityCode);
                hashMap1.put("start", start);
                hashMap1.put("end", end);
                hashMap1.put("typeFormat", "%Y-%m-%d %H:%i:%s");
                hashMap1.put("month", month);
                List<String> pmSamples = getSample(hashMap1, years);
                if (pmSamples.size() > 100) {
                    hashMap1.put("windDir", windDir);
                    hashMap1.put("windScale", windScale);
                    pmSamples = getSample(hashMap1, years);
                    if (pmSamples.size() > 100) {
                        hashMap1.put("startPressure", pressure - 5);
                        hashMap1.put("endPressure", pressure + 5);
                        pmSamples = getSample(hashMap1, years);
                    }
                }
                hashMap1.put("slicedTime1", slicedTime1);
                hashMap1.put("slicedTime2", slicedTime2);
                Map<String, List<String>> sectionTimesMap1 = getSectionTimes(hashMap1, pmSamples, years);
                List<String> times4 = sectionTimesMap1.get("times1");
                List<String> times5 = sectionTimesMap1.get("times2");
                List<String> times6 = sectionTimesMap1.get("times3");
                Map<String, Object> params1 = new HashMap<>();
                params1.put("cityCode", cityCode);
                params1.put("typeFormat", "%Y-%m-%d %H:%i:%s");
                params1.put("sensorKey", "PM2_5");
                params1.put("sensorKey1", "PM25C");
                Double PM25Avg1 = getAvg(params1, times4);
                Double PM25Avg2 = getAvg(params1, times5);
                Double PM25Avg3 = getAvg(params1, times6);
                params1.put("sensorKey", "PM10");
                params1.put("sensorKey1", "PM10C");
                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;
                nextDayMap.put("PM25C", String.valueOf(Math.round(resultPM25)));
                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);
                if (beam != null) {
@@ -229,23 +274,27 @@
        return samples;
    }
    private Double getAvgO3(Map<String, Object> params, List<String> times) {
    private Double getAvg(Map<String, Object> params, List<String> times) {
        List<String> values = new ArrayList<>();
        if (times.size() == 0) {
            return 0.0;
        }
        for (String time : times) {
            String s = time.substring(0, 4);
            params.put("time", time);
            String O3 = aqiMapper.getO3ByTime(params);
            if (O3 == null) {
            String value = aqiMapper.getAqiFromHangzhou(params);
            if (value == null) {
                params.put("timeUnits", s);
                O3 = aqiMapper.getO3FromHistory(params);
                value = aqiMapper.getAqiFromHistory(params);
            }
            if (O3 != null) {
                values.add(O3.replace("\"", ""));
            if (value != null) {
                values.add(value.replace("\"", ""));
            }
        }
        Double sum = 0.0;
        for (String value : values) {
            sum += Double.valueOf(value);
        for (String v : values) {
            sum += Double.valueOf(v);
        }
        return sum / values.size();
    }
@@ -261,8 +310,6 @@
        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()) {
@@ -273,32 +320,38 @@
                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 (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) {
                    hours.add(params.get("sHour").toString());
                    hours.add(params.get("eHour").toString());
                    params.put("hours", hours);
                    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);
                }
                hashMap.put(key, values);
            }
        }
        return hashMap;
@@ -613,7 +666,7 @@
            windScale = 10;
        } else if (windSpeed <= 32.6) {
            windScale = 11;
        }else if (windSpeed <= 36.9) {
        } else if (windSpeed <= 36.9) {
            windScale = 12;
        }
        return windScale;
src/main/resources/mapper/AQIMapper.xml
@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.moral.mapper.AQIMapper">
    <cache/>
    <select id="getCityAqiConfig" resultType="java.util.LinkedHashMap">
        select * from city_aqi_config;
    </select>
@@ -26,8 +24,8 @@
        </foreach>
    </select>
    <select id="getO3ByTime" resultType="java.lang.String" useCache="true">
        select ifnull(aqi_json->'$.O3C',aqi_json->'$.O3') as 'O3'
    <select id="getAqiFromHangzhou" resultType="java.lang.String">
        select ifnull(aqi_json->'$.${sensorKey}',aqi_json->'$.${sensorKey1}') as '${sensorKey}'
        from hangzhou_aqi
        where city_code=#{cityCode}
        and DATE_FORMAT(time, #{typeFormat})=#{time}
@@ -41,8 +39,8 @@
        and DATE_FORMAT(time, '%Y%H')=#{yearAndHour}
    </select>
    <select id="getO3FromHistory" resultType="java.lang.String" useCache="true">
        select value ->'$.O3C' as 'O3'
    <select id="getAqiFromHistory" resultType="java.lang.String">
        select value ->'$.${sensorKey}' as '${sensorKey}'
        from history_aqi_${timeUnits}
        where city_code=#{cityCode}
        and DATE_FORMAT(time, #{typeFormat})=#{time}
src/main/resources/mapper/WeatherMapper.xml
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.moral.mapper.WeatherMapper">
    <cache/>
    <select id="getCityWeatherConfig" resultType="java.util.Map">
        select city_code AS cityCode,
@@ -116,11 +115,20 @@
        )
    </select>
    <select id="getSampleFromHistoryWeather" resultType="java.lang.String" useCache="true">
    <select id="getSampleFromHistoryWeather" resultType="java.lang.String">
        select DATE_FORMAT(time, #{typeFormat}) as time
        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}
            </if>
@@ -160,10 +168,13 @@
                </foreach>
            </if>
            <if test="month != null">
                and DATE_FORMAT(time, '%m') = #{month}
            </if>
        </where>
    </select>
    <select id="getSampleFromRealWeather" resultType="java.lang.String" useCache="true">
    <select id="getSampleFromRealWeather" resultType="java.lang.String">
        select DATE_FORMAT(time, #{typeFormat}) as time
        from real_weather
        <where>
@@ -171,6 +182,10 @@
            <if test="windDir != null">
                and json->'$.windDir' = #{windDir}
            </if>
            <if test="windScale != null">
                and json->'$.windScale' = #{windScale}
            </if>
            <if test="condition == 0">
@@ -213,7 +228,7 @@
            </if>
            <if test="month != null">
                and DATE_FORMAT(time, '%m')=#{month}
                and DATE_FORMAT(time, '%m') = #{month}
            </if>
        </where>
    </select>