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