dengzedong
4 天以前 5665c6d9ff476abd922b1f6a43ea1dac0600cb05
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package com.iailab.module.model.mcs.pre.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iailab.framework.common.pojo.PageResult;
import com.iailab.framework.common.service.impl.BaseServiceImpl;
import com.iailab.framework.common.util.date.DateUtils;
import com.iailab.module.data.api.point.DataPointApi;
import com.iailab.module.data.api.point.dto.ApiPointDTO;
import com.iailab.module.data.api.point.dto.ApiPointValueDTO;
import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO;
import com.iailab.module.data.enums.DataPointFreqEnum;
import com.iailab.module.model.api.mcs.dto.MmPredictInfluenceFactorHandleReqVO;
import com.iailab.module.model.mcs.pre.dao.MmPredictInfluenceFactorDao;
import com.iailab.module.model.mcs.pre.entity.MmPredictInfluenceFactorConfigEntity;
import com.iailab.module.model.mcs.pre.entity.MmPredictInfluenceFactorResultEntity;
import com.iailab.module.model.mcs.pre.enums.InfluenceFactorPatternEnum;
import com.iailab.module.model.mcs.pre.service.MmItemResultJsonService;
import com.iailab.module.model.mcs.pre.service.MmPredictInfluenceFactorResultService;
import com.iailab.module.model.mcs.pre.service.MmPredictInfluenceFactorService;
import com.iailab.module.model.mcs.pre.vo.MmPredictInfluenceFactorConfigVO;
import com.iailab.module.model.mcs.pre.vo.MmPredictInfluenceFactorHandleVO;
import com.iailab.module.model.mcs.pre.vo.MmPredictInfluenceFactorPageReqVO;
import com.iailab.module.model.mcs.pre.vo.MmPredictInfluenceFactorVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
@Slf4j
@Service
public class MmPredictInfluenceFactorServiceImpl extends BaseServiceImpl<MmPredictInfluenceFactorDao, MmPredictInfluenceFactorConfigEntity> implements MmPredictInfluenceFactorService {
 
    @Autowired
    private MmItemResultJsonService mmItemResultJsonService;
 
    @Autowired
    private DataPointApi dataPointApi;
 
    @Autowired
    private MmPredictInfluenceFactorResultService influenceFactorResultService;
 
    @Override
    public PageResult<MmPredictInfluenceFactorConfigVO> page(MmPredictInfluenceFactorPageReqVO params) {
        IPage<MmPredictInfluenceFactorConfigVO> page = baseDao.selectPage(params);
        return new PageResult<>(page.getRecords(), page.getTotal());
    }
 
    @Override
    public MmPredictInfluenceFactorConfigVO getInfo(String id) {
        return baseDao.getInfo(id);
    }
 
    @Override
    public void create(MmPredictInfluenceFactorConfigVO vo) {
        MmPredictInfluenceFactorConfigEntity entity = new MmPredictInfluenceFactorConfigEntity();
        String configId = UUID.randomUUID().toString();
        entity.setId(configId);
        entity.setOutputId(vo.getOutputId());
        entity.setPattern(vo.getPattern());
        entity.setIsEnable(vo.getIsEnable());
        entity.setCreateTime(new Date());
        baseDao.insert(entity);
 
        // 添加因素
        if (CollectionUtils.isEmpty(vo.getInfluenceFactors())) {
            return;
        }
        List<MmPredictInfluenceFactorVO> influenceFactors = vo.getInfluenceFactors();
        influenceFactors.forEach(e -> {
            e.setId(UUID.randomUUID().toString());
            e.setConfigId(configId);
        });
 
        baseDao.insertInfluenceFactor(influenceFactors);
    }
 
    @Override
    public void update(MmPredictInfluenceFactorConfigVO vo) {
        MmPredictInfluenceFactorConfigEntity entity = new MmPredictInfluenceFactorConfigEntity();
        entity.setId(vo.getId());
        entity.setOutputId(vo.getOutputId());
        entity.setPattern(vo.getPattern());
        entity.setIsEnable(vo.getIsEnable());
        baseDao.updateById(entity);
 
        // 删除因素
        baseDao.deleteInfluenceFactor(vo.getId());
        // 添加因素
        if (CollectionUtils.isEmpty(vo.getInfluenceFactors())) {
            return;
        }
        List<MmPredictInfluenceFactorVO> influenceFactors = vo.getInfluenceFactors();
        influenceFactors.forEach(e -> {
            if (StringUtils.isBlank(e.getId())) {
                e.setId(UUID.randomUUID().toString());
            }
            e.setConfigId(vo.getId());
        });
 
        baseDao.insertInfluenceFactor(influenceFactors);
 
    }
 
