|  |  | 
 |  |  | package com.moral.api.service.impl; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSON; | 
 |  |  | import com.alibaba.fastjson.JSONArray; | 
 |  |  | 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.SysArea; | 
 |  |  | import com.moral.api.mapper.CityAqiYearlyMapper; | 
 |  |  | import com.moral.api.pojo.dto.cityAQI.CityPollutionLevel; | 
 |  |  | import com.moral.api.pojo.dto.cityAQI.ComplianceDaysDTO; | 
 |  |  | import com.moral.api.pojo.dto.cityAQI.DataPercentRange; | 
 |  |  | import com.moral.api.pojo.vo.cityAQI.ComplianceDaysVO; | 
 |  |  | import com.moral.api.service.CityAqiDailyService; | 
 |  |  | import com.moral.api.service.CityAqiYearlyService; | 
 |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
 |  |  | import com.moral.api.service.SysAreaService; | 
 |  |  | import com.moral.constant.Constants; | 
 |  |  | import com.moral.util.AQIUtils; | 
 |  |  | import com.moral.util.AmendUtils; | 
 |  |  | import com.moral.util.DateUtils; | 
 |  |  | import com.moral.util.MathUtils; | 
 |  |  | 
 |  |  | import org.springframework.util.ObjectUtils; | 
 |  |  |  | 
 |  |  | import java.text.DateFormat; | 
 |  |  | import java.text.SimpleDateFormat; | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  | 
 |  |  |             return null; | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<ComplianceDaysDTO> analysisComplianceDays(String year, Integer cityCode) { | 
 |  |  |         List<ComplianceDaysDTO> dtos = new ArrayList<>(); | 
 |  |  |         //获取指定年份开始结束时间 | 
 |  |  |         Date posYear = DateUtils.getDate(year, "yyyy"); | 
 |  |  |         Date posYearStartDate = DateUtils.getFirstDayOfYear(posYear); | 
 |  |  |         Date posYearEndDate = DateUtils.getLastDayOfYear(posYear); | 
 |  |  |         //获取同比年份开始结束时间 | 
 |  |  |         String comYearStr = (Integer.parseInt(year)-1)+""; | 
 |  |  |         Date comYear = DateUtils.getDate(comYearStr,"yyyy"); | 
 |  |  |         Date comYearStartDate = DateUtils.getFirstDayOfYear(comYear); | 
 |  |  |         Date comYearEndDate = DateUtils.getLastDayOfYear(comYear); | 
 |  |  |         //查询指定年和同比年的数据 | 
 |  |  |         List<CityAqiDaily> posYearDatas = cityAqiDailyService.getCityAqiDailyByRegionCodeAndTime(cityCode, posYearStartDate, posYearEndDate); | 
 |  |  |         List<CityAqiDaily> comYearDatas = cityAqiDailyService.getCityAqiDailyByRegionCodeAndTime(cityCode, comYearStartDate, comYearEndDate); | 
 |  |  |         //计算指定年份最大月份 | 
 |  |  |         int month = maxMonth(posYearDatas); | 
 |  |  |         if(month==0) | 
 |  |  |             return dtos; | 
 |  |  |         //循环计算每个月的数据 | 
 |  |  |         for (int i = 1; i <= month; i++) { | 
 |  |  |             ComplianceDaysDTO dto = analysisComplianceDaysForMonth(posYearDatas, comYearDatas, i); | 
 |  |  |             dtos.add(dto); | 
 |  |  |         } | 
 |  |  |         return dtos; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //计算每个月的达标天数统计分析数据 | 
 |  |  |     private ComplianceDaysDTO analysisComplianceDaysForMonth(List<CityAqiDaily> posYearDatas, | 
 |  |  |                                                             List<CityAqiDaily> comYearDatas, | 
 |  |  |                                                             int month){ | 
 |  |  |         ComplianceDaysDTO dto = new ComplianceDaysDTO(); | 
 |  |  |         //取出指定月份数据 | 
 |  |  |         List<CityAqiDaily> posMonthDatas = new ArrayList<>(); | 
 |  |  |         List<CityAqiDaily> comMonthDatas = new ArrayList<>(); | 
 |  |  |         for (CityAqiDaily posYearData : posYearDatas) { | 
 |  |  |             Date time = posYearData.getTime(); | 
 |  |  |             if(DateUtils.getMonth(time)==month) | 
 |  |  |                 posMonthDatas.add(posYearData); | 
 |  |  |         } | 
 |  |  |         for (CityAqiDaily comYearData : comYearDatas) { | 
 |  |  |             Date time = comYearData.getTime(); | 
 |  |  |             if(DateUtils.getMonth(time)==month) | 
 |  |  |                 comMonthDatas.add(comYearData); | 
 |  |  |         } | 
 |  |  |         //计算指定年份对应的月份数据 | 
 |  |  |         for (CityAqiDaily posMonthData : posMonthDatas) { | 
 |  |  |             //获取数据 | 
 |  |  |             Map<String,String> dataMap = JSON.parseObject(posMonthData.getValue(),Map.class); | 
 |  |  |             String aqi = String.valueOf(dataMap.get("AQI")); | 
 |  |  |             String PM2_5 = String.valueOf(dataMap.get("PM2_5")); | 
 |  |  |             String PM10 = String.valueOf(dataMap.get("PM10")); | 
 |  |  |             String SO2 = String.valueOf(dataMap.get("SO2")); | 
 |  |  |             String NO2 = String.valueOf(dataMap.get("NO2")); | 
 |  |  |             String O3 = String.valueOf(dataMap.get("O3")); | 
 |  |  |             String CO = String.valueOf(dataMap.get("CO")); | 
 |  |  |             //判断aqi以及6因子是否达标 | 
 |  |  |             if(aqi!=null){ | 
 |  |  |                 if(AQIUtils.aqiIsStandard(Integer.parseInt(aqi))) | 
 |  |  |                     dto.setAqiComplianceDays(dto.getAqiComplianceDays()+1); | 
 |  |  |             } | 
 |  |  |             if(PM2_5!=null){ | 
 |  |  |                 if(!AQIUtils.PM2_5IsStandard(Double.parseDouble(PM2_5))) | 
 |  |  |                     dto.setPM2_5Days(dto.getPM2_5Days()+1); | 
 |  |  |             } | 
 |  |  |             if(PM10!=null){ | 
 |  |  |                 if(!AQIUtils.PM10IsStandard(Double.parseDouble(PM10))) | 
 |  |  |                     dto.setPM10Days(dto.getPM10Days()+1); | 
 |  |  |             } | 
 |  |  |             if(SO2!=null){ | 
 |  |  |                 if(!AQIUtils.SO2IsStandard(Double.parseDouble(SO2))) | 
 |  |  |                     dto.setSO2Days(dto.getSO2Days()+1); | 
 |  |  |             } | 
 |  |  |             if(NO2!=null){ | 
 |  |  |                 if(!AQIUtils.NO2IsStandard(Double.parseDouble(NO2))) | 
 |  |  |                     dto.setNO2Days(dto.getNO2Days()+1); | 
 |  |  |             } | 
 |  |  |             if(O3!=null){ | 
 |  |  |                 if(!AQIUtils.O3IsStandard(Double.parseDouble(O3))) | 
 |  |  |                     dto.setO3Days(dto.getO3Days()+1); | 
 |  |  |             } | 
 |  |  |             if(CO!=null){ | 
 |  |  |                 if(!AQIUtils.COIsStandard(Double.parseDouble(CO))) | 
 |  |  |                     dto.setCODays(dto.getCODays()+1); | 
 |  |  |             } | 
 |  |  |             //获取首要污染物 | 
 |  |  |             Object primaryPollutantO = dataMap.get("primaryPollutant"); | 
 |  |  |             if(primaryPollutantO!=null){ | 
 |  |  |                 JSONArray primaryPollutantArray = (JSONArray) primaryPollutantO; | 
 |  |  |                 List<String> primaryPollutant = JSON.parseObject(primaryPollutantArray.toJSONString(),List.class); | 
 |  |  |                 for (String s : primaryPollutant) { | 
 |  |  |                     if(s.equals("PM2.5")) | 
 |  |  |                         dto.setPM2_5FirstDays(dto.getPM2_5FirstDays()+1); | 
 |  |  |                     if(s.equals("PM10")) | 
 |  |  |                         dto.setPM10FirstDays(dto.getPM10FirstDays()+1); | 
 |  |  |                     if(s.equals("SO2")) | 
 |  |  |                         dto.setSO2FirstDays(dto.getSO2FirstDays()+1); | 
 |  |  |                     if(s.equals("NO2")) | 
 |  |  |                         dto.setNO2FirstDays(dto.getNO2FirstDays()+1); | 
 |  |  |                     if(s.equals("CO")) | 
 |  |  |                         dto.setCOFirstDays(dto.getCOFirstDays()+1); | 
 |  |  |                     if(s.equals("O3")) | 
 |  |  |                         dto.setO3FirstDays(dto.getO3FirstDays()+1); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         //计算同期对比数据 | 
 |  |  |         for (CityAqiDaily comMonthData : comMonthDatas) { | 
 |  |  |             //获取数据 | 
 |  |  |             Map<String,String> dataMap = JSON.parseObject(comMonthData.getValue(),Map.class); | 
 |  |  |             String aqi = String.valueOf(dataMap.get("AQI")); | 
 |  |  |             //判断aqi是否达标 | 
 |  |  |             if(aqi!=null){ | 
 |  |  |                 if(AQIUtils.aqiIsStandard(Integer.parseInt(aqi))) | 
 |  |  |                     dto.setComAqiComplianceDays(dto.getComAqiComplianceDays()+1); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         //计算指定年份和同期年份达标率 | 
 |  |  |         Double posAqiComplianceDays = Double.parseDouble(dto.getAqiComplianceDays()+""); | 
 |  |  |         Double posDays = Double.parseDouble(posMonthDatas.size()+""); | 
 |  |  |         Double comAqiComplianceDays = Double.parseDouble(dto.getComAqiComplianceDays()+""); | 
 |  |  |         Double comDays = Double.parseDouble(comMonthDatas.size()+""); | 
 |  |  |         if(posDays==0d) | 
 |  |  |             dto.setAqiCompliancePer(0d); | 
 |  |  |         else | 
 |  |  |             dto.setAqiCompliancePer(MathUtils.division(posAqiComplianceDays*100,posDays,2)); | 
 |  |  |  | 
 |  |  |         if(comDays==0d) | 
 |  |  |             dto.setComAqiCompliancePer(0d); | 
 |  |  |         else | 
 |  |  |             dto.setComAqiCompliancePer(MathUtils.division(comAqiComplianceDays*100,comDays,2)); | 
 |  |  |  | 
 |  |  |         dto.setMonth(month); | 
 |  |  |         return dto; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     //计算优良天数和同比以及省内,28城市,省内通道的排名。 | 
 |  |  | 
 |  |  |         return DateUtils.dateToDateString(new Date(), "yyyy").equals(year); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //查询数据集合最大月份 | 
 |  |  |     private int maxMonth( List<CityAqiDaily> datas){ | 
 |  |  |         int i = 0; | 
 |  |  |         for (CityAqiDaily data : datas) { | 
 |  |  |             Date time = data.getTime(); | 
 |  |  |             int month = DateUtils.getMonth(time); | 
 |  |  |             if(month>i) | 
 |  |  |                 i = month; | 
 |  |  |         } | 
 |  |  |         return i; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  | } |