潘志宝
2024-09-05 7fd198b8ebe97cd06b10f96b9179caebe679783c
提交 | 用户 | 时间
7fd198 1 package com.iailab.module.model.mdk.sample;
2
3 import com.iailab.module.data.api.point.DataPointApi;
4 import com.iailab.module.data.api.point.dto.ApiPointDTO;
5 import com.iailab.module.data.enums.DataPointFreq;
6 import com.iailab.module.model.mdk.factory.ItemEntityFactory;
7 import com.iailab.module.model.mdk.sample.dto.ColumnItem;
8 import com.iailab.module.model.mdk.sample.dto.ColumnItemPort;
9 import com.iailab.module.model.mdk.sample.dto.SampleInfo;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.stereotype.Component;
12
13 import java.sql.Timestamp;
14 import java.util.Calendar;
15 import java.util.Date;
16 import java.util.List;
17
18 /**
19  * @author PanZhibao
20  * @Description
21  * @createTime 2024年09月03日
22  */
23 abstract class SampleInfoConstructor {
24
25     @Autowired
26     private DataPointApi dataPointApi;
27
28     @Autowired
29     private ItemEntityFactory itemEntityFactory;
30
31     /**
32      * prepareSampleInfo
33      *
34      * @param modelId
35      * @param predictTime
36      * @return
37      */
38     public SampleInfo prepareSampleInfo(String modelId, Date predictTime) {
39         SampleInfo sampleInfo = new SampleInfo();
40         //调用样本列数的方法
41         sampleInfo.setSampleColumn(getSampleColumn(modelId));
42         //样本的列信息
43         sampleInfo.setColumnInfo(getColumnInfo(modelId, predictTime));
44         //样本的采样周期
45         sampleInfo.setSampleCycle(getSampleCycle(modelId));
46         return sampleInfo;
47     }
48
49     /**
50      * 返回样本矩阵的列数
51      *
52      * @param modelId
53      * @return
54      */
55     protected abstract Integer getSampleColumn(String modelId);
56
57     /**
58      * 获取开始时间
59      *
60      * @param columnItem
61      * @param originalTime
62      * @return
63      * @throws Exception
64      */
65     protected Date getStartTime(ColumnItem columnItem, Date originalTime) {
66         Date dateTime = new Date();
67         Calendar calendar = Calendar.getInstance();
68         calendar.setTime(originalTime);
69         switch (columnItem.getParamType()) {
70             case "DATAPOINT":
71                 ApiPointDTO dataPoint = dataPointApi.getPointById(columnItem.getParamId());
72                 if (dataPoint == null) {
73                     return null;
74                 }
75                 dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), DataPointFreq.getEumByCode(dataPoint.getMinfreqid()).getValue());
76                 break;
77             case "PREDICTITEM":
78                 dateTime = calendar.getTime();
79                 break;
80             case "IND-HIS":
81                 dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), 60);
82                 break;
83             case "IND-PLAN":
84                 dateTime = calendar.getTime();
85                 break;
86             default:
87                 break;
88         }
89         return dateTime;
90     }
91
92     /**
93      * 获取结束时间
94      *
95      * @param columnItem
96      * @param originalTime
97      * @return
98      * @throws Exception
99      */
100     protected Date getEndTime(ColumnItem columnItem, Date originalTime) {
101         Date dateTime = new Date();
102         Calendar calendar = Calendar.getInstance();
103         calendar.setTime(originalTime);
104         switch (columnItem.getParamType()) {
105             case "DATAPOINT":
106                 ApiPointDTO dataPoint = dataPointApi.getPointById(columnItem.getParamId());
107                 if (dataPoint == null) {
108                     return null;
109                 }
110                 calendar.add(Calendar.SECOND, DataPointFreq.getEumByCode(dataPoint.getMinfreqid()).getValue() * -1);
111                 break;
112             case "PREDICTITEM":
113                 dateTime = calculateTime(originalTime, false, columnItem.getDataLength(), 60);
114                 break;
115             case "IND-HIS":
116                 dateTime = calendar.getTime();
117                 break;
118             case "IND-PLAN":
119                 dateTime = calculateTime(originalTime, false, columnItem.getDataLength(), 60);
120                 break;
121             default:
122                 break;
123         }
124         return dateTime;
125     }
126
127     /**
128      * 获取粒度,s
129      *
130      * @param columnItem
131      * @return
132      * @throws Exception
133      */
134     protected Integer getGranularity(ColumnItem columnItem) {
135         Integer granularity = 60;
136         switch (columnItem.getParamType()) {
137             case "DATAPOINT":
138                 ApiPointDTO dataPoint = dataPointApi.getPointById(columnItem.getParamId());
139                 granularity = DataPointFreq.getEumByCode(dataPoint.getMinfreqid()).getValue();
140                 break;
141             case "PREDICTITEM":
142                 granularity = itemEntityFactory.getItemById(columnItem.getParamId()).getGranularity();
143                 break;
144             case "IND-HIS":
145                 granularity = 60;
146                 break;
147             case "IND-PLAN":
148                 granularity = 60;
149                 break;
150             default:
151                 break;
152         }
153         return granularity;
154     }
155
156     /**
157      * 样本的列信息
158      *
159      * @param modelId
160      * @param predictTime
161      * @return
162      */
163     protected abstract List<ColumnItemPort> getColumnInfo(String modelId, Date predictTime);
164
165     /**
166      * 样本的采样周期
167      *
168      * @param modelId
169      * @return
170      */
171     protected abstract Integer getSampleCycle(String modelId);
172
173     /**
174      * 计算取值的时间
175      *
176      * @param originalTime
177      * @param backward
178      * @param dataLength
179      * @param granularity
180      * @return
181      */
182     public Date calculateTime(Date originalTime, Boolean backward, int dataLength, int granularity) {
183         int timeLength;
184         if (backward) {
185             timeLength = (-1) * dataLength;
186         } else {
187             timeLength = dataLength - 1;
188         }
189         Date desTime = originalTime;
190         Calendar calendar = Calendar.getInstance();
191         calendar.setTime(desTime);
192         calendar.set(Calendar.MILLISECOND, 0);
193         // 数据长度 * 粒度
194         calendar.add(Calendar.SECOND, timeLength * granularity);
195         return calendar.getTime();
196     }
197 }