From 3ec35029adfb63e79ad17bf351317768e687ec2b Mon Sep 17 00:00:00 2001 From: jinpengyong <jpy123456> Date: Mon, 17 Jan 2022 16:22:25 +0800 Subject: [PATCH] city_aqi月均值,年均值update --- screen-job/src/main/java/com/moral/api/service/impl/CityAqiYearlyServiceImpl.java | 94 +++++++++++++++++++++------------------------- 1 files changed, 43 insertions(+), 51 deletions(-) diff --git a/screen-job/src/main/java/com/moral/api/service/impl/CityAqiYearlyServiceImpl.java b/screen-job/src/main/java/com/moral/api/service/impl/CityAqiYearlyServiceImpl.java index 95ef4b0..fbea9a0 100644 --- a/screen-job/src/main/java/com/moral/api/service/impl/CityAqiYearlyServiceImpl.java +++ b/screen-job/src/main/java/com/moral/api/service/impl/CityAqiYearlyServiceImpl.java @@ -8,7 +8,6 @@ import com.moral.api.service.CityAqiDailyService; import com.moral.api.service.CityAqiYearlyService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.moral.constant.Constants; import com.moral.util.AmendUtils; import com.moral.util.ComprehensiveIndexUtils; import com.moral.util.DateUtils; @@ -18,15 +17,12 @@ import org.springframework.util.ObjectUtils; import java.text.DecimalFormat; -import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.OptionalDouble; import java.util.stream.Collectors; -import java.util.stream.DoubleStream; /** * <p> @@ -48,7 +44,12 @@ @Override public void insertCityAqiYearly() { - //��������������������������� + /* + * ��������������� + * PM2.5,PM10,CO,���������95��������� + * SO2,NO2,���������98��������� + * O3���������������90��������� + * */ List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2"); //���������������������1��� @@ -59,7 +60,7 @@ Date end = DateUtils.getDate(DateUtils.getDateAddYear(DateUtils.dateToDateString(start, DateUtils.yyyy), 1), DateUtils.yyyy); - //������������������aqi������������ + //������������������aqi��������� QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>(); wrapper.select("city_code", "time", "value") .ge("time", DateUtils.dateToDateString(start)) @@ -70,7 +71,18 @@ return; } //���city_code������ - Map<String, List<Map<String, Object>>> data = monthlyData.parallelStream().collect(Collectors.groupingBy(o -> o.get("city_code").toString())); + Map<String, List<Map<String, Object>>> data = monthlyData.stream() + .collect(Collectors.groupingBy(o -> o.get("city_code").toString())); + + //������������ + QueryWrapper<CityAqiYearly> queryWrapper = new QueryWrapper<>(); + queryWrapper.select("city_code", "value") + .eq("time", DateUtils.dateToDateString(lastLastYear)); + //������������������ + List<CityAqiYearly> lastCityAqiYearly = cityAqiYearlyMapper.selectList(queryWrapper); + Map<Integer, List<CityAqiYearly>> lastYearData = lastCityAqiYearly.stream() + .collect(Collectors.groupingBy(CityAqiYearly::getCityCode)); + CityAqiYearly cityAqiYearly = new CityAqiYearly(); data.forEach((cityCode, value) -> { @@ -78,57 +90,39 @@ cityAqiYearly.setCityCode(Integer.parseInt(cityCode)); cityAqiYearly.setTime(start); + //PM2.5 + Double pm25Avg = AmendUtils.getAvgOfYear(value, "PM2_5"); + jsonMap.put("PM2_5", pm25Avg); - Map<String, Object> params = new HashMap<>(); - List<Map<String, Object>> temp = new ArrayList<>(); + //PM10 + Double pm10Avg = AmendUtils.getAvgOfYear(value, "PM10"); + jsonMap.put("PM10", pm10Avg); - for (Map<String, Object> map : value) { - Map<String, Object> sensorsValue = JSONObject.parseObject(map.get("value").toString(), Map.class); - Map<String, Object> tempMap = new HashMap<>(); - tempMap.put(Constants.SENSOR_CODE_CO, sensorsValue.get("PM2_5")); - tempMap.put(Constants.SENSOR_CODE_O3, sensorsValue.get("O3")); - Map<String, Object> hashMap = new HashMap<>(); - hashMap.put("value", JSONObject.toJSONString(tempMap)); - temp.add(hashMap); - } - params.put("data", temp); - //1. CO 95������������������������ - Map<String, Object> coAvgOfWeekOrMonth = AmendUtils.getCOAvgOfWeekOrMonth(params); - if (!ObjectUtils.isEmpty(coAvgOfWeekOrMonth)) { - jsonMap.put("CO", coAvgOfWeekOrMonth.get(Constants.SENSOR_CODE_CO)); - } + //SO2 + Double so2Avg = AmendUtils.getAvgOfYear(value, "SO2"); + jsonMap.put("SO2", so2Avg); - //2. O3 90������������������������ - Map<String, Object> o3AvgOfWeekOrMonth = AmendUtils.getO3AvgOfWeekOrMonth(params); - if (!ObjectUtils.isEmpty(o3AvgOfWeekOrMonth)) { - jsonMap.put("O3", o3AvgOfWeekOrMonth.get(Constants.SENSOR_CODE_O3)); - } + //NO2 + Double no2Avg = AmendUtils.getAvgOfYear(value, "NO2"); + jsonMap.put("NO2", no2Avg); - sensors.forEach(sensor -> { - OptionalDouble optionalDouble = value.parallelStream().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); - }).average(); - if (optionalDouble.isPresent()) { - //��������������������� - jsonMap.put(sensor, AmendUtils.sciCal(optionalDouble.getAsDouble(), 0)); - } - }); + //CO + Double coAvg = AmendUtils.getAvgOfYear(value, "CO"); + jsonMap.put("CO", coAvg); + + //O3 + Double o3Avg = AmendUtils.getAvgOfYear(value, "O3"); + jsonMap.put("O3", o3Avg); //��������������������� Double compositeIndex = ComprehensiveIndexUtils.dailyData(jsonMap); jsonMap.put("compositeIndex", compositeIndex); - //��������������������������� - QueryWrapper<CityAqiYearly> queryWrapper = new QueryWrapper<>(); - queryWrapper.select("value") - .eq("city_code", cityCode) - .eq("time", DateUtils.dateToDateString(lastLastYear)); - //��������������������� - CityAqiYearly lastCityAqiYearly = cityAqiYearlyMapper.selectOne(queryWrapper); - if (lastCityAqiYearly != null) { - Map<String, Object> map = JSONObject.parseObject(lastCityAqiYearly.getValue(), Map.class); + //��������������������������� + List<CityAqiYearly> cityAqiYearlies = lastYearData.get(Integer.parseInt(cityCode)); + if (!ObjectUtils.isEmpty(cityAqiYearlies)) { + CityAqiYearly yearly = cityAqiYearlies.get(0); + Map<String, Object> map = JSONObject.parseObject(yearly.getValue(), Map.class); double lastCompositeIndex = Double.parseDouble(map.get("compositeIndex").toString()); DecimalFormat decimalFormat = new DecimalFormat("0.00%"); String format = decimalFormat.format((compositeIndex - lastCompositeIndex) / lastCompositeIndex); @@ -137,7 +131,5 @@ cityAqiYearly.setValue(JSONObject.toJSONString(jsonMap)); cityAqiYearlyMapper.insert(cityAqiYearly); }); - - } } -- Gitblit v1.8.0