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