From 994d4b65f78e5121680de31b14161003620d746e Mon Sep 17 00:00:00 2001 From: jinpengyong <jpy123456> Date: Wed, 16 Nov 2022 10:45:22 +0800 Subject: [PATCH] 修改张家港国控站数据 --- screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java | 104 ++++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 89 insertions(+), 15 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 7fd2098..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 @@ -2,6 +2,7 @@ import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.moral.api.entity.CityAqiDaily; import com.moral.api.entity.CityAqiMonthly; import com.moral.api.mapper.CityAqiMonthlyMapper; @@ -57,25 +58,67 @@ //���������������������1��� Date end = DateUtils.addMonths(start, 1); + //���������1������������������������������������ + //������1��������������������������������������������������������� + int day = DateUtils.getDay(new Date()); + UpdateWrapper<CityAqiMonthly> cityAqiMonthlyUpdateWrapper = new UpdateWrapper<>(); + if (day == 1) { + cityAqiMonthlyUpdateWrapper.eq("time", start); + } else { + start = end; + end = DateUtils.addMonths(start, 1); + lastLastMonth = DateUtils.addMonths(start, -1); + cityAqiMonthlyUpdateWrapper.eq("time", start); + } + cityAqiMonthlyMapper.delete(cityAqiMonthlyUpdateWrapper); + //������������������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.parallelStream().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", lastLastMonth); + //������������������ + 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<>(); + + Date finalStart = start; data.forEach((cityCode, value) -> { - Map<String, Object> jsonMap = new HashMap<>(); CityAqiMonthly cityAqiMonthly = new CityAqiMonthly(); - cityAqiMonthly.setCityCode(Integer.parseInt(cityCode)); - cityAqiMonthly.setTime(start); + Map<String, Object> jsonMap = new HashMap<>(); + cityAqiMonthly.setCityCode(cityCode); + cityAqiMonthly.setTime(finalStart); Map<String, Object> params = new HashMap<>(); List<Map<String, Object>> temp = new ArrayList<>(); @@ -102,7 +145,7 @@ } sensors.forEach(sensor -> { - OptionalDouble optionalDouble = value.parallelStream().flatMapToDouble(v -> { + OptionalDouble optionalDouble = value.stream().flatMapToDouble(v -> { Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class); double aDouble = Double.parseDouble(dataValue.get(sensor).toString()); return DoubleStream.of(aDouble); @@ -117,13 +160,8 @@ Double compositeIndex = ComprehensiveIndexUtils.dailyData(jsonMap); jsonMap.put("compositeIndex", compositeIndex); - //��������������������������� - QueryWrapper<CityAqiMonthly> queryWrapper = new QueryWrapper<>(); - queryWrapper.select("value") - .eq("city_code", cityCode) - .eq("time", DateUtils.dateToDateString(lastLastMonth)); - //��������������������� - CityAqiMonthly lastCityAqiMonthly = cityAqiMonthlyMapper.selectOne(queryWrapper); + //���������������������������(������������������) + 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()); @@ -131,8 +169,44 @@ 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)); - cityAqiMonthlyMapper.insert(cityAqiMonthly); + 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