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