From 1205f84f732610763d46935c0ec31757005376f4 Mon Sep 17 00:00:00 2001 From: Jay <csj123456> Date: 星期二, 15 十月 2024 08:38:27 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/controller/admin/MmPredictItemController.java | 253 +++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 217 insertions(+), 36 deletions(-) diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/controller/admin/MmPredictItemController.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/controller/admin/MmPredictItemController.java index eb504cc..ceeac24 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/controller/admin/MmPredictItemController.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/controller/admin/MmPredictItemController.java @@ -1,48 +1,85 @@ package com.iailab.module.model.mcs.pre.controller.admin; +import com.baomidou.dynamic.datasource.annotation.DSTransactional; import com.iailab.framework.common.pojo.CommonResult; import com.iailab.framework.common.pojo.PageResult; +import com.iailab.framework.common.util.date.DateUtils; +import com.iailab.module.model.common.enums.CommonConstant; import com.iailab.module.model.mcs.pre.dto.MmPredictItemDTO; +import com.iailab.module.model.mcs.pre.entity.DmModuleEntity; +import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity; +import com.iailab.module.model.mcs.pre.entity.MmPredictItemEntity; +import com.iailab.module.model.mcs.pre.service.DmModuleService; +import com.iailab.module.model.mcs.pre.service.MmItemOutputService; import com.iailab.module.model.mcs.pre.service.MmPredictItemService; -import com.iailab.module.model.mcs.pre.vo.CountItemtypeVO; -import com.iailab.module.model.mcs.pre.vo.MmPredictItemRespVO; +import com.iailab.module.model.mcs.pre.vo.*; +import com.iailab.module.model.mpk.common.utils.IAILModelUtil; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.util.CollectionUtils; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import javax.annotation.security.PermitAll; +import java.io.File; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.*; -import static com.iailab.framework.common.pojo.CommonResult.error; import static com.iailab.framework.common.pojo.CommonResult.success; /** * @author PanZhibao * @date 2021年04月26日 14:42 */ +@Slf4j @RestController -@RequestMapping("/pre/predict-item") +@RequestMapping("/model/pre/item") public class MmPredictItemController { + + @Value("${mpk.model-file-path}") + private String modelPath; + + @Autowired + private IAILModelUtil iAILModelUtil; @Autowired private MmPredictItemService mmPredictItemService; + @Autowired + private DmModuleService dmModuleService; + + @Autowired + private MmItemOutputService mmItemOutputService; + + private int HOUR_MINS = 60; /** * 预测项列表 */ @GetMapping("/page") - public CommonResult<PageResult<MmPredictItemRespVO>> page(@RequestParam Map<String, Object> params) { - PageResult<MmPredictItemRespVO> page = mmPredictItemService.getPageList(params); + @PreAuthorize("@ss.hasPermission('model:pre-item:query')") + public CommonResult<PageResult<MmPredictItemRespVO>> page(@Validated MmPredictItemPageReqVO reqVO) { + PageResult<MmPredictItemRespVO> page = mmPredictItemService.queryPage(reqVO); return success(page); + } + + @GetMapping("/list") + public CommonResult<List<MmPredictItemRespVO>> list(Map<String, Object> params) { + List<MmPredictItemRespVO> list = mmPredictItemService.list(params); + return success(list); } /** * 预测项信息 */ @GetMapping("/get/{id}") - public CommonResult<MmPredictItemDTO> info(@PathVariable("id") String id, @RequestParam Map<String, Object> params){ - MmPredictItemDTO predictItem = mmPredictItemService.getDetailById(id, params); + @PreAuthorize("@ss.hasPermission('model:pre-item:query')") + public CommonResult<MmPredictItemDTO> info(@PathVariable("id") String id) { + MmPredictItemDTO predictItem = mmPredictItemService.info(id); return success(predictItem); } @@ -50,12 +87,10 @@ * 保存预测项 */ @PostMapping("/create") - public CommonResult<Boolean> save(@RequestBody MmPredictItemDTO mmPredictItemDto){ - int count = mmPredictItemService.check(mmPredictItemDto.getMmPredictItem()); - if (count > 0) { - return error(999, "名称或编号重复"); - } - mmPredictItemService.savePredictItem(mmPredictItemDto); + @PreAuthorize("@ss.hasPermission('model:pre-item:create')") + @DSTransactional(rollbackFor = Exception.class) + public CommonResult<Boolean> save(@RequestBody MmPredictItemDTO mmPredictItemDto) { + mmPredictItemService.add(mmPredictItemDto); return success(true); } @@ -63,12 +98,10 @@ * 修改预测项 */ @PutMapping("/update") - public CommonResult<Boolean> update(@RequestBody MmPredictItemDTO mmPredictItemDto){ - int count = mmPredictItemService.check(mmPredictItemDto.getMmPredictItem()); - if (count > 0) { - return error(999, "名称或编号重复"); - } - mmPredictItemService.update(mmPredictItemDto); + @PreAuthorize("@ss.hasPermission('model:pre-item:update')") + @DSTransactional(rollbackFor = Exception.class) + public CommonResult<Boolean> update(@RequestBody MmPredictItemDTO mmPredictItemDTO) { + mmPredictItemService.update(mmPredictItemDTO); return success(true); } @@ -76,6 +109,8 @@ * 删除预测项 */ @DeleteMapping("/delete") + @PreAuthorize("@ss.hasPermission('model:pre-item:delete')") + @DSTransactional(rollbackFor = Exception.class) public CommonResult<Boolean> delete(@RequestParam("id") String id) { mmPredictItemService.deleteBatch(new String[]{id}); return success(true); @@ -85,26 +120,172 @@ * 预测项列表 */ @GetMapping("/count-itemtype") - public CommonResult<List<CountItemtypeVO>> countItemtype(@RequestParam Map<String, Object> params){ + public CommonResult<List<CountItemtypeVO>> countItemtype(@RequestParam Map<String, Object> params) { List<CountItemtypeVO> list = new ArrayList<>(); return success(list); } /** - * 数量 - */ - @GetMapping("/count") - public CommonResult<Long> count() { - Long count = mmPredictItemService.count(); - return success(count); - } - - /** * 上传模型 */ - @PostMapping("/uploadModel") - public CommonResult<Boolean> uploadModel(@RequestParam("file") MultipartFile file) throws Exception { + @PermitAll + @PostMapping("/upload-model") + public CommonResult<Map<String, Object>> uploadModel(@RequestParam("file") MultipartFile file) throws Exception { + String uploadDir = modelPath + file.getOriginalFilename(); + file.transferTo(new File(uploadDir)); + Map<String, Object> result = iAILModelUtil.parseModel(uploadDir); + result.put("originalFilename", file.getOriginalFilename().replace(CommonConstant.MDK_SUFFIX, "")); + return success(result); + } - return success(true); + @GetMapping("/tree") + public CommonResult<List<PreItemOptionVO>> itemTree() { + List<PreItemOptionVO> result = new ArrayList<>(); + + List<DmModuleEntity> moduleList = dmModuleService.list(new HashMap<>()); + if (CollectionUtils.isEmpty(moduleList)) { + return success(result); + } + moduleList.forEach(item -> { + PreItemOptionVO moduleOpt = new PreItemOptionVO(); + moduleOpt.setId(item.getId()); + moduleOpt.setLabel(item.getModulename()); + List<PreItemOptionVO> children = new ArrayList<>(); + Map<String, Object> params = new HashMap<>(2); + params.put("status", 1); + params.put("moduleid", item.getId()); + List<MmPredictItemRespVO> itemList = mmPredictItemService.list(params); + itemList.forEach(item1 -> { + PreItemOptionVO chd = new PreItemOptionVO(); + chd.setLabel(item1.getItemname()); + chd.setId(item1.getId()); + children.add(chd); + }); + moduleOpt.setChildren(children); + result.add(moduleOpt); + }); + return success(result); + } + + @GetMapping("/view-charts") + public CommonResult<PreDataBarLineVO> viewCharts(@RequestParam Map<String, Object> params) { + PreDataBarLineVO result = new PreDataBarLineVO(); + List<String> legends = new ArrayList<>(); + List<PreDataViewVO> dataViewList = new ArrayList<>(); + if (params.get("itemIds") == null) { + return success(result); + } + List<String> itemIdList = Arrays.asList(params.get("itemIds").toString().split(",")); + + Date predictTime; + if (StringUtils.isBlank((String) params.get("predictTime"))) { + DmModuleEntity dmModule = dmModuleService.getModuleByItemId(itemIdList.get(0)); + if (dmModule != null) { + predictTime = dmModule.getPredicttime(); + } else { + Calendar calendar = Calendar.getInstance(); + calendar.set(Calendar.MILLISECOND, 0); + calendar.set(Calendar.SECOND, 0); + predictTime = calendar.getTime(); + } + } else { + predictTime = DateUtils.parse(params.get("predictTime").toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND); + } + Date startTime; + if (StringUtils.isBlank((String) params.get("startTime"))) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(predictTime); + calendar.add(Calendar.HOUR_OF_DAY, -1); + startTime = calendar.getTime(); + } else { + startTime = DateUtils.parse(params.get("startTime").toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND); + } + Date endTime = null; + if (StringUtils.isBlank((String) params.get("endTime"))) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(predictTime); + calendar.add(Calendar.HOUR_OF_DAY, 1); + endTime = calendar.getTime(); + } else { + endTime = DateUtils.parse(params.get("endTime").toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND); + } + + for (int i = 0; i < itemIdList.size(); i++) { + PreDataViewVO viewDto = new PreDataViewVO(); + String itemId = itemIdList.get(i); + MmItemOutputEntity mmItemOutput = mmItemOutputService.getByItemid(itemId); + MmPredictItemEntity predictItem = mmPredictItemService.getById(itemId); + if (predictItem == null) { + log.info("itemId=" + itemId + "; is null"); + continue; + } + try { + viewDto.setItemId(itemId); + viewDto.setItemName(predictItem.getItemname()); + viewDto.setRealData(mmPredictItemService.getHisData(itemId, startTime, endTime)); +// viewDto.setPreDataN(mmItemResultService.getData(mmItemOutput.getId(), startTime, endTime)); +// viewDto.setPreDataL(mmItemResultLastPointService.getData(mmItemOutput.getId(), startTime, endTime)); +// viewDto.setCurData(mmItemResultJsonService.getData(mmItemOutput.getId(), predictTime)); +// viewDto.setAdjData(scheduleAdjustResultService.getData(itemId, predictTime)); + legends.add(predictItem.getItemname()); + List<Double> values = new ArrayList<>(); + if (!CollectionUtils.isEmpty(viewDto.getRealData())) { + List<Double> hisValues = new ArrayList<>(); + viewDto.getRealData().forEach(item -> { + values.add(Double.parseDouble(item[1].toString())); + hisValues.add(Double.parseDouble(item[1].toString())); + }); + viewDto.setHisMax(BigDecimal.valueOf(hisValues.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, RoundingMode.HALF_UP)); + viewDto.setHisMin(BigDecimal.valueOf(hisValues.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, RoundingMode.HALF_UP)); + viewDto.setHisAvg(BigDecimal.valueOf(hisValues.stream().mapToDouble(Double::doubleValue).average().getAsDouble()).setScale(2, RoundingMode.HALF_UP)); + viewDto.setHisCumulant(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).sum()) + .divide(new BigDecimal(HOUR_MINS), 2, BigDecimal.ROUND_HALF_UP)); + } + if (!CollectionUtils.isEmpty(viewDto.getPreDataN())) { + viewDto.getPreDataN().forEach(item -> { + values.add(Double.parseDouble(item[1].toString())); + }); + } + if (!CollectionUtils.isEmpty(viewDto.getPreDataL())) { + List<Double> preValues = new ArrayList<>(); + viewDto.getPreDataL().forEach(item -> { + values.add(Double.parseDouble(item[1].toString())); + preValues.add(Double.parseDouble(item[1].toString())); + }); + viewDto.setPreMax(BigDecimal.valueOf(preValues.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, RoundingMode.HALF_UP)); + viewDto.setPreMin(BigDecimal.valueOf(preValues.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, RoundingMode.HALF_UP)); + viewDto.setPreAvg(BigDecimal.valueOf(preValues.stream().mapToDouble(Double::doubleValue).average().getAsDouble()).setScale(2, RoundingMode.HALF_UP)); + } + if (!CollectionUtils.isEmpty(viewDto.getCurData())) { + List<Double> preValues = new ArrayList<>(); + viewDto.getCurData().forEach(item -> { + values.add(Double.parseDouble(item[1].toString())); + preValues.add(Double.parseDouble(item[1].toString())); + }); + viewDto.setPreCumulant(BigDecimal.valueOf(preValues.stream().mapToDouble(Double::doubleValue).sum()) + .divide(new BigDecimal(HOUR_MINS), 2, RoundingMode.HALF_UP)); + } + if (!CollectionUtils.isEmpty(viewDto.getAdjData())) { + viewDto.getAdjData().forEach(item -> { + values.add(Double.parseDouble(item[1].toString())); + }); + } + if (!CollectionUtils.isEmpty(values)) { + viewDto.setMaxValue(BigDecimal.valueOf(values.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, RoundingMode.HALF_UP)); + viewDto.setMinValue(BigDecimal.valueOf(values.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, RoundingMode.HALF_UP)); + } + + dataViewList.add(viewDto); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + result.setStartTime(startTime); + result.setEndTime(endTime); + result.setPredictTime(predictTime); + result.setCategories(DateUtils.getTimeScale(startTime, endTime, 60)); + result.setLegend(legends); + result.setDataViewList(dataViewList); + return success(result); } } -- Gitblit v1.9.3