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