提交 | 用户 | 时间
|
7fd198
|
1 |
package com.iailab.module.model.api; |
潘 |
2 |
|
|
3 |
import com.alibaba.fastjson.JSON; |
|
4 |
import com.iailab.module.model.api.mdk.MdkApi; |
|
5 |
import com.iailab.module.model.api.mdk.dto.*; |
|
6 |
import com.iailab.module.model.mcs.pre.entity.DmModuleEntity; |
|
7 |
import com.iailab.module.model.mcs.pre.service.DmModuleService; |
|
8 |
import com.iailab.module.model.mcs.pre.service.MmPredictItemService; |
ac52ae
|
9 |
import com.iailab.module.model.mcs.sche.service.StScheduleRecordService; |
潘 |
10 |
import com.iailab.module.model.mcs.sche.service.StScheduleSchemeService; |
7fd198
|
11 |
import com.iailab.module.model.mdk.predict.PredictModuleHandler; |
9162d9
|
12 |
import com.iailab.module.model.mdk.predict.PredictResultHandler; |
054fb9
|
13 |
import com.iailab.module.model.mdk.schedule.ScheduleModelHandler; |
9162d9
|
14 |
import com.iailab.module.model.mdk.vo.DataValueVO; |
7fd198
|
15 |
import com.iailab.module.model.mdk.vo.ItemVO; |
潘 |
16 |
import com.iailab.module.model.mdk.vo.PredictResultVO; |
054fb9
|
17 |
import com.iailab.module.model.mdk.vo.ScheduleResultVO; |
7fd198
|
18 |
import lombok.extern.slf4j.Slf4j; |
潘 |
19 |
import org.springframework.beans.factory.annotation.Autowired; |
9162d9
|
20 |
import org.springframework.util.CollectionUtils; |
7fd198
|
21 |
import org.springframework.validation.annotation.Validated; |
潘 |
22 |
import org.springframework.web.bind.annotation.RestController; |
|
23 |
|
b2aca2
|
24 |
import java.util.*; |
7fd198
|
25 |
import java.util.stream.Collectors; |
潘 |
26 |
|
|
27 |
/** |
|
28 |
* @author PanZhibao |
|
29 |
* @Description |
|
30 |
* @createTime 2024年08月26日 |
|
31 |
*/ |
|
32 |
@Slf4j |
|
33 |
@RestController |
|
34 |
@Validated |
|
35 |
public class MdkApiImpl implements MdkApi { |
|
36 |
|
|
37 |
@Autowired |
|
38 |
private DmModuleService dmModuleService; |
|
39 |
|
|
40 |
@Autowired |
|
41 |
private MmPredictItemService mmPredictItemService; |
|
42 |
|
|
43 |
@Autowired |
|
44 |
private PredictModuleHandler predictModuleHandler; |
9162d9
|
45 |
|
潘 |
46 |
@Autowired |
|
47 |
private PredictResultHandler predictResultHandler; |
054fb9
|
48 |
|
潘 |
49 |
@Autowired |
|
50 |
private ScheduleModelHandler scheduleModelHandler; |
ac52ae
|
51 |
|
潘 |
52 |
@Autowired |
|
53 |
private StScheduleRecordService stScheduleRecordService; |
|
54 |
|
|
55 |
@Autowired |
|
56 |
private StScheduleSchemeService stScheduleSchemeService; |
7fd198
|
57 |
|
潘 |
58 |
/** |
|
59 |
* 按模块预测 |
|
60 |
* |
|
61 |
* @param reqDTO |
|
62 |
* @return |
|
63 |
*/ |
|
64 |
@Override |
148842
|
65 |
public MdkPredictModuleRespDTO predictModule(MdkPredictReqDTO reqDTO) { |
7fd198
|
66 |
MdkPredictModuleRespDTO resp = new MdkPredictModuleRespDTO(); |
69bd5e
|
67 |
resp.setPredictTime(reqDTO.getPredictTime()); |
D |
68 |
resp.setModuleType(reqDTO.getModuleType()); |
|
69 |
|
7fd198
|
70 |
Map<String, MdkPredictItemRespDTO> predictItemRespMap = new HashMap<>(); |
潘 |
71 |
try { |
|
72 |
if (reqDTO.getPredictTime() == null) { |
|
73 |
throw new Exception("PredictTime不能为空"); |
|
74 |
} |
|
75 |
if (reqDTO.getModuleType() == null) { |
|
76 |
throw new Exception("ModuleType不能为空"); |
|
77 |
} |
b2aca2
|
78 |
Calendar calendar = Calendar.getInstance(); |
D |
79 |
calendar.setTime(reqDTO.getPredictTime()); |
|
80 |
calendar.set(Calendar.MILLISECOND, 0); |
|
81 |
calendar.set(Calendar.SECOND, 0); |
|
82 |
reqDTO.setPredictTime(calendar.getTime()); |
7fd198
|
83 |
log.info("预测参数:" + JSON.toJSONString(reqDTO)); |
潘 |
84 |
MdkPredictModuleRespDTO result = new MdkPredictModuleRespDTO(); |
|
85 |
result.setPredictTime(reqDTO.getPredictTime()); |
|
86 |
result.setModuleType(reqDTO.getModuleType()); |
|
87 |
List<DmModuleEntity> moduleList = dmModuleService.getModuleByModuleType(reqDTO.getModuleType()); |
|
88 |
log.info("预测计算开始: " + System.currentTimeMillis()); |
|
89 |
for (DmModuleEntity module : moduleList) { |
|
90 |
int intervalTime = 0; |
|
91 |
if (module.getPredicttime() != null) { |
|
92 |
intervalTime = (int) (reqDTO.getPredictTime().getTime() - module.getPredicttime().getTime()) / (1000 * 60); |
|
93 |
} |
|
94 |
List<ItemVO> predictItemList = mmPredictItemService.getByModuleId(module.getId()); |
07890e
|
95 |
Map<String, PredictResultVO> predictResultMap = new HashMap<>(predictItemList.size()); |
D |
96 |
// 分组,先运行normal预测项,再将结果传递给merge预测项 |
|
97 |
List<ItemVO> normalItems = predictItemList.stream().filter(e -> e.getItemType().equals("NormalItem")).collect(Collectors.toList()); |
|
98 |
if (!CollectionUtils.isEmpty(normalItems)) { |
ac52ae
|
99 |
predictModuleHandler.predict(normalItems, reqDTO.getPredictTime(), intervalTime, predictResultMap); |
fde993
|
100 |
List<ItemVO> mergeItems = predictItemList.stream().filter(e -> e.getItemType().equals("MergeItem")).collect(Collectors.toList()); |
D |
101 |
if (!CollectionUtils.isEmpty(mergeItems)) { |
ac52ae
|
102 |
predictModuleHandler.predict(mergeItems, reqDTO.getPredictTime(), intervalTime, predictResultMap); |
07890e
|
103 |
} |
D |
104 |
} |
4f1717
|
105 |
// 更新Module时间 |
潘 |
106 |
dmModuleService.updatePredictTime(module.getId(), reqDTO.getPredictTime()); |
|
107 |
if (reqDTO.getIsResult() == null || !reqDTO.getIsResult()) { |
|
108 |
return resp; |
|
109 |
} |
|
110 |
for (Map.Entry<String, PredictResultVO> entry : predictResultMap.entrySet()) { |
|
111 |
MdkPredictItemRespDTO itemResp = new MdkPredictItemRespDTO(); |
|
112 |
itemResp.setItemId(entry.getKey()); |
|
113 |
itemResp.setPredictTime(reqDTO.getPredictTime()); |
|
114 |
Map<String, List<MdkPredictDataDTO>> itemPredictData = new HashMap<>(); |
|
115 |
|
|
116 |
Map<String, List<DataValueVO>> predictLists = predictResultHandler.convertToPredictData2(entry.getValue()); |
|
117 |
for (Map.Entry<String, List<DataValueVO>> dataListEntry : predictLists.entrySet()) { |
|
118 |
List<MdkPredictDataDTO> predictData = dataListEntry.getValue().stream().map(t -> { |
|
119 |
MdkPredictDataDTO dto1 = new MdkPredictDataDTO(); |
|
120 |
dto1.setDataTime(t.getDataTime()); |
|
121 |
dto1.setDataValue(t.getDataValue()); |
|
122 |
return dto1; |
|
123 |
}).collect(Collectors.toList()); |
|
124 |
itemPredictData.put(dataListEntry.getKey(), predictData); |
|
125 |
} |
|
126 |
itemResp.setPredictData(itemPredictData); |
|
127 |
predictItemRespMap.put(entry.getKey(), itemResp); |
|
128 |
} |
7fd198
|
129 |
} |
潘 |
130 |
log.info("预测计算结束: " + System.currentTimeMillis()); |
|
131 |
} catch (Exception ex) { |
148842
|
132 |
ex.printStackTrace(); |
潘 |
133 |
return resp; |
7fd198
|
134 |
} |
潘 |
135 |
resp.setPredictItemRespMap(predictItemRespMap); |
148842
|
136 |
return resp; |
7fd198
|
137 |
} |
潘 |
138 |
|
|
139 |
/** |
|
140 |
* 单个预测 |
|
141 |
* |
|
142 |
* @param reqDTO |
|
143 |
* @return |
|
144 |
*/ |
|
145 |
@Override |
148842
|
146 |
public MdkPredictItemRespDTO predictItem(MdkPredictReqDTO reqDTO) { |
7fd198
|
147 |
MdkPredictItemRespDTO resp = new MdkPredictItemRespDTO(); |
9162d9
|
148 |
try { |
1178da
|
149 |
|
D |
150 |
ItemVO itemByItemNo = mmPredictItemService.getItemByItemNo(reqDTO.getItemNo()); |
|
151 |
List<ItemVO> predictItemList = new ArrayList<>(); |
|
152 |
predictItemList.add(itemByItemNo); |
|
153 |
Map<String, PredictResultVO> predictResultMap = new HashMap<>(predictItemList.size()); |
ac52ae
|
154 |
predictModuleHandler.predict(predictItemList, reqDTO.getPredictTime(), 0, predictResultMap); |
1178da
|
155 |
|
D |
156 |
Map<String, List<MdkPredictDataDTO>> itemPredictData = new HashMap<>(); |
|
157 |
|
|
158 |
Map<String, List<DataValueVO>> predictLists = predictResultHandler.convertToPredictData2(predictResultMap.get(reqDTO.getItemNo())); |
|
159 |
for (Map.Entry<String, List<DataValueVO>> dataListEntry : predictLists.entrySet()) { |
|
160 |
List<MdkPredictDataDTO> predictData = dataListEntry.getValue().stream().map(t -> { |
|
161 |
MdkPredictDataDTO dto1 = new MdkPredictDataDTO(); |
|
162 |
dto1.setDataTime(t.getDataTime()); |
|
163 |
dto1.setDataValue(t.getDataValue()); |
|
164 |
return dto1; |
|
165 |
}).collect(Collectors.toList()); |
|
166 |
itemPredictData.put(dataListEntry.getKey(), predictData); |
9162d9
|
167 |
} |
1178da
|
168 |
resp.setItemId(reqDTO.getItemNo()); |
9162d9
|
169 |
resp.setPredictTime(reqDTO.getPredictTime()); |
1178da
|
170 |
resp.setPredictData(itemPredictData); |
D |
171 |
} catch (Exception e) { |
|
172 |
throw new RuntimeException(e); |
9162d9
|
173 |
} |
潘 |
174 |
|
148842
|
175 |
return resp; |
7fd198
|
176 |
} |
潘 |
177 |
|
|
178 |
/** |
|
179 |
* 预测调整 |
|
180 |
* |
|
181 |
* @param reqDTO |
|
182 |
* @return |
|
183 |
*/ |
|
184 |
@Override |
148842
|
185 |
public Boolean predictAutoAdjust(MdkPredictReqDTO reqDTO) { |
7fd198
|
186 |
|
潘 |
187 |
|
148842
|
188 |
return true; |
7fd198
|
189 |
} |
潘 |
190 |
|
|
191 |
/** |
|
192 |
* 执行调度模型 |
|
193 |
* |
|
194 |
* @param reqDTO |
|
195 |
* @return |
|
196 |
*/ |
|
197 |
@Override |
148842
|
198 |
public MdkScheduleRespDTO doSchedule(MdkScheduleReqDTO reqDTO) { |
7fd198
|
199 |
MdkScheduleRespDTO resp = new MdkScheduleRespDTO(); |
054fb9
|
200 |
resp.setScheduleCode(reqDTO.getScheduleCode()); |
潘 |
201 |
resp.setScheduleTime(reqDTO.getScheduleTime()); |
|
202 |
try { |
|
203 |
log.info("调度计算开始: " + System.currentTimeMillis()); |
51c1c2
|
204 |
ScheduleResultVO scheduleResult = scheduleModelHandler.doSchedule(reqDTO.getScheduleCode(), reqDTO.getScheduleTime()); |
054fb9
|
205 |
resp.setResult(scheduleResult.getResult()); |
ac52ae
|
206 |
stScheduleRecordService.create(scheduleResult); |
潘 |
207 |
stScheduleSchemeService.updateTime(scheduleResult.getSchemeId(), scheduleResult.getScheduleTime()); |
054fb9
|
208 |
log.info("预测计算结束: " + System.currentTimeMillis()); |
潘 |
209 |
} catch (Exception ex) { |
|
210 |
log.info("调度计算异常: " + System.currentTimeMillis()); |
ac52ae
|
211 |
ex.printStackTrace(); |
148842
|
212 |
return resp; |
054fb9
|
213 |
} |
148842
|
214 |
return resp; |
7fd198
|
215 |
} |
潘 |
216 |
} |