liriming
2024-12-03 97f3c16ac595493405e8cf76399dc703c088ef6f
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
package com.iailab.module.model.mdk.sample;
 
import com.iailab.module.data.api.point.DataPointApi;
import com.iailab.module.data.api.point.dto.ApiPointDTO;
import com.iailab.module.data.enums.DataPointFreqEnum;
import com.iailab.module.model.mdk.common.enums.ModelParamType;
import com.iailab.module.model.mdk.sample.dto.SampleData;
import com.iailab.module.model.mdk.sample.dto.SampleInfo;
import com.iailab.module.model.mdk.vo.DataValueVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
 
import java.sql.Timestamp;
import java.util.*;
 
abstract class SampleDataConstructor {
 
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    @Autowired
    private DataPointApi dataPointApi;
 
    /**
     * prepareSampleData
     *
     * @param sampleInfo
     * @return
     */
    public abstract List<SampleData> prepareSampleData(SampleInfo sampleInfo);
 
    /**
     * 补全数据
     *
     * @param length
     * @param dataEntityList
     * @param startTime
     * @param endTime
     * @return
     */
    public List<DataValueVO> completionData(int length, List<DataValueVO> dataEntityList, Date startTime, Date endTime,
                                            String paramId, String paramType) {
        if (CollectionUtils.isEmpty(dataEntityList) || length == dataEntityList.size()) {
            return dataEntityList;
        } else if (length < dataEntityList.size()) {
            return dataEntityList.subList(dataEntityList.size() - length, dataEntityList.size());
        }
 
        List<DataValueVO> completionDataEntityList = new ArrayList<>();
        long oneMin = 0L;
 
        long start = startTime.getTime();
        long end = endTime.getTime();
        long mins = 0L;
 
        switch (ModelParamType.getEumByCode(paramType)) {
            case NORMALITEM:
            case MERGEITEM:
                // 预测值
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startTime);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.add(Calendar.DAY_OF_YEAR, 1);
                startTime = calendar.getTime();
                start = startTime.getTime();
 
                calendar.setTime(endTime);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                endTime = calendar.getTime();
                end = endTime.getTime();
 
                oneMin = 24 * 60 * 60 * 1000;
                mins = ((end - start) / oneMin);
                break;
            case DATAPOINT:
                // 测点值
                ApiPointDTO dataPoint = dataPointApi.getInfoById(paramId);
                oneMin = 1000L * DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue();
                // 设置时间偏移量
                start = start - (start % oneMin) + oneMin;
                end = end - (end % oneMin) + oneMin;
                mins = ((end - start) / oneMin);
                break;
            case IND:
                // 指标数据
                oneMin = 24 * 60 * 60 * 1000;
                Calendar calendar2 = Calendar.getInstance();
                calendar2.setTime(startTime);
                calendar2.set(Calendar.HOUR_OF_DAY, 0);
                calendar2.set(Calendar.MINUTE, 0);
                calendar2.set(Calendar.SECOND, 0);
                start = calendar2.getTime().getTime();
 
                calendar2.setTime(endTime);
                calendar2.set(Calendar.HOUR_OF_DAY, 0);
                calendar2.set(Calendar.MINUTE, 0);
                calendar2.set(Calendar.SECOND, 0);
                end = calendar2.getTime().getTime();
                mins = ((end - start) / oneMin);
                break;
            default:
                break;
        }
        Map<Long, Double> sourceDataMap = new HashMap<>(dataEntityList.size());
        for (DataValueVO dataEntity : dataEntityList) {
            sourceDataMap.put(dataEntity.getDataTime().getTime(), dataEntity.getDataValue());
        }
 
        //找出缺少项
        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;
 
            DataValueVO dataEntity = new DataValueVO();
            dataEntity.setDataTime(new Date(item.getKey()));
            dataEntity.setDataValue(item.getValue());
            completionDataEntityList.add(dataEntity);
        }
        return completionDataEntityList;
    }
 
    /**
     * getFirstValue
     *
     * @param dataMap
     * @return
     */
    private Double getFirstValue(Map<Long, Double> dataMap) {
        for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
            if (item.getValue() != null) {
                return item.getValue();
            }
        }
        return null;
    }
 
}