From 6344a8b6b7e2853dbfa9e6506ff2e17a31930297 Mon Sep 17 00:00:00 2001 From: cjl <276999030@qq.com> Date: Mon, 10 Jul 2023 14:17:40 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/cjl' into dev --- screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java | 77 +++++++++++++++++++++++++++++++------- 1 files changed, 63 insertions(+), 14 deletions(-) diff --git a/screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java b/screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java index 1745e77..346a637 100644 --- a/screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java +++ b/screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java @@ -76,25 +76,40 @@ //������������������aqi������������ QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>(); wrapper.select("city_code", "time", "value") - .ge("time", DateUtils.dateToDateString(start)) - .lt("time", DateUtils.dateToDateString(end)); + .ge("time", start) + .lt("time", end); List<Map<String, Object>> monthlyData = cityAqiDailyService.listMaps(wrapper); if (monthlyData.size() == 0) { return; } //���city_code������ - Map<String, List<Map<String, Object>>> data = monthlyData.stream() - .collect(Collectors.groupingBy(o -> o.get("city_code").toString())); + Map<Integer, List<Map<String, Object>>> data = monthlyData.stream() + .collect(Collectors.groupingBy(o ->Integer.parseInt(o.get("city_code").toString()) )); //������������������������������������������������ QueryWrapper<CityAqiMonthly> queryWrapper = new QueryWrapper<>(); queryWrapper.select("city_code", "value") - .eq("time", DateUtils.dateToDateString(lastLastMonth)); + .eq("time", lastLastMonth); //������������������ - List<CityAqiMonthly> lastCityAqiMonthly = cityAqiMonthlyMapper.selectList(queryWrapper); - Map<Integer, List<CityAqiMonthly>> lastMonthData = lastCityAqiMonthly.stream() - .collect(Collectors.groupingBy(CityAqiMonthly::getCityCode)); + List<CityAqiMonthly> lastCityAqiMonthlyList = cityAqiMonthlyMapper.selectList(queryWrapper); + Map<Integer, CityAqiMonthly> lastMonthData = new HashMap<>(); + for (CityAqiMonthly cityAqiMonthly : lastCityAqiMonthlyList) { + lastMonthData.put(cityAqiMonthly.getCityCode(), cityAqiMonthly); + } + + + //������������������������ + Date thisMonthOfLastYear = DateUtils.addYears(start, -1); + queryWrapper.clear(); + queryWrapper.select("city_code", "value") + .eq("time", thisMonthOfLastYear); + List<CityAqiMonthly> thisMonthOfLastYearList = cityAqiMonthlyMapper.selectList(queryWrapper); + Map<Integer, CityAqiMonthly> thisMonthOfLastYearData = new HashMap<>(); + for (CityAqiMonthly cityAqiMonthly : thisMonthOfLastYearList) { + thisMonthOfLastYearData.put(cityAqiMonthly.getCityCode(), cityAqiMonthly); + } + List<CityAqiMonthly> cityAqiMonthlyList = new ArrayList<>(); @@ -102,7 +117,7 @@ data.forEach((cityCode, value) -> { CityAqiMonthly cityAqiMonthly = new CityAqiMonthly(); Map<String, Object> jsonMap = new HashMap<>(); - cityAqiMonthly.setCityCode(Integer.parseInt(cityCode)); + cityAqiMonthly.setCityCode(cityCode); cityAqiMonthly.setTime(finalStart); Map<String, Object> params = new HashMap<>(); @@ -145,19 +160,53 @@ 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); - Map<String, Object> map = JSONObject.parseObject(monthly.getValue(), Map.class); + //���������������������������(������������������) + CityAqiMonthly lastCityAqiMonthly = lastMonthData.get(cityCode); + if (lastCityAqiMonthly != null) { + Map<String, Object> map = JSONObject.parseObject(lastCityAqiMonthly.getValue(), Map.class); double lastCompositeIndex = Double.parseDouble(map.get("compositeIndex").toString()); DecimalFormat decimalFormat = new DecimalFormat("0.00%"); String format = decimalFormat.format((compositeIndex - lastCompositeIndex) / lastCompositeIndex); jsonMap.put("monthContrast", format); } + + //��������������������������������������������� + CityAqiMonthly thisMonthOfLastYears = thisMonthOfLastYearData.get(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(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 (thisMonthOfLastYearData != null) { + result = new HashMap<>(); + Map<String, Object> map = JSONObject.parseObject(thisMonthOfLastYearData.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; + } } -- Gitblit v1.8.0