|  |  | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
 |  |  | import com.moral.api.config.properties.SpecialCitiesProperties; | 
 |  |  | import com.moral.api.dto.CityAqiDailyListDTO; | 
 |  |  | import com.moral.api.entity.*; | 
 |  |  | import com.moral.api.mapper.CityAqiMapper; | 
 |  |  | import com.moral.api.mapper.DeviceMapper; | 
 |  |  | import com.moral.api.mapper.ForecastMapper; | 
 |  |  | import com.moral.api.mapper.SysAreaMapper; | 
 |  |  | import com.moral.api.mapper.*; | 
 |  |  | 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.util.MathUtils; | 
 |  |  |  | 
 |  |  | import org.apache.commons.collections4.CollectionUtils; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private CityAqiMapper cityAqiMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private CityAqiDailyMapper cityAqiDailyMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private ForecastMapper forecastMapper; | 
 |  |  | 
 |  |  |     @Override | 
 |  |  |     public Map<String, Object> query24HoursAqiByRegionCode(Integer regionCode) { | 
 |  |  |         //查询最新一条数据,用于获取最新的时间 | 
 |  |  |         QueryWrapper<CityAqi> lastDataWrapper = new QueryWrapper<>(); | 
 |  |  |         /*QueryWrapper<CityAqi> lastDataWrapper = new QueryWrapper<>(); | 
 |  |  |         lastDataWrapper.eq("city_code", regionCode); | 
 |  |  |         lastDataWrapper.orderByDesc("time"); | 
 |  |  |         lastDataWrapper.last(true, "limit 1"); | 
 |  |  | 
 |  |  |         //查询数据 | 
 |  |  |         QueryWrapper<CityAqi> wrapper = new QueryWrapper<>(); | 
 |  |  |         wrapper.between("time", startDate, endDate); | 
 |  |  |         wrapper.eq("city_code", regionCode); | 
 |  |  |         List<CityAqi> cityAqis = cityAqiMapper.selectList(wrapper); | 
 |  |  |         wrapper.eq("city_code", regionCode);*/ | 
 |  |  |         List<CityAqi> cityAqis = cityAqiMapper.listCity(regionCode); | 
 |  |  |         //如果数据不足24小时则补全 | 
 |  |  |         if (cityAqis.size() != 24) { | 
 |  |  |             Map<Date, CityAqi> dateCityAqiMap = new HashMap<>(); | 
 |  |  |             cityAqis.forEach(value -> dateCityAqiMap.put(value.getTime(), value)); | 
 |  |  |         if(CollectionUtils.isEmpty(cityAqis)){ | 
 |  |  |             return new HashMap<>(); | 
 |  |  |         } | 
 |  |  |         Date startDate = cityAqis.get(0).getTime(); | 
 |  |  |         if(cityAqis.size()<24){ | 
 |  |  |             for (int i = 0; i < 24; i++) { | 
 |  |  |                 Date date = DateUtils.addHours(startDate, i); | 
 |  |  |                 CityAqi cityAqi1 = dateCityAqiMap.get(date); | 
 |  |  |                 if (cityAqi1 == null) { | 
 |  |  |                 Date dateTime = DateUtils.addHours(startDate, i); | 
 |  |  |                 if(cityAqis.size()-1 <i){ | 
 |  |  |                     CityAqi newCityAqi = new CityAqi(); | 
 |  |  |                     newCityAqi.setTime(date); | 
 |  |  |                     newCityAqi.setTime(dateTime); | 
 |  |  |                     newCityAqi.setValue("0"); | 
 |  |  |                     cityAqis.add(newCityAqi); | 
 |  |  |                     continue; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             //按照时间进行排序 | 
 |  |  |             cityAqis.sort(Comparator.comparing(CityAqi::getTime)); | 
 |  |  |         } | 
 |  |  |             //按照时间进行排序 | 
 |  |  |         cityAqis.sort(Comparator.comparing(CityAqi::getTime)); | 
 |  |  |  | 
 |  |  |         //封装返回数据,map的key为yyyy-MM-dd HH:mm格式的时间,value为aqi的数值 | 
 |  |  |         Map<String, Object> result = new LinkedHashMap<>(); | 
 |  |  |         for (CityAqi aqi : cityAqis) { | 
 |  |  |             String key = DateUtils.dateToDateString(aqi.getTime(), "yyyy-MM-dd HH:mm"); | 
 |  |  |             String allDataJson = aqi.getValue(); | 
 |  |  |             if (allDataJson == null) { | 
 |  |  |                 result.put(key, ""); | 
 |  |  |                 continue; | 
 |  |  |             } | 
 |  |  |             Map<String, Object> allDataMap = JSON.parseObject(allDataJson, Map.class); | 
 |  |  |             Object aqiData = allDataMap.get("AQI"); | 
 |  |  |             if (aqiData == null) | 
 |  |  |                 result.put(key, ""); | 
 |  |  |             else | 
 |  |  |                 result.put(key, aqiData); | 
 |  |  |             result.put(key,aqi.getValue()); | 
 |  |  |         } | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  | 
 |  |  |         Date now = new Date(); | 
 |  |  |         //昨日 | 
 |  |  |         Date yesterday = DateUtils.dataToTimeStampTime(DateUtils.getDateOfDay(now, -1), DateUtils.yyyy_MM_dd_EN); | 
 |  |  |         String dateString = DateUtils.dateToDateString(yesterday, DateUtils.yyyy_MM_dd_HH_mm_ss_EN); | 
 |  |  |         String dateString = DateUtils.dateToDateString(yesterday, DateUtils.yyyy_MM_dd_EN); | 
 |  |  |  | 
 |  |  |         String s = String.valueOf(regionCode); | 
 |  |  |         //获取省,市code | 
 |  |  |         Integer provinceCode = Integer.parseInt(s.substring(0, 2) + "0000"); | 
 |  |  |         Integer cityCode = Integer.parseInt(s.substring(0, 4) + "00"); | 
 |  |  |         //获取省内所有city_code | 
 |  |  |         Map<String,Object> params = new HashMap<>(); | 
 |  |  |         params.put("cityCode",provinceCode); | 
 |  |  |         params.put("start",dateString); | 
 |  |  |         params.put("end",dateString); | 
 |  |  |         params.put("type",1); | 
 |  |  |         List<CityAqiDailyListDTO> dayList = cityAqiDailyMapper.CityAqiDailyMap(params); | 
 |  |  |         String start = DateUtils.dateToDateString(DateUtils.addMonths(DateUtils.getFirstDayOfLastMonth(), 1)); | 
 |  |  |         params.put("start",start); | 
 |  |  |         params.put("type",2); | 
 |  |  |         List<CityAqiDailyListDTO> monList = cityAqiDailyMapper.CityAqiDailyMap(params); | 
 |  |  |         start =DateUtils.dateToDateString(now, DateUtils.yyyy); | 
 |  |  |         params.put("start",start); | 
 |  |  |         params.put("end",start); | 
 |  |  |         params.put("type",3); | 
 |  |  |         List<CityAqiDailyListDTO> yearList = cityAqiDailyMapper.CityAqiDailyMap(params); | 
 |  |  |         int indexDay = dayList.stream().map(CityAqiDailyListDTO::getCityCode).collect(Collectors.toList()).indexOf(cityCode.toString()); | 
 |  |  |         int indexMon = monList.stream().map(CityAqiDailyListDTO::getCityCode).collect(Collectors.toList()).indexOf(cityCode.toString()); | 
 |  |  |         int indexYear = yearList.stream().map(CityAqiDailyListDTO::getCityCode).collect(Collectors.toList()).indexOf(cityCode.toString()); | 
 |  |  |         result.put("day",mapResult(dayList,indexDay)); | 
 |  |  |         result.put("month",mapResult(monList,indexMon)); | 
 |  |  |         result.put("year",mapResult(yearList,indexYear)); | 
 |  |  |        /* //获取省内所有city_code | 
 |  |  |         QueryWrapper<SysArea> wrapper = new QueryWrapper<>(); | 
 |  |  |         wrapper.select("area_code").eq("parent_code", provinceCode); | 
 |  |  |         List<Object> cityCodes = sysAreaService.listObjs(wrapper); | 
 |  |  | 
 |  |  |             yearMap.put("size", null); | 
 |  |  |         } | 
 |  |  |         yearMap.put("compositeIndex", yearMap.remove("value")); | 
 |  |  |         result.put("year", yearMap); | 
 |  |  |         result.put("year", yearMap);*/ | 
 |  |  |  | 
 |  |  |         //时间,昨日 | 
 |  |  |         result.put("time", DateUtils.dateToDateString(yesterday, DateUtils.yyyy_MM_dd_EN)); | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private Map<String,Object> mapResult(List<CityAqiDailyListDTO> list, int length){ | 
 |  |  |         Map<String,Object> map = new HashMap<>(); | 
 |  |  |         map.put("AQI",CollectionUtils.isNotEmpty(list)&&length>0&&Objects.nonNull(list.get(length-1))?list.get(length-1).getAqi():0); | 
 |  |  |         map.put("rank",CollectionUtils.isNotEmpty(list)&&length>0?length:0); | 
 |  |  |         map.put("size",CollectionUtils.isNotEmpty(list)&&length>0?list.size():0); | 
 |  |  |         return map; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<AirQualityComparisonVO> queryAirQualityComparison(AirQualityComparisonForm form) { | 
 |  |  |         //取参 |