| | |
| | | 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; |
| | |
| | | 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 |
| | |
| | | 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; |
| | | } |
| | | |
| | | |
| | | } |
| | | |