jinpengyong
2021-11-03 9485a0cb33bb4535bb553a5bf49debeadbe0c89d
screen-job/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java
@@ -26,6 +26,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
/**
 * <p>
@@ -55,11 +57,20 @@
    @Override
    public void insertCityAqi() {
        Date now = new Date();
        Date time = DateUtils.dataToTimeStampTime(now, DateUtils.yyyy_MM_dd_HH_EN);
        String format = DateUtils.yyyy_MM_dd_EN;
        Date start = null;
        if (DateUtils.getHour(time) == 0 || DateUtils.getHour(time) >= 8) {
            start = DateUtils.addHours(time, -7);
        }
        //获取城市配置
        QueryWrapper<CityConfig> wrapper = new QueryWrapper<>();
        wrapper.select("city_code", "city_id").eq("is_delete", Constants.NOT_DELETE);
        List<CityConfig> list = cityConfigService.list(wrapper);
        Date time = DateUtils.dataToTimeStampTime(new Date(), DateUtils.yyyy_MM_dd_HH_EN);
        for (CityConfig cityConfig : list) {
            Integer cityCode = cityConfig.getCityCode();
            Map<String, Object> data;
@@ -85,18 +96,25 @@
            aqi.put("aqi", aqi.remove("value"));
            //o3滑动值计算
            //获取一天内每个小时o3数据
            if (start != null) {
            QueryWrapper<CityAqi> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("time", "value").eq("city_code", cityCode);
                queryWrapper.select("time", "value")
                        .eq("city_code", cityCode)
                        .ge("time", DateUtils.dateToDateString(start));
            List<Map<String, Object>> cityAqis = cityAqiMapper.selectMaps(queryWrapper);
            List<Map<String, Object>> o3_8H = AmendUtils.getO3_8H(cityAqis);
            if (!ObjectUtils.isEmpty(o3_8H)) {
                for (Map<String, Object> o3Map : o3_8H) {
                    String hourTime = o3Map.get("time").toString();
                    if (DateUtils.dateToDateString(time, DateUtils.yyyy_MM_dd_HH_mm_ss_EN).equals(hourTime)) {
                        o3Map.get("o3");
                        aqi.put("o3_8h", o3Map.get("o3"));
                        break;
                Map<String, Object> params = new HashMap<>();
                params.put("time", DateUtils.dateToDateString(time,DateUtils.yyyy_MM_dd_HH_mm_ss_S_EN));
                params.put("value", JSONObject.toJSONString(aqi));
                cityAqis.add(params);
                if (cityAqis.size() >= 6) {
                    OptionalDouble average = cityAqis.parallelStream().flatMapToDouble(v -> {
                        Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class);
                        double o3 = Double.parseDouble(dataValue.get("o3").toString());
                        return DoubleStream.of(o3);
                    }).average();
                    if (average.isPresent()) {
                        double o3_8h = AmendUtils.sciCal(average.getAsDouble(), 0);
                        aqi.put("o3_8h", o3_8h);
                    }
                }
            }
@@ -107,13 +125,11 @@
            Double compositeIndex = ComprehensiveIndexUtils.dailyData(temp);
            aqi.put("compositeIndex", compositeIndex);
            //首要污染物计算
            Map<String, Object> pollutant = AQIUtils.cityAqiPollutant(aqi);
            if (!ObjectUtils.isEmpty(pollutant)) {
                aqi.putAll(pollutant);
            }
            CityAqi cityAqi = new CityAqi();
            cityAqi.setCityCode(cityCode);