From 9f049d2c92a1fa6ceb8d7d6680a55c87eb7a0d6a Mon Sep 17 00:00:00 2001 From: Jay <csj123456> Date: 星期一, 30 九月 2024 17:14:45 +0800 Subject: [PATCH] 预测数据分析后端代码 --- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/DmModuleService.java | 2 iailab-module-model/iailab-module-model-biz/src/main/resources/mapper/pre/DmModuleDao.xml | 11 ++ iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/ModelServiceApplication.java | 20 +-- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/controller/admin/MmPredictItemController.java | 178 ++++++++++++++++++++++++++++++++++- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/dao/DmModuleDao.java | 4 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/DmModuleServiceImpl.java | 10 ++ iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/MmPredictItemService.java | 5 + iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmPredictItemServiceImpl.java | 15 +++ iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/vo/PreItemOptionVO.java | 22 ++++ 9 files changed, 248 insertions(+), 19 deletions(-) diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/ModelServiceApplication.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/ModelServiceApplication.java index 8376d57..10db3e6 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/ModelServiceApplication.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/ModelServiceApplication.java @@ -1,16 +1,10 @@ package com.iailab; -import iail.mdk.model.common.Environment; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import com.iail.IAILMDK; -import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.scheduling.annotation.EnableAsync; -import java.io.File; -import java.io.InputStream; -import java.util.Objects; import java.util.Properties; @EnableAsync @@ -21,13 +15,13 @@ //加载动态链接库 try { Properties properties = new Properties(); - InputStream in = ModelServiceApplication.class.getClassLoader().getResourceAsStream("iailmdk.properties"); - properties.load(in); - String mdkInitPath = properties.getProperty("mdk-init-path"); - System.out.println("mdkInitPath=" + mdkInitPath); - System.load(mdkInitPath + File.separator + "IAIL.MDK.Mid.Windows.dll"); - Environment env = new Environment(); - env.init(); +// InputStream in = ModelServiceApplication.class.getClassLoader().getResourceAsStream("iailmdk.properties"); +// properties.load(in); +// String mdkInitPath = properties.getProperty("mdk-init-path"); +// System.out.println("mdkInitPath=" + mdkInitPath); +// System.load(mdkInitPath + File.separator + "IAIL.MDK.Mid.Windows.dll"); +// Environment env = new Environment(); +// env.init(); } catch (Exception e) { e.printStackTrace(); System.out.println("动态链接库IAIL.MDK.Mid.Windows.dll初始化失败"); 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 b1e1f28..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 @@ -3,25 +3,32 @@ 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.MmPredictItemPageReqVO; -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 javax.annotation.security.PermitAll; import java.io.File; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.*; import static com.iailab.framework.common.pojo.CommonResult.success; @@ -29,6 +36,7 @@ * @author PanZhibao * @date 2021年04月26日 14:42 */ +@Slf4j @RestController @RequestMapping("/model/pre/item") public class MmPredictItemController { @@ -42,6 +50,13 @@ @Autowired private MmPredictItemService mmPredictItemService; + @Autowired + private DmModuleService dmModuleService; + + @Autowired + private MmItemOutputService mmItemOutputService; + + private int HOUR_MINS = 60; /** * 预测项列表 */ @@ -122,4 +137,155 @@ result.put("originalFilename", file.getOriginalFilename().replace(CommonConstant.MDK_SUFFIX, "")); return success(result); } + + @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); + } } diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/dao/DmModuleDao.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/dao/DmModuleDao.java index 3b992a7..d033172 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/dao/DmModuleDao.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/dao/DmModuleDao.java @@ -8,6 +8,8 @@ import org.apache.ibatis.annotations.Mapper; import com.iailab.framework.tenant.core.db.dynamic.TenantDS; +import java.util.List; + /** * @author PanZhibao * @date 2021年04月23日 8:30 @@ -16,6 +18,8 @@ @Mapper public interface DmModuleDao extends BaseMapperX<DmModuleEntity> { + List<DmModuleEntity> getModuleByItemId(String itemId); + default PageResult<DmModuleEntity> selectPage(DmModulePageReqVO reqVO) { return selectPage(reqVO, new LambdaQueryWrapperX<DmModuleEntity>() .likeIfPresent(DmModuleEntity::getModulename, reqVO.getModulename()) diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/DmModuleService.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/DmModuleService.java index 2f02af2..ea7d041 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/DmModuleService.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/DmModuleService.java @@ -28,4 +28,6 @@ List<DmModuleEntity> getModuleByModuleType(String moduletype); DmModuleEntity info(String id); + + DmModuleEntity getModuleByItemId(String itemId); } diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/MmPredictItemService.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/MmPredictItemService.java index 8a7d4bf..ef80336 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/MmPredictItemService.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/MmPredictItemService.java @@ -8,6 +8,7 @@ import com.iailab.module.model.mdk.vo.ItemVO; import com.iailab.module.model.mdk.vo.MergeItemVO; +import java.util.Date; import java.util.List; import java.util.Map; @@ -38,4 +39,8 @@ void update(MmPredictItemDTO mmPredictItemDTO); List<MmPredictItemRespVO> list(Map<String, Object> params); + + MmPredictItemEntity getById(String id); + + List<Object[]> getHisData(String itemId, Date startTime, Date endTime); } diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/DmModuleServiceImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/DmModuleServiceImpl.java index 5b678c8..962f7e8 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/DmModuleServiceImpl.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/DmModuleServiceImpl.java @@ -10,6 +10,7 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; import java.util.*; @@ -40,6 +41,15 @@ return dmModuleDao.selectById(id); } + @Override + public DmModuleEntity getModuleByItemId(String itemId) { + List<DmModuleEntity> list = dmModuleDao.getModuleByItemId(itemId); + if (CollectionUtils.isEmpty(list)) { + return null; + } + return list.get(0); + } + private QueryWrapper<DmModuleEntity> getWrapper(Map<String, Object> params) { String modulename = (String) params.get("modulename"); String moduletype = (String) params.get("moduletype"); diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmPredictItemServiceImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmPredictItemServiceImpl.java index 344df0c..f0ceded 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmPredictItemServiceImpl.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmPredictItemServiceImpl.java @@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.iailab.framework.common.pojo.PageResult; import com.iailab.framework.common.util.object.BeanUtils; +import com.iailab.module.data.api.point.DataPointApi; import com.iailab.module.model.mcs.pre.dao.MmPredictItemDao; import com.iailab.module.model.mcs.pre.dto.MmPredictItemDTO; import com.iailab.module.model.mcs.pre.entity.*; @@ -58,6 +59,9 @@ @Autowired private MmPredictItemDao mmPredictItemDao; + @Autowired + private DataPointApi dataPointApi; + @Override public PageResult<MmPredictItemRespVO> queryPage(MmPredictItemPageReqVO reqVO) { IPage<MmPredictItemRespVO> page = mmPredictItemDao.selectPage(reqVO); @@ -70,6 +74,17 @@ return mmPredictItemDao.queryList(params); } + @Override + public MmPredictItemEntity getById(String id) { + return mmPredictItemDao.selectById(id); + } + + @Override + public List<Object[]> getHisData(String itemId, Date startTime, Date endTime) { + List<Object[]> result = new ArrayList<>(); + return result; + } + @DSTransactional(rollbackFor = Exception.class) @Override public void add(MmPredictItemDTO mmPredictItemDto) { diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/vo/PreItemOptionVO.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/vo/PreItemOptionVO.java new file mode 100644 index 0000000..5a67166 --- /dev/null +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/vo/PreItemOptionVO.java @@ -0,0 +1,22 @@ +package com.iailab.module.model.mcs.pre.vo; + +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +/** + * @author PanZhibao + * @Description + * @createTime 2024年07月31日 + */ +@Data +public class PreItemOptionVO implements Serializable { + private static final long serialVersionUID = 1L; + + private String id; + + private String label; + + private List<PreItemOptionVO> children; +} \ No newline at end of file diff --git a/iailab-module-model/iailab-module-model-biz/src/main/resources/mapper/pre/DmModuleDao.xml b/iailab-module-model/iailab-module-model-biz/src/main/resources/mapper/pre/DmModuleDao.xml new file mode 100644 index 0000000..e4827dd --- /dev/null +++ b/iailab-module-model/iailab-module-model-biz/src/main/resources/mapper/pre/DmModuleDao.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> +<mapper namespace="com.iailab.module.model.mcs.pre.dao.DmModuleDao"> + + <select id="getModuleByItemId" resultType="com.iailab.module.model.mcs.pre.entity.DmModuleEntity"> + select t1.* + from T_DM_MODULE t1 + left join T_DM_MODULE_ITEM t2 on t2.MODULEID = t1.ID + where t2.ITEMID = #{value} + </select> +</mapper> \ No newline at end of file -- Gitblit v1.9.3