|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
|---|
|  |  |  | import com.moral.api.entity.CityAqiDaily; | 
|---|
|  |  |  | import com.moral.api.entity.CityAqiYearly; | 
|---|
|  |  |  | import com.moral.api.mapper.CityAqiYearlyMapper; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.text.DecimalFormat; | 
|---|
|  |  |  | import java.util.Arrays; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | 
|---|
|  |  |  | * SO2,NO2,日均值98百分位 | 
|---|
|  |  |  | * O3,日最大值90百分位 | 
|---|
|  |  |  | * */ | 
|---|
|  |  |  | List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //开始时间,去年1号 | 
|---|
|  |  |  | Date start = DateUtils.getFirstDayOfLastYear(); | 
|---|
|  |  |  | 
|---|
|  |  |  | //结束时间,本年1号 | 
|---|
|  |  |  | Date end = DateUtils.getDate(DateUtils.getDateAddYear(DateUtils.dateToDateString(start, DateUtils.yyyy), 1), DateUtils.yyyy); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //如果是1月1号,先删除去年数据 | 
|---|
|  |  |  | //不是1月1号,统计的是本年累计值,先删除本年数据 | 
|---|
|  |  |  | String monthAndDay = DateUtils.dateToDateString(new Date(), DateUtils.MM_dd_EN); | 
|---|
|  |  |  | UpdateWrapper<CityAqiYearly> cityAqiYearlyUpdateWrapper = new UpdateWrapper<>(); | 
|---|
|  |  |  | if ("01-01".equals(monthAndDay)) { | 
|---|
|  |  |  | cityAqiYearlyUpdateWrapper.eq("time", start); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | start = end; | 
|---|
|  |  |  | end = DateUtils.getDate(DateUtils.getDateAddYear(DateUtils.dateToDateString(start, DateUtils.yyyy), 1), DateUtils.yyyy); | 
|---|
|  |  |  | lastLastYear = DateUtils.getDate(DateUtils.getDateAddYear(DateUtils.dateToDateString(start, DateUtils.yyyy), -1), DateUtils.yyyy); | 
|---|
|  |  |  | cityAqiYearlyUpdateWrapper.eq("time", start); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | cityAqiYearlyMapper.delete(cityAqiYearlyUpdateWrapper); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //获取所有城市aqi日数据 | 
|---|
|  |  |  | QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.select("city_code", "time", "value") | 
|---|
|  |  |  | .ge("time", DateUtils.dateToDateString(start)) | 
|---|
|  |  |  | .lt("time", DateUtils.dateToDateString(end)); | 
|---|
|  |  |  | .ge("time", start) | 
|---|
|  |  |  | .lt("time", end); | 
|---|
|  |  |  | List<Map<String, Object>> monthlyData = cityAqiDailyService.listMaps(wrapper); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (monthlyData.size() == 0) { | 
|---|
|  |  |  | 
|---|
|  |  |  | //上年数据 | 
|---|
|  |  |  | QueryWrapper<CityAqiYearly> queryWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | queryWrapper.select("city_code", "value") | 
|---|
|  |  |  | .eq("time", DateUtils.dateToDateString(lastLastYear)); | 
|---|
|  |  |  | .eq("time", lastLastYear); | 
|---|
|  |  |  | //获取上年数据 | 
|---|
|  |  |  | List<CityAqiYearly> lastCityAqiYearly = cityAqiYearlyMapper.selectList(queryWrapper); | 
|---|
|  |  |  | Map<Integer, List<CityAqiYearly>> lastYearData = lastCityAqiYearly.stream() | 
|---|
|  |  |  | .collect(Collectors.groupingBy(CityAqiYearly::getCityCode)); | 
|---|
|  |  |  | List<CityAqiYearly> lastCityAqiYearlyList = cityAqiYearlyMapper.selectList(queryWrapper); | 
|---|
|  |  |  | Map<Integer, CityAqiYearly> lastYearData = new HashMap<>(); | 
|---|
|  |  |  | for (CityAqiYearly cityAqiYearly : lastCityAqiYearlyList) { | 
|---|
|  |  |  | lastYearData.put(cityAqiYearly.getCityCode(), cityAqiYearly); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | CityAqiYearly cityAqiYearly = new CityAqiYearly(); | 
|---|
|  |  |  | List<CityAqiYearly> cityAqiYearlyList = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Date finalStart = start; | 
|---|
|  |  |  | data.forEach((cityCode, value) -> { | 
|---|
|  |  |  | CityAqiYearly cityAqiYearly = new CityAqiYearly(); | 
|---|
|  |  |  | Map<String, Object> jsonMap = new HashMap<>(); | 
|---|
|  |  |  | cityAqiYearly.setCityCode(Integer.parseInt(cityCode)); | 
|---|
|  |  |  | cityAqiYearly.setTime(start); | 
|---|
|  |  |  | cityAqiYearly.setTime(finalStart); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //PM2.5 | 
|---|
|  |  |  | Double pm25Avg = AmendUtils.getAvgOfYear(value, "PM2_5"); | 
|---|
|  |  |  | 
|---|
|  |  |  | jsonMap.put("compositeIndex", compositeIndex); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //上年数据同本年对比 | 
|---|
|  |  |  | List<CityAqiYearly> cityAqiYearlies = lastYearData.get(Integer.parseInt(cityCode)); | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(cityAqiYearlies)) { | 
|---|
|  |  |  | CityAqiYearly yearly = cityAqiYearlies.get(0); | 
|---|
|  |  |  | Map<String, Object> map = JSONObject.parseObject(yearly.getValue(), Map.class); | 
|---|
|  |  |  | CityAqiYearly lastCityAqiYearly = lastYearData.get(Integer.parseInt(cityCode)); | 
|---|
|  |  |  | if (lastCityAqiYearly != null) { | 
|---|
|  |  |  | Map<String, Object> map = JSONObject.parseObject(lastCityAqiYearly.getValue(), Map.class); | 
|---|
|  |  |  | double lastCompositeIndex = Double.parseDouble(map.get("compositeIndex").toString()); | 
|---|
|  |  |  | DecimalFormat decimalFormat = new DecimalFormat("0.00%"); | 
|---|
|  |  |  | String format = decimalFormat.format((compositeIndex - lastCompositeIndex) / lastCompositeIndex); | 
|---|
|  |  |  | jsonMap.put("yearContrast", format); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | cityAqiYearly.setValue(JSONObject.toJSONString(jsonMap)); | 
|---|
|  |  |  | cityAqiYearlyMapper.insert(cityAqiYearly); | 
|---|
|  |  |  | cityAqiYearlyList.add(cityAqiYearly); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | cityAqiYearlyMapper.insertCityAqiYearly(cityAqiYearlyList); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|