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