jinpengyong
2021-12-22 9d91dc402f279630eaa100024fd3b1542fbeb41c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.CityAqiDaily;
import com.moral.api.entity.CityAqiYearly;
import com.moral.api.mapper.CityAqiYearlyMapper;
import com.moral.api.service.CityAqiDailyService;
import com.moral.api.service.CityAqiYearlyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.util.AmendUtils;
import com.moral.util.ComprehensiveIndexUtils;
import com.moral.util.DateUtils;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
 
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.OptionalDouble;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
 
/**
 * <p>
 * 城市aqi年数据表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-11-04
 */
@Service
public class CityAqiYearlyServiceImpl extends ServiceImpl<CityAqiYearlyMapper, CityAqiYearly> implements CityAqiYearlyService {
 
    @Autowired
    private CityAqiDailyService cityAqiDailyService;
 
    @Autowired
    private CityAqiYearlyMapper cityAqiYearlyMapper;
 
 
    @Override
    public void insertCityAqiYearly() {
        //需要均值计算的因子
        List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2");
 
        //开始时间,去年1号
        Date start = DateUtils.getFirstDayOfLastYear();
        //上上年
        Date lastLastYear = DateUtils.getDate(DateUtils.getDateAddYear(DateUtils.dateToDateString(start, DateUtils.yyyy), -1), DateUtils.yyyy);
        //结束时间,本年1号
        Date end = DateUtils.getDate(DateUtils.getDateAddYear(DateUtils.dateToDateString(start, DateUtils.yyyy), 1), DateUtils.yyyy);
 
 
        //获取所有城市aqi小时数据
        QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>();
        wrapper.select("city_code", "time", "value")
                .ge("time", DateUtils.dateToDateString(start))
                .lt("time", DateUtils.dateToDateString(end));
        List<Map<String, Object>> monthlyData = cityAqiDailyService.listMaps(wrapper);
 
        if (monthlyData.size() == 0) {
            return;
        }
        //按city_code分组
        Map<String, List<Map<String, Object>>> data = monthlyData.parallelStream().collect(Collectors.groupingBy(o -> o.get("city_code").toString()));
 
        data.forEach((cityCode, value) -> {
            Map<String, Object> jsonMap = new HashMap<>();
            CityAqiYearly cityAqiYearly = new CityAqiYearly();
            cityAqiYearly.setCityCode(Integer.parseInt(cityCode));
            cityAqiYearly.setTime(start);
 
 
            Map<String, Object> params = new HashMap<>();
            List<Map<String, Object>> temp = new ArrayList<>();
 
            for (Map<String, Object> map : value) {
                Map<String, Object> sensorsValue = JSONObject.parseObject(map.get("value").toString(), Map.class);
                Map<String, Object> tempMap = new HashMap<>();
                tempMap.put(Constants.SENSOR_CODE_CO, sensorsValue.get("PM2_5"));
                tempMap.put(Constants.SENSOR_CODE_O3, sensorsValue.get("O3"));
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("value", JSONObject.toJSONString(tempMap));
                temp.add(hashMap);
            }
            params.put("data", temp);
            //1. CO 95百分位计算并修约
            Map<String, Object> coAvgOfWeekOrMonth = AmendUtils.getCOAvgOfWeekOrMonth(params);
            if (!ObjectUtils.isEmpty(coAvgOfWeekOrMonth)) {
                jsonMap.put("CO", coAvgOfWeekOrMonth.get(Constants.SENSOR_CODE_CO));
            }
 
            //2. O3 90百分位计算并修约
            Map<String, Object> o3AvgOfWeekOrMonth = AmendUtils.getO3AvgOfWeekOrMonth(params);
            if (!ObjectUtils.isEmpty(o3AvgOfWeekOrMonth)) {
                jsonMap.put("O3", o3AvgOfWeekOrMonth.get(Constants.SENSOR_CODE_O3));
            }
 
            sensors.forEach(sensor -> {
                OptionalDouble optionalDouble = value.parallelStream().flatMapToDouble(v -> {
                    Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class);
                    double aDouble = Double.parseDouble(dataValue.get(sensor).toString());
                    return DoubleStream.of(aDouble);
                }).average();
                if (optionalDouble.isPresent()) {
                    //银行家算法修约
                    jsonMap.put(sensor, AmendUtils.sciCal(optionalDouble.getAsDouble(), 0));
                }
            });
 
            //本月月综指计算
            Double compositeIndex = ComprehensiveIndexUtils.dailyData(jsonMap);
            jsonMap.put("compositeIndex", compositeIndex);
 
            //本月综指同上月对比
            QueryWrapper<CityAqiYearly> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("value")
                    .eq("city_code", cityCode)
                    .eq("time", DateUtils.dateToDateString(lastLastYear));
            //获取上上月数据
            CityAqiYearly lastCityAqiYearly = cityAqiYearlyMapper.selectOne(queryWrapper);
            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);
        });
 
 
    }
}