Jay
2024-11-12 3d9106399d9a2b9c8ba7d2dea621f54fd71d2ca7
提交 | 用户 | 时间
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      */
b2aca2 42     public List<DataValueVO> completionData(int length, List<DataValueVO> dataEntityList, Date startTime, Date endTime, String paramId,String paramType) {
D 43         if (CollectionUtils.isEmpty(dataEntityList) || length == dataEntityList.size()) {
7fd198 44             return dataEntityList;
b2aca2 45         }else if (length < dataEntityList.size()){
D 46             return dataEntityList.subList(dataEntityList.size()-length,dataEntityList.size());
7fd198 47         }
48
b2aca2 49         List<DataValueVO> completionDataEntityList = new ArrayList<>();
D 50         long oneMin = 0L;
51
52         long start = startTime.getTime();
53         long end = endTime.getTime();
54         long mins = 0L;
55
56         switch (ModelParamType.getEumByCode(paramType)) {
57             case PREDICTITEM:
58                 // 预测值
59                 Calendar calendar = Calendar.getInstance();
60                 calendar.setTime(startTime);
61                 calendar.set(Calendar.HOUR_OF_DAY,0);
62                 calendar.set(Calendar.MINUTE,0);
63                 calendar.set(Calendar.SECOND,0);
64                 calendar.add(Calendar.DAY_OF_YEAR,1);
65                 startTime = calendar.getTime();
66                 start = startTime.getTime();
67
68                 calendar.setTime(endTime);
69                 calendar.set(Calendar.HOUR_OF_DAY,0);
70                 calendar.set(Calendar.MINUTE,0);
71                 calendar.set(Calendar.SECOND,0);
72                 endTime = calendar.getTime();
73                 end = endTime.getTime();
74
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                 // 设置时间偏移量
85                 start = start - (start % oneMin) + oneMin;
86                 end = end - (end % oneMin) + oneMin;
87
88
89                 mins = ((end - start) / oneMin);
90                 break;
91             case IND:
92                 // 指标数据
93                 oneMin = 24 * 60 * 60 * 1000;
94                 Calendar calendar2 = Calendar.getInstance();
95                 calendar2.setTime(startTime);
96                 calendar2.set(Calendar.HOUR_OF_DAY,0);
97                 calendar2.set(Calendar.MINUTE,0);
98                 calendar2.set(Calendar.SECOND,0);
99                 start = calendar2.getTime().getTime();
100
101                 calendar2.setTime(endTime);
102                 calendar2.set(Calendar.HOUR_OF_DAY,0);
103                 calendar2.set(Calendar.MINUTE,0);
104                 calendar2.set(Calendar.SECOND,0);
105                 end = calendar2.getTime().getTime();
106
107
108                 mins = ((end - start) / oneMin);
109                 break;
110             default:
111                 break;
112         }
7fd198 113         Map<Long, Double> sourceDataMap = new HashMap<>(dataEntityList.size());
114         for (DataValueVO dataEntity : dataEntityList) {
115             sourceDataMap.put(dataEntity.getDataTime().getTime(), dataEntity.getDataValue());
116         }
117
118         //找出缺少项
119         Map<Long, Double> dataMap = new LinkedHashMap<>();
b2aca2 120         for (int i = 0; i < mins; i ++) {
7fd198 121             Long key = start + oneMin * i;
122             Double value = sourceDataMap.get(key);
123             dataMap.put(key, value);
124         }
125
126         //补充缺少项
127         int k = 0;
128         Map.Entry<Long, Double> lastItem = null;
129         for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
130             if (k == 0 && item.getValue() == null) {
131                 item.setValue(getFirstValue(dataMap));
132             } else if (item.getValue() == null) {
133                 item.setValue(lastItem.getValue());
134             }
b2aca2 135             k ++;
7fd198 136             lastItem = item;
137
138             DataValueVO dataEntity = new DataValueVO();
b2aca2 139             dataEntity.setDataTime(new Timestamp(item.getKey()));
7fd198 140             dataEntity.setDataValue(item.getValue());
141             completionDataEntityList.add(dataEntity);
142         }
143         return completionDataEntityList;
144     }
145
146     /**
147      * getFirstValue
148      *
149      * @param dataMap
150      * @return
151      */
152     private Double getFirstValue(Map<Long, Double> dataMap) {
153         for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
154             if (item.getValue() != null) {
155                 return item.getValue();
156             }
157         }
158         return null;
159     }
160
161 }