jinpengyong
2022-03-22 21ce462f92abbaff092ce6adf49a3ee555bd0121
screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java
@@ -96,6 +96,17 @@
        Map<Integer, List<CityAqiMonthly>> lastMonthData = lastCityAqiMonthly.stream()
                .collect(Collectors.groupingBy(CityAqiMonthly::getCityCode));
        //获取去年本月数据
        Date thisMonthOfLastYear = DateUtils.addYears(start, -1);
        queryWrapper.clear();
        queryWrapper.select("city_code", "value")
                .eq("time", DateUtils.dateToDateString(thisMonthOfLastYear));
        List<CityAqiMonthly> thisMonthOfLastYearList = cityAqiMonthlyMapper.selectList(queryWrapper);
        Map<Integer, List<CityAqiMonthly>> thisMonthOfLastYearData = thisMonthOfLastYearList.stream()
                .collect(Collectors.groupingBy(CityAqiMonthly::getCityCode));
        List<CityAqiMonthly> cityAqiMonthlyList = new ArrayList<>();
        Date finalStart = start;
@@ -145,7 +156,7 @@
            Double compositeIndex = ComprehensiveIndexUtils.dailyData(jsonMap);
            jsonMap.put("compositeIndex", compositeIndex);
            //本月综指同上月对比
            //本月综指同上月对比(综合指数环比)
            List<CityAqiMonthly> cityAqiMonthlies = lastMonthData.get(Integer.parseInt(cityCode));
            if (!ObjectUtils.isEmpty(cityAqiMonthlies)) {
                CityAqiMonthly monthly = cityAqiMonthlies.get(0);
@@ -155,9 +166,44 @@
                String format = decimalFormat.format((compositeIndex - lastCompositeIndex) / lastCompositeIndex);
                jsonMap.put("monthContrast", format);
            }
            //获取去年本月数据,用于计算同比
            List<CityAqiMonthly> thisMonthOfLastYears = thisMonthOfLastYearData.get(Integer.parseInt(cityCode));
            //各因子同比计算
            Map<String, Object> yearOnYearValue = yearOnYearOfSensor(thisMonthOfLastYears, jsonMap);
            if (yearOnYearValue != null) {
                jsonMap.putAll(yearOnYearValue);
            }
            cityAqiMonthly.setValue(JSONObject.toJSONString(jsonMap));
            cityAqiMonthlyList.add(cityAqiMonthly);
        });
        cityAqiMonthlyMapper.insertCityAqiMonthly(cityAqiMonthlyList);
    }
    /**
     * @param thisMonthOfLastYearData 去年本月数据
     * @param currentData             当前月该数据
     */
    private Map<String, Object> yearOnYearOfSensor(List<CityAqiMonthly> thisMonthOfLastYearData, Map<String, Object> currentData) {
        Map<String, Object> result = null;
        //需要计算同比的因子
        List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2", "CO", "O3", "compositeIndex");
        if (!ObjectUtils.isEmpty(thisMonthOfLastYearData)) {
            result = new HashMap<>();
            CityAqiMonthly monthly = thisMonthOfLastYearData.get(0);
            Map<String, Object> map = JSONObject.parseObject(monthly.getValue(), Map.class);
            for (String sensor : sensors) {
                //去年本月该因子值
                double thisMonthOfLeastYearValue = Double.parseDouble(map.get(sensor).toString());
                //当前该因子值
                double currentValue = Double.parseDouble(currentData.get(sensor).toString());
                DecimalFormat decimalFormat = new DecimalFormat("0.00%");
                String format = decimalFormat.format((currentValue - thisMonthOfLeastYearValue) / thisMonthOfLeastYearValue);
                result.put(sensor + "_yearOnYear", format);
            }
        }
        return result;
    }
}