From 5c427a35595e2deb2c1a025ff9a4c164d498e01d Mon Sep 17 00:00:00 2001 From: jinpengyong <jpy123456> Date: Fri, 24 Dec 2021 11:00:44 +0800 Subject: [PATCH] 行业贡献率:行业列表,传感器列表接口 --- screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java | 596 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 571 insertions(+), 25 deletions(-) diff --git a/screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java b/screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java index 03b92c6..9ab60a0 100644 --- a/screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java +++ b/screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java @@ -5,20 +5,26 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.moral.api.entity.CityAqi; import com.moral.api.entity.CityAqiDaily; +import com.moral.api.entity.CityAqiMonthly; +import com.moral.api.entity.CityAqiYearly; import com.moral.api.entity.Forecast; import com.moral.api.entity.Organization; import com.moral.api.entity.SysArea; import com.moral.api.mapper.CityAqiMapper; import com.moral.api.mapper.ForecastMapper; import com.moral.api.service.CityAqiDailyService; +import com.moral.api.service.CityAqiMonthlyService; import com.moral.api.service.CityAqiService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.moral.api.service.CityAqiYearlyService; import com.moral.api.service.OrganizationService; import com.moral.api.service.SysAreaService; import com.moral.constant.Constants; import com.moral.constant.RedisConstants; +import com.moral.pojo.AQI; import com.moral.util.AQIUtils; import com.moral.util.AmendUtils; +import com.moral.util.ComprehensiveIndexUtils; import com.moral.util.DateUtils; import com.moral.util.MathUtils; @@ -28,7 +34,9 @@ import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; +import java.text.DecimalFormat; import java.util.*; +import java.util.stream.Collectors; import java.util.stream.DoubleStream; /** @@ -60,6 +68,12 @@ @Autowired private CityAqiDailyService cityAqiDailyService; + @Autowired + private CityAqiMonthlyService cityAqiMonthlyService; + + @Autowired + private CityAqiYearlyService cityAqiYearlyService; + @Override public List<Map<String, Object>> measuredCompareForecastOfO3(Map<String, Object> params) { String regionCode = params.get("regionCode").toString(); @@ -88,7 +102,7 @@ Date date = (Date) forecastDatum.get("time"); String value = forecastDatum.get("value").toString(); Map<String, Object> data = JSONObject.parseObject(value, Map.class); - Object o3 = data.get("o3"); + Object o3 = data.get("O3"); if (i == DateUtils.getHour(date) * 2) { if (!ObjectUtils.isEmpty(o3)) { map.put("O3", o3); @@ -103,7 +117,7 @@ Date date = (Date) measuredDatum.get("time"); String value = measuredDatum.get("value").toString(); Map<String, Object> data = JSONObject.parseObject(value, Map.class); - Object o3 = data.get("o3"); + Object o3 = data.get("O3"); if (i == (DateUtils.getHour(date) * 2 + 1)) { if (!ObjectUtils.isEmpty(o3)) { map.put("O3", o3); @@ -122,9 +136,9 @@ if (value == null) value = queryCityAqiByRegionCodeFromDB(regionCode); //������AQI������������������ - if (value == null || value.get("aqi") == null) + if (value == null || value.get("AQI") == null) return null; - Integer aqi = Integer.parseInt(value.get("aqi").toString()); + Integer aqi = Integer.parseInt(value.get("AQI").toString()); String category = AQIUtils.classOfPollutionByAqi(aqi); value.put("category", category); return value; @@ -162,17 +176,17 @@ //������������������������ cityAqis.sort(Comparator.comparing(CityAqi::getTime)); } - //������������������,map���key���HH:mm������������������value���aqi��������� + //������������������,map���key���yyyy-MM-dd HH:mm������������������value���aqi��������� Map<String, Object> result = new LinkedHashMap<>(); for (CityAqi aqi : cityAqis) { - String key = DateUtils.dateToDateString(aqi.getTime(), "HH:mm"); + String key = DateUtils.dateToDateString(aqi.getTime(), "yyyy-MM-dd HH:mm"); String allDataJson = aqi.getValue(); if (allDataJson == null) { result.put(key, ""); continue; } Map<String, Object> allDataMap = JSON.parseObject(allDataJson, Map.class); - Object aqiData = allDataMap.get("aqi"); + Object aqiData = allDataMap.get("AQI"); if (aqiData == null) result.put(key, ""); else @@ -195,11 +209,545 @@ //��������������� Map<String, Object> sixParamAvg = calculate6ParamAvg(cityAqis); //������������aqi������������������ - Map<String, Object> result = AQIUtils.hourlyAqi_pollutant(sixParamAvg); + Map<String, Object> result = new HashMap<>(); + AQI aqi = AQIUtils.hourlyAQI(sixParamAvg); + result.put("aqi", aqi.getAQIValue()); + result.put("pollutant", aqi.getPrimaryPollutantNames()); //��������������������� CityAqi lastCityAqi = cityAqis.get(cityAqis.size() - 1); String time = DateUtils.dateToDateString(lastCityAqi.getTime(), "HH:mm"); result.put("time", time); + return result; + } + + @Override + public List<Map<String, Object>> rankingDetails(Map<String, Object> params) { + List<Map<String, Object>> result = new ArrayList<>(); + int regionCode = Integer.parseInt(params.get("regionCode").toString()); + String type = params.get("type").toString(); + String time = null; + if (!ObjectUtils.isEmpty(params.get("time"))) { + time = params.get("time").toString(); + } + String start = null; + String end = null; + if (!ObjectUtils.isEmpty(params.get("start")) || !ObjectUtils.isEmpty(params.get("end"))) { + start = params.get("start").toString(); + end = params.get("end").toString(); + } + String cityType = params.get("cityType").toString(); + + String s = String.valueOf(regionCode); + //���������������,���code + Integer curProvinceCode = Integer.parseInt(s.substring(0, 2) + "0000"); + Integer curCityCode = Integer.parseInt(s.substring(0, 4) + "00"); + + QueryWrapper<SysArea> areaWrapper = new QueryWrapper<>(); + if ("province".equals(cityType)) { + //��������������������� + areaWrapper.select("area_code").eq("parent_code", curProvinceCode); + } else { + //������������������������ + areaWrapper.select("area_code").eq("parent_code", curCityCode); + } + List<Object> regionCodes = sysAreaService.listObjs(areaWrapper); + + switch (type) { + case "today": + result = accumulatedTodayRank(regionCodes); + break; + case "hour": + time = new StringBuilder(time).replace(10, 11, " ").toString() + ":00:00"; + result = hourRank(regionCodes, time); + break; + case "day": + time = time + " 00:00:00"; + result = dayRank(regionCodes, time); + break; + case "month": + time = time + "-01 00:00:00"; + result = monthRank(regionCodes, time); + break; + case "year": + time = time + "-01-01 00:00:00"; + result = yearRank(regionCodes, time); + break; + case "custom": + start = start + " 00:00:00"; + end = end + " :00:00"; + result = customRank(regionCodes, start, end); + break; + default: + break; + } + return result; + } + + /** + * @param regionCodes ��������������������������������������� + * @return ��������������������������� + */ + private List<Map<String, Object>> accumulatedTodayRank(List<Object> regionCodes) { + List<Map<String, Object>> result = new ArrayList<>(); + List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2", "CO", "O3"); + Date now = new Date(); + String today = DateUtils.dateToDateString(now, DateUtils.yyyy_MM_dd_EN); + QueryWrapper<CityAqi> wrapper = new QueryWrapper<>(); + wrapper.select("city_code", "value") + .ge("time", today) + .in("city_code", regionCodes); + List<Map<String, Object>> cumulativeData = cityAqiMapper.selectMaps(wrapper); + //���city_code������ + Map<String, List<Map<String, Object>>> data = cumulativeData.parallelStream().collect(Collectors.groupingBy(o -> o.get("city_code").toString())); + data.forEach((cityCode, value) -> { + List<Double> doubles = new ArrayList<>(); + for (Map<String, Object> objectMap : value) { + Object o = JSONObject.parseObject((String) objectMap.get("value"), Map.class).get("O3_8H"); + if (!ObjectUtils.isEmpty(o)) { + double v = Double.parseDouble(o.toString()); + doubles.add(v); + } + } + + Map<String, Object> dataMap = new HashMap<>(); + sensors.forEach(sensor -> { + OptionalDouble optionalDouble = value.parallelStream().flatMapToDouble(v -> { + Map<String, Object> sensorValue = JSONObject.parseObject((String) v.get("value"), Map.class); + Object o = sensorValue.get(sensor); + if (ObjectUtils.isEmpty(o)) { + return null; + } + double aDouble = Double.parseDouble(o.toString()); + return DoubleStream.of(aDouble); + }).average(); + + if (optionalDouble.isPresent()) { + //��������������������� + double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 0); + if ("CO".equals(sensor)) { + sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 1); + } + dataMap.put(sensor, sciCal); + } + }); + + //������������O3_8H������,������������O3������8H��������� + if (!ObjectUtils.isEmpty(doubles)) { + dataMap.put("O3_8H", Collections.max(doubles)); + } + + //������������aqi,��������������������� + Map<String, Object> sixParamMap = new HashMap<>(); + sixParamMap.put(Constants.SENSOR_CODE_PM25, dataMap.get("PM2_5")); + sixParamMap.put(Constants.SENSOR_CODE_PM10, dataMap.get("PM10")); + sixParamMap.put(Constants.SENSOR_CODE_SO2, dataMap.get("SO2")); + sixParamMap.put(Constants.SENSOR_CODE_NO2, dataMap.get("NO2")); + sixParamMap.put(Constants.SENSOR_CODE_CO, dataMap.get("CO")); + sixParamMap.put(Constants.SENSOR_CODE_O3, dataMap.get("O3")); + AQI aqi = AQIUtils.dailyAQI(sixParamMap); + dataMap.put("AQI", aqi.getAQIValue()); + List<String> primaryPollutantNames = aqi.getPrimaryPollutantNames(); + String primaryPollutant = ""; + if (!ObjectUtils.isEmpty(primaryPollutantNames)) { + primaryPollutant = primaryPollutantNames.stream().map(String::valueOf).collect(Collectors.joining(",")); + } + dataMap.put("primaryPollutant", primaryPollutant); + + //������������������������������,O3������������O3_8H������ + Map<String, Object> compositeIndexMap = new HashMap<>(dataMap); + compositeIndexMap.put("O3", compositeIndexMap.get("O3_8H")); + Double compositeIndex = ComprehensiveIndexUtils.dailyData(compositeIndexMap); + dataMap.put("compositeIndex", compositeIndex); + + //��������� + QueryWrapper<SysArea> queryWrapper = new QueryWrapper<>(); + queryWrapper.select("area_name").eq("area_code", cityCode); + String areaName = sysAreaService.getOne(queryWrapper).getAreaName(); + dataMap.put("cityName", areaName); + + result.add(dataMap); + }); + return result; + } + + /** + * @param regionCodes ��������������������������������� + * @param time ��������������������������� 2021-11-04 13:00:00 + * @return ��������������������� + */ + private List<Map<String, Object>> hourRank(List<Object> regionCodes, String time) { + List<Map<String, Object>> result = new ArrayList<>(); + QueryWrapper<CityAqi> wrapper = new QueryWrapper<>(); + wrapper.select("value") + .eq("time", time) + .in("city_code", regionCodes); + List<Map<String, Object>> hourData = cityAqiMapper.selectMaps(wrapper); + for (Map<String, Object> hourDatum : hourData) { + Map<String, Object> value = JSONObject.parseObject((String) hourDatum.get("value"), Map.class); + List<String> primaryPollutantNames = (List<String>) value.get("primaryPollutant"); + String primaryPollutant = ""; + if (!ObjectUtils.isEmpty(primaryPollutantNames)) { + primaryPollutant = primaryPollutantNames.stream().map(String::valueOf).collect(Collectors.joining(",")); + } + value.put("primaryPollutant", primaryPollutant); + value.remove("pubtime"); + value.remove("rank"); + result.add(value); + } + return result; + } + + /** + * @param regionCodes ��������������������������������� + * @param time ��������������������������� 2021-11-04 00:00:00 + * @return ������������������ + */ + private List<Map<String, Object>> dayRank(List<Object> regionCodes, String time) { + List<Map<String, Object>> result = new ArrayList<>(); + QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>(); + wrapper.select("city_code", "value") + .eq("time", time) + .in("city_code", regionCodes); + List<Map<String, Object>> dayData = cityAqiDailyService.listMaps(wrapper); + for (Map<String, Object> dayDatum : dayData) { + Map<String, Object> value = JSONObject.parseObject((String) dayDatum.get("value"), Map.class); + List<String> primaryPollutantNames = (List<String>) value.get("primaryPollutant"); + String primaryPollutant = ""; + if (!ObjectUtils.isEmpty(primaryPollutantNames)) { + primaryPollutant = primaryPollutantNames.stream().map(String::valueOf).collect(Collectors.joining(",")); + } + value.put("primaryPollutant", primaryPollutant); + //��������� + QueryWrapper<SysArea> queryWrapper = new QueryWrapper<>(); + queryWrapper.select("area_name") + .eq("area_code", dayDatum.get("city_code")); + String areaName = sysAreaService.getOne(queryWrapper).getAreaName(); + value.put("cityName", areaName); + result.add(value); + } + return result; + } + + /** + * @param regionCodes ��������������������������������� + * @param time ��������������������������� 2021-11-01 00:00:00 ������1��� + * @return ������������������ + */ + private List<Map<String, Object>> monthRank(List<Object> regionCodes, String time) { + //��������������������������� + List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2"); + List<Map<String, Object>> result = new ArrayList<>(); + //���������������,������������������������������city_aqi_monthly������ + if (!time.substring(0, 7).equals(DateUtils.dateToDateString(new Date(), DateUtils.yyyy_MM_EN))) { + QueryWrapper<CityAqiMonthly> cityAqiMonthlyQueryWrapper = new QueryWrapper<>(); + for (Object regionCode : regionCodes) { + cityAqiMonthlyQueryWrapper.clear(); + cityAqiMonthlyQueryWrapper.select("value") + .eq("city_code", regionCode) + .eq("time", time); + CityAqiMonthly cityAqiMonthly = cityAqiMonthlyService.getOne(cityAqiMonthlyQueryWrapper); + if (cityAqiMonthly == null) { + continue; + } + String value = cityAqiMonthly.getValue(); + Map<String, Object> resultMap = JSONObject.parseObject(value, Map.class); + //��������� + QueryWrapper<SysArea> sysAreaQueryWrapper = new QueryWrapper<>(); + sysAreaQueryWrapper.select("area_name") + .eq("area_code", regionCode); + String areaName = sysAreaService.getOne(sysAreaQueryWrapper).getAreaName(); + resultMap.put("cityName", areaName); + result.add(resultMap); + } + return result; + } + + QueryWrapper<CityAqiDaily> cityAqiDailyQueryWrapper = new QueryWrapper<>(); + cityAqiDailyQueryWrapper.select("city_code", "value") + .ge("time", time) + .in("city_code", regionCodes); + List<Map<String, Object>> thisMonthData = cityAqiDailyService.listMaps(cityAqiDailyQueryWrapper); + //���city_code������ + Map<String, List<Map<String, Object>>> thisMonthMap = thisMonthData.parallelStream().collect(Collectors.groupingBy(o -> o.get("city_code").toString())); + thisMonthMap.forEach((cityCode, value) -> { + Map<String, Object> resultMap = new HashMap<>(); + + Map<String, Object> params = new HashMap<>(); + List<Map<String, Object>> temp = new ArrayList<>(); + 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("CO")); + 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)) { + resultMap.put("CO", coAvgOfWeekOrMonth.get(Constants.SENSOR_CODE_CO)); + } + + //2. O3 90������������������������ + Map<String, Object> o3AvgOfWeekOrMonth = AmendUtils.getO3AvgOfWeekOrMonth(params); + if (!ObjectUtils.isEmpty(o3AvgOfWeekOrMonth)) { + resultMap.put("O3", o3AvgOfWeekOrMonth.get(Constants.SENSOR_CODE_O3)); + } + + sensors.forEach(sensor -> { + OptionalDouble optionalDouble = value.parallelStream().flatMapToDouble(v -> { + Map<String, Object> sensorValue = JSONObject.parseObject((String) v.get("value"), Map.class); + Object o = sensorValue.get(sensor); + if (ObjectUtils.isEmpty(o)) { + return null; + } + double aDouble = Double.parseDouble(o.toString()); + return DoubleStream.of(aDouble); + }).average(); + + if (optionalDouble.isPresent()) { + //��������������������� + double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 0); + resultMap.put(sensor, sciCal); + } + }); + + //������������������ + Double compositeIndex = ComprehensiveIndexUtils.dailyData(resultMap); + resultMap.put("compositeIndex", compositeIndex); + + //������O3���O3_8H������ + resultMap.put("O3_8H", resultMap.remove("O3")); + + //��������������������������� + Date lastMonth = DateUtils.addMonths(DateUtils.getDate(time), -1); + QueryWrapper<CityAqiMonthly> queryWrapper = new QueryWrapper<>(); + queryWrapper.select("value") + .eq("city_code", cityCode) + .eq("time", DateUtils.dateToDateString(lastMonth)); + //������������������ + CityAqiMonthly lastCityAqiMonthly = cityAqiMonthlyService.getOne(queryWrapper); + String monthContrast = ""; + 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%"); + monthContrast = decimalFormat.format((compositeIndex - lastCompositeIndex) / lastCompositeIndex); + } + resultMap.put("monthContrast", monthContrast); + + //��������� + QueryWrapper<SysArea> sysAreaQueryWrapper = new QueryWrapper<>(); + sysAreaQueryWrapper.select("area_name") + .eq("area_code", cityCode); + String areaName = sysAreaService.getOne(sysAreaQueryWrapper).getAreaName(); + resultMap.put("cityName", areaName); + + result.add(resultMap); + }); + return result; + } + + /** + * @param regionCodes ��������������������������������� + * @param time ��������������������������� 2021-11-01 00:00:00 ������1���1��� + * @return ������������������ + */ + private List<Map<String, Object>> yearRank(List<Object> regionCodes, String time) { + //��������������������������� + List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2"); + List<Map<String, Object>> result = new ArrayList<>(); + //���������������,������������������������������city_aqi_monthly������ + if (!time.substring(0, 4).equals(DateUtils.dateToDateString(new Date(), DateUtils.yyyy))) { + QueryWrapper<CityAqiYearly> cityAqiYearlyQueryWrapper = new QueryWrapper<>(); + for (Object regionCode : regionCodes) { + cityAqiYearlyQueryWrapper.clear(); + cityAqiYearlyQueryWrapper.select("value") + .eq("city_code", regionCode) + .eq("time", time); + CityAqiYearly cityAqiYearly = cityAqiYearlyService.getOne(cityAqiYearlyQueryWrapper); + if (cityAqiYearly == null) { + continue; + } + String value = cityAqiYearly.getValue(); + Map<String, Object> resultMap = JSONObject.parseObject(value, Map.class); + //��������� + QueryWrapper<SysArea> sysAreaQueryWrapper = new QueryWrapper<>(); + sysAreaQueryWrapper.select("area_name") + .eq("area_code", regionCode); + String areaName = sysAreaService.getOne(sysAreaQueryWrapper).getAreaName(); + resultMap.put("cityName", areaName); + result.add(resultMap); + } + return result; + } + + QueryWrapper<CityAqiDaily> cityAqiDailyQueryWrapper = new QueryWrapper<>(); + cityAqiDailyQueryWrapper.select("city_code", "value") + .ge("time", time) + .in("city_code", regionCodes); + List<Map<String, Object>> thisMonthData = cityAqiDailyService.listMaps(cityAqiDailyQueryWrapper); + //���city_code������ + Map<String, List<Map<String, Object>>> thisYearMap = thisMonthData.parallelStream().collect(Collectors.groupingBy(o -> o.get("city_code").toString())); + thisYearMap.forEach((cityCode, value) -> { + Map<String, Object> resultMap = new HashMap<>(); + + Map<String, Object> params = new HashMap<>(); + List<Map<String, Object>> temp = new ArrayList<>(); + 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("CO")); + 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)) { + resultMap.put("CO", coAvgOfWeekOrMonth.get(Constants.SENSOR_CODE_CO)); + } + + //2. O3 90������������������������ + Map<String, Object> o3AvgOfWeekOrMonth = AmendUtils.getO3AvgOfWeekOrMonth(params); + if (!ObjectUtils.isEmpty(o3AvgOfWeekOrMonth)) { + resultMap.put("O3", o3AvgOfWeekOrMonth.get(Constants.SENSOR_CODE_O3)); + } + + sensors.forEach(sensor -> { + OptionalDouble optionalDouble = value.parallelStream().flatMapToDouble(v -> { + Map<String, Object> sensorValue = JSONObject.parseObject((String) v.get("value"), Map.class); + Object o = sensorValue.get(sensor); + if (ObjectUtils.isEmpty(o)) { + return null; + } + double aDouble = Double.parseDouble(o.toString()); + return DoubleStream.of(aDouble); + }).average(); + + if (optionalDouble.isPresent()) { + //��������������������� + double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 0); + resultMap.put(sensor, sciCal); + } + }); + + //������������������ + Double compositeIndex = ComprehensiveIndexUtils.dailyData(resultMap); + resultMap.put("compositeIndex", compositeIndex); + + //������O3���O3_8H������ + resultMap.put("O3_8H", resultMap.remove("O3")); + + //��������������������������� + String lastYear = DateUtils.getDateAddYear(time.substring(0, 4), -1); + QueryWrapper<CityAqiYearly> queryWrapper = new QueryWrapper<>(); + queryWrapper.select("value") + .eq("city_code", cityCode) + .eq("time", lastYear); + //������������������ + CityAqiYearly lastCityAqiYearly = cityAqiYearlyService.getOne(queryWrapper); + String yearContrast = ""; + if (lastCityAqiYearly != null) { + Map<String, Object> map = JSONObject.parseObject(lastCityAqiYearly.getValue(), Map.class); + double lastCompositeIndex = Double.parseDouble(map.get("compositeIndex").toString()); + DecimalFormat decimalFormat = new DecimalFormat("0.00%"); + yearContrast = decimalFormat.format((compositeIndex - lastCompositeIndex) / lastCompositeIndex); + } + resultMap.put("yearContrast", yearContrast); + //��������� + QueryWrapper<SysArea> sysAreaQueryWrapper = new QueryWrapper<>(); + sysAreaQueryWrapper.select("area_name").eq("area_code", cityCode); + String areaName = sysAreaService.getOne(sysAreaQueryWrapper).getAreaName(); + resultMap.put("cityName", areaName); + + result.add(resultMap); + }); + return result; + } + + /** + * @param regionCodes ��������������������������������� + * @param start ���������������������������������,���������������2021-11-03 00:00:00 + * @param end ���������������������������������,���������������2021-11-25 00:00:00 + * @return ������������������������ + */ + private List<Map<String, Object>> customRank(List<Object> regionCodes, String start, String end) { + //��������������������������� + List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2"); + List<Map<String, Object>> result = new ArrayList<>(); + QueryWrapper<CityAqiDaily> cityAqiDailyQueryWrapper = new QueryWrapper<>(); + cityAqiDailyQueryWrapper.select("city_code", "value") + .ge("time", start) + .le("time", end) + .in("city_code", regionCodes); + List<Map<String, Object>> thisMonthData = cityAqiDailyService.listMaps(cityAqiDailyQueryWrapper); + //���city_code������ + Map<String, List<Map<String, Object>>> customMap = thisMonthData.parallelStream().collect(Collectors.groupingBy(o -> o.get("city_code").toString())); + customMap.forEach((cityCode, value) -> { + Map<String, Object> resultMap = new HashMap<>(); + + Map<String, Object> params = new HashMap<>(); + List<Map<String, Object>> temp = new ArrayList<>(); + 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("CO")); + 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)) { + resultMap.put("CO", coAvgOfWeekOrMonth.get(Constants.SENSOR_CODE_CO)); + } + + //2. O3 90������������������������ + Map<String, Object> o3AvgOfWeekOrMonth = AmendUtils.getO3AvgOfWeekOrMonth(params); + if (!ObjectUtils.isEmpty(o3AvgOfWeekOrMonth)) { + resultMap.put("O3", o3AvgOfWeekOrMonth.get(Constants.SENSOR_CODE_O3)); + } + + sensors.forEach(sensor -> { + OptionalDouble optionalDouble = value.parallelStream().flatMapToDouble(v -> { + Map<String, Object> sensorValue = JSONObject.parseObject((String) v.get("value"), Map.class); + Object o = sensorValue.get(sensor); + if (ObjectUtils.isEmpty(o)) { + return null; + } + double aDouble = Double.parseDouble(o.toString()); + return DoubleStream.of(aDouble); + }).average(); + + if (optionalDouble.isPresent()) { + //��������������������� + double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 0); + resultMap.put(sensor, sciCal); + } + }); + + //��������������������� + Double compositeIndex = ComprehensiveIndexUtils.dailyData(resultMap); + resultMap.put("compositeIndex", compositeIndex); + + //������O3���O3_8H������ + resultMap.put("O3_8H", resultMap.remove("O3")); + + //��������� + QueryWrapper<SysArea> sysAreaQueryWrapper = new QueryWrapper<>(); + sysAreaQueryWrapper.select("area_name").eq("area_code", cityCode); + String areaName = sysAreaService.getOne(sysAreaQueryWrapper).getAreaName(); + resultMap.put("cityName", areaName); + + result.add(resultMap); + }); return result; } @@ -212,12 +760,12 @@ * @Date: 2021/11/2 */ private Map<String, Object> calculate6ParamAvg(List<CityAqi> cityAqiList) { - Double co = calculateSensorAvg(cityAqiList, "co"); - Double pm2_5 = calculateSensorAvg(cityAqiList, "pm2_5"); - Double pm10 = calculateSensorAvg(cityAqiList, "pm10"); - Double so2 = calculateSensorAvg(cityAqiList, "so2"); - Double no2 = calculateSensorAvg(cityAqiList, "no2"); - Double o3 = calculateSensorAvg(cityAqiList, "o3"); + Double co = calculateSensorAvg(cityAqiList, "CO"); + Double pm2_5 = calculateSensorAvg(cityAqiList, "PM2_5"); + Double pm10 = calculateSensorAvg(cityAqiList, "PM10"); + Double so2 = calculateSensorAvg(cityAqiList, "SO2"); + Double no2 = calculateSensorAvg(cityAqiList, "NO2"); + Double o3 = calculateSensorAvg(cityAqiList, "O3"); Map<String, Object> result = new HashMap<>(); result.put(Constants.SENSOR_CODE_CO, co); result.put(Constants.SENSOR_CODE_NO2, no2); @@ -278,7 +826,7 @@ } @Override - public Map<String, Object> provincialRanking(Integer organizationId) { + public Map<String, Object> provincialRanking(Integer regionCode) { //��������� Map<String, Object> result = new HashMap<>(); @@ -287,10 +835,10 @@ Date yesterday = DateUtils.dataToTimeStampTime(DateUtils.getDateOfDay(now, -1), DateUtils.yyyy_MM_dd_EN); String dateString = DateUtils.dateToDateString(yesterday, DateUtils.yyyy_MM_dd_HH_mm_ss_EN); + String s = String.valueOf(regionCode); //���������,���code - Organization organization = organizationService.getById(organizationId); - Integer provinceCode = organization.getProvinceCode(); - Integer cityCode = organization.getCityCode(); + Integer provinceCode = Integer.parseInt(s.substring(0, 2) + "0000"); + Integer cityCode = Integer.parseInt(s.substring(0, 4) + "00"); //������������������city_code QueryWrapper<SysArea> wrapper = new QueryWrapper<>(); wrapper.select("area_code").eq("parent_code", provinceCode); @@ -308,9 +856,8 @@ if (!ObjectUtils.isEmpty(one)) { String value = one.getValue(); Map<String, Object> valueMap = JSONObject.parseObject(value, Map.class); - rankMap.put("aqi", valueMap.get("aqi")); + rankMap.put("AQI", valueMap.get("AQI")); } - //2.������������������������������������������������ queryWrapper.clear(); @@ -349,15 +896,15 @@ } //���������������aqi������ - ranks.removeIf(o -> o.get("aqi") == null); - sortByField(ranks, "aqi"); + ranks.removeIf(o -> o.get("AQI") == null); + sortByField(ranks, "AQI"); //��������������� - Map<String, Object> dayMap = rankByField(ranks, cityCode, "aqi", cityCodes.size()); + Map<String, Object> dayMap = rankByField(ranks, cityCode, "AQI", cityCodes.size()); if (ObjectUtils.isEmpty(dayMap)) { dayMap.put("rank", null); dayMap.put("size", null); } - dayMap.put("aqi", dayMap.remove("value")); + dayMap.put("AQI", dayMap.remove("value")); result.put("day", dayMap); //������������������������������ @@ -419,5 +966,4 @@ } return result; } - } -- Gitblit v1.8.0