    @Override
    public void delete(String id) {
        // 删除因素
        baseDao.deleteById(id);
    }
 
    @Override
    public void influenceFactorHandle(MmPredictInfluenceFactorHandleReqVO reqVO) {
        // 影响时间
        Date influenceTime = new Date(reqVO.getInfluenceTime());
        // 判断影响时间一定是历史时间
        if (influenceTime.after(new Date())) {
            log.info("计算影响因素失败:影响时间大于当前时间");
            return;
        }
        // 查询所有影响因素
        Map<String, Object> params = new HashMap<>();
        params.put("isEnable",1);
        List<MmPredictInfluenceFactorHandleVO> influenceFactorConfigs = baseDao.selectList(params);
 
        // 根据统计方式分组
        Map<String, List<MmPredictInfluenceFactorHandleVO>> patternMap = influenceFactorConfigs.stream().collect(Collectors.groupingBy(MmPredictInfluenceFactorHandleVO::getPattern));
        List<MmPredictInfluenceFactorResultEntity> resultList = new ArrayList<>(influenceFactorConfigs.size());
        for (Map.Entry<String, List<MmPredictInfluenceFactorHandleVO>> entry : patternMap.entrySet()) {
            List<MmPredictInfluenceFactorHandleVO> influenceFactors = entry.getValue();
            // 缓存 因素预测项id-结果,相同因素预测项id只用计算一次
            HashMap<String,Double> values = new HashMap<>();
            switch (InfluenceFactorPatternEnum.fromCode(entry.getKey())) {
                case ACCUMULATE:
                    for (MmPredictInfluenceFactorHandleVO influenceFactor : influenceFactors) {
                        Double value = influenceFactorAccumulateCalculate(influenceFactor, values, influenceTime);
                        if (value == null) {
                            continue;
                        }
                        MmPredictInfluenceFactorResultEntity resultEntity = new MmPredictInfluenceFactorResultEntity();
                        resultEntity.setFactorId(influenceFactor.getId());
                        resultEntity.setTime(influenceTime);
                        resultEntity.setValue(value);
                        resultList.add(resultEntity);
                    }
                    break;
                case ACCURACY:
                    for (MmPredictInfluenceFactorHandleVO influenceFactor : influenceFactors) {
                        Double value = influenceFactorAccuracyCalculate(influenceFactor, values, influenceTime);
                        if (value == null) {
                            continue;
                        }
                        MmPredictInfluenceFactorResultEntity resultEntity = new MmPredictInfluenceFactorResultEntity();
                        resultEntity.setFactorId(influenceFactor.getId());
                        resultEntity.setTime(influenceTime);
                        resultEntity.setValue(value);
                        resultList.add(resultEntity);
                    }
                    break;
                default:
            }
        }
        if (CollectionUtils.isEmpty(resultList)) {
            return;
        }
        influenceFactorResultService.insert(resultList);
 
    }
 
    @Override
    public List<MmPredictInfluenceFactorVO> getListByOutId(String outId) {
        List<MmPredictInfluenceFactorVO> list = baseDao.getListByOutId(outId);
        return list;
    }
 
