| | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.moral.api.config.properties.SpecialCitiesProperties; |
| | | import com.moral.api.entity.CityAqi; |
| | | import com.moral.api.entity.CityAqiDaily; |
| | | import com.moral.api.entity.CityAqiMonthly; |
| | |
| | | import com.moral.api.entity.SysArea; |
| | | import com.moral.api.mapper.CityAqiMapper; |
| | | import com.moral.api.mapper.ForecastMapper; |
| | | import com.moral.api.pojo.dto.cityAQI.CityPollutionLevel; |
| | | import com.moral.api.pojo.dto.cityAQI.ConcentrationAndPercent; |
| | | import com.moral.api.pojo.form.aqi.AirQualityComparisonForm; |
| | | import com.moral.api.pojo.vo.cityAQI.AirQualityComparisonVO; |
| | | import com.moral.api.service.CityAqiDailyService; |
| | | import com.moral.api.service.CityAqiMonthlyService; |
| | | import com.moral.api.service.CityAqiService; |
| | |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | @Autowired |
| | | private OrganizationService organizationService; |
| | | |
| | | @Autowired |
| | | private SysAreaService sysAreaService; |
| | | |
| | | @Autowired |
| | |
| | | |
| | | @Autowired |
| | | private CityAqiYearlyService cityAqiYearlyService; |
| | | |
| | | @Autowired |
| | | private SpecialCitiesProperties specialCitiesProperties; |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> measuredCompareForecastOfO3(Map<String, Object> params) { |
| | |
| | | value = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.CITY_AQI, String.valueOf(regionCode)); |
| | | if (value == null) |
| | | value = queryCityAqiByRegionCodeFromDB(regionCode); |
| | | }else{ |
| | | } else { |
| | | return null; |
| | | } |
| | | } |
| | |
| | | lastDataWrapper.orderByDesc("time"); |
| | | lastDataWrapper.last(true, "limit 1"); |
| | | CityAqi cityAqi = cityAqiMapper.selectOne(lastDataWrapper); |
| | | if (cityAqi == null) |
| | | return null; |
| | | //算出前24小时的时间点 |
| | | Date endDate = cityAqi.getTime(); |
| | | Date startDate = DateUtils.addHours(endDate, -23); |
| | |
| | | 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); |
| | | |
| | | List<SysArea> sysAreas; |
| | | if ("28".equals(cityType)) { |
| | | //获取2+26城市 |
| | | sysAreas = specialCitiesProperties.getTwentyEightCities(); |
| | | } else { |
| | | //获取市内所有县区 |
| | | areaWrapper.select("area_code").eq("parent_code", curCityCode); |
| | | if ("province".equals(cityType)) { |
| | | //获取省内所有市 |
| | | areaWrapper.select("area_code", "area_name").eq("parent_code", curProvinceCode); |
| | | } else { |
| | | //获取市内所有县区 |
| | | areaWrapper.select("area_code", "area_name").eq("parent_code", curCityCode); |
| | | } |
| | | sysAreas = sysAreaService.list(areaWrapper); |
| | | } |
| | | List<Object> regionCodes = sysAreaService.listObjs(areaWrapper); |
| | | |
| | | switch (type) { |
| | | case "today": |
| | | result = accumulatedTodayRank(regionCodes); |
| | | result = accumulatedTodayRank(sysAreas); |
| | | break; |
| | | case "hour": |
| | | time = new StringBuilder(time).replace(10, 11, " ").toString() + ":00:00"; |
| | | result = hourRank(regionCodes, time); |
| | | result = hourRank(sysAreas, time); |
| | | break; |
| | | case "day": |
| | | time = time + " 00:00:00"; |
| | | result = dayRank(regionCodes, time); |
| | | result = dayRank(sysAreas, time); |
| | | break; |
| | | case "month": |
| | | time = time + "-01 00:00:00"; |
| | | result = monthRank(regionCodes, time); |
| | | result = monthRank(sysAreas, time); |
| | | break; |
| | | case "year": |
| | | time = time + "-01-01 00:00:00"; |
| | | result = yearRank(regionCodes, time); |
| | | result = yearRank(sysAreas, time); |
| | | break; |
| | | case "custom": |
| | | start = start + " 00:00:00"; |
| | | end = end + " :00:00"; |
| | | result = customRank(regionCodes, start, end); |
| | | result = customRank(sysAreas, start, end); |
| | | break; |
| | | default: |
| | | break; |
| | |
| | | } |
| | | |
| | | /** |
| | | * @param regionCodes 所要获取城市的地去编程集合 |
| | | * @param sysAreas 所要获取城市集合 |
| | | * @return 功能:今日累计排名 |
| | | */ |
| | | private List<Map<String, Object>> accumulatedTodayRank(List<Object> regionCodes) { |
| | | private List<Map<String, Object>> accumulatedTodayRank(List<SysArea> sysAreas) { |
| | | List<Integer> regionCodes = sysAreas.stream() |
| | | .map(SysArea::getAreaCode) |
| | | .collect(Collectors.toList()); |
| | | |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2", "CO", "O3"); |
| | | Date now = new Date(); |
| | |
| | | .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())); |
| | | Map<Integer, List<Map<String, Object>>> data = cumulativeData.parallelStream().collect(Collectors.groupingBy(o -> Integer.parseInt(o.get("city_code").toString()))); |
| | | data.forEach((cityCode, value) -> { |
| | | List<Double> doubles = new ArrayList<>(); |
| | | for (Map<String, Object> objectMap : value) { |
| | |
| | | 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); |
| | | |
| | | for (SysArea sysArea : sysAreas) { |
| | | if (cityCode.equals(sysArea.getAreaCode())) { |
| | | dataMap.put("cityName", sysArea.getAreaName()); |
| | | break; |
| | | } |
| | | } |
| | | result.add(dataMap); |
| | | }); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @param regionCodes 所要获取城市的编码集合 |
| | | * @param time 所要获取数据的时间 2021-11-04 13:00:00 |
| | | * @param sysAreas 所要获取城市集合 |
| | | * @param time 所要获取数据的时间 2021-11-04 13:00:00 |
| | | * @return 功能:小时排名 |
| | | */ |
| | | private List<Map<String, Object>> hourRank(List<Object> regionCodes, String time) { |
| | | private List<Map<String, Object>> hourRank(List<SysArea> sysAreas, String time) { |
| | | List<Integer> regionCodes = sysAreas.stream() |
| | | .map(SysArea::getAreaCode) |
| | | .collect(Collectors.toList()); |
| | | |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | QueryWrapper<CityAqi> wrapper = new QueryWrapper<>(); |
| | | wrapper.select("value") |
| | |
| | | } |
| | | |
| | | /** |
| | | * @param regionCodes 所要获取城市的编码集合 |
| | | * @param time 所要获取数据的时间 2021-11-04 00:00:00 |
| | | * @param sysAreas 所要获取城市集合 |
| | | * @param time 所要获取数据的时间 2021-11-04 00:00:00 |
| | | * @return 功能:日排名 |
| | | */ |
| | | private List<Map<String, Object>> dayRank(List<Object> regionCodes, String time) { |
| | | private List<Map<String, Object>> dayRank(List<SysArea> sysAreas, String time) { |
| | | List<Integer> regionCodes = sysAreas.stream() |
| | | .map(SysArea::getAreaCode) |
| | | .collect(Collectors.toList()); |
| | | |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>(); |
| | | wrapper.select("city_code", "value") |
| | |
| | | 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); |
| | | for (SysArea sysArea : sysAreas) { |
| | | if (dayDatum.get("city_code").equals(sysArea.getAreaCode())) { |
| | | value.put("cityName", sysArea.getAreaName()); |
| | | break; |
| | | } |
| | | } |
| | | result.add(value); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @param regionCodes 所要获取城市的编码集合 |
| | | * @param time 所要获取数据的时间 2021-11-01 00:00:00 每月1号 |
| | | * @param sysAreas 所要获取城市集合 |
| | | * @param time 所要获取数据的时间 2021-11-01 00:00:00 每月1号 |
| | | * @return 功能:月排名 |
| | | */ |
| | | private List<Map<String, Object>> monthRank(List<Object> regionCodes, String time) { |
| | | private List<Map<String, Object>> monthRank(List<SysArea> sysAreas, String time) { |
| | | List<Integer> regionCodes = sysAreas.stream() |
| | | .map(SysArea::getAreaCode) |
| | | .collect(Collectors.toList()); |
| | | |
| | | //需要均值计算的因子 |
| | | List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2"); |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | //如果是本月,实时计算,其他直接从city_aqi_monthly获取 |
| | | //如果部是本月,实时计算,其他直接从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) { |
| | | for (Integer regionCode : regionCodes) { |
| | | cityAqiMonthlyQueryWrapper.clear(); |
| | | cityAqiMonthlyQueryWrapper.select("value") |
| | | .eq("city_code", regionCode) |
| | |
| | | } |
| | | 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); |
| | | for (SysArea sysArea : sysAreas) { |
| | | if (regionCode.equals(sysArea.getAreaCode())) { |
| | | resultMap.put("cityName", sysArea.getAreaName()); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | |
| | | .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())); |
| | | Map<Integer, List<Map<String, Object>>> thisMonthMap = thisMonthData.parallelStream() |
| | | .collect(Collectors.groupingBy(o -> Integer.parseInt(o.get("city_code").toString()))); |
| | | |
| | | thisMonthMap.forEach((cityCode, value) -> { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | |
| | |
| | | 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); |
| | | |
| | | for (SysArea sysArea : sysAreas) { |
| | | if (cityCode.equals(sysArea.getAreaCode())) { |
| | | resultMap.put("cityName", sysArea.getAreaName()); |
| | | break; |
| | | } |
| | | } |
| | | result.add(resultMap); |
| | | }); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @param regionCodes 所要获取城市的编码集合 |
| | | * @param time 所要获取数据的时间 2021-11-01 00:00:00 每年1月1号 |
| | | * @param sysAreas 所要获取城市集合 |
| | | * @param time 所要获取数据的时间 2021-11-01 00:00:00 每年1月1号 |
| | | * @return 功能:年排名 |
| | | */ |
| | | private List<Map<String, Object>> yearRank(List<Object> regionCodes, String time) { |
| | | private List<Map<String, Object>> yearRank(List<SysArea> sysAreas, String time) { |
| | | List<Integer> regionCodes = sysAreas.stream() |
| | | .map(SysArea::getAreaCode) |
| | | .collect(Collectors.toList()); |
| | | |
| | | //需要均值计算的因子 |
| | | 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) { |
| | | for (Integer regionCode : regionCodes) { |
| | | cityAqiYearlyQueryWrapper.clear(); |
| | | cityAqiYearlyQueryWrapper.select("value") |
| | | .eq("city_code", regionCode) |
| | |
| | | } |
| | | 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); |
| | | for (SysArea sysArea : sysAreas) { |
| | | if (regionCode.equals(sysArea.getAreaCode())) { |
| | | resultMap.put("cityName", sysArea.getAreaName()); |
| | | break; |
| | | } |
| | | } |
| | | result.add(resultMap); |
| | | } |
| | | return result; |
| | |
| | | .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())); |
| | | Map<Integer, List<Map<String, Object>>> thisYearMap = thisMonthData.parallelStream() |
| | | .collect(Collectors.groupingBy(o -> Integer.parseInt(o.get("city_code").toString()))); |
| | | thisYearMap.forEach((cityCode, value) -> { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | |
| | |
| | | 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); |
| | | |
| | | |
| | | //城市名 |
| | | for (SysArea sysArea : sysAreas) { |
| | | if (cityCode.equals(sysArea.getAreaCode())) { |
| | | resultMap.put("cityName", sysArea.getAreaName()); |
| | | break; |
| | | } |
| | | } |
| | | result.add(resultMap); |
| | | }); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @param regionCodes 所要获取城市的编码集合 |
| | | * @param start 所要获取数据的开始时间,精确到日,2021-11-03 00:00:00 |
| | | * @param end 所要获取数据的结束时间,精确到日,2021-11-25 00:00:00 |
| | | * @param sysAreas 所要获取城市集合 |
| | | * @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) { |
| | | private List<Map<String, Object>> customRank(List<SysArea> sysAreas, String start, String end) { |
| | | List<Integer> regionCodes = sysAreas.stream() |
| | | .map(SysArea::getAreaCode) |
| | | .collect(Collectors.toList()); |
| | | |
| | | //需要均值计算的因子 |
| | | List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2"); |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | |
| | | .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())); |
| | | Map<Integer, List<Map<String, Object>>> customMap = thisMonthData.parallelStream() |
| | | .collect(Collectors.groupingBy(o -> Integer.parseInt(o.get("city_code").toString()))); |
| | | customMap.forEach((cityCode, value) -> { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | |
| | |
| | | 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); |
| | | |
| | | for (SysArea sysArea : sysAreas) { |
| | | if (cityCode.equals(sysArea.getAreaCode())) { |
| | | resultMap.put("cityName", sysArea.getAreaName()); |
| | | break; |
| | | } |
| | | } |
| | | result.add(resultMap); |
| | | }); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 计算6参平均值 |
| | | * @Param: [cityAqiList] |
| | | * @return: java.util.Map<java.lang.String , java.lang.Double> |
| | | * 返回值key为sensorCode,value为值 |
| | | * @Author: 陈凯裕 |
| | | * @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"); |
| | | Map<String, Object> result = new HashMap<>(); |
| | | result.put(Constants.SENSOR_CODE_CO, co); |
| | | result.put(Constants.SENSOR_CODE_NO2, no2); |
| | | result.put(Constants.SENSOR_CODE_SO2, so2); |
| | | result.put(Constants.SENSOR_CODE_O3, o3); |
| | | result.put(Constants.SENSOR_CODE_PM25, pm2_5); |
| | | result.put(Constants.SENSOR_CODE_PM10, pm10); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 计算因子的平均值 |
| | | * @Param: [cityAqiList, sensor] |
| | | * ,sensor是要计算的因子名称 |
| | | * @return: java.lang.Double |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/11/2 |
| | | */ |
| | | private Double calculateSensorAvg(List<CityAqi> cityAqiList, String sensor) { |
| | | Double sum = 0d; |
| | | int num = 0; |
| | | for (CityAqi cityAqi : cityAqiList) { |
| | | String value = cityAqi.getValue(); |
| | | if (value == null) |
| | | continue; |
| | | Map<String, Object> valueMap = JSON.parseObject(value, Map.class); |
| | | Object sensorValueObject = valueMap.get(sensor); |
| | | if (sensorValueObject == null) |
| | | continue; |
| | | Double sensorValue = Double.valueOf(sensorValueObject.toString()); |
| | | sum = MathUtils.add(sum, sensorValue); |
| | | num++; |
| | | } |
| | | if (num == 0) |
| | | return null; |
| | | Double avg = MathUtils.division(sum, num, 2); |
| | | return avg; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 从数据库查询数据 |
| | | * @Param: [regionCode] |
| | | * @return: java.util.Map<java.lang.String , java.lang.Object> |
| | | * @return: java.util.Map<java.lang.String , java.lang.Object> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/10/28 |
| | | */ |
| | |
| | | |
| | | //年排名,按累计综指排 |
| | | sortByField(ranks, "compositeIndexYear"); |
| | | //年排名结果 |
| | | sortByField(ranks, "compositeIndexYear"); |
| | | Map<String, Object> yearMap = rankByField(ranks, cityCode, "compositeIndexYear", cityCodes.size()); |
| | | if (ObjectUtils.isEmpty(yearMap)) { |
| | | yearMap.put("rank", null); |
| | |
| | | result.put("time", DateUtils.dateToDateString(yesterday, DateUtils.yyyy_MM_dd_EN)); |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public List<AirQualityComparisonVO> queryAirQualityComparison(AirQualityComparisonForm form) { |
| | | //取参 |
| | | Integer regionCode = form.getRegionCode(); |
| | | String regionType = form.getRegionType(); |
| | | Date startDate = form.getStartDate(); |
| | | Date endDate = form.getEndDate(); |
| | | Date comparisonStartDate = form.getComparisonStartDate(); |
| | | Date comparisonEndDate = form.getComparisonEndDate(); |
| | | String dateType = form.getDateType(); |
| | | //获取城市/区县 |
| | | List<SysArea> areas = getSysAreasByRegionType(regionType, regionCode); |
| | | if (ObjectUtils.isEmpty(areas)) |
| | | return null; |
| | | List<AirQualityComparisonVO> vos = new ArrayList<>(); |
| | | for (SysArea area : areas) { |
| | | //获取查询时间和对比时间的6参和综合指数 |
| | | Map<String, Object> data = getDataByTimeTypeAndRegionCode(dateType, startDate, endDate, area.getAreaCode()); |
| | | Map<String, Object> comparisonData = getDataByTimeTypeAndRegionCode(dateType, comparisonStartDate, comparisonEndDate, area.getAreaCode()); |
| | | if (ObjectUtils.isEmpty(data) || ObjectUtils.isEmpty(comparisonData)) |
| | | continue; |
| | | //查询优良天数以及重污染天数 |
| | | CityPollutionLevel days = cityAqiDailyService.calculateDaysByTimeAndSysArea(area, startDate, endDate); |
| | | CityPollutionLevel comparisonDays = cityAqiDailyService.calculateDaysByTimeAndSysArea(area, comparisonStartDate, comparisonEndDate); |
| | | int fineDays = days.getExcellentWeatherDays() + days.getGoodWeatherDays(); |
| | | int serverDays = days.getSeriousWeatherDays() + days.getServerWeatherDays(); |
| | | int comparisonFineDays = comparisonDays.getExcellentWeatherDays() + comparisonDays.getGoodWeatherDays(); |
| | | int comparisonServerDays = comparisonDays.getSeriousWeatherDays() + comparisonDays.getServerWeatherDays(); |
| | | //对比6参和综合指数 |
| | | Map<String, ConcentrationAndPercent> sixParamAndComIndexResult = contrastSixParamAndComIndex(data, comparisonData); |
| | | //对比优良天数以及重污染天气数 |
| | | ConcentrationAndPercent fine = contrastDays(fineDays, comparisonFineDays); |
| | | ConcentrationAndPercent server = contrastDays(serverDays, comparisonServerDays); |
| | | //创建返回对象 |
| | | AirQualityComparisonVO vo = new AirQualityComparisonVO(); |
| | | vo.setFineDays(fine); |
| | | vo.setServerDays(server); |
| | | vo.setCO(sixParamAndComIndexResult.get("CO")); |
| | | vo.setO3(sixParamAndComIndexResult.get("O3")); |
| | | vo.setPM25(sixParamAndComIndexResult.get("PM2_5")); |
| | | vo.setPM10(sixParamAndComIndexResult.get("PM10")); |
| | | vo.setSO2(sixParamAndComIndexResult.get("SO2")); |
| | | vo.setNO2(sixParamAndComIndexResult.get("NO2")); |
| | | vo.setCompositeIndex(sixParamAndComIndexResult.get("compositeIndex")); |
| | | vo.setCityName(area.getAreaName()); |
| | | vos.add(vo); |
| | | } |
| | | return vos; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 计算6参和综合指数对比的百分比 |
| | | * @Param: [data, comparisonData] |
| | | * @return: java.util.Map<java.lang.String , com.moral.api.pojo.dto.cityAQI.ConcentrationAndPercent> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2022/1/17 |
| | | */ |
| | | private Map<String, ConcentrationAndPercent> contrastSixParamAndComIndex(Map<String, Object> data, Map<String, Object> comparisonData) { |
| | | Map<String, ConcentrationAndPercent> result = new HashMap<>(); |
| | | result.put("CO", contrastParam(Double.parseDouble(data.get("CO").toString()), Double.parseDouble(comparisonData.get("CO").toString()), "CO")); |
| | | result.put("NO2", contrastParam(Double.parseDouble(data.get("NO2").toString()), Double.parseDouble(comparisonData.get("NO2").toString()), "NO2")); |
| | | result.put("SO2", contrastParam(Double.parseDouble(data.get("SO2").toString()), Double.parseDouble(comparisonData.get("SO2").toString()), "SO2")); |
| | | result.put("O3", contrastParam(Double.parseDouble(data.get("O3").toString()), Double.parseDouble(comparisonData.get("O3").toString()), "O3")); |
| | | result.put("PM2_5", contrastParam(Double.parseDouble(data.get("PM2_5").toString()), Double.parseDouble(comparisonData.get("PM2_5").toString()), "PM2_5")); |
| | | result.put("PM10", contrastParam(Double.parseDouble(data.get("PM10").toString()), Double.parseDouble(comparisonData.get("PM10").toString()), "PM10")); |
| | | result.put("compositeIndex", contrastParam(Double.parseDouble(data.get("compositeIndex").toString()), Double.parseDouble(comparisonData.get("compositeIndex").toString()), "compositeIndex")); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 计算6参和综合指数同比/环比百分比数据 |
| | | * @Param: [data, comparisonData] |
| | | * @return: com.moral.api.pojo.dto.cityAQI.ConcentrationAndPercent |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2022/1/17 |
| | | */ |
| | | private ConcentrationAndPercent contrastParam(Double data, Double comparisonData, String sensor) { |
| | | double percentD = MathUtils.division(data - comparisonData, comparisonData, 4); |
| | | String percent = MathUtils.mul(percentD,100d) + "%"; |
| | | ConcentrationAndPercent concentrationAndPercent = new ConcentrationAndPercent(); |
| | | concentrationAndPercent.setPercent(percent); |
| | | if (sensor.equals("CO")) {//CO小数点保留一位 |
| | | Double CO = AmendUtils.sciCal(data, 1); |
| | | concentrationAndPercent.setConcentration(CO.toString()); |
| | | }else if (sensor.equals("compositeIndex")){ |
| | | concentrationAndPercent.setConcentration(data.toString()); |
| | | }else{ |
| | | Double sensorD = AmendUtils.sciCal(data, 0); |
| | | Integer sensorI = new Double(sensorD).intValue(); |
| | | concentrationAndPercent.setConcentration(sensorI.toString()); |
| | | } |
| | | return concentrationAndPercent; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 对比天数,返回天数差值 |
| | | * @Param: [days, comparisonDays] |
| | | * @return: com.moral.api.pojo.dto.cityAQI.ConcentrationAndPercent |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2022/1/17 |
| | | */ |
| | | private ConcentrationAndPercent contrastDays(Integer days, Integer comparisonDays) { |
| | | ConcentrationAndPercent concentrationAndPercent = new ConcentrationAndPercent(); |
| | | concentrationAndPercent.setConcentration(days.toString()); |
| | | Integer result = days - comparisonDays; |
| | | concentrationAndPercent.setPercent(result.toString() + "天"); |
| | | return concentrationAndPercent; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 根据时间类型查询对应的6参以及综合指数,自定义时间类型用日数据做均值处理 |
| | | * @Param: [comparisonType, startDate, endDate, regionCode] |
| | | * @return: java.util.Map<java.lang.String , java.lang.Object> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2022/1/17 |
| | | */ |
| | | private Map<String, Object> getDataByTimeTypeAndRegionCode(String TimeType, Date startDate, Date endDate, Integer regionCode) { |
| | | Map<String, Object> data; |
| | | if (Constants.MONTH.equals(TimeType) && (!DateUtils.isCurrentMonth(startDate) || !DateUtils.isCurrentYear(startDate))) {//月数据处理 不包括本月 |
| | | List<CityAqiMonthly> cityAqis = cityAqiMonthlyService.getCityAqiMonthByRegionCodeAndTime(regionCode, startDate, endDate); |
| | | if (ObjectUtils.isEmpty(cityAqis)) |
| | | return null; |
| | | data = JSON.parseObject(cityAqis.get(0).getValue(), Map.class); |
| | | } else if (Constants.YEAR.equals(TimeType) && (!DateUtils.isCurrentYear(startDate))) {//年数据处理 不包括本年 |
| | | List<CityAqiYearly> cityAqis = cityAqiYearlyService.getCityAqiYearlyByRegionCodeAndTime(regionCode, startDate, endDate); |
| | | if (ObjectUtils.isEmpty(cityAqis)) |
| | | return null; |
| | | data = JSON.parseObject(cityAqis.get(0).getValue(), Map.class); |
| | | } else {//自定义数据处理 |
| | | List<CityAqiDaily> cityAqis = cityAqiDailyService.getCityAqiDailyByRegionCodeAndTime(regionCode, startDate, endDate); |
| | | if (ObjectUtils.isEmpty(cityAqis)) |
| | | return null; |
| | | List<CityAqi> newCityAqis = new ArrayList<>(); |
| | | List<Map<String, Object>> dailyDataMaps = new ArrayList<>(); |
| | | cityAqis.forEach((value) -> { |
| | | newCityAqis.add(new CityAqi(value)); |
| | | dailyDataMaps.add(JSON.parseObject(value.getValue(), Map.class)); |
| | | }); |
| | | //计算均值 |
| | | data = calculate6ParamAvg(newCityAqis); |
| | | //小数点处理 |
| | | data.put("CO", Double.parseDouble(data.remove(Constants.SENSOR_CODE_CO).toString())); |
| | | data.put("NO2", Double.parseDouble(data.remove(Constants.SENSOR_CODE_NO2).toString())); |
| | | data.put("SO2", Double.parseDouble(data.remove(Constants.SENSOR_CODE_SO2).toString())); |
| | | data.put("O3", Double.parseDouble(data.remove(Constants.SENSOR_CODE_O3).toString())); |
| | | data.put("PM2_5", Double.parseDouble(data.remove(Constants.SENSOR_CODE_PM25).toString())); |
| | | data.put("PM10", Double.parseDouble(data.remove(Constants.SENSOR_CODE_PM10).toString())); |
| | | //计算综合指数 |
| | | Double compositeIndex = ComprehensiveIndexUtils.dailyData(data); |
| | | data.put("compositeIndex", compositeIndex); |
| | | } |
| | | return data; |
| | | } |
| | | |
| | | |
| | | //按某字段排序 |
| | | private void sortByField(List<Map<String, Object>> list, String sortField) { |
| | |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 根据类型和地区码获取所有的城市或者区县 |
| | | * @Param: [regionType, regionCode] |
| | | * @return: java.util.List<com.moral.api.entity.SysArea> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2022/1/14 |
| | | */ |
| | | private List<SysArea> getSysAreasByRegionType(String regionType, Integer regionCode) { |
| | | List<SysArea> areas; |
| | | if (regionType.equals(Constants.TWENTY_EIGHT_CITIES)) { |
| | | SpecialCitiesProperties properties = new SpecialCitiesProperties(); |
| | | areas = properties.getTwentyEightCities(); |
| | | } else { |
| | | areas = sysAreaService.getChildren(regionCode); |
| | | } |
| | | return areas; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 计算6参平均值 |
| | | * @Param: [cityAqiList] |
| | | * @return: java.util.Map<java.lang.String , java.lang.Double> |
| | | * 返回值key为sensorCode,value为值 |
| | | * @Author: 陈凯裕 |
| | | * @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"); |
| | | Map<String, Object> result = new HashMap<>(); |
| | | result.put(Constants.SENSOR_CODE_CO, co); |
| | | result.put(Constants.SENSOR_CODE_NO2, no2); |
| | | result.put(Constants.SENSOR_CODE_SO2, so2); |
| | | result.put(Constants.SENSOR_CODE_O3, o3); |
| | | result.put(Constants.SENSOR_CODE_PM25, pm2_5); |
| | | result.put(Constants.SENSOR_CODE_PM10, pm10); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 计算因子的平均值 |
| | | * @Param: [cityAqiList, sensor] |
| | | * ,sensor是要计算的因子名称 |
| | | * @return: java.lang.Double |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/11/2 |
| | | */ |
| | | private Double calculateSensorAvg(List<CityAqi> cityAqiList, String sensor) { |
| | | Double sum = 0d; |
| | | int num = 0; |
| | | for (CityAqi cityAqi : cityAqiList) { |
| | | String value = cityAqi.getValue(); |
| | | if (value == null) |
| | | continue; |
| | | Map<String, Object> valueMap = JSON.parseObject(value, Map.class); |
| | | Object sensorValueObject = valueMap.get(sensor); |
| | | if (sensorValueObject == null) |
| | | continue; |
| | | Double sensorValue = Double.valueOf(sensorValueObject.toString()); |
| | | sum = MathUtils.add(sum, sensorValue); |
| | | num++; |
| | | } |
| | | if (num == 0) |
| | | return null; |
| | | Double avg = MathUtils.division(sum, num, 2); |
| | | return avg; |
| | | } |
| | | } |