jinpengyong
2022-01-20 f052b63c0428d4f3f723e9c729f19a62bfe3172e
screen-job/src/main/java/com/moral/api/service/impl/CityAqiDailyServiceImpl.java
@@ -48,7 +48,8 @@
    @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();
        //开始时间,昨日
@@ -59,49 +60,33 @@
        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) {
            return;
        }
        //按city_code分组
        Map<String, List<Map<String, Object>>> data = dailyData.parallelStream().collect(Collectors.groupingBy(o -> o.get("city_code").toString()));
        Map<String, List<Map<String, Object>>> data = dailyData.stream()
                .collect(Collectors.groupingBy(o -> o.get("city_code").toString()));
        List<CityAqiDaily> cityAqiDailies = new ArrayList<>();
        CityAqiDaily cityAqiDaily = new CityAqiDaily();
        data.forEach((cityCode, value) -> {
            Map<String, Object> jsonMap = new HashMap<>();
            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.stream().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;
@@ -139,7 +124,8 @@
            jsonMap.put("compositeIndex", compositeIndex);
            cityAqiDaily.setValue(JSONObject.toJSONString(jsonMap));
            cityAqiDailyMapper.insert(cityAqiDaily);
            cityAqiDailies.add(cityAqiDaily);
        });
        cityAqiDailyMapper.insertCityAqiDaily(cityAqiDailies);
    }
}