潘志宝
2024-11-15 a4891a78db2d8ac9d17ff01d79e72e8aab38785d
提交 | 用户 | 时间
7fd198 1 package com.iailab.module.model.mdk.sample;
2
b2aca2 3 import com.iailab.module.data.api.point.DataPointApi;
D 4 import com.iailab.module.data.api.point.dto.ApiPointDTO;
5 import com.iailab.module.data.enums.DataPointFreqEnum;
6 import com.iailab.module.model.mdk.common.enums.ModelParamType;
7fd198 7 import com.iailab.module.model.mdk.sample.dto.SampleData;
8 import com.iailab.module.model.mdk.sample.dto.SampleInfo;
9 import com.iailab.module.model.mdk.vo.DataValueVO;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
b2aca2 12 import org.springframework.beans.factory.annotation.Autowired;
7fd198 13 import org.springframework.util.CollectionUtils;
14
b2aca2 15 import java.sql.Timestamp;
7fd198 16 import java.util.*;
17
18 abstract class SampleDataConstructor {
19
20     private Logger logger = LoggerFactory.getLogger(getClass());
b2aca2 21
D 22     @Autowired
23     private DataPointApi dataPointApi;
7fd198 24
25     /**
26      * prepareSampleData
27      *
28      * @param sampleInfo
29      * @return
30      */
31     public abstract List<SampleData> prepareSampleData(SampleInfo sampleInfo);
32
33     /**
34      * 补全数据
35      *
36      * @param length
37      * @param dataEntityList
38      * @param startTime
39      * @param endTime
40      * @return
41      */
a4891a 42     public List<DataValueVO> completionData(int length, List<DataValueVO> dataEntityList, Date startTime, Date endTime,
43                                             String paramId, String paramType) {
b2aca2 44         if (CollectionUtils.isEmpty(dataEntityList) || length == dataEntityList.size()) {
7fd198 45             return dataEntityList;
a4891a 46         } else if (length < dataEntityList.size()) {
47             return dataEntityList.subList(dataEntityList.size() - length, dataEntityList.size());
7fd198 48         }
49
b2aca2 50         List<DataValueVO> completionDataEntityList = new ArrayList<>();
D 51         long oneMin = 0L;
52
53         long start = startTime.getTime();
54         long end = endTime.getTime();
55         long mins = 0L;
56
57         switch (ModelParamType.getEumByCode(paramType)) {
58             case PREDICTITEM:
59                 // 预测值
60                 Calendar calendar = Calendar.getInstance();
61                 calendar.setTime(startTime);
a4891a 62                 calendar.set(Calendar.HOUR_OF_DAY, 0);
63                 calendar.set(Calendar.MINUTE, 0);
64                 calendar.set(Calendar.SECOND, 0);
65                 calendar.add(Calendar.DAY_OF_YEAR, 1);
b2aca2 66                 startTime = calendar.getTime();
D 67                 start = startTime.getTime();
68
69                 calendar.setTime(endTime);
a4891a 70                 calendar.set(Calendar.HOUR_OF_DAY, 0);
71                 calendar.set(Calendar.MINUTE, 0);
72                 calendar.set(Calendar.SECOND, 0);
b2aca2 73                 endTime = calendar.getTime();
D 74                 end = endTime.getTime();
75
76                 oneMin = 24 * 60 * 60 * 1000;
77                 mins = ((end - start) / oneMin);
78                 break;
79             case DATAPOINT:
80                 // 测点值
81                 ApiPointDTO dataPoint = dataPointApi.getInfoById(paramId);
82                 oneMin = 1000L * DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue();
83                 // 设置时间偏移量
84                 start = start - (start % oneMin) + oneMin;
85                 end = end - (end % oneMin) + oneMin;
86                 mins = ((end - start) / oneMin);
87                 break;
88             case IND:
89                 // 指标数据
90                 oneMin = 24 * 60 * 60 * 1000;
91                 Calendar calendar2 = Calendar.getInstance();
92                 calendar2.setTime(startTime);
a4891a 93                 calendar2.set(Calendar.HOUR_OF_DAY, 0);
94                 calendar2.set(Calendar.MINUTE, 0);
95                 calendar2.set(Calendar.SECOND, 0);
b2aca2 96                 start = calendar2.getTime().getTime();
D 97
98                 calendar2.setTime(endTime);
a4891a 99                 calendar2.set(Calendar.HOUR_OF_DAY, 0);
100                 calendar2.set(Calendar.MINUTE, 0);
101                 calendar2.set(Calendar.SECOND, 0);
b2aca2 102                 end = calendar2.getTime().getTime();
D 103                 mins = ((end - start) / oneMin);
104                 break;
105             default:
106                 break;
107         }
7fd198 108         Map<Long, Double> sourceDataMap = new HashMap<>(dataEntityList.size());
109         for (DataValueVO dataEntity : dataEntityList) {
110             sourceDataMap.put(dataEntity.getDataTime().getTime(), dataEntity.getDataValue());
111         }
112
113         //找出缺少项
114         Map<Long, Double> dataMap = new LinkedHashMap<>();
a4891a 115         for (int i = 0; i < mins; i++) {
7fd198 116             Long key = start + oneMin * i;
117             Double value = sourceDataMap.get(key);
118             dataMap.put(key, value);
119         }
120
121         //补充缺少项
122         int k = 0;
123         Map.Entry<Long, Double> lastItem = null;
124         for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
125             if (k == 0 && item.getValue() == null) {
126                 item.setValue(getFirstValue(dataMap));
127             } else if (item.getValue() == null) {
128                 item.setValue(lastItem.getValue());
129             }
a4891a 130             k++;
7fd198 131             lastItem = item;
132
133             DataValueVO dataEntity = new DataValueVO();
b2aca2 134             dataEntity.setDataTime(new Timestamp(item.getKey()));
7fd198 135             dataEntity.setDataValue(item.getValue());
136             completionDataEntityList.add(dataEntity);
137         }
138         return completionDataEntityList;
139     }
140
141     /**
142      * getFirstValue
143      *
144      * @param dataMap
145      * @return
146      */
147     private Double getFirstValue(Map<Long, Double> dataMap) {
148         for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
149             if (item.getValue() != null) {
150                 return item.getValue();
151             }
152         }
153         return null;
154     }
155
156 }