lizijie
2022-08-15 d0857416b2d72cac3b5b3cac449907c66feac737
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;
@@ -66,13 +67,17 @@
        ks.put("cityCode", 320583);
        ks.put("cityId", 101190404);
        cityList.add(ks);
        int count = 0;
        Map<String, Object> hq = new HashMap<>();
        hq.put("cityCode", 320505);
        hq.put("cityId", 101190406);
        cityList.add(hq);
        int state = 200;
        for (Map<String, Object> map : cityList) {
            String cityCode = map.get("cityCode").toString();
            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;
            if (!dataMap.get("code").equals("200")) {
                return 500;
            }
            String json = JSONObject.toJSONString(dataMap);
            dataMap = (Map<String, Object>) JSONObject.parse(json);
@@ -125,6 +130,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);
@@ -132,14 +138,13 @@
                hashMap.put("end", end);
                hashMap.put("typeFormat", "%Y-%m-%d %H:%i:%s");
                int condition = Integer.parseInt(nextDayMap.get("condition").toString());
                double temp = Double.parseDouble(nextDayMap.get("temp").toString());
                int temp = Integer.parseInt(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());
                //风级
                int windScale = Integer.parseInt(nextDayMap.get("windScale").toString());
                String time = nextDayMap.remove("time").toString();
                //当前小时
                hours.add(time.substring(11, 13));
                c.setTime(sdf1.parse(time));
@@ -164,35 +169,83 @@
                }
                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);
                //根据天气筛选
                List<Map<String, Object>> O3Samples = getSample(hashMap, years);
                if (O3Samples.size() > 100) {
                    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) {
                        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, 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颗粒物预测,温度,风向风级,气压
                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("startTemp", temp - 1);
                hashMap1.put("endTemp", temp + 1);
                List<Map<String, Object>> pmSamples = getSample(hashMap1, years);
                if (pmSamples.size() > 100) {
                    pmSamples = pmSamples.parallelStream()
                            .filter(sample -> Integer.parseInt(sample.get("windScale").toString()) == windScale)
                            .collect(Collectors.toList());
                    if (pmSamples.size() > 100) {
                        pmSamples = pmSamples.parallelStream()
                                .filter(sample -> sample.get("windDir").toString().replace("\"", "").equals(windDir))
                                .collect(Collectors.toList());
                        if (pmSamples.size() > 100) {
                            pmSamples = pmSamples.parallelStream()
                                    .filter(sample -> Integer.parseInt(sample.get("pressure").toString()) >= pressure - 5 && Integer.parseInt(sample.get("pressure").toString()) <= pressure + 5)
                                    .collect(Collectors.toList());
                        }
                    }
                }
                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) {
@@ -212,13 +265,13 @@
            parameters.put("end", c.getTime());
            parameters.put("cityCode", cityCode);
            weatherMapper.deleteByTime(parameters);
            count = count + weatherMapper.insertForecastWeather(resultList);
            weatherMapper.insertForecastWeather(resultList);
        }
        return count;
        return state;
    }
    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);
@@ -229,28 +282,32 @@
        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);
        double sum = 0.0;
        for (String v : values) {
            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<>();
@@ -261,46 +318,21 @@
        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);
        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);
        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;
    }
@@ -320,15 +352,15 @@
        RestTemplate restTemplate = new RestTemplate();
        c.setTime(new Date());
        Date endTime = sdf.parse(sdf.format(c.getTime()));
        c.add(Calendar.MONTH, -2);
        c.add(Calendar.MONTH, -1);
        Date startTime = sdf.parse(sdf.format(c.getTime()));
        int count = 0;
        int state = 200;
        List<Map<String, Object>> cityList = weatherMapper.getCityWeatherConfig();
        for (Map<String, Object> map : cityList) {
            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;
            if (!dataMap.get("code").equals("200")) {
                return 500;
            }
            String json = JSONObject.toJSONString(dataMap);
            dataMap = (Map<String, Object>) JSONObject.parse(json);
@@ -486,8 +518,8 @@
                        sum2 = sum2 + temp * temp;
                    }
                    //光照
                    double cloud = Double.valueOf(listMap.get("cloud").toString().replace("\"", "").split("\\.")[0]);
                    if (listMap.get("beam") != null) {
                    if (listMap.get("beam") != null&&listMap.get("cloud") != null) {
                        double cloud = Double.valueOf(listMap.get("cloud").toString().replace("\"", "").split("\\.")[0]);
                        double beam = Double.valueOf(listMap.get("beam").toString().replace("\"", "").split("\\.")[0]);
                        cloudSum = cloudSum + cloud;
                        beamSum = beamSum + beam;
@@ -541,9 +573,9 @@
            parameters.put("end", end);
            parameters.put("cityCode", cityCode);
            weatherMapper.deleteByTime(parameters);
            count = count + weatherMapper.insertForecastWeather(resultList);
            weatherMapper.insertForecastWeather(resultList);
        }
        return count;
        return state;
    }
    @Override
@@ -555,12 +587,12 @@
        Date now = sdf1.parse(sdf.format(c.getTime()) + ":00:00");
        RestTemplate restTemplate = new RestTemplate();
        List<Map<String, Object>> cityList = weatherMapper.getCityWeatherConfig();
        int count = 0;
        int state = 200;
        for (Map<String, Object> map : cityList) {
            String id = map.get("cityId").toString();
            Map<String, Object> dataMap = restTemplate.getForObject("https://api.heweather.net/v7/weather/now?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, id);
            if (dataMap == null) {
                return count;
            if (!dataMap.get("code").equals("200")) {
                return 500;
            }
            String json = JSONObject.toJSONString(dataMap);
            dataMap = (Map<String, Object>) JSONObject.parse(json);
@@ -586,7 +618,8 @@
            resultList.add(resultMap);
        }
        weatherMapper.deleteRealWeather(sdf1.format(now));
        return weatherMapper.insertRealWeather(resultList);
        weatherMapper.insertRealWeather(resultList);
        return state;
    }
    private int getWindScale(Double windSpeed) {
@@ -613,7 +646,7 @@
            windScale = 10;
        } else if (windSpeed <= 32.6) {
            windScale = 11;
        }else if (windSpeed <= 36.9) {
        } else if (windSpeed <= 36.9) {
            windScale = 12;
        }
        return windScale;