jinpengyong
2021-07-02 f5765e4ff4ea188e8a6ba4d66f3c4e7fc2268fdd
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
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.HistoryDaily;
import com.moral.api.entity.HistoryMonthly;
import com.moral.api.entity.Sensor;
import com.moral.api.mapper.HistoryMonthlyMapper;
import com.moral.api.service.HistoryDailyService;
import com.moral.api.service.HistoryMonthlyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.SensorService;
import com.moral.constant.Constants;
import com.moral.util.AmendUtils;
import com.moral.util.DateUtils;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
 
import java.util.ArrayList;
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>
 * 月数据 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-07-01
 */
@Service
public class HistoryMonthlyServiceImpl extends ServiceImpl<HistoryMonthlyMapper, HistoryMonthly> implements HistoryMonthlyService {
 
    @Autowired
    private HistoryMonthlyMapper historyMonthlyMapper;
 
    @Autowired
    private SensorService sensorService;
 
    @Autowired
    private HistoryDailyService historyDailyService;
 
    @Override
    public void insertHistoryMonthly() {
        //开始时间,上月1号
        Date start = DateUtils.getFirstDayOfLastMonth();
        //结束时间,本月1号
        Date end = DateUtils.addMonths(start, 1);
        //因子
        QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>();
        sensorQueryWrapper.select("code").eq("is_delete", Constants.NOT_DELETE);
        List<Object> sensorCodes = sensorService.listObjs(sensorQueryWrapper);
 
        //获取所有设备日数据
        QueryWrapper<HistoryDaily> historyDailyQueryWrapper = new QueryWrapper<>();
        historyDailyQueryWrapper.ge("time", DateUtils.dateToDateString(start)).lt("time", DateUtils.dateToDateString(end));
        List<Map<String, Object>> weeklyData = historyDailyService.listMaps(historyDailyQueryWrapper);
        if (weeklyData.size() == 0) {
            return;
        }
        //按mac分组
        Map<String, List<Map<String, Object>>> data = weeklyData.parallelStream().collect(Collectors.groupingBy(o -> (String) o.get("mac")));
 
        //存入数据库的结果集
        List<Map<String, Object>> insertData = new ArrayList<>();
 
        data.forEach((key, value) -> {
            Map<String, Object> dataMap = new HashMap<>();
            Map<String, Object> jsonMap = new HashMap<>();
            dataMap.put("mac", key);
            dataMap.put("time", start);
 
            //风向均值计算并修约
            Object windDirAvg = AmendUtils.getWindDirAvg(value);
            if (windDirAvg != null) {
                jsonMap.put(Constants.SENSOR_CODE_WIND_DIR, windDirAvg);
            }
 
            //CO 95百分位计算并修约
            Object coAvg = AmendUtils.getCOAvgOfWeekOrMonth(value);
            if (coAvg != null) {
                jsonMap.put(Constants.SENSOR_CODE_CO, coAvg);
            }
 
            //O3 90百分位计算并修约
            Object o3Avg = AmendUtils.getO3AvgOfWeekOrMonth(value);
            if (o3Avg != null) {
                jsonMap.put(Constants.SENSOR_CODE_O3, o3Avg);
            }
 
            sensorCodes.forEach(sensorCode -> {
                OptionalDouble optionalDouble = value.parallelStream()
                        .flatMapToDouble(v -> {
                            Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class);
                            Object sensorValue = dataValue.get(sensorCode.toString());
                            if (ObjectUtils.isEmpty(sensorValue)) {
                                return null;
                            }
                            //风向另外计算
                            if (Constants.SENSOR_CODE_WIND_DIR.equals(sensorCode)) {
                                return null;
                            }
                            //CO另外计算
                            if (Constants.SENSOR_CODE_CO.equals(sensorCode)) {
                                return null;
                            }
                            //O3另外计算
                            if (Constants.SENSOR_CODE_O3.equals(sensorCode)) {
                                return null;
                            }
                            return DoubleStream.of(Double.parseDouble(sensorValue.toString()));
                        }).average();
                if (optionalDouble.isPresent()) {
                    //银行家算法修约
                    double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 4);
                    jsonMap.put(sensorCode.toString(), sciCal);
                }
            });
            dataMap.put("value", JSONObject.toJSONString(jsonMap));
            insertData.add(dataMap);
        });
 
        //存入数据库
        historyMonthlyMapper.insertHistoryMonthly(insertData);
    }
}