From 788f82742eb94c4732c854b52d14493390763846 Mon Sep 17 00:00:00 2001 From: mh Date: Sat, 25 Jan 2025 14:17:13 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E8=83=BD=E8=80=97=E5=88=86=E6=9E=90?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../energy/SysEnergyAnalyzeController.java | 235 ++++++++++++++++++ .../mapper/energy/EnergyAnalyzeMapper.java | 206 +++++++++++++++ 2 files changed, 441 insertions(+) create mode 100644 mh-admin/src/main/java/com/mh/web/controller/energy/SysEnergyAnalyzeController.java create mode 100644 mh-system/src/main/java/com/mh/system/mapper/energy/EnergyAnalyzeMapper.java diff --git a/mh-admin/src/main/java/com/mh/web/controller/energy/SysEnergyAnalyzeController.java b/mh-admin/src/main/java/com/mh/web/controller/energy/SysEnergyAnalyzeController.java new file mode 100644 index 0000000..cfafe8c --- /dev/null +++ b/mh-admin/src/main/java/com/mh/web/controller/energy/SysEnergyAnalyzeController.java @@ -0,0 +1,235 @@ +package com.mh.web.controller.energy; + +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.util.ListUtils; +import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; +import com.mh.common.core.controller.BaseController; +import com.mh.common.core.domain.AjaxResult; +import com.mh.common.core.domain.ColumnFilter; +import com.mh.common.core.domain.entity.SysDictData; +import com.mh.common.core.domain.vo.EnergyQueryVO; +import com.mh.common.core.page.TableDataInfo; +import com.mh.system.service.energy.EnergyAnalyzeService; +import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; +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; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * @author LJF + * @version 1.0 + * @project EEMCS + * @description 系统能源查询 + * @date 2023-12-13 09:39:02 + */ +@Slf4j +@RestController +@RequestMapping("/energy") +public class SysEnergyAnalyzeController extends BaseController { + + @Resource + private EnergyAnalyzeService energyAnalyzeService; + + @Resource + private DeviceManageService deviceManageService; + + /** + * 整体机房设备组图形,表格数据查询(公用一个接口) + * @param vo + * @return + */ + @PostMapping("/sys/analyze") + public AjaxResult sysAnalyze(@RequestBody EnergyQueryVO vo) { + startPage(); + return AjaxResult.success(energyAnalyzeService.sysAnalyze(vo)); + } + + /** + * 整体机房表格数据查询 + * @param vo + * @param response + */ + @PostMapping("/sys/analyze/export") + public void exportAnalyzeTable(@RequestBody EnergyQueryVO vo, HttpServletResponse response) { + ExcelWriter build = null; + try { + // 文件名 + String fileName = "机房整体能耗分析表.xlsx"; + // 设置响应格式 + response.setContentType("application/vdn.ms-excel;charset=utf-8"); + response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\""); + response.setCharacterEncoding("UTF-8"); + // 从数据库获取数据 + JSONObject resultJson = energyAnalyzeService.sysAnalyze(vo); + List all = resultJson.getList("all", ColumnFilter.class); + List chiller = resultJson.getList("chiller", ColumnFilter.class); + List chillerPump = resultJson.getList("chillerPump", ColumnFilter.class); + List coolPump = resultJson.getList("coolPump", ColumnFilter.class); + List coolTower = resultJson.getList("coolTower", ColumnFilter.class); + // 时间范围 + String timerRange = vo.getStartTime() + "至" + vo.getEndTime(); + + List> head = ListUtils.newArrayList(); + List head0 = ListUtils.newArrayList(); + head0.add("日期"); + List head1 = ListUtils.newArrayList(); + head1.add("设备类型"); + List head2 = ListUtils.newArrayList(); + head2.add("耗电量"); + List head3 = ListUtils.newArrayList(); + head3.add("占比"); + head.add(head0); + head.add(head1); + head.add(head2); + head.add(head3); + List> excelDataList = ListUtils.newArrayList(); + + createExcelData(all, timerRange, excelDataList); + build = EasyExcel.write(response.getOutputStream()).build(); + ExcelWriterSheetBuilder allExcel = EasyExcel.writerSheet(0, "机房整体能耗").head(head); + build.write(excelDataList, allExcel.build()); + + createExcelData(chiller, timerRange, excelDataList); + ExcelWriterSheetBuilder chillerExcel = EasyExcel.writerSheet(1, "主机能耗").head(head); + build.write(excelDataList, chillerExcel.build()); + + createExcelData(chillerPump, timerRange, excelDataList); + ExcelWriterSheetBuilder chillerPumpExcel = EasyExcel.writerSheet(2, "冷冻水泵能耗").head(head); + build.write(excelDataList, chillerPumpExcel.build()); + + createExcelData(coolPump, timerRange, excelDataList); + ExcelWriterSheetBuilder coolPumpExcel = EasyExcel.writerSheet(3, "冷却水泵能耗").head(head); + build.write(excelDataList, coolPumpExcel.build()); + + createExcelData(coolTower, timerRange, excelDataList); + ExcelWriterSheetBuilder coolTowerExcel = EasyExcel.writerSheet(4, "冷却塔能耗").head(head); + build.write(excelDataList, coolTowerExcel.build()); + + } catch (Exception e) { + log.error("导出异常==>", e); + throw new RuntimeException("下载报表异常"); + } finally { + if (null != build) { + build.finish(); + } + } + } + + /** + * 产生excel数据 + * @param all + * @param timerRange + * @param excelDataList + */ + private static void createExcelData(List all, String timerRange, List> excelDataList) { + excelDataList.clear(); + if (null != all && all.size() > 0) { + double sum = all.stream().mapToDouble(value -> Double.parseDouble(value.getValue())).sum(); + BigDecimal totalSum = new BigDecimal(sum); + if (totalSum.compareTo(BigDecimal.ZERO) > 0) { + for (ColumnFilter columnFilter : all) { + List list1 = ListUtils.newArrayList(); + list1.add(timerRange); + list1.add(columnFilter.getName()); + list1.add(columnFilter.getValue()); + list1.add(new BigDecimal(columnFilter.getValue()).multiply(new BigDecimal(100)).divide(totalSum, 2, RoundingMode.HALF_UP) + "%"); + excelDataList.add(list1); + } + } + } + } + + /** + * 设备单组图形,表格数据查询 + * @param vo + * @return + */ + @PostMapping("/sys/analyze/device") + public HttpResult deviceAnalyze(@RequestBody EnergyQueryVO vo) { + return energyAnalyzeService.deviceAnalyze(vo); + } + + /** + * 设备单组图形,表格数据查询 + * @param vo + * @return + */ + @PostMapping("/sys/analyze/device/export") + public void deviceAnalyzeExport(@RequestBody EnergyQueryVO vo, HttpServletResponse response) { + // 文件名 + try { + String fileName = "设备能耗分析.xlsx"; + // 从数据库获取数据 + PageResult data = (PageResult)energyAnalyzeService.deviceAnalyze(vo).getData(); + List> dataList = (List>) data.getContent(); + if (dataList != null) { + // 设置响应格式 + response.setContentType("application/vdn.ms-excel;charset=utf-8"); + response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\""); + response.setCharacterEncoding("UTF-8"); + + // 根据deviceNum获取有多少列数据 + List deviceManageEntities = deviceManageService.queryAllDevice(); + + List> head = ListUtils.newArrayList(); + List titleArr = new ArrayList<>(); + List timeStr = new ArrayList<>(); + List columnDataList = new ArrayList<>(); + for (Map.Entry map : dataList) { + if (map.getKey().equals("title")) { + List head0 = ListUtils.newArrayList(); + head0.add("日期"); + head.add(head0); + titleArr = (List) map.getValue(); + for (String columnValue : titleArr) { + Optional device = deviceManageEntities.stream().filter(val -> val.getDeviceNum().equals(columnValue)).findFirst(); + String columnName = columnValue; + if (device.isPresent()) { + DeviceManageEntity deviceManageEntity = device.get(); + columnName = deviceManageEntity.getRemark(); + } + List head1 = ListUtils.newArrayList(); + head1.add(columnName); + head.add(head1); + } + } + if (map.getKey().equals("timeStr")) { + timeStr = (List) map.getValue(); + } + if (map.getKey().equals("dataList")) { + columnDataList = (List) map.getValue(); + } + } + List> excelDataList = ListUtils.newArrayList(); + for (int i = 0; i < timeStr.size(); i++) { + List list1 = ListUtils.newArrayList(); + list1.add(timeStr.get(i)); + for (String columnValue : titleArr) { + int index = i; + columnDataList.stream().filter(val -> val.getName().equals(columnValue)).findFirst().ifPresent(val -> list1.add(val.getValue()[index])); + } + excelDataList.add(list1); + } + // 内容格式 + EasyExcel.write(response.getOutputStream()).head(head).sheet("设备能耗分析").doWrite(excelDataList); + } + } catch (Exception e) { + log.error("导出异常==>", e); + throw new RuntimeException("下载报表异常"); + } + } + +} diff --git a/mh-system/src/main/java/com/mh/system/mapper/energy/EnergyAnalyzeMapper.java b/mh-system/src/main/java/com/mh/system/mapper/energy/EnergyAnalyzeMapper.java new file mode 100644 index 0000000..68104fc --- /dev/null +++ b/mh-system/src/main/java/com/mh/system/mapper/energy/EnergyAnalyzeMapper.java @@ -0,0 +1,206 @@ +package com.mh.system.mapper.energy; + +import com.mh.common.core.domain.ColumnFilter; +import com.mh.common.core.domain.entity.DeviceTypeEnergyEntity; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * @author LJF + * @version 1.0 + * @project NewZhujiang_Server + * @description 能耗分析mapper + * @date 2023-12-14 14:04:48 + */ +@Mapper +public interface EnergyAnalyzeMapper { + + /** + * 跨表查询操作 + * @param startTime 开始时间 + * @param endTime 结束时间 + * @param lastTableName 上一个表名 + * @param curTableName 当前表名 + * @return + */ + @Select("") + List queryManyTable(@Param("startTime") String startTime, + @Param("endTime") String endTime, + @Param("lastTableName") String lastTableName, + @Param("curTableName") String curTableName, + @Param("len") String dateLen, + @Param("paramType") String paramType); + + /** + * 单表查询操作 + * @param startTime 开始时间 + * @param endTime 结束时间 + * @param lastTableName 上一个表名 + * @param curTableName 当前表名 + * @return + */ + @Select("") + List queryOneTable(@Param("startTime") String startTime, + @Param("endTime") String endTime, + @Param("lastTableName") String lastTableName, + @Param("curTableName") String curTableName, + @Param("len") String dateLen, + @Param("paramType") String paramType); + + @Select("" ) + List queryDeviceOneTable(@Param("startTime") String startTime, + @Param("endTime") String endTime, + @Param("lastTableName") String lastTableName, + @Param("curTableName") String curTableName, + @Param("len") String dateLen, + @Param("params") List params); + + @Select("" ) + List queryDeviceManyTable(@Param("startTime") String startTime, + @Param("endTime") String endTime, + @Param("lastTableName") String lastTableName, + @Param("curTableName") String curTableName, + @Param("len") String dateLen, + @Param("params") List params); +}