| | |
| | | import com.moral.api.service.CityAqiDailyService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.api.service.CityAqiService; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.pojo.AQI; |
| | | import com.moral.util.AQIUtils; |
| | | import com.moral.util.AmendUtils; |
| | |
| | | |
| | | @Override |
| | | public void insertCityAqiDaily() { |
| | | List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2", "CO", "O3", "AQI"); |
| | | //需要均值计算的因子 |
| | | List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2", "CO"); |
| | | String format = DateUtils.yyyy_MM_dd_EN; |
| | | Date now = new Date(); |
| | | //开始时间,昨日 |
| | |
| | | QueryWrapper<CityAqi> wrapper = new QueryWrapper<>(); |
| | | wrapper.select("city_code", "time", "value") |
| | | .ge("time", DateUtils.dateToDateString(start)) |
| | | .le("time", DateUtils.dateToDateString(end)); |
| | | .lt("time", DateUtils.dateToDateString(end)); |
| | | List<Map<String, Object>> dailyData = cityAqiService.listMaps(wrapper); |
| | | |
| | | if (dailyData.size() == 0) { |
| | |
| | | CityAqiDaily cityAqiDaily = new CityAqiDaily(); |
| | | cityAqiDaily.setCityCode(Integer.parseInt(cityCode)); |
| | | cityAqiDaily.setTime(start); |
| | | |
| | | //中间变量,用于计算除臭氧外其它因子 |
| | | List<Map<String, Object>> tempValue = new ArrayList<>(value); |
| | | |
| | | //移除第一天数据(0点的),O3滑动值第一条数据是从1点-8点 |
| | | value.removeIf(map -> ((Date) map.get("time")).getTime() == start.getTime()); |
| | | |
| | | //臭氧日均值计算,滑动值算法 |
| | | Double o3OfDay = AmendUtils.o3OfDay(value); |
| | | if (o3OfDay != null) { |
| | | jsonMap.put("O3", o3OfDay); |
| | | //O3日均值单独计算,滑动值算法 |
| | | if (!ObjectUtils.isEmpty(value)) { |
| | | Double o3OfDay = AmendUtils.o3OfDay(value); |
| | | if (o3OfDay != null) { |
| | | jsonMap.put("O3", o3OfDay); |
| | | } |
| | | } |
| | | |
| | | //除臭氧外其他因子均值计算 |
| | | tempValue.removeIf(o -> ((Date) o.get("time")).getTime() == end.getTime()); |
| | | |
| | | sensors.forEach(sensor -> { |
| | | OptionalDouble optionalDouble = tempValue.parallelStream().flatMapToDouble(v -> { |
| | | OptionalDouble optionalDouble = value.parallelStream().flatMapToDouble(v -> { |
| | | Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class); |
| | | //臭氧单独计算 |
| | | if ("O3".equals(sensor)) { |
| | | return null; |
| | | } |
| | | |
| | | //aqi单独计算 |
| | | if ("AQI".equals(sensor)) { |
| | | return null; |
| | | } |
| | | |
| | | Object sensorValue = dataValue.get(sensor); |
| | | if (ObjectUtils.isEmpty(sensorValue)) { |
| | | return null; |
| | |
| | | } |
| | | }); |
| | | |
| | | //日aqi计算 |
| | | AQI aqi = AQIUtils.dailyAQI(jsonMap); |
| | | //日aqi,首要污染物计算 |
| | | Map<String, Object> sixParamMap = new HashMap<>(); |
| | | sixParamMap.put(Constants.SENSOR_CODE_PM25, jsonMap.get("PM2_5")); |
| | | sixParamMap.put(Constants.SENSOR_CODE_PM10, jsonMap.get("PM10")); |
| | | sixParamMap.put(Constants.SENSOR_CODE_SO2, jsonMap.get("SO2")); |
| | | sixParamMap.put(Constants.SENSOR_CODE_NO2, jsonMap.get("NO2")); |
| | | sixParamMap.put(Constants.SENSOR_CODE_CO, jsonMap.get("CO")); |
| | | sixParamMap.put(Constants.SENSOR_CODE_O3, jsonMap.get("O3")); |
| | | |
| | | AQI aqi = AQIUtils.dailyAQI(sixParamMap); |
| | | jsonMap.put("AQI", aqi.getAQIValue()); |
| | | jsonMap.put("primaryPollutant", aqi.getPrimaryPollutantNames()); |
| | | |
| | | //日综合指数计算 |
| | | Double compositeIndex = ComprehensiveIndexUtils.dailyData(jsonMap); |
| | | jsonMap.put("compositeIndex", compositeIndex); |
| | | |
| | | //首要污染物计算 |
| | | jsonMap.put("primaryPollutant", aqi.getPrimaryPollutantNames()); |
| | | |
| | | cityAqiDaily.setValue(JSONObject.toJSONString(jsonMap)); |
| | | cityAqiDailyMapper.insert(cityAqiDaily); |