提交 | 用户 | 时间
|
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()); |
|
103 |
if (reqDTO.getIsResult() == null || !reqDTO.getIsResult()) { |
|
104 |
return resp; |
|
105 |
} |
|
106 |
for (Map.Entry<String, PredictResultVO> entry : predictResultMap.entrySet()) { |
|
107 |
MdkPredictItemRespDTO itemResp = new MdkPredictItemRespDTO(); |
|
108 |
itemResp.setItemId(entry.getKey()); |
|
109 |
itemResp.setPredictTime(reqDTO.getPredictTime()); |
|
110 |
Map<String, List<MdkPredictDataDTO>> itemPredictData = new HashMap<>(); |
|
111 |
|
|
112 |
Map<String, List<DataValueVO>> predictLists = predictResultHandler.convertToPredictData2(entry.getValue()); |
|
113 |
for (Map.Entry<String, List<DataValueVO>> dataListEntry : predictLists.entrySet()) { |
|
114 |
List<MdkPredictDataDTO> predictData = dataListEntry.getValue().stream().map(t -> { |
|
115 |
MdkPredictDataDTO dto1 = new MdkPredictDataDTO(); |
|
116 |
dto1.setDataTime(t.getDataTime()); |
|
117 |
dto1.setDataValue(t.getDataValue()); |
|
118 |
return dto1; |
|
119 |
}).collect(Collectors.toList()); |
|
120 |
itemPredictData.put(dataListEntry.getKey(), predictData); |
|
121 |
} |
|
122 |
itemResp.setPredictData(itemPredictData); |
|
123 |
predictItemRespMap.put(entry.getKey(), itemResp); |
|
124 |
} |
7fd198
|
125 |
} |
潘 |
126 |
log.info("预测计算结束: " + System.currentTimeMillis()); |
|
127 |
} catch (Exception ex) { |
148842
|
128 |
ex.printStackTrace(); |
潘 |
129 |
return resp; |
7fd198
|
130 |
} |
潘 |
131 |
resp.setPredictItemRespMap(predictItemRespMap); |
148842
|
132 |
return resp; |
7fd198
|
133 |
} |
潘 |
134 |
|
|
135 |
/** |
|
136 |
* 单个预测 |
|
137 |
* |
|
138 |
* @param reqDTO |
|
139 |
* @return |
|
140 |
*/ |
|
141 |
@Override |
148842
|
142 |
public MdkPredictItemRespDTO predictItem(MdkPredictReqDTO reqDTO) { |
7fd198
|
143 |
MdkPredictItemRespDTO resp = new MdkPredictItemRespDTO(); |
潘 |
144 |
|
9162d9
|
145 |
try { |
潘 |
146 |
log.info("预测计算开始: " + System.currentTimeMillis()); |
4f1717
|
147 |
Map<String, List<MdkPredictDataDTO>> predictData = new HashMap<>(); |
9162d9
|
148 |
ItemVO predictItem = itemEntityFactory.getItemByItemNo(reqDTO.getItemNo()); |
4f1717
|
149 |
PredictItemHandler predictItemHandler = predictItemFactory.create(predictItem.getId()); |
9162d9
|
150 |
PredictResultVO predictResult = predictItemHandler.predict(reqDTO.getPredictTime(), predictItem); |
潘 |
151 |
Map<String, List<DataValueVO>> resultMap = predictResultHandler.convertToPredictData(predictResult); |
|
152 |
if (!CollectionUtils.isEmpty(resultMap)) { |
|
153 |
for (Map.Entry<String, List<DataValueVO>> entry : resultMap.entrySet()) { |
4f1717
|
154 |
List<MdkPredictDataDTO> data = ConvertUtils.sourceToTarget(entry.getValue(), MdkPredictDataDTO.class); |
潘 |
155 |
predictData.put(entry.getKey(), data); |
9162d9
|
156 |
} |
潘 |
157 |
} |
|
158 |
resp.setPredictData(predictData); |
|
159 |
resp.setItemId(predictItem.getId()); |
|
160 |
resp.setPredictTime(reqDTO.getPredictTime()); |
|
161 |
log.info("预测计算结束: " + System.currentTimeMillis()); |
|
162 |
} catch (Exception ex) { |
4f1717
|
163 |
log.info("预测计算异常: " + System.currentTimeMillis(), ex); |
148842
|
164 |
return resp; |
9162d9
|
165 |
} |
潘 |
166 |
|
148842
|
167 |
return resp; |
7fd198
|
168 |
} |
潘 |
169 |
|
|
170 |
/** |
|
171 |
* 预测调整 |
|
172 |
* |
|
173 |
* @param reqDTO |
|
174 |
* @return |
|
175 |
*/ |
|
176 |
@Override |
148842
|
177 |
public Boolean predictAutoAdjust(MdkPredictReqDTO reqDTO) { |
7fd198
|
178 |
|
潘 |
179 |
|
148842
|
180 |
return true; |
7fd198
|
181 |
} |
潘 |
182 |
|
|
183 |
/** |
|
184 |
* 执行调度模型 |
|
185 |
* |
|
186 |
* @param reqDTO |
|
187 |
* @return |
|
188 |
*/ |
|
189 |
@Override |
148842
|
190 |
public MdkScheduleRespDTO doSchedule(MdkScheduleReqDTO reqDTO) { |
7fd198
|
191 |
MdkScheduleRespDTO resp = new MdkScheduleRespDTO(); |
054fb9
|
192 |
resp.setScheduleCode(reqDTO.getScheduleCode()); |
潘 |
193 |
resp.setScheduleTime(reqDTO.getScheduleTime()); |
|
194 |
try { |
|
195 |
log.info("调度计算开始: " + System.currentTimeMillis()); |
51c1c2
|
196 |
ScheduleResultVO scheduleResult = scheduleModelHandler.doSchedule(reqDTO.getScheduleCode(), reqDTO.getScheduleTime()); |
054fb9
|
197 |
resp.setResult(scheduleResult.getResult()); |
潘 |
198 |
log.info("预测计算结束: " + System.currentTimeMillis()); |
|
199 |
} catch (Exception ex) { |
|
200 |
log.info("调度计算异常: " + System.currentTimeMillis()); |
|
201 |
ex.printStackTrace(); |
148842
|
202 |
return resp; |
054fb9
|
203 |
} |
148842
|
204 |
return resp; |
7fd198
|
205 |
} |
潘 |
206 |
} |