dengzedong
4 天以前 7f0bcd00c556889ba890e5e68c681f1b5d4267e1
提交 | 用户 | 时间
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)) {
19a4fb 58             case NORMALITEM:
D 59             case MERGEITEM:
b2aca2 60                 // 预测值
D 61                 Calendar calendar = Calendar.getInstance();
62                 calendar.setTime(startTime);
a4891a 63                 calendar.set(Calendar.HOUR_OF_DAY, 0);
64                 calendar.set(Calendar.MINUTE, 0);
65                 calendar.set(Calendar.SECOND, 0);
66                 calendar.add(Calendar.DAY_OF_YEAR, 1);
b2aca2 67                 startTime = calendar.getTime();
D 68                 start = startTime.getTime();
69
70                 calendar.setTime(endTime);
a4891a 71                 calendar.set(Calendar.HOUR_OF_DAY, 0);
72                 calendar.set(Calendar.MINUTE, 0);
73                 calendar.set(Calendar.SECOND, 0);
b2aca2 74                 endTime = calendar.getTime();
D 75                 end = endTime.getTime();
76
77                 oneMin = 24 * 60 * 60 * 1000;
78                 mins = ((end - start) / oneMin);
79                 break;
80             case DATAPOINT:
81                 // 测点值
82                 ApiPointDTO dataPoint = dataPointApi.getInfoById(paramId);
83                 oneMin = 1000L * DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue();
84                 // 设置时间偏移量
85                 start = start - (start % oneMin) + oneMin;
86                 end = end - (end % oneMin) + oneMin;
87                 mins = ((end - start) / oneMin);
88                 break;
89             case IND:
90                 // 指标数据
91                 oneMin = 24 * 60 * 60 * 1000;
92                 Calendar calendar2 = Calendar.getInstance();
93                 calendar2.setTime(startTime);
a4891a 94                 calendar2.set(Calendar.HOUR_OF_DAY, 0);
95                 calendar2.set(Calendar.MINUTE, 0);
96                 calendar2.set(Calendar.SECOND, 0);
b2aca2 97                 start = calendar2.getTime().getTime();
D 98
99                 calendar2.setTime(endTime);
a4891a 100                 calendar2.set(Calendar.HOUR_OF_DAY, 0);
101                 calendar2.set(Calendar.MINUTE, 0);
102                 calendar2.set(Calendar.SECOND, 0);
b2aca2 103                 end = calendar2.getTime().getTime();
D 104                 mins = ((end - start) / oneMin);
105                 break;
106             default:
107                 break;
108         }
7fd198 109         Map<Long, Double> sourceDataMap = new HashMap<>(dataEntityList.size());
110         for (DataValueVO dataEntity : dataEntityList) {
111             sourceDataMap.put(dataEntity.getDataTime().getTime(), dataEntity.getDataValue());
112         }
113
114         //找出缺少项
115         Map<Long, Double> dataMap = new LinkedHashMap<>();
a4891a 116         for (int i = 0; i < mins; i++) {
7fd198 117             Long key = start + oneMin * i;
118             Double value = sourceDataMap.get(key);
119             dataMap.put(key, value);
120         }
121
122         //补充缺少项
123         int k = 0;
124         Map.Entry<Long, Double> lastItem = null;
125         for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
126             if (k == 0 && item.getValue() == null) {
127                 item.setValue(getFirstValue(dataMap));
128             } else if (item.getValue() == null) {
129                 item.setValue(lastItem.getValue());
130             }
a4891a 131             k++;
7fd198 132             lastItem = item;
133
134             DataValueVO dataEntity = new DataValueVO();
e0b86e 135             dataEntity.setDataTime(new Date(item.getKey()));
7fd198 136             dataEntity.setDataValue(item.getValue());
137             completionDataEntityList.add(dataEntity);
138         }
139         return completionDataEntityList;
140     }
141
142     /**
143      * getFirstValue
144      *
145      * @param dataMap
146      * @return
147      */
148     private Double getFirstValue(Map<Long, Double> dataMap) {
149         for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
150             if (item.getValue() != null) {
151                 return item.getValue();
152             }
153         }
154         return null;
155     }
156
157 }