    private Double influenceFactorAccuracyCalculate(MmPredictInfluenceFactorHandleVO influenceFactor, HashMap<String, Double> values, Date influenceTime) {
        if (values.containsKey(influenceFactor.getFactorOutputId())) {
            return values.get(influenceFactor.getFactorOutputId());
        }
        // 根据 预测长度 和 预测粒度 推算预测时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(influenceTime);
        calendar.add(Calendar.SECOND,-1 * influenceFactor.getPredictlength() * influenceFactor.getGranularity());
        Date predictTime = calendar.getTime();
        // 获取result_json,预测历史数据
        List<Object[]> predictData = mmItemResultJsonService.getData(influenceFactor.getFactorOutputId(), predictTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
        if (CollectionUtils.isEmpty(predictData)) {
            return null;
        }
        // 获取真实测点历史值
        ApiPointValueQueryDTO queryDTO = new ApiPointValueQueryDTO();
        ApiPointDTO point = dataPointApi.getInfoById(influenceFactor.getPointid());
        queryDTO.setPointNo(point.getPointNo());
        queryDTO.setStart(predictTime);
        queryDTO.setEnd(influenceTime);
        List<ApiPointValueDTO> dataList = dataPointApi.queryPointHistoryValue(queryDTO);
        if (CollectionUtils.isEmpty(dataList)) {
            return null;
        }
        dataList = completionData(influenceFactor.getPredictlength(),dataList,predictTime,influenceTime,point.getMinfreqid());
        Map<String, Double> dataMap = dataList.stream().collect(Collectors.toMap(e -> DateUtils.format(e.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND), ApiPointValueDTO::getV, (e1, e2) -> e1));
        // 计算精确度误差
        int sum = 0;
        for (int i = 0; i < predictData.size(); i++) {
            Object[] objects = predictData.get(i);
            if (dataMap.containsKey(objects[0].toString())) {
                double abs = Math.abs(new BigDecimal(objects[1].toString()).subtract(BigDecimal.valueOf(dataMap.get(objects[0].toString()))).doubleValue());
                if (abs > influenceFactor.getDeviationValue()) {
                    sum++;
                }
            }
        }
 
        double value = BigDecimal.valueOf(sum).divide(BigDecimal.valueOf(predictData.size()), 2, BigDecimal.ROUND_HALF_UP).doubleValue();
 
        values.put(influenceFactor.getFactorOutputId(),value);
        return value;
    }
 
    private Double influenceFactorAccumulateCalculate(MmPredictInfluenceFactorHandleVO influenceFactor, HashMap<String, Double> values,Date influenceTime) {
        if (values.containsKey(influenceFactor.getFactorOutputId())) {
            return values.get(influenceFactor.getFactorOutputId());
        }
        // 根据 预测长度 和 预测粒度 推算预测时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(influenceTime);
        calendar.add(Calendar.SECOND,-1 * influenceFactor.getPredictlength() * influenceFactor.getGranularity());
        Date predictTime = calendar.getTime();
        // 获取result_json,预测历史数据
        double[] predictData = mmItemResultJsonService.getSimpleData(influenceFactor.getFactorOutputId(), predictTime, influenceFactor.getPredictlength());
        if (predictData == null || predictData.length == 0) {
            return null;
        }
        // 获取真实测点历史值
        ApiPointValueQueryDTO queryDTO = new ApiPointValueQueryDTO();
        ApiPointDTO point = dataPointApi.getInfoById(influenceFactor.getPointid());
        queryDTO.setPointNo(point.getPointNo());
        queryDTO.setStart(predictTime);
        queryDTO.setEnd(influenceTime);
        List<ApiPointValueDTO> dataList = dataPointApi.queryPointHistoryValue(queryDTO);
        if (CollectionUtils.isEmpty(dataList)) {
            return null;
        }
        dataList = completionData(influenceFactor.getPredictlength(),dataList,predictTime,influenceTime,point.getMinfreqid());
        // 计算累计量误差
        double value = Math.abs(dataList.stream().mapToDouble(ApiPointValueDTO::getV).sum() - Arrays.stream(predictData).sum());
 
        values.put(influenceFactor.getFactorOutputId(),value);
        return value;
    }
 
    private List<ApiPointValueDTO> completionData(int length, List<ApiPointValueDTO> dataList, Date startTime, Date endTime, String minfreqid) {
        if (CollectionUtils.isEmpty(dataList) || length == dataList.size()) {
            return dataList;
        } else if (length < dataList.size()) {
            return dataList.subList(dataList.size() - length, dataList.size());
        }
 
        List<ApiPointValueDTO> result = new ArrayList<>();
        long start = startTime.getTime();
        long end = endTime.getTime();
        long oneMin = 1000L * DataPointFreqEnum.getEumByCode(minfreqid).getValue();
        long mins = (end - start) / oneMin;
 
        //找出缺少项
        Map<Long, Double> sourceDataMap = new HashMap<>(dataList.size());
        for (ApiPointValueDTO pv : dataList) {
            sourceDataMap.put(pv.getT().getTime(), pv.getV());
        }
 
        Map<Long, Double> dataMap = new LinkedHashMap<>();
        for (int i = 0; i < mins; i++) {
            Long key = start + oneMin * i;
            Double value = sourceDataMap.get(key);
            dataMap.put(key, value);
        }
 
        //补充缺少项
        int k = 0;
        Map.Entry<Long, Double> lastItem = null;
        for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
            if (k == 0 && item.getValue() == null) {
                item.setValue(getFirstValue(dataMap));
            } else if (item.getValue() == null) {
                item.setValue(lastItem.getValue());
            }
            k++;
            lastItem = item;
 
            ApiPointValueDTO dataEntity = new ApiPointValueDTO();
            dataEntity.setT(new Date(item.getKey()));
            dataEntity.setV(item.getValue());
            result.add(dataEntity);
        }
        return result;
    }
 
    private Double getFirstValue(Map<Long, Double> dataMap) {
        for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
            if (item.getValue() != null) {
                return item.getValue();
            }
        }
        return 0.0;
    }
}