潘志宝
2024-09-30 303715d81f0c1cd8b32cd659b7734c01565369a6
提交 | 用户 | 时间
c06f48 1 package com.iailab.module.model.mcs.pre.controller.admin;
7fd198 2
5c6007 3 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
7fd198 4 import com.iailab.framework.common.pojo.CommonResult;
5 import com.iailab.framework.common.pojo.PageResult;
9f049d 6 import com.iailab.framework.common.util.date.DateUtils;
137356 7 import com.iailab.module.model.common.enums.CommonConstant;
7fd198 8 import com.iailab.module.model.mcs.pre.dto.MmPredictItemDTO;
9f049d 9 import com.iailab.module.model.mcs.pre.entity.DmModuleEntity;
J 10 import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity;
11 import com.iailab.module.model.mcs.pre.entity.MmPredictItemEntity;
12 import com.iailab.module.model.mcs.pre.service.DmModuleService;
13 import com.iailab.module.model.mcs.pre.service.MmItemOutputService;
7fd198 14 import com.iailab.module.model.mcs.pre.service.MmPredictItemService;
9f049d 15 import com.iailab.module.model.mcs.pre.vo.*;
137356 16 import com.iailab.module.model.mpk.common.utils.IAILModelUtil;
9f049d 17 import lombok.extern.slf4j.Slf4j;
J 18 import org.apache.commons.lang3.StringUtils;
7fd198 19 import org.springframework.beans.factory.annotation.Autowired;
137356 20 import org.springframework.beans.factory.annotation.Value;
abba54 21 import org.springframework.security.access.prepost.PreAuthorize;
9f049d 22 import org.springframework.util.CollectionUtils;
5c6007 23 import org.springframework.validation.annotation.Validated;
7fd198 24 import org.springframework.web.bind.annotation.*;
25 import org.springframework.web.multipart.MultipartFile;
26
137356 27 import javax.annotation.security.PermitAll;
28 import java.io.File;
9f049d 29 import java.math.BigDecimal;
J 30 import java.math.RoundingMode;
31 import java.util.*;
7fd198 32
33 import static com.iailab.framework.common.pojo.CommonResult.success;
34
35 /**
36  * @author PanZhibao
37  * @date 2021年04月26日 14:42
38  */
9f049d 39 @Slf4j
7fd198 40 @RestController
137356 41 @RequestMapping("/model/pre/item")
7fd198 42 public class MmPredictItemController {
137356 43
44     @Value("${mpk.model-file-path}")
45     private String modelPath;
46
47     @Autowired
48     private IAILModelUtil iAILModelUtil;
7fd198 49
50     @Autowired
51     private MmPredictItemService mmPredictItemService;
48c57b 52
9f049d 53     @Autowired
J 54     private DmModuleService dmModuleService;
55
56     @Autowired
57     private MmItemOutputService mmItemOutputService;
58
59     private int HOUR_MINS = 60;
7fd198 60     /**
61      * 预测项列表
62      */
63     @GetMapping("/page")
905742 64     @PreAuthorize("@ss.hasPermission('model:pre-item:query')")
5c6007 65     public CommonResult<PageResult<MmPredictItemRespVO>> page(@Validated MmPredictItemPageReqVO reqVO) {
137356 66         PageResult<MmPredictItemRespVO> page = mmPredictItemService.queryPage(reqVO);
67         return success(page);
7fd198 68     }
69
5d299c 70     @GetMapping("/list")
a97b38 71     public CommonResult<List<MmPredictItemRespVO>> list(Map<String, Object> params) {
72         List<MmPredictItemRespVO> list = mmPredictItemService.list(params);
5d299c 73         return success(list);
L 74     }
75
7fd198 76     /**
77      * 预测项信息
78      */
d395d2 79     @GetMapping("/get/{id}")
905742 80     @PreAuthorize("@ss.hasPermission('model:pre-item:query')")
137356 81     public CommonResult<MmPredictItemDTO> info(@PathVariable("id") String id) {
5c6007 82         MmPredictItemDTO predictItem = mmPredictItemService.info(id);
7fd198 83         return success(predictItem);
84     }
85
86     /**
87      * 保存预测项
88      */
d395d2 89     @PostMapping("/create")
905742 90     @PreAuthorize("@ss.hasPermission('model:pre-item:create')")
137356 91     @DSTransactional(rollbackFor = Exception.class)
92     public CommonResult<Boolean> save(@RequestBody MmPredictItemDTO mmPredictItemDto) {
5d299c 93         mmPredictItemService.add(mmPredictItemDto);
7fd198 94         return success(true);
95     }
96
97     /**
98      * 修改预测项
99      */
d395d2 100     @PutMapping("/update")
905742 101     @PreAuthorize("@ss.hasPermission('model:pre-item:update')")
137356 102     @DSTransactional(rollbackFor = Exception.class)
103     public CommonResult<Boolean> update(@RequestBody MmPredictItemDTO mmPredictItemDTO) {
5d299c 104         mmPredictItemService.update(mmPredictItemDTO);
7fd198 105         return success(true);
106     }
107
108     /**
109      * 删除预测项
110      */
d395d2 111     @DeleteMapping("/delete")
905742 112     @PreAuthorize("@ss.hasPermission('model:pre-item:delete')")
137356 113     @DSTransactional(rollbackFor = Exception.class)
d395d2 114     public CommonResult<Boolean> delete(@RequestParam("id") String id) {
L 115         mmPredictItemService.deleteBatch(new String[]{id});
7fd198 116         return success(true);
117     }
118
119     /**
120      * 预测项列表
121      */
122     @GetMapping("/count-itemtype")
137356 123     public CommonResult<List<CountItemtypeVO>> countItemtype(@RequestParam Map<String, Object> params) {
7fd198 124         List<CountItemtypeVO> list = new ArrayList<>();
125         return success(list);
126     }
127
128     /**
129      * 上传模型
130      */
137356 131     @PermitAll
132     @PostMapping("/upload-model")
133     public CommonResult<Map<String, Object>> uploadModel(@RequestParam("file") MultipartFile file) throws Exception {
134         String uploadDir = modelPath + file.getOriginalFilename();
135         file.transferTo(new File(uploadDir));
136         Map<String, Object> result = iAILModelUtil.parseModel(uploadDir);
137         result.put("originalFilename", file.getOriginalFilename().replace(CommonConstant.MDK_SUFFIX, ""));
138         return success(result);
7fd198 139     }
9f049d 140
J 141     @GetMapping("/tree")
142     public CommonResult<List<PreItemOptionVO>> itemTree() {
143         List<PreItemOptionVO> result = new ArrayList<>();
144
145         List<DmModuleEntity> moduleList = dmModuleService.list(new HashMap<>());
146         if (CollectionUtils.isEmpty(moduleList)) {
147             return success(result);
148         }
149         moduleList.forEach(item -> {
150             PreItemOptionVO moduleOpt = new PreItemOptionVO();
151             moduleOpt.setId(item.getId());
152             moduleOpt.setLabel(item.getModulename());
153             List<PreItemOptionVO> children = new ArrayList<>();
154             Map<String, Object> params = new HashMap<>(2);
155             params.put("status", 1);
156             params.put("moduleid", item.getId());
157             List<MmPredictItemRespVO> itemList = mmPredictItemService.list(params);
158             itemList.forEach(item1 -> {
159                 PreItemOptionVO chd = new PreItemOptionVO();
160                 chd.setLabel(item1.getItemname());
161                 chd.setId(item1.getId());
162                 children.add(chd);
163             });
164             moduleOpt.setChildren(children);
165             result.add(moduleOpt);
166         });
167         return success(result);
168     }
169
170     @GetMapping("/view-charts")
171     public CommonResult<PreDataBarLineVO> viewCharts(@RequestParam Map<String, Object> params) {
172         PreDataBarLineVO result = new PreDataBarLineVO();
173         List<String> legends = new ArrayList<>();
174         List<PreDataViewVO> dataViewList = new ArrayList<>();
175         if (params.get("itemIds") == null) {
176             return success(result);
177         }
178         List<String> itemIdList = Arrays.asList(params.get("itemIds").toString().split(","));
179
180         Date predictTime;
181         if (StringUtils.isBlank((String) params.get("predictTime"))) {
182             DmModuleEntity dmModule = dmModuleService.getModuleByItemId(itemIdList.get(0));
183             if (dmModule != null) {
184                 predictTime = dmModule.getPredicttime();
185             } else {
186                 Calendar calendar = Calendar.getInstance();
187                 calendar.set(Calendar.MILLISECOND, 0);
188                 calendar.set(Calendar.SECOND, 0);
189                 predictTime = calendar.getTime();
190             }
191         } else {
192             predictTime = DateUtils.parse(params.get("predictTime").toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
193         }
194         Date startTime;
195         if (StringUtils.isBlank((String) params.get("startTime"))) {
196             Calendar calendar = Calendar.getInstance();
197             calendar.setTime(predictTime);
198             calendar.add(Calendar.HOUR_OF_DAY, -1);
199             startTime = calendar.getTime();
200         } else {
201             startTime = DateUtils.parse(params.get("startTime").toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
202         }
203         Date endTime = null;
204         if (StringUtils.isBlank((String) params.get("endTime"))) {
205             Calendar calendar = Calendar.getInstance();
206             calendar.setTime(predictTime);
207             calendar.add(Calendar.HOUR_OF_DAY, 1);
208             endTime = calendar.getTime();
209         } else {
210             endTime = DateUtils.parse(params.get("endTime").toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
211         }
212
213         for (int i = 0; i < itemIdList.size(); i++) {
214             PreDataViewVO viewDto = new PreDataViewVO();
215             String itemId = itemIdList.get(i);
216             MmItemOutputEntity mmItemOutput = mmItemOutputService.getByItemid(itemId);
217             MmPredictItemEntity predictItem = mmPredictItemService.getById(itemId);
218             if (predictItem == null) {
219                 log.info("itemId=" + itemId + "; is null");
220                 continue;
221             }
222             try {
223                 viewDto.setItemId(itemId);
224                 viewDto.setItemName(predictItem.getItemname());
225                 viewDto.setRealData(mmPredictItemService.getHisData(itemId, startTime, endTime));
226 //                viewDto.setPreDataN(mmItemResultService.getData(mmItemOutput.getId(), startTime, endTime));
227 //                viewDto.setPreDataL(mmItemResultLastPointService.getData(mmItemOutput.getId(), startTime, endTime));
228 //                viewDto.setCurData(mmItemResultJsonService.getData(mmItemOutput.getId(), predictTime));
229 //                viewDto.setAdjData(scheduleAdjustResultService.getData(itemId, predictTime));
230                 legends.add(predictItem.getItemname());
231                 List<Double> values = new ArrayList<>();
232                 if (!CollectionUtils.isEmpty(viewDto.getRealData())) {
233                     List<Double> hisValues = new ArrayList<>();
234                     viewDto.getRealData().forEach(item -> {
235                         values.add(Double.parseDouble(item[1].toString()));
236                         hisValues.add(Double.parseDouble(item[1].toString()));
237                     });
238                     viewDto.setHisMax(BigDecimal.valueOf(hisValues.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, RoundingMode.HALF_UP));
239                     viewDto.setHisMin(BigDecimal.valueOf(hisValues.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, RoundingMode.HALF_UP));
240                     viewDto.setHisAvg(BigDecimal.valueOf(hisValues.stream().mapToDouble(Double::doubleValue).average().getAsDouble()).setScale(2, RoundingMode.HALF_UP));
241                     viewDto.setHisCumulant(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).sum())
242                             .divide(new BigDecimal(HOUR_MINS), 2, BigDecimal.ROUND_HALF_UP));
243                 }
244                 if (!CollectionUtils.isEmpty(viewDto.getPreDataN())) {
245                     viewDto.getPreDataN().forEach(item -> {
246                         values.add(Double.parseDouble(item[1].toString()));
247                     });
248                 }
249                 if (!CollectionUtils.isEmpty(viewDto.getPreDataL())) {
250                     List<Double> preValues = new ArrayList<>();
251                     viewDto.getPreDataL().forEach(item -> {
252                         values.add(Double.parseDouble(item[1].toString()));
253                         preValues.add(Double.parseDouble(item[1].toString()));
254                     });
255                     viewDto.setPreMax(BigDecimal.valueOf(preValues.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, RoundingMode.HALF_UP));
256                     viewDto.setPreMin(BigDecimal.valueOf(preValues.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, RoundingMode.HALF_UP));
257                     viewDto.setPreAvg(BigDecimal.valueOf(preValues.stream().mapToDouble(Double::doubleValue).average().getAsDouble()).setScale(2, RoundingMode.HALF_UP));
258                 }
259                 if (!CollectionUtils.isEmpty(viewDto.getCurData())) {
260                     List<Double> preValues = new ArrayList<>();
261                     viewDto.getCurData().forEach(item -> {
262                         values.add(Double.parseDouble(item[1].toString()));
263                         preValues.add(Double.parseDouble(item[1].toString()));
264                     });
265                     viewDto.setPreCumulant(BigDecimal.valueOf(preValues.stream().mapToDouble(Double::doubleValue).sum())
266                             .divide(new BigDecimal(HOUR_MINS), 2, RoundingMode.HALF_UP));
267                 }
268                 if (!CollectionUtils.isEmpty(viewDto.getAdjData())) {
269                     viewDto.getAdjData().forEach(item -> {
270                         values.add(Double.parseDouble(item[1].toString()));
271                     });
272                 }
273                 if (!CollectionUtils.isEmpty(values)) {
274                     viewDto.setMaxValue(BigDecimal.valueOf(values.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, RoundingMode.HALF_UP));
275                     viewDto.setMinValue(BigDecimal.valueOf(values.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, RoundingMode.HALF_UP));
276                 }
277
278                 dataViewList.add(viewDto);
279             } catch (Exception ex) {
280                 ex.printStackTrace();
281             }
282         }
283         result.setStartTime(startTime);
284         result.setEndTime(endTime);
285         result.setPredictTime(predictTime);
286         result.setCategories(DateUtils.getTimeScale(startTime, endTime, 60));
287         result.setLegend(legends);
288         result.setDataViewList(dataViewList);
289         return success(result);
290     }
7fd198 291 }