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