|  |  | 
 |  |  | package com.moral.api.service.impl; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSON; | 
 |  |  | import com.alibaba.fastjson.JSONObject; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
 |  |  | import com.moral.api.config.properties.BulletinProperties; | 
 |  |  | import com.moral.api.config.properties.SpecialCitiesProperties; | 
 |  |  | import com.moral.api.entity.CityAqiDaily; | 
 |  |  | import com.moral.api.entity.CityAqiMonthly; | 
 |  |  | import com.moral.api.entity.CityAqiYearly; | 
 |  |  | import com.moral.api.entity.SysArea; | 
 |  |  | import com.moral.api.mapper.CityAqiDailyMapper; | 
 |  |  | import com.moral.api.pojo.dto.dataDisplay.PieChartOfPollutionLevelDTO; | 
 |  |  | import com.moral.api.pojo.form.aqi.QueryPieChartOfPollutionLevelForm; | 
 |  |  | import com.moral.api.mapper.CityAqiMonthlyMapper; | 
 |  |  | import com.moral.api.pojo.dto.cityAQI.CityPollutionLevel; | 
 |  |  | import com.moral.api.pojo.dto.cityAQI.ConcentrationAndPercent; | 
 |  |  | import com.moral.api.pojo.dto.cityAQI.MonthlyPollutionLevel; | 
 |  |  | import com.moral.api.pojo.dto.cityAQI.PollutionDaysAndProportion; | 
 |  |  | import com.moral.api.pojo.form.aqi.*; | 
 |  |  | import com.moral.api.pojo.vo.cityAQI.CityAreaRangeVO; | 
 |  |  | import com.moral.api.pojo.vo.cityAQI.PieChartOfPollutionLevelVO; | 
 |  |  | import com.moral.api.service.CityAqiDailyService; | 
 |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
 |  |  | import com.moral.api.service.CityAqiMonthlyService; | 
 |  |  | import com.moral.api.service.CityAqiYearlyService; | 
 |  |  | import com.moral.api.service.SysAreaService; | 
 |  |  | import com.moral.constant.Constants; | 
 |  |  | import com.moral.util.AQIUtils; | 
 |  |  | import com.moral.util.DateUtils; | 
 |  |  | import com.moral.util.MathUtils; | 
 |  |  | import com.moral.util.*; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  | import org.springframework.util.ObjectUtils; | 
 |  |  |  | 
 |  |  | import java.text.DecimalFormat; | 
 |  |  | import java.text.SimpleDateFormat; | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     CityAqiDailyMapper cityAqiDailyMapper; | 
 |  |  |     @Autowired | 
 |  |  |     SysAreaService sysAreaService; | 
 |  |  |     @Autowired | 
 |  |  |     SpecialCitiesProperties specialCitiesProperties; | 
 |  |  |     @Autowired | 
 |  |  |     BulletinProperties bulletinProperties; | 
 |  |  |     @Autowired | 
 |  |  |     CityAqiMonthlyService cityAqiMonthlyService; | 
 |  |  |     @Autowired | 
 |  |  |     CityAqiYearlyService cityAqiYearlyService; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public PieChartOfPollutionLevelDTO queryPieChartOfPollutionLevels(QueryPieChartOfPollutionLevelForm form) { | 
 |  |  |     public List<CityAqiDaily> getCityAqiDailyByRegionCodeAndTime(Integer regionCode, Date startDate, Date endDate) { | 
 |  |  |         QueryWrapper<CityAqiDaily> queryWrapper = new QueryWrapper<>(); | 
 |  |  |         queryWrapper.eq("city_code", regionCode); | 
 |  |  |         queryWrapper.between("time", startDate, endDate); | 
 |  |  |         return cityAqiDailyMapper.selectList(queryWrapper); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public PieChartOfPollutionLevelVO queryPieChartOfPollutionLevels(ChartOfPollutionLevelForm form) { | 
 |  |  |         //取参 | 
 |  |  |         Integer regionCode = form.getRegionCode(); | 
 |  |  |         Date startDate = form.getStartDate(); | 
 |  |  |         Date endDate = form.getEndDate(); | 
 |  |  |         //查询数据 | 
 |  |  |         QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>(); | 
 |  |  |         wrapper.eq("city_code",regionCode); | 
 |  |  |         wrapper.between("time",startDate,endDate); | 
 |  |  |         wrapper.eq("city_code", regionCode); | 
 |  |  |         wrapper.between("time", startDate, endDate); | 
 |  |  |         List<CityAqiDaily> cityAqiDailies = cityAqiDailyMapper.selectList(wrapper); | 
 |  |  |         //取开始结束时间,拼装返回time | 
 |  |  |         if(cityAqiDailies.size()==0) | 
 |  |  |         if (cityAqiDailies.size() == 0) | 
 |  |  |             return null; | 
 |  |  |         String startTime = DateUtils.dateToDateString(cityAqiDailies.get(0).getTime(),"yyyy-MM-dd"); | 
 |  |  |         String endTime = DateUtils.dateToDateString(cityAqiDailies.get(cityAqiDailies.size()-1).getTime(),"yyyy-MM-dd"); | 
 |  |  |         String time = startTime+" 至 "+endTime; | 
 |  |  |         String startTime = DateUtils.dateToDateString(cityAqiDailies.get(0).getTime(), "yyyy-MM-dd"); | 
 |  |  |         String endTime = DateUtils.dateToDateString(cityAqiDailies.get(cityAqiDailies.size() - 1).getTime(), "yyyy-MM-dd"); | 
 |  |  |         String time = startTime + " 至 " + endTime; | 
 |  |  |         //如果数据的时间有重复则去重 | 
 |  |  |         Map<String,CityAqiDaily> tmpMap = new HashMap<>(); | 
 |  |  |         Map<String, CityAqiDaily> tmpMap = new HashMap<>(); | 
 |  |  |         for (CityAqiDaily cityAqiDaily : cityAqiDailies) { | 
 |  |  |             String tmpTime = DateUtils.dateToDateString(cityAqiDaily.getTime(),"yyyy-MM-dd"); | 
 |  |  |             tmpMap.put(tmpTime,cityAqiDaily); | 
 |  |  |             String tmpTime = DateUtils.dateToDateString(cityAqiDaily.getTime(), "yyyy-MM-dd"); | 
 |  |  |             tmpMap.put(tmpTime, cityAqiDaily); | 
 |  |  |         } | 
 |  |  |         cityAqiDailies = new ArrayList<CityAqiDaily>(tmpMap.values()); | 
 |  |  |         //统计各污染等级的天数 | 
 |  |  |         Map<String,Integer> pollutionDaysMap = new LinkedHashMap<>(); | 
 |  |  |         pollutionDaysMap.put(Constants.EXCELLENT_WEATHER_CODE,0); | 
 |  |  |         pollutionDaysMap.put(Constants.GOOD_WEATHER_CODE,0); | 
 |  |  |         pollutionDaysMap.put(Constants.MILD_WEATHER_CODE,0); | 
 |  |  |         pollutionDaysMap.put(Constants.MIDDLE_WEATHER_CODE,0); | 
 |  |  |         pollutionDaysMap.put(Constants.SERIOUS_WEATHER_CODE,0); | 
 |  |  |         pollutionDaysMap.put(Constants.SERVER_WEATHER_CODE,0); | 
 |  |  |         Map<String, Integer> pollutionDaysMap = new LinkedHashMap<>(); | 
 |  |  |         pollutionDaysMap.put(Constants.EXCELLENT_WEATHER_CODE, 0); | 
 |  |  |         pollutionDaysMap.put(Constants.GOOD_WEATHER_CODE, 0); | 
 |  |  |         pollutionDaysMap.put(Constants.MILD_WEATHER_CODE, 0); | 
 |  |  |         pollutionDaysMap.put(Constants.MIDDLE_WEATHER_CODE, 0); | 
 |  |  |         pollutionDaysMap.put(Constants.SERIOUS_WEATHER_CODE, 0); | 
 |  |  |         pollutionDaysMap.put(Constants.SERVER_WEATHER_CODE, 0); | 
 |  |  |         for (CityAqiDaily cityAqiDaily : cityAqiDailies) { | 
 |  |  |             Map<String,Object> valueMap = JSON.parseObject(cityAqiDaily.getValue(),Map.class); | 
 |  |  |             if(valueMap.get("AQI")==null) | 
 |  |  |             Map<String, Object> valueMap = JSON.parseObject(cityAqiDaily.getValue(), Map.class); | 
 |  |  |             if (valueMap.get("AQI") == null) | 
 |  |  |                 continue; | 
 |  |  |             Integer aqi = Integer.valueOf(valueMap.get("AQI").toString()); | 
 |  |  |             //计算AQI污染等级 | 
 |  |  |             String pollutionClass = AQIUtils.classCodeOfPollutionByAqi(aqi); | 
 |  |  |             Integer days = pollutionDaysMap.get(pollutionClass); | 
 |  |  |             pollutionDaysMap.put(pollutionClass,days+1); | 
 |  |  |             pollutionDaysMap.put(pollutionClass, days + 1); | 
 |  |  |         } | 
 |  |  |         //计算比例 | 
 |  |  |         List<Map<String,Object>> valueMap = new ArrayList<>(); | 
 |  |  |         List<Map<String, Object>> valueMap = new ArrayList<>(); | 
 |  |  |         Set<Map.Entry<String, Integer>> entries = pollutionDaysMap.entrySet(); | 
 |  |  |         Iterator<Map.Entry<String, Integer>> iterator = entries.iterator(); | 
 |  |  |         Integer totalDays = 0; | 
 |  |  |         while(iterator.hasNext()){ | 
 |  |  |         while (iterator.hasNext()) { | 
 |  |  |             Map.Entry<String, Integer> entry = iterator.next(); | 
 |  |  |             Map<String,Object> pollutionMap = new HashMap<>(); | 
 |  |  |             Map<String, Object> pollutionMap = new HashMap<>(); | 
 |  |  |             String pollution = entry.getKey(); | 
 |  |  |             Integer days = entry.getValue(); | 
 |  |  |             Double proportionTmp = MathUtils.division(Double.valueOf(days),Double.valueOf(cityAqiDailies.size()),3); | 
 |  |  |             Double proportionD = MathUtils.mul(proportionTmp,100); | 
 |  |  |             String proportion = proportionD.toString()+"%"; | 
 |  |  |             pollutionMap.put("proportion",proportion); | 
 |  |  |             pollutionMap.put("pollution",pollution); | 
 |  |  |             pollutionMap.put("days",days); | 
 |  |  |             Double proportionTmp = MathUtils.division(Double.valueOf(days), Double.valueOf(cityAqiDailies.size()), 3); | 
 |  |  |             Double proportionD = MathUtils.mul(proportionTmp, 100); | 
 |  |  |             String proportion = proportionD.toString() + "%"; | 
 |  |  |             pollutionMap.put("proportion", proportion); | 
 |  |  |             pollutionMap.put("pollution", pollution); | 
 |  |  |             pollutionMap.put("days", days); | 
 |  |  |             valueMap.add(pollutionMap); | 
 |  |  |             totalDays+=days; | 
 |  |  |             totalDays += days; | 
 |  |  |         } | 
 |  |  |         //封装返回对象 | 
 |  |  |         PieChartOfPollutionLevelDTO dto = new PieChartOfPollutionLevelDTO(); | 
 |  |  |         dto.setTime(time); | 
 |  |  |         dto.setValues(valueMap); | 
 |  |  |         dto.setTotalDays(totalDays); | 
 |  |  |         return dto; | 
 |  |  |         PieChartOfPollutionLevelVO VO = new PieChartOfPollutionLevelVO(); | 
 |  |  |         VO.setTime(time); | 
 |  |  |         VO.setValues(valueMap); | 
 |  |  |         VO.setTotalDays(totalDays); | 
 |  |  |         return VO; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<MonthlyPollutionLevel> queryBarChartOfPollutionLevels(ChartOfPollutionLevelForm form) { | 
 |  |  |         //取参 | 
 |  |  |         Integer regionCode = form.getRegionCode(); | 
 |  |  |         Date startDate = form.getStartDate(); | 
 |  |  |         Date endDate = form.getEndDate(); | 
 |  |  |         //查询数据 | 
 |  |  |         QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>(); | 
 |  |  |         wrapper.eq("city_code", regionCode); | 
 |  |  |         wrapper.between("time", startDate, endDate); | 
 |  |  |         wrapper.orderByAsc("time"); | 
 |  |  |         List<CityAqiDaily> cityAqiDailies = cityAqiDailyMapper.selectList(wrapper); | 
 |  |  |         //如果数据的时间有重复则去重 | 
 |  |  |         Map<String, CityAqiDaily> tmpMap = new LinkedHashMap<>(); | 
 |  |  |         for (CityAqiDaily cityAqiDaily : cityAqiDailies) { | 
 |  |  |             String tmpTime = DateUtils.dateToDateString(cityAqiDaily.getTime(), "yyyy-MM-dd"); | 
 |  |  |             tmpMap.put(tmpTime, cityAqiDaily); | 
 |  |  |         } | 
 |  |  |         cityAqiDailies = new ArrayList<CityAqiDaily>(tmpMap.values()); | 
 |  |  |         //按照月份对数据进行分类 | 
 |  |  |         Map<String, List<CityAqiDaily>> monthlyDataMap = new LinkedHashMap<>(); | 
 |  |  |         cityAqiDailies.forEach(value -> { | 
 |  |  |             String month = DateUtils.dateToDateString(value.getTime(), "yyyy-MM"); | 
 |  |  |             List<CityAqiDaily> dataList = monthlyDataMap.get(month); | 
 |  |  |             if (ObjectUtils.isEmpty(dataList)) | 
 |  |  |                 dataList = new ArrayList<>(); | 
 |  |  |             dataList.add(value); | 
 |  |  |             monthlyDataMap.put(month, dataList); | 
 |  |  |         }); | 
 |  |  |         //计算污染天数和比例 | 
 |  |  |         List<MonthlyPollutionLevel> monthlyPollutionLevels = new ArrayList<>(); | 
 |  |  |         Set<Map.Entry<String, List<CityAqiDaily>>> monthlyEntries = monthlyDataMap.entrySet(); | 
 |  |  |         Iterator<Map.Entry<String, List<CityAqiDaily>>> monthlyIterator = monthlyEntries.iterator(); | 
 |  |  |         while (monthlyIterator.hasNext()) { | 
 |  |  |             Map.Entry<String, List<CityAqiDaily>> monthlyEntry = monthlyIterator.next(); | 
 |  |  |             List<CityAqiDaily> monthlyValues = monthlyEntry.getValue(); | 
 |  |  |             //用于统计各个污染等级的天数 | 
 |  |  |             Map<String, Integer> pollutionDaysMap = new LinkedHashMap<>(); | 
 |  |  |             pollutionDaysMap.put(Constants.EXCELLENT_WEATHER_CODE, 0); | 
 |  |  |             pollutionDaysMap.put(Constants.GOOD_WEATHER_CODE, 0); | 
 |  |  |             pollutionDaysMap.put(Constants.MILD_WEATHER_CODE, 0); | 
 |  |  |             pollutionDaysMap.put(Constants.MIDDLE_WEATHER_CODE, 0); | 
 |  |  |             pollutionDaysMap.put(Constants.SERIOUS_WEATHER_CODE, 0); | 
 |  |  |             pollutionDaysMap.put(Constants.SERVER_WEATHER_CODE, 0); | 
 |  |  |             for (CityAqiDaily cityAqiDaily : monthlyValues) { | 
 |  |  |                 Map<String, Object> valueMap = JSON.parseObject(cityAqiDaily.getValue(), Map.class); | 
 |  |  |                 if (valueMap.get("AQI") == null) | 
 |  |  |                     continue; | 
 |  |  |                 Integer aqi = Integer.valueOf(valueMap.get("AQI").toString()); | 
 |  |  |                 //计算AQI污染等级 | 
 |  |  |                 String pollutionClass = AQIUtils.classCodeOfPollutionByAqi(aqi); | 
 |  |  |                 Integer days = pollutionDaysMap.get(pollutionClass); | 
 |  |  |                 pollutionDaysMap.put(pollutionClass, days + 1); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             //计算比例 | 
 |  |  |             List<PollutionDaysAndProportion> pollutionDaysAndProportions = new ArrayList<>(); | 
 |  |  |             Set<Map.Entry<String, Integer>> entries = pollutionDaysMap.entrySet(); | 
 |  |  |             Iterator<Map.Entry<String, Integer>> iterator = entries.iterator(); | 
 |  |  |             while (iterator.hasNext()) { | 
 |  |  |                 Map.Entry<String, Integer> entry = iterator.next(); | 
 |  |  |                 String pollution = entry.getKey(); | 
 |  |  |                 Integer days = entry.getValue(); | 
 |  |  |                 Double proportionTmp = MathUtils.division(Double.valueOf(days), Double.valueOf(monthlyValues.size()), 3); | 
 |  |  |                 Double proportionD = MathUtils.mul(proportionTmp, 100); | 
 |  |  |                 String proportion = proportionD.toString() + "%"; | 
 |  |  |                 PollutionDaysAndProportion pollutionDaysAndProportion = new PollutionDaysAndProportion(); | 
 |  |  |                 pollutionDaysAndProportion.setDays(days); | 
 |  |  |                 pollutionDaysAndProportion.setPollutionLevel(pollution); | 
 |  |  |                 pollutionDaysAndProportion.setProportion(proportion); | 
 |  |  |                 pollutionDaysAndProportions.add(pollutionDaysAndProportion); | 
 |  |  |             } | 
 |  |  |             //封装返回对象 | 
 |  |  |             MonthlyPollutionLevel monthlyPollutionLevel = new MonthlyPollutionLevel(); | 
 |  |  |             monthlyPollutionLevel.setPollutionDaysAndProportions(pollutionDaysAndProportions); | 
 |  |  |             monthlyPollutionLevel.setTime(monthlyEntry.getKey()); | 
 |  |  |             monthlyPollutionLevels.add(monthlyPollutionLevel); | 
 |  |  |         } | 
 |  |  |         return monthlyPollutionLevels; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public Map<String, Map<String, Object>> oneYearsData(Map map) { | 
 |  |  |         int city_code = Integer.parseInt(map.get("cityCode").toString()); | 
 |  |  |         String year = map.get("year").toString(); | 
 |  |  |         String startTime = year + "-01-01 00:00:00"; | 
 |  |  |         String endTime = year + "-12-31 23:59:59"; | 
 |  |  |         QueryWrapper<CityAqiDaily> cityAqiDailyQueryWrapper = new QueryWrapper<>(); | 
 |  |  |         cityAqiDailyQueryWrapper.eq("city_code", city_code); | 
 |  |  |         cityAqiDailyQueryWrapper.between("time", startTime, endTime); | 
 |  |  |         List<CityAqiDaily> cityAqiDailies = cityAqiDailyMapper.selectList(cityAqiDailyQueryWrapper); | 
 |  |  |         Map<String, Map<String, Object>> resultMap = new HashMap<>(); | 
 |  |  |         for (int i = 1; i < 13; i++) { | 
 |  |  |             Map<String, Object> monthMap = new HashMap<>(); | 
 |  |  |             String month = null; | 
 |  |  |             if (i < 10) { | 
 |  |  |                 month = "0" + i; | 
 |  |  |             } else { | 
 |  |  |                 month = String.valueOf(i); | 
 |  |  |             } | 
 |  |  |             String monthDate = year + "-" + month; | 
 |  |  |             String monthFirstDay = year + "-" + month + "-01"; | 
 |  |  |             int monthDay = DateUtils.getMonthDay(monthFirstDay); | 
 |  |  |             for (int j = 1; j < monthDay + 1; j++) { | 
 |  |  |                 String day = null; | 
 |  |  |                 if (j < 10) { | 
 |  |  |                     day = "0" + j; | 
 |  |  |                 } else { | 
 |  |  |                     day = String.valueOf(j); | 
 |  |  |                 } | 
 |  |  |                 String dayDate = year + "-" + month + "-" + day; | 
 |  |  |                 monthMap.put(dayDate, null); | 
 |  |  |             } | 
 |  |  |             resultMap.put(monthDate, monthMap); | 
 |  |  |         } | 
 |  |  |         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | 
 |  |  |         for (CityAqiDaily cityAqiDaily : cityAqiDailies) { | 
 |  |  |             String time = sdf.format(cityAqiDaily.getTime()); | 
 |  |  |             String monthDate = time.substring(0, 7); | 
 |  |  |             String dayDate = time.substring(0, 10); | 
 |  |  |             Double PM2_5 = null; | 
 |  |  |             Double PM10 = null; | 
 |  |  |             Double SO2 = null; | 
 |  |  |             Double NO2 = null; | 
 |  |  |             Double CO = null; | 
 |  |  |             Double O3 = null; | 
 |  |  |             String primaryPollutant = null; | 
 |  |  |             String AQI = null; | 
 |  |  |             String value = cityAqiDaily.getValue(); | 
 |  |  |             JSONObject jsonObject = new JSONObject(); | 
 |  |  |             jsonObject = JSONObject.parseObject(value); | 
 |  |  |             if (!ObjectUtils.isEmpty(jsonObject.get("PM2_5"))) { | 
 |  |  |                 PM2_5 = (Double.parseDouble(jsonObject.get("PM2_5").toString())); | 
 |  |  |             } | 
 |  |  |             if (!ObjectUtils.isEmpty(jsonObject.get("PM10"))) { | 
 |  |  |                 PM10 = (Double.parseDouble(jsonObject.get("PM10").toString())); | 
 |  |  |             } | 
 |  |  |             if (!ObjectUtils.isEmpty(jsonObject.get("SO2"))) { | 
 |  |  |                 SO2 = (Double.parseDouble(jsonObject.get("SO2").toString())); | 
 |  |  |             } | 
 |  |  |             if (!ObjectUtils.isEmpty(jsonObject.get("NO2"))) { | 
 |  |  |                 NO2 = (Double.parseDouble(jsonObject.get("NO2").toString())); | 
 |  |  |             } | 
 |  |  |             if (!ObjectUtils.isEmpty(jsonObject.get("CO"))) { | 
 |  |  |                 CO = (Double.parseDouble(jsonObject.get("CO").toString())); | 
 |  |  |             } | 
 |  |  |             if (!ObjectUtils.isEmpty(jsonObject.get("O3"))) { | 
 |  |  |                 O3 = (Double.parseDouble(jsonObject.get("O3").toString())); | 
 |  |  |             } | 
 |  |  |             if (!ObjectUtils.isEmpty(jsonObject.get("primaryPollutant"))) { | 
 |  |  |                 primaryPollutant = jsonObject.get("primaryPollutant").toString(); | 
 |  |  |             } | 
 |  |  |             if (!ObjectUtils.isEmpty(jsonObject.get("AQI"))) { | 
 |  |  |                 AQI = jsonObject.get("AQI").toString(); | 
 |  |  |             } | 
 |  |  |             Map<String, Object> valueMap = new HashMap<>(); | 
 |  |  |             valueMap.put("PM2_5", PM2_5); | 
 |  |  |             valueMap.put("PM10", PM10); | 
 |  |  |             valueMap.put("SO2", SO2); | 
 |  |  |             valueMap.put("NO2", NO2); | 
 |  |  |             valueMap.put("CO", CO); | 
 |  |  |             valueMap.put("O3", O3); | 
 |  |  |             valueMap.put("AQI", AQI); | 
 |  |  |             valueMap.put("primaryPollutant", primaryPollutant); | 
 |  |  |             Map<String, Object> monthMap = new HashMap<>(); | 
 |  |  |             monthMap = resultMap.get(monthDate); | 
 |  |  |             monthMap.put(dayDate, valueMap); | 
 |  |  |             resultMap.put(monthDate, monthMap); | 
 |  |  |         } | 
 |  |  |         return resultMap; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<CityPollutionLevel> queryAreaPollutionLevel(AreaPollutionLevelForm form) { | 
 |  |  |         //取参 | 
 |  |  |         Integer regionCode = form.getRegionCode(); | 
 |  |  |         Date startDate = form.getStartDate(); | 
 |  |  |         Date endDate = form.getEndDate(); | 
 |  |  |         //查询该市区下所有的县 | 
 |  |  |         List<SysArea> allAreas = sysAreaService.getChildren(regionCode); | 
 |  |  |         //创建返回数据对象 | 
 |  |  |         List<CityPollutionLevel> datas = new ArrayList<>(); | 
 |  |  |         //遍历每个县级市 查询数据 | 
 |  |  |         for (SysArea area : allAreas) { | 
 |  |  |             CityPollutionLevel cityPollutionLevel = calculateDaysByTimeAndSysArea(area, startDate, endDate); | 
 |  |  |             datas.add(cityPollutionLevel); | 
 |  |  |         } | 
 |  |  |         return datas; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<CityPollutionLevel> querySpecialCitiesPollutionLevel(SpecialCitiesPollutionLevelForm form) { | 
 |  |  |         //取参 | 
 |  |  |         Date startDate = form.getStartDate(); | 
 |  |  |         Date endDate = form.getEndDate(); | 
 |  |  |         List<SysArea> areas = specialCitiesProperties.getTwentyEightCities(); | 
 |  |  |         //创建返回数据对象 | 
 |  |  |         List<CityPollutionLevel> datas = new ArrayList<>(); | 
 |  |  |         //遍历每个县级市 查询数据 | 
 |  |  |         for (SysArea area : areas) { | 
 |  |  |             CityPollutionLevel cityPollutionLevel = calculateDaysByTimeAndSysArea(area, startDate, endDate); | 
 |  |  |             datas.add(cityPollutionLevel); | 
 |  |  |         } | 
 |  |  |         return datas; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<CityAreaRangeVO> cityAreaRange(Integer regionCode, Date time) { | 
 |  |  |         //判断时间是否大于昨天,大于昨天返回null | 
 |  |  |         if (DateUtils.getYesterdayDate().getTime() < time.getTime()) | 
 |  |  |             return null; | 
 |  |  |         //获取城市对应所有的县级市 | 
 |  |  |         List<SysArea> allAreas = sysAreaService.getChildren(regionCode); | 
 |  |  |         //获取同比时间 | 
 |  |  |         Date compareTime = DateUtils.addMonths(time, -12); | 
 |  |  |         //获取指定时间的月份开始结束时间 | 
 |  |  |         Date startDate = DateUtils.getFirstDayOfMonth(time); | 
 |  |  |         Date endDate = DateUtils.getLastDayOfMonth(time); | 
 |  |  |         //获取同比时间的月份开始结束时间 | 
 |  |  |         Date compareStartDate = DateUtils.getFirstDayOfMonth(compareTime); | 
 |  |  |         Date compareEndDate = DateUtils.getLastDayOfMonth(compareTime); | 
 |  |  |         //创建结果集 | 
 |  |  |         List<CityAreaRangeVO> vos = new ArrayList<>(); | 
 |  |  |         for (SysArea area : allAreas) { | 
 |  |  |             CityAreaRangeVO vo = new CityAreaRangeVO(); | 
 |  |  |             Map<String, Object> data = new HashMap<>(); | 
 |  |  |             Map<String, Object> compareData = new HashMap<>(); | 
 |  |  |             //获取指定时间的数据 | 
 |  |  |             QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>(); | 
 |  |  |             wrapper.eq("time", time); | 
 |  |  |             wrapper.eq("city_code", area.getAreaCode()); | 
 |  |  |             List<CityAqiDaily> datas = cityAqiDailyMapper.selectList(wrapper); | 
 |  |  |             if (!ObjectUtils.isEmpty(datas)) | 
 |  |  |                 data = JSON.parseObject(datas.get(0).getValue(), Map.class); | 
 |  |  |             //获取同比时间的数据 | 
 |  |  |             QueryWrapper<CityAqiDaily> compareWrapper = new QueryWrapper<>(); | 
 |  |  |             compareWrapper.eq("time", compareTime); | 
 |  |  |             compareWrapper.eq("city_code", area.getAreaCode()); | 
 |  |  |             List<CityAqiDaily> compareDatas = cityAqiDailyMapper.selectList(compareWrapper); | 
 |  |  |             if (!ObjectUtils.isEmpty(compareDatas)) | 
 |  |  |                 compareData = JSON.parseObject(compareDatas.get(0).getValue(), Map.class); | 
 |  |  |             if (ObjectUtils.isEmpty(compareDatas) && ObjectUtils.isEmpty(datas)) { | 
 |  |  |                 vos.add(vo); | 
 |  |  |                 continue; | 
 |  |  |             } | 
 |  |  |             //获取6参数以及综合指数相关数据 | 
 |  |  |             ConcentrationAndPercent PM25 = getConcentrationAndPercent(data.get("PM2_5"), compareData.get("PM2_5")); | 
 |  |  |             ConcentrationAndPercent PM10 = getConcentrationAndPercent(data.get("PM10"), compareData.get("PM10")); | 
 |  |  |             ConcentrationAndPercent SO2 = getConcentrationAndPercent(data.get("SO2"), compareData.get("SO2")); | 
 |  |  |             ConcentrationAndPercent NO2 = getConcentrationAndPercent(data.get("NO2"), compareData.get("NO2")); | 
 |  |  |             ConcentrationAndPercent CO = getConcentrationAndPercent(data.get("CO"), compareData.get("CO")); | 
 |  |  |             ConcentrationAndPercent O3 = getConcentrationAndPercent(data.get("O3"), compareData.get("O3")); | 
 |  |  |             ConcentrationAndPercent compositeIndex = getConcentrationAndPercent(data.get("compositeIndex"), compareData.get("compositeIndex")); | 
 |  |  |             //获取优良天气和污染天气数据 | 
 |  |  |             CityPollutionLevel days = calculateDaysByTimeAndSysArea(area, startDate, endDate); | 
 |  |  |             CityPollutionLevel compareDays = calculateDaysByTimeAndSysArea(area, compareStartDate, compareEndDate); | 
 |  |  |             Integer fineDays = null; | 
 |  |  |             Integer pollutionDays = null; | 
 |  |  |             Integer compareFineDays = null; | 
 |  |  |             Integer comparePollutionDays = null; | 
 |  |  |             if (days != null) { | 
 |  |  |                 fineDays = days.getExcellentWeatherDays() + days.getGoodWeatherDays(); | 
 |  |  |                 pollutionDays = days.getMildWeatherDays() + days.getServerWeatherDays() + days.getSeriousWeatherDays() + days.getMiddleWeatherDays(); | 
 |  |  |             } | 
 |  |  |             if (compareDays != null) { | 
 |  |  |                 compareFineDays = compareDays.getExcellentWeatherDays() + compareDays.getGoodWeatherDays(); | 
 |  |  |                 comparePollutionDays = compareDays.getMildWeatherDays() + compareDays.getServerWeatherDays() + compareDays.getSeriousWeatherDays() + compareDays.getMiddleWeatherDays(); | 
 |  |  |             } | 
 |  |  |             ConcentrationAndPercent fineDaysData = getFineDaysConcentrationAndPercent(fineDays, compareFineDays); | 
 |  |  |             ConcentrationAndPercent pollutionDaysData = getFineDaysConcentrationAndPercent(pollutionDays, comparePollutionDays); | 
 |  |  |             //封装对象 | 
 |  |  |             vo.setCityName(area.getAreaName()); | 
 |  |  |             vo.setCompositeIndex(compositeIndex); | 
 |  |  |             vo.setPM10(PM10); | 
 |  |  |             vo.setPM25(PM25); | 
 |  |  |             vo.setNO2(NO2); | 
 |  |  |             vo.setSO2(SO2); | 
 |  |  |             vo.setO3(O3); | 
 |  |  |             vo.setCO(CO); | 
 |  |  |             vo.setFineDays(fineDaysData); | 
 |  |  |             vo.setServerDays(pollutionDaysData); | 
 |  |  |             vos.add(vo); | 
 |  |  |         } | 
 |  |  |         //对数据进行排名 | 
 |  |  |         vos.sort(Comparator.comparing(value -> Double.valueOf(value.getCompositeIndex().getConcentration()))); | 
 |  |  |         for(int i =1;i<vos.size()+1;i++){ | 
 |  |  |             vos.get(i-1).setRange(i); | 
 |  |  |         } | 
 |  |  |         return vos; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<CityPollutionLevel> queryProvinceCitiesPollutionLevel(ProvinceCitiesPollutionLevelForm form) { | 
 |  |  |         //取参 | 
 |  |  |         Date startDate = form.getStartDate(); | 
 |  |  |         Date endDate = form.getEndDate(); | 
 |  |  |         Integer regionCode = form.getRegionCode(); | 
 |  |  |         //查询省下所有的市 | 
 |  |  |         List<SysArea> allCities = sysAreaService.getChildren(regionCode); | 
 |  |  |         //创建返回数据对象 | 
 |  |  |         List<CityPollutionLevel> datas = new ArrayList<>(); | 
 |  |  |         //遍历每个县级市 查询数据 | 
 |  |  |         for (SysArea city : allCities) { | 
 |  |  |             CityPollutionLevel cityPollutionLevel = calculateDaysByTimeAndSysArea(city, startDate, endDate); | 
 |  |  |             datas.add(cityPollutionLevel); | 
 |  |  |         } | 
 |  |  |         return datas; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public CityPollutionLevel calculateDaysByTimeAndSysArea(SysArea area, Date startDate, Date endDate) { | 
 |  |  |         //查询数据 | 
 |  |  |         QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>(); | 
 |  |  |         wrapper.eq("city_code", area.getAreaCode()); | 
 |  |  |         wrapper.between("time", startDate, endDate); | 
 |  |  |         wrapper.orderByAsc("time"); | 
 |  |  |         List<CityAqiDaily> cityAqiDailies = cityAqiDailyMapper.selectList(wrapper); | 
 |  |  |         if (ObjectUtils.isEmpty(cityAqiDailies)) | 
 |  |  |             return null; | 
 |  |  |         //如果数据的时间有重复则去重 | 
 |  |  |         Map<String, CityAqiDaily> tmpMap = new LinkedHashMap<>(); | 
 |  |  |         for (CityAqiDaily cityAqiDaily : cityAqiDailies) { | 
 |  |  |             String tmpTime = DateUtils.dateToDateString(cityAqiDaily.getTime(), "yyyy-MM-dd"); | 
 |  |  |             tmpMap.put(tmpTime, cityAqiDaily); | 
 |  |  |         } | 
 |  |  |         cityAqiDailies = new ArrayList<CityAqiDaily>(tmpMap.values()); | 
 |  |  |         //用于统计各个污染等级的天数 | 
 |  |  |         Map<String, Integer> pollutionDaysMap = new LinkedHashMap<>(); | 
 |  |  |         pollutionDaysMap.put(Constants.EXCELLENT_WEATHER_CODE, 0); | 
 |  |  |         pollutionDaysMap.put(Constants.GOOD_WEATHER_CODE, 0); | 
 |  |  |         pollutionDaysMap.put(Constants.MILD_WEATHER_CODE, 0); | 
 |  |  |         pollutionDaysMap.put(Constants.MIDDLE_WEATHER_CODE, 0); | 
 |  |  |         pollutionDaysMap.put(Constants.SERIOUS_WEATHER_CODE, 0); | 
 |  |  |         pollutionDaysMap.put(Constants.SERVER_WEATHER_CODE, 0); | 
 |  |  |         for (CityAqiDaily cityAqiDaily : cityAqiDailies) { | 
 |  |  |             Map<String, Object> valueMap = JSON.parseObject(cityAqiDaily.getValue(), Map.class); | 
 |  |  |             if (valueMap.get("AQI") == null) | 
 |  |  |                 continue; | 
 |  |  |             Integer aqi = Integer.valueOf(valueMap.get("AQI").toString()); | 
 |  |  |             //计算AQI污染等级 | 
 |  |  |             String pollutionClass = AQIUtils.classCodeOfPollutionByAqi(aqi); | 
 |  |  |             Integer days = pollutionDaysMap.get(pollutionClass); | 
 |  |  |             pollutionDaysMap.put(pollutionClass, days + 1); | 
 |  |  |         } | 
 |  |  |         //封装对象 | 
 |  |  |         CityPollutionLevel cityPollutionLevel = new CityPollutionLevel(); | 
 |  |  |         cityPollutionLevel.setRegionName(area.getAreaName()); | 
 |  |  |         cityPollutionLevel.setExcellentWeatherDays(pollutionDaysMap.get(Constants.EXCELLENT_WEATHER_CODE)); | 
 |  |  |         cityPollutionLevel.setGoodWeatherDays(pollutionDaysMap.get(Constants.GOOD_WEATHER_CODE)); | 
 |  |  |         cityPollutionLevel.setMildWeatherDays(pollutionDaysMap.get(Constants.MILD_WEATHER_CODE)); | 
 |  |  |         cityPollutionLevel.setMiddleWeatherDays(pollutionDaysMap.get(Constants.MIDDLE_WEATHER_CODE)); | 
 |  |  |         cityPollutionLevel.setSeriousWeatherDays(pollutionDaysMap.get(Constants.SERIOUS_WEATHER_CODE)); | 
 |  |  |         cityPollutionLevel.setServerWeatherDays(pollutionDaysMap.get(Constants.SERVER_WEATHER_CODE)); | 
 |  |  |         return cityPollutionLevel; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<String> airQualityBulletin(String regionCode, Date time) { | 
 |  |  |         List<String> result = new ArrayList<>(); | 
 |  |  |         //查询日数据 | 
 |  |  |         QueryWrapper<CityAqiDaily> dailyWrapper = new QueryWrapper<>(); | 
 |  |  |         dailyWrapper.eq("city_code", regionCode); | 
 |  |  |         dailyWrapper.eq("time", time); | 
 |  |  |         CityAqiDaily cityAqiDaily = cityAqiDailyMapper.selectOne(dailyWrapper); | 
 |  |  |         if (cityAqiDaily == null) | 
 |  |  |             return null; | 
 |  |  |         Map<String, Object> dailyDataMap = JSON.parseObject(cityAqiDaily.getValue(), Map.class); | 
 |  |  |         //获取年月日 | 
 |  |  |         int year = DateUtils.getYear(time); | 
 |  |  |         int month = DateUtils.getMonth(time); | 
 |  |  |         int day = DateUtils.getDay(time); | 
 |  |  |         //获取简报title | 
 |  |  |         String title = getBulletinTitle(month + "", day + ""); | 
 |  |  |         if (title == null) | 
 |  |  |             return null; | 
 |  |  |         //获取简报第一段文字 | 
 |  |  |         String paragraphOne = getParagraphOne(month + "", day + "", dailyDataMap); | 
 |  |  |         if (paragraphOne == null) | 
 |  |  |             return null; | 
 |  |  |         //获取第二段文字 | 
 |  |  |         List<String> paragraphTwoAndFour = getParagraphTwoAndFour(year + "", month + "", day + "", regionCode, time); | 
 |  |  |         if (paragraphTwoAndFour == null) | 
 |  |  |             return null; | 
 |  |  |         String paragraphTwo = paragraphTwoAndFour.get(0); | 
 |  |  |         //获取简报第三段文字 | 
 |  |  |         String paragraphThree = getParagraphThree(month + "", day + "", regionCode, time); | 
 |  |  |         if (paragraphThree == null) | 
 |  |  |             return null; | 
 |  |  |         //获取第四段文字 | 
 |  |  |         String paragraphFour = paragraphTwoAndFour.get(1); | 
 |  |  |         result.add(title); | 
 |  |  |         result.add(paragraphOne); | 
 |  |  |         result.add(paragraphTwo); | 
 |  |  |         result.add(paragraphThree); | 
 |  |  |         result.add(paragraphFour); | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //获取简报title信息 | 
 |  |  |     private String getBulletinTitle(String month, String day) { | 
 |  |  |         String title = bulletinProperties.getTitle(); | 
 |  |  |         title = title.replace("{month}", month); | 
 |  |  |         title = title.replace("{day}", day); | 
 |  |  |         return title; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //获取简报第一段信息 | 
 |  |  |     private String getParagraphOne(String month, String day, Map<String, Object> dailyData) { | 
 |  |  |         //计算污染等级 | 
 |  |  |         String pollutionLevelDescribe = AQIUtils.classOfPollutionByAqi(Integer.parseInt(dailyData.get("AQI").toString())); | 
 |  |  |         String pollutionLevel = AQIUtils.classCodeOfPollutionByAqi(Integer.parseInt(dailyData.get("AQI").toString())); | 
 |  |  |         //计算未超标因子数目 | 
 |  |  |         int standardCount = AQIUtils.standardCount(dailyData); | 
 |  |  |         String paragraphOne = bulletinProperties.getParagraphOne(); | 
 |  |  |         paragraphOne = paragraphOne.replace("{month}", month); | 
 |  |  |         paragraphOne = paragraphOne.replace("{day}", day); | 
 |  |  |         paragraphOne = paragraphOne.replace("{pollutionLevel}", pollutionLevel); | 
 |  |  |         paragraphOne = paragraphOne.replace("{pollutionLevelDescribe}", pollutionLevelDescribe); | 
 |  |  |         paragraphOne = paragraphOne.replace("{standardCount}", standardCount + ""); | 
 |  |  |         paragraphOne = paragraphOne.replace("{PM2.5}", dailyData.get("PM2_5").toString()); | 
 |  |  |         paragraphOne = paragraphOne.replace("{SO2}", dailyData.get("SO2").toString()); | 
 |  |  |         paragraphOne = paragraphOne.replace("{NO2}", dailyData.get("NO2").toString()); | 
 |  |  |         paragraphOne = paragraphOne.replace("{CO}", dailyData.get("CO").toString()); | 
 |  |  |         paragraphOne = paragraphOne.replace("{O3}", dailyData.get("O3").toString()); | 
 |  |  |         paragraphOne = paragraphOne.replace("{PM10}", dailyData.get("PM10").toString()); | 
 |  |  |         return paragraphOne; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //获取简报第二段和第四段信息 | 
 |  |  |     private List<String> getParagraphTwoAndFour(String year, String month, String day, String regionCode, Date time) { | 
 |  |  |         List<String> result = new ArrayList<>(); | 
 |  |  |         String paragraphTwo = bulletinProperties.getParagraphTwo(); | 
 |  |  |         String paragraphFour = bulletinProperties.getParagraphFour(); | 
 |  |  |         Date startTime = DateUtils.getFirstDayOfYear(time); | 
 |  |  |         Date endTime = time; | 
 |  |  |         Date compareStartTime = DateUtils.addMonths(startTime, -12); | 
 |  |  |         Date compareEndTime = DateUtils.addMonths(endTime, -12); | 
 |  |  |         SysArea area = new SysArea(); | 
 |  |  |         area.setAreaCode(Integer.parseInt(regionCode)); | 
 |  |  |         //查询年度优良天气 | 
 |  |  |         CityPollutionLevel days = calculateDaysByTimeAndSysArea(area, startTime, endTime); | 
 |  |  |         //查询对比年度优良天气 | 
 |  |  |         CityPollutionLevel compareDays = calculateDaysByTimeAndSysArea(area, compareStartTime, compareEndTime); | 
 |  |  |         if (days == null || compareDays == null) | 
 |  |  |             return null; | 
 |  |  |         //查询年度数据 | 
 |  |  |         List<CityAqiYearly> yearDataList = cityAqiYearlyService.getCityAqiYearlyByRegionCodeAndTime(Integer.parseInt(regionCode), startTime, endTime); | 
 |  |  |         List<CityAqiYearly> compareYearDataList = cityAqiYearlyService.getCityAqiYearlyByRegionCodeAndTime(Integer.parseInt(regionCode), compareStartTime, compareEndTime); | 
 |  |  |         if (ObjectUtils.isEmpty(yearDataList) || ObjectUtils.isEmpty(compareYearDataList)) | 
 |  |  |             return null; | 
 |  |  |         CityAqiYearly yearData = yearDataList.get(0); | 
 |  |  |         CityAqiYearly compareYearData = compareYearDataList.get(0); | 
 |  |  |         Map<String, Object> dataMap = JSON.parseObject(yearData.getValue(), Map.class); | 
 |  |  |         Map<String, Object> compareDataMap = JSON.parseObject(compareYearData.getValue(), Map.class); | 
 |  |  |         //封装第二段信息 | 
 |  |  |         String yearPM_5 = dataMap.get("PM2_5").toString(); | 
 |  |  |         String fineDays = days.getExcellentWeatherDays() + days.getGoodWeatherDays() + ""; | 
 |  |  |         int allDays = DateUtils.getDays(startTime, endTime) + 1; | 
 |  |  |         paragraphTwo = paragraphTwo.replace("{year}", year); | 
 |  |  |         paragraphTwo = paragraphTwo.replace("{yearPM2.5}", yearPM_5); | 
 |  |  |         paragraphTwo = paragraphTwo.replace("{fineDays}", fineDays); | 
 |  |  |         paragraphTwo = paragraphTwo.replace("{days}", allDays + ""); | 
 |  |  |         result.add(paragraphTwo); | 
 |  |  |  | 
 |  |  |         //封装第四段信息 | 
 |  |  |         //获取综合指数数据 | 
 |  |  |         Double compositeIndex = Double.valueOf(dataMap.get("compositeIndex").toString()); | 
 |  |  |         Double compareCompositeIndex = Double.valueOf(compareDataMap.get("compositeIndex").toString()); | 
 |  |  |         //获取综合指数上升下降变量 | 
 |  |  |         String yoyYearCompositeIndexUpDown = getUpOrDown(compositeIndex, compareCompositeIndex); | 
 |  |  |         //获取综合指数同比差值比例变量 | 
 |  |  |         String yoyYearCompositeIndex = calculateComparePerPositive(compositeIndex, compareCompositeIndex).toString(); | 
 |  |  |  | 
 |  |  |         //获取年度PM2.5 | 
 |  |  |         Double PM2_5 = Double.valueOf(dataMap.get("PM2_5").toString()); | 
 |  |  |         Double comparePM2_5 = Double.valueOf(compareDataMap.get("PM2_5").toString()); | 
 |  |  |         //获取PM2.5上升下降变量 | 
 |  |  |         String yoyYearPM2_5UpDown = getUpOrDown(PM2_5, comparePM2_5); | 
 |  |  |         //获取PM2.5同比差值比例变量 | 
 |  |  |         String yoyYearPM2_5 = calculateComparePerPositive(PM2_5, comparePM2_5).toString(); | 
 |  |  |  | 
 |  |  |         //获取年度优良天数 | 
 |  |  |         int yearFineDays = days.getExcellentWeatherDays() + days.getGoodWeatherDays(); | 
 |  |  |         //获取同比年度优良天数 | 
 |  |  |         int compareYearFineDays = compareDays.getExcellentWeatherDays() + days.getGoodWeatherDays(); | 
 |  |  |         //获取优良天数差值 | 
 |  |  |         int yoyYearFineDaysInt = yearFineDays - compareYearFineDays; | 
 |  |  |         String yoyYearFineDays = yoyYearFineDaysInt > 0 ? yoyYearFineDaysInt + "" : yoyYearFineDaysInt * (-1) + ""; | 
 |  |  |         //获取优良天增加减少变量 | 
 |  |  |         String yoyYearFineDaysUpDown = getIncreaseOrDecrease(yearFineDays, compareYearFineDays); | 
 |  |  |         //获取年度污染天数 | 
 |  |  |         int yearPollutionDays = days.getMildWeatherDays() + days.getMiddleWeatherDays() + days.getSeriousWeatherDays() + days.getServerWeatherDays(); | 
 |  |  |         //获取年度优良天气占比 | 
 |  |  |         String yearFineDaysPer = String.valueOf(MathUtils.division(yearFineDays * 100, allDays, 2)); | 
 |  |  |  | 
 |  |  |         //获取168城市综合指数排名和综合指数变化率排名 | 
 |  |  |         List<Integer> oneSixEightRanges = rangeByCities(compositeIndex, | 
 |  |  |                 calculateCompare(compositeIndex, compareCompositeIndex), | 
 |  |  |                 startTime, endTime, compareStartTime, compareEndTime, | 
 |  |  |                 specialCitiesProperties.getOneSixEightCities()); | 
 |  |  |  | 
 |  |  |         //获取28城市综合指数排名和综合指数变化率排名 | 
 |  |  |         List<Integer> twentyEightRanges = rangeByCities(compositeIndex, | 
 |  |  |                 calculateCompare(compositeIndex, compareCompositeIndex), | 
 |  |  |                 startTime, endTime, compareStartTime, compareEndTime, | 
 |  |  |                 specialCitiesProperties.getTwentyEightCities()); | 
 |  |  |  | 
 |  |  |         String compositeIndex28range = twentyEightRanges.get(0).toString(); | 
 |  |  |         String compositeIndexPer28range = twentyEightRanges.get(1).toString(); | 
 |  |  |         String compositeIndex168range = oneSixEightRanges.get(0).toString(); | 
 |  |  |         String compositeIndexPer168range = oneSixEightRanges.get(1).toString(); | 
 |  |  |         //封装返回信息 | 
 |  |  |         paragraphFour = paragraphFour.replace("{month}", month); | 
 |  |  |         paragraphFour = paragraphFour.replace("{day}", day); | 
 |  |  |         paragraphFour = paragraphFour.replace("{yearCompositeIndex}", compositeIndex + ""); | 
 |  |  |         paragraphFour = paragraphFour.replace("{yoyYearCompositeIndex}", yoyYearCompositeIndex); | 
 |  |  |         paragraphFour = paragraphFour.replace("{yoyYearCompositeIndexUpDown}", yoyYearCompositeIndexUpDown); | 
 |  |  |         paragraphFour = paragraphFour.replace("{yearPM2.5}", PM2_5 + ""); | 
 |  |  |         paragraphFour = paragraphFour.replace("{yoyYearPM2.5UpDown}", yoyYearPM2_5UpDown); | 
 |  |  |         paragraphFour = paragraphFour.replace("{yoyYearPM2.5}", yoyYearPM2_5); | 
 |  |  |         paragraphFour = paragraphFour.replace("{yearFineDays}", yearFineDays + ""); | 
 |  |  |         paragraphFour = paragraphFour.replace("{yearFineDaysPer}", yearFineDaysPer); | 
 |  |  |         paragraphFour = paragraphFour.replace("{yearPollutionDays}", yearPollutionDays + ""); | 
 |  |  |         paragraphFour = paragraphFour.replace("{yoyYearFineDaysUpDown}", yoyYearFineDaysUpDown); | 
 |  |  |         paragraphFour = paragraphFour.replace("{yoyYearFineDays}", yoyYearFineDays); | 
 |  |  |         paragraphFour = paragraphFour.replace("{compositeIndex28range}", compositeIndex28range); | 
 |  |  |         paragraphFour = paragraphFour.replace("{compositeIndexPer28range}", compositeIndexPer28range); | 
 |  |  |         paragraphFour = paragraphFour.replace("{compositeIndex168range}", compositeIndex168range); | 
 |  |  |         paragraphFour = paragraphFour.replace("{compositeIndexPer168range}", compositeIndexPer168range); | 
 |  |  |         result.add(paragraphFour); | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //获取城市综合指数和综合指数变化率排名 | 
 |  |  |     private List<Integer> rangeByCities( | 
 |  |  |             Double compositeIndex, | 
 |  |  |             Double compositeIndexPer, | 
 |  |  |             Date startTime, | 
 |  |  |             Date endTime, | 
 |  |  |             Date compareStartTime, | 
 |  |  |             Date compareEndTime, List<SysArea> areas) { | 
 |  |  |         List<Double> compositeIndexList = new ArrayList<>(); | 
 |  |  |         List<Double> compositePerList = new ArrayList<>(); | 
 |  |  |         for (SysArea sysArea : areas) { | 
 |  |  |             //获取数据 | 
 |  |  |             List<CityAqiYearly> data = cityAqiYearlyService.getCityAqiYearlyByRegionCodeAndTime(sysArea.getAreaCode(), startTime, endTime); | 
 |  |  |             //获取对比数据 | 
 |  |  |             List<CityAqiYearly> compareData = cityAqiYearlyService.getCityAqiYearlyByRegionCodeAndTime(sysArea.getAreaCode(), compareStartTime, compareEndTime); | 
 |  |  |             if (ObjectUtils.isEmpty(data) || ObjectUtils.isEmpty(compareData)) | 
 |  |  |                 continue; | 
 |  |  |             Map<String, Object> dataMap = JSON.parseObject(data.get(0).getValue(), Map.class); | 
 |  |  |             Map<String, Object> compareDataMap = JSON.parseObject(compareData.get(0).getValue(), Map.class); | 
 |  |  |             //获取综合指数 | 
 |  |  |             compositeIndexList.add(Double.valueOf(dataMap.get("compositeIndex").toString())); | 
 |  |  |             //计算综合指数变化率 | 
 |  |  |             Double per = calculateCompare(Double.valueOf(dataMap.get("compositeIndex").toString()), Double.valueOf(compareDataMap.get("compositeIndex").toString())); | 
 |  |  |             compositePerList.add(per); | 
 |  |  |         } | 
 |  |  |         Collections.sort(compositeIndexList); | 
 |  |  |         Collections.sort(compositePerList); | 
 |  |  |         Integer compositeIndexRange = compositeIndexList.indexOf(compositeIndex); | 
 |  |  |         Integer compositePerRange = compositePerList.indexOf(compositeIndexPer); | 
 |  |  |         return Arrays.asList((compositeIndexRange + 1), (compositePerRange + 1)); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //获取简报第三段信息 | 
 |  |  |     private String getParagraphThree(String month, String day, String regionCode, Date time) { | 
 |  |  |         String paragraphThree = bulletinProperties.getParagraphThree(); | 
 |  |  |         //查询指定月度数据 | 
 |  |  |         CityAqiMonthly monthlyData = cityAqiMonthlyService.getCityAqiMonthByRegionCodeAndTime(Integer.parseInt(regionCode), time); | 
 |  |  |         //查询同比月度指数 | 
 |  |  |         Date compareTime = DateUtils.addMonths(time, -12); | 
 |  |  |         CityAqiMonthly compareMonthlyData = cityAqiMonthlyService.getCityAqiMonthByRegionCodeAndTime(Integer.parseInt(regionCode), compareTime); | 
 |  |  |         if (monthlyData == null || compareMonthlyData == null) | 
 |  |  |             return null; | 
 |  |  |         Map<String, Object> monthlyDataMap = JSON.parseObject(monthlyData.getValue(), Map.class); | 
 |  |  |         Map<String, Object> compareMonthlyDataMap = JSON.parseObject(compareMonthlyData.getValue(), Map.class); | 
 |  |  |         //查询优良天数 | 
 |  |  |         Date endTime = time; | 
 |  |  |         Date startTime = DateUtils.getFirstDayOfMonth(time); | 
 |  |  |         Date compareStartTime = DateUtils.addMonths(startTime, -12); | 
 |  |  |         Date compareEndTime = DateUtils.addMonths(endTime, -12); | 
 |  |  |         SysArea area = new SysArea(); | 
 |  |  |         area.setAreaCode(Integer.parseInt(regionCode)); | 
 |  |  |         CityPollutionLevel days = calculateDaysByTimeAndSysArea(area, startTime, endTime); | 
 |  |  |         CityPollutionLevel compareDays = calculateDaysByTimeAndSysArea(area, compareStartTime, compareEndTime); | 
 |  |  |         if (days == null || compareDays == null) | 
 |  |  |             return null; | 
 |  |  |         //获取综合指数数据 | 
 |  |  |         Double compositeIndex = Double.valueOf(monthlyDataMap.get("compositeIndex").toString()); | 
 |  |  |         Double compareCompositeIndex = Double.valueOf(compareMonthlyDataMap.get("compositeIndex").toString()); | 
 |  |  |         //获取综合指数上升下降变量 | 
 |  |  |         String yoyMonthCompositeIndexUpDown = getUpOrDown(compositeIndex, compareCompositeIndex); | 
 |  |  |         //获取综合指数同比差值比例变量 | 
 |  |  |         String yoyMonthCompositeIndex = calculateComparePerPositive(compositeIndex, compareCompositeIndex).toString(); | 
 |  |  |  | 
 |  |  |         //获取月度PM2.5 | 
 |  |  |         Double PM2_5 = Double.valueOf(monthlyDataMap.get("PM2_5").toString()); | 
 |  |  |         Double comparePM2_5 = Double.valueOf(compareMonthlyDataMap.get("PM2_5").toString()); | 
 |  |  |         //获取PM2.5上升下降变量 | 
 |  |  |         String yoyMonthPM2_5UpDown = getUpOrDown(PM2_5, comparePM2_5); | 
 |  |  |         //获取PM2.5同比差值比例变量 | 
 |  |  |         String yoyMonthPM2_5 = calculateComparePerPositive(PM2_5, comparePM2_5).toString(); | 
 |  |  |  | 
 |  |  |         //获取月度优良天数 | 
 |  |  |         int monthFineDays = days.getExcellentWeatherDays() + days.getGoodWeatherDays(); | 
 |  |  |         //获取同比月度优良天数 | 
 |  |  |         int compareMonthFineDays = compareDays.getExcellentWeatherDays() + days.getGoodWeatherDays(); | 
 |  |  |         //获取优良天数差值 | 
 |  |  |         int yoyMonthFineDaysInt = monthFineDays - compareMonthFineDays; | 
 |  |  |         String yoyMonthFineDays = yoyMonthFineDaysInt > 0 ? yoyMonthFineDaysInt + "" : yoyMonthFineDaysInt * (-1) + ""; | 
 |  |  |         //获取优良天增加减少变量 | 
 |  |  |         String yoyMonthFineDaysUpDown = getIncreaseOrDecrease(monthFineDays, compareMonthFineDays); | 
 |  |  |  | 
 |  |  |         paragraphThree = paragraphThree.replace("{month}", month); | 
 |  |  |         paragraphThree = paragraphThree.replace("{day}", day); | 
 |  |  |         paragraphThree = paragraphThree.replace("{monthCompositeIndex}", compositeIndex.toString()); | 
 |  |  |         paragraphThree = paragraphThree.replace("{yoyMonthCompositeIndex}", yoyMonthCompositeIndex); | 
 |  |  |         paragraphThree = paragraphThree.replace("{yoyMonthCompositeIndexUpDown}", yoyMonthCompositeIndexUpDown); | 
 |  |  |         paragraphThree = paragraphThree.replace("{monthPM2.5}", PM2_5.toString()); | 
 |  |  |         paragraphThree = paragraphThree.replace("{yoyMonthPM2.5UpDown}", yoyMonthPM2_5UpDown); | 
 |  |  |         paragraphThree = paragraphThree.replace("{yoyMonthPM2.5}", yoyMonthPM2_5); | 
 |  |  |         paragraphThree = paragraphThree.replace("{monthFineDays}", monthFineDays + ""); | 
 |  |  |         paragraphThree = paragraphThree.replace("{yoyMonthFineDaysUpDown}", yoyMonthFineDaysUpDown); | 
 |  |  |         paragraphThree = paragraphThree.replace("{yoyMonthFineDays}", yoyMonthFineDays); | 
 |  |  |         return paragraphThree; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //根据currentData和compareData的大小获取"上升","下降".currentData>compareData则返回上升 | 
 |  |  |     private String getUpOrDown(Double currentData, Double compareData) { | 
 |  |  |         if (MathUtils.sub(currentData, compareData) > 0) | 
 |  |  |             return "上升"; | 
 |  |  |         return "下降"; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //根据当前优良天数和对比优良天数的大小获取"增加","减少".currentData>compareData则返回增加 | 
 |  |  |     private String getIncreaseOrDecrease(int currentDays, int compareDays) { | 
 |  |  |         if (currentDays - compareDays > 0) | 
 |  |  |             return "增加"; | 
 |  |  |         return "减少"; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     //计算同比上升/下降比例 公式(当前数据-同比数据)/同比数据 只返回正数 | 
 |  |  |     private Double calculateComparePerPositive(Double currentData, Double compareData) { | 
 |  |  |         double tmp1 = MathUtils.sub(currentData, compareData); | 
 |  |  |         double result = MathUtils.mul(MathUtils.division(tmp1, compareData, 4), 100d); | 
 |  |  |         return result > 0 ? result : MathUtils.mul(result, -1d); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //计算同比上升/下降比例 公式(当前数据-同比数据)/同比数据 | 
 |  |  |     private Double calculateCompare(Double currentData, Double compareData) { | 
 |  |  |         double tmp1 = MathUtils.sub(currentData, compareData); | 
 |  |  |         return MathUtils.mul(MathUtils.division(tmp1, compareData, 4), 100d); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     //获取六参以及综合指数的指数 同比浓度 以及同比 | 
 |  |  |     private ConcentrationAndPercent getConcentrationAndPercent(Object data, Object compareData) { | 
 |  |  |         ConcentrationAndPercent cap = new ConcentrationAndPercent(); | 
 |  |  |         cap.setConcentration(data != null ? data.toString() : "-"); | 
 |  |  |         cap.setCompareConcentration(compareData != null ? compareData.toString() : "-"); | 
 |  |  |         if (data != null && compareData != null) | 
 |  |  |             cap.setPercent(calculateCompare(Double.valueOf(data.toString()), Double.valueOf(compareData.toString())) + "%"); | 
 |  |  |         else | 
 |  |  |             cap.setPercent("-"); | 
 |  |  |         return cap; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //获取污染天气和优良天的天数和同比 | 
 |  |  |     private ConcentrationAndPercent getFineDaysConcentrationAndPercent(Integer days, Integer compareDays) { | 
 |  |  |         ConcentrationAndPercent cap = new ConcentrationAndPercent(); | 
 |  |  |         cap.setConcentration(days != null ? days + "天" : "-"); | 
 |  |  |         cap.setPercent(compareDays != null ? (days - compareDays) + "天" : "-"); | 
 |  |  |         return cap; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  | } | 
 |  |  |  |