|  |  | 
 |  |  | import com.moral.api.mapper.CityAqiDailyMapper; | 
 |  |  | 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; | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @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(); | 
 |  |  | 
 |  |  |             return null; | 
 |  |  |         //获取第二段文字 | 
 |  |  |         List<String> paragraphTwoAndFour = getParagraphTwoAndFour(year + "", month + "", day + "", regionCode, time); | 
 |  |  |         if(paragraphTwoAndFour==null) | 
 |  |  |         if (paragraphTwoAndFour == null) | 
 |  |  |             return null; | 
 |  |  |         String paragraphTwo =  paragraphTwoAndFour.get(0); | 
 |  |  |         String paragraphTwo = paragraphTwoAndFour.get(0); | 
 |  |  |         //获取简报第三段文字 | 
 |  |  |         String paragraphThree = getParagraphThree(month + "", day + "", regionCode, time); | 
 |  |  |         if (paragraphThree == null) | 
 |  |  |             return null; | 
 |  |  |         //获取第四段文字 | 
 |  |  |         String paragraphFour =  paragraphTwoAndFour.get(1); | 
 |  |  |         String paragraphFour = paragraphTwoAndFour.get(1); | 
 |  |  |         result.add(title); | 
 |  |  |         result.add(paragraphOne); | 
 |  |  |         result.add(paragraphTwo); | 
 |  |  | 
 |  |  |         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); | 
 |  |  | 
 |  |  |         //封装第二段信息 | 
 |  |  |         String yearPM_5 = dataMap.get("PM2_5").toString(); | 
 |  |  |         String fineDays = days.getExcellentWeatherDays() + days.getGoodWeatherDays() + ""; | 
 |  |  |         int allDays = DateUtils.getDays(startTime, endTime)+1; | 
 |  |  |         int allDays = DateUtils.getDays(startTime, endTime) + 1; | 
 |  |  |         paragraphTwo = paragraphTwo.replace("{year}", year); | 
 |  |  |         paragraphTwo = paragraphTwo.replace("{yearPM2.5}", yearPM_5); | 
 |  |  |         paragraphTwo = paragraphTwo.replace("{fineDays}", fineDays); | 
 |  |  | 
 |  |  |         //获取年度污染天数 | 
 |  |  |         int yearPollutionDays = days.getMildWeatherDays() + days.getMiddleWeatherDays() + days.getSeriousWeatherDays() + days.getServerWeatherDays(); | 
 |  |  |         //获取年度优良天气占比 | 
 |  |  |         String yearFineDaysPer = String.valueOf(MathUtils.division(yearFineDays*100 ,allDays,2)); | 
 |  |  |         String yearFineDaysPer = String.valueOf(MathUtils.division(yearFineDays * 100, allDays, 2)); | 
 |  |  |  | 
 |  |  |         //获取168城市综合指数排名和综合指数变化率排名 | 
 |  |  |         List<Integer> oneSixEightRanges = rangeByCities(compositeIndex, | 
 |  |  | 
 |  |  |         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); | 
 |  |  |         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){ | 
 |  |  |             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)) | 
 |  |  |             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); | 
 |  |  |             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())); | 
 |  |  |             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)); | 
 |  |  |         return Arrays.asList((compositeIndexRange + 1), (compositePerRange + 1)); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //获取简报第三段信息 | 
 |  |  | 
 |  |  |     //计算同比上升/下降比例 公式(当前数据-同比数据)/同比数据 只返回正数 | 
 |  |  |     private Double calculateComparePerPositive(Double currentData, Double compareData) { | 
 |  |  |         double tmp1 = MathUtils.sub(currentData, compareData); | 
 |  |  |         double result = MathUtils.mul(MathUtils.division(tmp1, compareData, 4),100d); | 
 |  |  |         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); | 
 |  |  |         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; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  |