| | |
| | | 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; |
| | |
| | | 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 |
| | | */ |
| | |
| | | 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) { |
| | | list.sort((o1, o2) -> { |
| | |
| | | } |
| | | 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; |
| | | } |
| | | } |