From ffb95d6b0976abc630d84b6e861daf3dfca7d9b6 Mon Sep 17 00:00:00 2001 From: mh Date: Mon, 24 Mar 2025 20:13:06 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E7=BB=BC=E5=90=88=E8=83=BD=E8=80=97?= =?UTF-8?q?=EF=BC=9A=E8=83=BD=E8=80=97=E7=BB=93=E6=9E=84=E5=88=86=E6=9E=90?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...prehensiveEnergyConsumptionController.java | 38 ++++ .../core/domain/dto/EnergyStructureDTO.java | 53 +++++ .../common/core/domain/vo/EnergyQueryVO.java | 14 +- .../ComprehensiveEnergyConsumptionMapper.java | 101 ++++++++++ ...ComprehensiveEnergyConsumptionService.java | 22 +++ ...rehensiveEnergyConsumptionServiceImpl.java | 185 ++++++++++++++++++ 6 files changed, 410 insertions(+), 3 deletions(-) create mode 100644 mh-admin/src/main/java/com/mh/web/controller/energy/ComprehensiveEnergyConsumptionController.java create mode 100644 mh-common/src/main/java/com/mh/common/core/domain/dto/EnergyStructureDTO.java create mode 100644 mh-system/src/main/java/com/mh/system/mapper/energy/ComprehensiveEnergyConsumptionMapper.java create mode 100644 mh-system/src/main/java/com/mh/system/service/energy/IComprehensiveEnergyConsumptionService.java create mode 100644 mh-system/src/main/java/com/mh/system/service/energy/impl/ComprehensiveEnergyConsumptionServiceImpl.java diff --git a/mh-admin/src/main/java/com/mh/web/controller/energy/ComprehensiveEnergyConsumptionController.java b/mh-admin/src/main/java/com/mh/web/controller/energy/ComprehensiveEnergyConsumptionController.java new file mode 100644 index 0000000..26c382e --- /dev/null +++ b/mh-admin/src/main/java/com/mh/web/controller/energy/ComprehensiveEnergyConsumptionController.java @@ -0,0 +1,38 @@ +package com.mh.web.controller.energy; + +import com.mh.common.core.controller.BaseController; +import com.mh.common.core.domain.vo.EnergyQueryVO; +import com.mh.common.core.page.TableDataInfo; +import com.mh.common.utils.DateUtils; +import com.mh.system.service.energy.IComprehensiveEnergyConsumptionService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author LJF + * @version 1.0 + * @project EEMCS + * @description 综合能管管理 + * @date 2025-03-24 15:53:13 + */ +@RestController +@RequestMapping("/energy/cec") +public class ComprehensiveEnergyConsumptionController extends BaseController { + + private final IComprehensiveEnergyConsumptionService comprehensiveEnergyConsumptionService; + + @Autowired + public ComprehensiveEnergyConsumptionController(IComprehensiveEnergyConsumptionService comprehensiveEnergyConsumptionService) { + this.comprehensiveEnergyConsumptionService = comprehensiveEnergyConsumptionService; + } + + @PostMapping("/struct") + public TableDataInfo structure(@RequestBody EnergyQueryVO vo) { + DateUtils.sysEnergyDateChange(vo); + return getDataTable(comprehensiveEnergyConsumptionService.structure(vo)); + } + +} diff --git a/mh-common/src/main/java/com/mh/common/core/domain/dto/EnergyStructureDTO.java b/mh-common/src/main/java/com/mh/common/core/domain/dto/EnergyStructureDTO.java new file mode 100644 index 0000000..a869b82 --- /dev/null +++ b/mh-common/src/main/java/com/mh/common/core/domain/dto/EnergyStructureDTO.java @@ -0,0 +1,53 @@ +package com.mh.common.core.domain.dto; + +import lombok.Getter; +import lombok.Setter; +import org.apache.commons.lang3.builder.ToStringBuilder; + +import java.math.BigDecimal; +import java.util.List; + +/** + * @author LJF + * @version 1.0 + * @project EEMCS + * @description 能源结构数据 + * @date 2025-03-24 15:40:13 + */ +@Setter +@Getter +public class EnergyStructureDTO { + + /** + * 名称 + */ + private String label; + + /** + * 能耗值 + */ + private BigDecimal eng; + + /** + * 同比 + */ + private BigDecimal yny; + + /** + * 环比 + */ + private BigDecimal ono; + + private List children; + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("label", label) + .append("eng", eng) + .append("yny", yny) + .append("ono", ono) + .append("children", children) + .toString(); + } +} diff --git a/mh-common/src/main/java/com/mh/common/core/domain/vo/EnergyQueryVO.java b/mh-common/src/main/java/com/mh/common/core/domain/vo/EnergyQueryVO.java index 804ea8d..4e2e9c1 100644 --- a/mh-common/src/main/java/com/mh/common/core/domain/vo/EnergyQueryVO.java +++ b/mh-common/src/main/java/com/mh/common/core/domain/vo/EnergyQueryVO.java @@ -14,9 +14,7 @@ import java.util.List; * @description 能耗查询公共参数 * @date 2023-12-13 16:11:24 */ -public class EnergyQueryVO extends BaseEntity implements Serializable { - - private static final long serialVersionUID = 10L; +public class EnergyQueryVO extends BaseEntity implements Cloneable { /** * 查询类型:hour,day,month,year @@ -139,4 +137,14 @@ public class EnergyQueryVO extends BaseEntity implements Serializable { .append("pageSize", pageSize) .toString(); } + + @Override + public EnergyQueryVO clone() { + try { + return (EnergyQueryVO) super.clone(); + } catch (CloneNotSupportedException e) { + throw new AssertionError(); // 通常不会发生 + } + } + } diff --git a/mh-system/src/main/java/com/mh/system/mapper/energy/ComprehensiveEnergyConsumptionMapper.java b/mh-system/src/main/java/com/mh/system/mapper/energy/ComprehensiveEnergyConsumptionMapper.java new file mode 100644 index 0000000..4a1a747 --- /dev/null +++ b/mh-system/src/main/java/com/mh/system/mapper/energy/ComprehensiveEnergyConsumptionMapper.java @@ -0,0 +1,101 @@ +package com.mh.system.mapper.energy; + +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; +import java.util.Map; + +/** + * @author LJF + * @version 1.0 + * @project EEMCS + * @description 综合能耗mapper + * @date 2025-03-24 17:24:45 + */ +@Mapper +public interface ComprehensiveEnergyConsumptionMapper { + + /** + * 查询单个表数据 + * @param curTableName + * @param isSum + * @param startTime + * @param endTime + * @param paramType + * @param systemType + * @return + */ + @Select("SELECT " + + " cpm.terminal_device_type as \"deviceType\", " + + " cpm.system_type as \"systemType\", " + + " SUM(COALESCE(dd.calc_value, 0)) as \"calcValue\" " + + "FROM " + + " ${tableName} dd " + + "JOIN " + + " collection_params_manage cpm " + + "ON " + + " cpm.mt_num = dd.device_num " + + "WHERE " + + " cpm.grade = #{grade} " + + " AND cpm.mt_is_sum = #{isSum} " + + " AND cpm.param_type = #{paramType} " + + " AND cpm.system_type = #{systemType} " + + " AND dd.cur_time BETWEEN #{startTime}::timestamp AND #{endTime}::timestamp " + + "GROUP BY " + + " cpm.terminal_device_type, " + + " cpm.system_type; ") + List> selectOneTableData(@Param("tableName") String curTableName, + @Param("isSum") int isSum, + @Param("startTime")String startTime, + @Param("endTime") String endTime, + @Param("paramType") String paramType, + @Param("systemType") String systemType, + @Param("grade") int grade); + + /** + * 查询多个表数据 + * @param curTableName + * @param lastTableName + * @param isSum + * @param startTime + * @param endTime + * @param paramType + * @param systemType + * @return + */ + @Select("SELECT " + + " cpm.terminal_device_type as \"deviceType\", " + + " cpm.system_type as \"systemType\", " + + " SUM(COALESCE(dd.calc_value, 0)) as \"calcValue\" " + + "FROM " + + " ( " + + " SELECT device_num, calc_value, cur_time FROM ${curTableName} " + + " where cur_time BETWEEN #{startTime}::timestamp AND #{endTime}::timestamp " + + " UNION ALL " + + " SELECT device_num, calc_value, cur_time FROM ${lastTableName} " + + " where cur_time BETWEEN #{startTime}::timestamp AND #{endTime}::timestamp " + + " ) dd " + + "JOIN " + + " collection_params_manage cpm " + + "ON " + + " cpm.mt_num = dd.device_num " + + "WHERE " + + " cpm.grade = #{grade} " + + " AND cpm.mt_is_sum = #{isSum} " + + " AND cpm.param_type = #{paramType} " + + " AND cpm.system_type = #{systemType} " + + " AND dd.cur_time BETWEEN #{startTime}::timestamp AND #{endTime}::timestamp " + + "GROUP BY " + + " cpm.terminal_device_type, " + + " cpm.system_type;") + List> selectMultiTableData(@Param("curTableName") String curTableName, + @Param("lastTableName") String lastTableName, + @Param("isSum") int isSum, + @Param("startTime") String startTime, + @Param("endTime") String endTime, + @Param("paramType") String paramType, + @Param("systemType") String systemType, + @Param("grade") int grade); +} diff --git a/mh-system/src/main/java/com/mh/system/service/energy/IComprehensiveEnergyConsumptionService.java b/mh-system/src/main/java/com/mh/system/service/energy/IComprehensiveEnergyConsumptionService.java new file mode 100644 index 0000000..69955ee --- /dev/null +++ b/mh-system/src/main/java/com/mh/system/service/energy/IComprehensiveEnergyConsumptionService.java @@ -0,0 +1,22 @@ +package com.mh.system.service.energy; + +import com.mh.common.core.domain.vo.EnergyQueryVO; + +import java.util.List; + +/** + * @author LJF + * @version 1.0 + * @project EEMCS + * @description 综合能耗结构数据 + * @date 2025-03-24 15:56:43 + */ +public interface IComprehensiveEnergyConsumptionService { + + /** + * 能耗结构图 + * @param vo + * @return + */ + List structure(EnergyQueryVO vo); +} diff --git a/mh-system/src/main/java/com/mh/system/service/energy/impl/ComprehensiveEnergyConsumptionServiceImpl.java b/mh-system/src/main/java/com/mh/system/service/energy/impl/ComprehensiveEnergyConsumptionServiceImpl.java new file mode 100644 index 0000000..be62623 --- /dev/null +++ b/mh-system/src/main/java/com/mh/system/service/energy/impl/ComprehensiveEnergyConsumptionServiceImpl.java @@ -0,0 +1,185 @@ +package com.mh.system.service.energy.impl; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.mh.common.core.domain.dto.EnergyStructureDTO; +import com.mh.common.core.domain.entity.CollectionParamsManage; +import com.mh.common.core.domain.entity.SysDictData; +import com.mh.common.core.domain.entity.SysParams; +import com.mh.common.core.domain.vo.EnergyQueryVO; +import com.mh.common.utils.DateUtils; +import com.mh.system.mapper.SysDictDataMapper; +import com.mh.system.mapper.SysParamsMapper; +import com.mh.system.mapper.device.CollectionParamsManageMapper; +import com.mh.system.mapper.energy.ComprehensiveEnergyConsumptionMapper; +import com.mh.system.service.energy.IComprehensiveEnergyConsumptionService; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author LJF + * @version 1.0 + * @project EEMCS + * @description 综合能耗结构服务实现 + * @date 2025-03-24 15:57:09 + */ +@Service +public class ComprehensiveEnergyConsumptionServiceImpl implements IComprehensiveEnergyConsumptionService { + + private final CollectionParamsManageMapper collectionParamsManageMapper; + + private final ComprehensiveEnergyConsumptionMapper comprehensiveEnergyConsumptionMapper; + + private final SysDictDataMapper sysDictDataMapper; + + private final SysParamsMapper sysParamsMapper; + + public ComprehensiveEnergyConsumptionServiceImpl(CollectionParamsManageMapper collectionParamsManageMapper, ComprehensiveEnergyConsumptionMapper comprehensiveEnergyConsumptionMapper, SysDictDataMapper sysDictDataMapper, SysParamsMapper sysParamsMapper) { + this.collectionParamsManageMapper = collectionParamsManageMapper; + this.comprehensiveEnergyConsumptionMapper = comprehensiveEnergyConsumptionMapper; + this.sysDictDataMapper = sysDictDataMapper; + this.sysParamsMapper = sysParamsMapper; + } + + @Override + public List structure(EnergyQueryVO vo) { + // 查询各个系统的能耗结构 + // 查询系统类型数据 + List sysTypeData = sysDictDataMapper.selectDictDataByType("sys_type"); + // 得出系统项目 + List sysParams = sysParamsMapper.selectSysParamsList(); + EnergyStructureDTO result = new EnergyStructureDTO(); + result.setLabel(sysParams.get(0).getProName()); + List result1 = new ArrayList<>(); + for (SysDictData dictData : sysTypeData) { + + EnergyStructureDTO energyStructureDTO = new EnergyStructureDTO(); + + energyStructureDTO.setLabel(dictData.getDictLabel()); + + // 获取当前系统值 + List> structData = getStructData(vo, dictData.getDictValue()); + + // 获取同比值 + String yoyStartTime = DateUtils.yoyDate(vo.getStartTime()); + String yoyEndTime = DateUtils.yoyDate(vo.getEndTime()); + EnergyQueryVO yoyVo = vo.clone(); + yoyVo.setStartTime(yoyStartTime); + yoyVo.setEndTime(yoyEndTime); + List> yoyStructData = getStructData(yoyVo, dictData.getDictValue()); + + // 获取环比值 + String hbyStartTime = DateUtils.momDate(vo.getStartTime(), vo.getTimeType(), "start"); + String hbyEndTime = DateUtils.momDate(vo.getEndTime(), vo.getTimeType(), "end"); + + EnergyQueryVO momVo = vo.clone(); + momVo.setStartTime(hbyStartTime); + momVo.setEndTime(hbyEndTime); + List> hbyStructData = getStructData(momVo, dictData.getDictValue()); + + // 拼接数据 + // 流式拼接数据,根据map的deviceType,systemType两个分组拼接数据,得出deviceType, systemType, curValue, yoyValue, hbyValue + // 得出新map,计算同比环比 + for (Map map : structData) { + String deviceType = map.get("deviceType").toString(); + String systemType = map.get("systemType").toString(); + // 当前值 + map.put("curValue", map.get("calcValue") == null ? 0 : map.get("calcValue")); + // 同比值 + map.put("yoyValue", yoyStructData.stream() + .filter(item -> item.get("deviceType").equals(deviceType) + && item.get("systemType").equals(systemType)) + .findFirst() + .map(item -> item.get("calcValue") == null ? 0 : item.get("calcValue")) + .orElse(0)); + + // 同比率计算 + BigDecimal curValue = new BigDecimal(String.valueOf(map.get("curValue"))); + BigDecimal yoyValue = new BigDecimal(String.valueOf(map.get("yoyValue"))); + map.put("yoyRate", curValue.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO + : yoyValue.divide(curValue, 2, BigDecimal.ROUND_HALF_UP)); + + // 环比值 + map.put("momValue", hbyStructData.stream() + .filter(item -> item.get("deviceType").equals(deviceType) + && item.get("systemType").equals(systemType)) + .findFirst() + .map(item -> item.get("calcValue") == null ? 0 : item.get("calcValue")) + .orElse(0)); + + // 环比率计算 + BigDecimal momValue = new BigDecimal(String.valueOf(map.get("momValue"))); + map.put("momRate", curValue.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO + : momValue.divide(curValue, 2, BigDecimal.ROUND_HALF_UP)); + } + + energyStructureDTO.setChildren(structData); + + result1.add(energyStructureDTO); + } + result.setChildren(result1); + // 查询各个设备的能耗结构 + return List.of(result); + } + + private List> getStructData(EnergyQueryVO vo, String systemType) { + String startTime = vo.getStartTime().substring(0, 4); + String endTime = vo.getEndTime().substring(0, 4); + String curTableName = "data_day"+startTime; + String lastTableName = "data_day"+endTime; + if (startTime.equals(endTime)) { + // 查询一个表 + lastTableName = "data_day"+startTime; + } else { + // 查询多个表 + lastTableName = "data_day"+startTime; + curTableName = "data_day"+endTime; + } + int isSum = queryCollectionParams(systemType, vo.getParamType(), 40); + // 根据时间范围查询 + if (lastTableName.equals(curTableName)) { + // 查询一个表 + List> map = comprehensiveEnergyConsumptionMapper.selectOneTableData(curTableName, + isSum, + vo.getStartTime(), + vo.getEndTime(), + vo.getParamType(), + systemType, + 40); + return map; + } else { + // 查询多个表 + List> map = comprehensiveEnergyConsumptionMapper.selectMultiTableData(curTableName, + lastTableName, + isSum, + vo.getStartTime(), + vo.getEndTime(), + vo.getParamType(), + systemType, + 40); + return map; + } + } + + private int queryCollectionParams(String sysType, String paramType, int grade) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("param_type", paramType) + .eq("system_type", sysType) + .eq("grade", grade); + + // 先查询总表 + queryWrapper.eq("mt_is_sum", 0); + Long count = collectionParamsManageMapper.selectCount(queryWrapper); + if (count > 0) { + return 0; + } + + // 查询分表 + return 1; + } + +}