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