houzhongjian
2024-12-05 a709abfd8ffec1524cefff30c3581f4425695433
提交 | 用户 | 时间
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      */
45520a 49     protected SampleInfo prepareSampleInfo(String modelId, Date predictTime) {
7fd198 50         SampleInfo sampleInfo = new SampleInfo();
51         //调用样本列数的方法
45520a 52 //        sampleInfo.setSampleColumn(getSampleColumn(modelId));
7fd198 53         //样本的列信息
54         sampleInfo.setColumnInfo(getColumnInfo(modelId, predictTime));
55         //样本的采样周期
45520a 56 //        sampleInfo.setSampleCycle(getSampleCycle(modelId));
7fd198 57         return sampleInfo;
58     }
59
60     /**
61      * 返回样本矩阵的列数
62      *
63      * @param modelId
64      * @return
65      */
66     protected abstract Integer getSampleColumn(String modelId);
45520a 67
D 68
69     /**
70      * 样本的列信息
71      *
72      * @param modelId
73      * @param predictTime
74      * @return
75      */
76     protected abstract List<ColumnItemPort> getColumnInfo(String modelId, Date predictTime);
77
78     /**
79      * 样本的采样周期
80      *
81      * @param modelId
82      * @return
83      */
84     protected abstract Integer getSampleCycle(String modelId);
7fd198 85
86     /**
87      * 获取开始时间
88      *
89      * @param columnItem
90      * @param originalTime
91      * @return
92      * @throws Exception
93      */
94     protected Date getStartTime(ColumnItem columnItem, Date originalTime) {
95         Date dateTime = new Date();
96         Calendar calendar = Calendar.getInstance();
97         calendar.setTime(originalTime);
e78121 98         switch (ModelParamType.getEumByCode(columnItem.getParamType())) {
99             case DATAPOINT:
536b8e 100                 ApiPointDTO dataPoint = dataPointApi.getInfoById(columnItem.getParamId());
7fd198 101                 if (dataPoint == null) {
102                     return null;
103                 }
e78121 104                 dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue());
7fd198 105                 break;
19a4fb 106             case NORMALITEM:
D 107             case MERGEITEM:
7fd198 108                 dateTime = calendar.getTime();
109                 break;
e78121 110             case IND:
7fd198 111                 dateTime = calculateTime(originalTime, true, columnItem.getDataLength(), 60);
112                 break;
e78121 113             case PLAN:
7fd198 114                 dateTime = calendar.getTime();
115                 break;
116             default:
117                 break;
118         }
119         return dateTime;
120     }
121
122     /**
123      * 获取结束时间
124      *
125      * @param columnItem
126      * @param originalTime
127      * @return
128      * @throws Exception
129      */
130     protected Date getEndTime(ColumnItem columnItem, Date originalTime) {
131         Date dateTime = new Date();
132         Calendar calendar = Calendar.getInstance();
133         calendar.setTime(originalTime);
e78121 134         switch (ModelParamType.getEumByCode(columnItem.getParamType())) {
135             case DATAPOINT:
536b8e 136                 ApiPointDTO dataPoint = dataPointApi.getInfoById(columnItem.getParamId());
7fd198 137                 if (dataPoint == null) {
138                     return null;
139                 }
b2aca2 140                 dateTime = calendar.getTime();
7fd198 141                 break;
19a4fb 142             case NORMALITEM:
D 143             case MERGEITEM:
b2aca2 144                 dateTime = calendar.getTime();
7fd198 145                 break;
e78121 146             case IND:
7fd198 147                 dateTime = calendar.getTime();
148                 break;
e78121 149             case PLAN:
150                 ApiPlanItemDTO planItem = planItemApi.getInfoByNo(columnItem.getParamId());
151                 if (planItem == null) {
152                     return null;
153                 }
154                 dateTime = calculateDate(originalTime, false, columnItem.getDataLength(),
155                         TimeGranularityEnum.getEumByCode(planItem.getTimeGranularity()).getValue());
7fd198 156                 break;
157             default:
158                 break;
159         }
160         return dateTime;
161     }
162
163     /**
164      * 获取粒度,s
165      *
166      * @param columnItem
167      * @return
168      * @throws Exception
169      */
170     protected Integer getGranularity(ColumnItem columnItem) {
91343d 171         // 默认60s
7fd198 172         Integer granularity = 60;
91343d 173         switch (ModelParamType.getEumByCode(columnItem.getParamType())) {
174             case DATAPOINT:
536b8e 175                 ApiPointDTO dataPoint = dataPointApi.getInfoById(columnItem.getParamId());
e78121 176                 granularity = DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue();
7fd198 177                 break;
19a4fb 178             case NORMALITEM:
D 179                 granularity = mmPredictItemService.getItemByOutPutId(columnItem.getParamId()).getGranularity();
180                 break;
181             case MERGEITEM:
91343d 182                 granularity = mmPredictItemService.getItemById(columnItem.getParamId()).getGranularity();
7fd198 183                 break;
91343d 184             case IND:
185                 ApiIndItemDTO indItemDTO = indItemApi.getInfoById(columnItem.getParamId());
186                 granularity = TimeGranularitySecEnum.getEumByCode(indItemDTO.getTimeGranularity()).getValue();;
7fd198 187                 break;
91343d 188             case PLAN:
189                 ApiPlanItemDTO apiPlanItemDTO = planItemApi.getInfoById(columnItem.getParamId());
190                 granularity = TimeGranularitySecEnum.getEumByCode(apiPlanItemDTO.getTimeGranularity()).getValue();
7fd198 191                 break;
192             default:
193                 break;
194         }
195         return granularity;
196     }
197
198     /**
199      * 计算取值的时间
200      *
201      * @param originalTime
202      * @param backward
203      * @param dataLength
204      * @param granularity
205      * @return
206      */
207     public Date calculateTime(Date originalTime, Boolean backward, int dataLength, int granularity) {
208         int timeLength;
209         if (backward) {
210             timeLength = (-1) * dataLength;
211         } else {
212             timeLength = dataLength - 1;
213         }
214         Date desTime = originalTime;
215         Calendar calendar = Calendar.getInstance();
216         calendar.setTime(desTime);
217         calendar.set(Calendar.MILLISECOND, 0);
218         // 数据长度 * 粒度
219         calendar.add(Calendar.SECOND, timeLength * granularity);
220         return calendar.getTime();
221     }
e78121 222
223     public Date calculateDate(Date originalTime, Boolean backward, int dataLength, int field) {
224         int timeLength;
225         if (backward) {
226             timeLength = (-1) * dataLength;
227         } else {
228             timeLength = dataLength - 1;
229         }
230         Date desTime = originalTime;
231         Calendar calendar = Calendar.getInstance();
232         calendar.setTime(desTime);
233         calendar.set(Calendar.MILLISECOND, 0);
234         // 数据长度 * 粒度
235         calendar.add(Calendar.SECOND, timeLength);
236         return calendar.getTime();
237     }
7fd198 238 }