2 changed files with 441 additions and 0 deletions
@ -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<ColumnFilter> all = resultJson.getList("all", ColumnFilter.class); |
||||
List<ColumnFilter> chiller = resultJson.getList("chiller", ColumnFilter.class); |
||||
List<ColumnFilter> chillerPump = resultJson.getList("chillerPump", ColumnFilter.class); |
||||
List<ColumnFilter> coolPump = resultJson.getList("coolPump", ColumnFilter.class); |
||||
List<ColumnFilter> coolTower = resultJson.getList("coolTower", ColumnFilter.class); |
||||
// 时间范围
|
||||
String timerRange = vo.getStartTime() + "至" + vo.getEndTime(); |
||||
|
||||
List<List<String>> head = ListUtils.newArrayList(); |
||||
List<String> head0 = ListUtils.newArrayList(); |
||||
head0.add("日期"); |
||||
List<String> head1 = ListUtils.newArrayList(); |
||||
head1.add("设备类型"); |
||||
List<String> head2 = ListUtils.newArrayList(); |
||||
head2.add("耗电量"); |
||||
List<String> head3 = ListUtils.newArrayList(); |
||||
head3.add("占比"); |
||||
head.add(head0); |
||||
head.add(head1); |
||||
head.add(head2); |
||||
head.add(head3); |
||||
List<List<Object>> 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<ColumnFilter> all, String timerRange, List<List<Object>> 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<Object> 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<PageResult> 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<Map.Entry<String, Object>> dataList = (List<Map.Entry<String, Object>>) 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<DeviceManageEntity> deviceManageEntities = deviceManageService.queryAllDevice(); |
||||
|
||||
List<List<String>> head = ListUtils.newArrayList(); |
||||
List<String> titleArr = new ArrayList<>(); |
||||
List<String> timeStr = new ArrayList<>(); |
||||
List<ColumnData> columnDataList = new ArrayList<>(); |
||||
for (Map.Entry<String, Object> map : dataList) { |
||||
if (map.getKey().equals("title")) { |
||||
List<String> head0 = ListUtils.newArrayList(); |
||||
head0.add("日期"); |
||||
head.add(head0); |
||||
titleArr = (List<String>) map.getValue(); |
||||
for (String columnValue : titleArr) { |
||||
Optional<DeviceManageEntity> device = deviceManageEntities.stream().filter(val -> val.getDeviceNum().equals(columnValue)).findFirst(); |
||||
String columnName = columnValue; |
||||
if (device.isPresent()) { |
||||
DeviceManageEntity deviceManageEntity = device.get(); |
||||
columnName = deviceManageEntity.getRemark(); |
||||
} |
||||
List<String> head1 = ListUtils.newArrayList(); |
||||
head1.add(columnName); |
||||
head.add(head1); |
||||
} |
||||
} |
||||
if (map.getKey().equals("timeStr")) { |
||||
timeStr = (List<String>) map.getValue(); |
||||
} |
||||
if (map.getKey().equals("dataList")) { |
||||
columnDataList = (List<ColumnData>) map.getValue(); |
||||
} |
||||
} |
||||
List<List<Object>> excelDataList = ListUtils.newArrayList(); |
||||
for (int i = 0; i < timeStr.size(); i++) { |
||||
List<Object> 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("下载报表异常"); |
||||
} |
||||
} |
||||
|
||||
} |
@ -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("<script>" + |
||||
"select " + |
||||
" dh.calc_value, " + |
||||
" dh.device_num, " + |
||||
" dm.remark, " + |
||||
" dm.grade " + |
||||
"from " + |
||||
" ${lastTableName} dh " + |
||||
"join device_manage dm " + |
||||
"on " + |
||||
" dh.device_num = dm.device_num " + |
||||
"where " + |
||||
" dh.cur_time >= #{startTime} " + |
||||
" and dh.cur_time <= #{endTime} " + |
||||
" and dm.grade < 40 " + |
||||
" and dm.device_type like '%meter%' " + |
||||
" and dm.status = 0" + |
||||
"<if test='paramType != null and paramType != \"\"'>" + |
||||
" and dh.device_type = #{paramType}" + |
||||
"</if>" + |
||||
"union all " + |
||||
"select " + |
||||
" dh.calc_value, " + |
||||
" dh.device_num, " + |
||||
" dm.remark, " + |
||||
" dm.grade " + |
||||
"from " + |
||||
" ${curTableName} dh " + |
||||
"join device_manage dm " + |
||||
"on " + |
||||
" dh.device_num = dm.device_num " + |
||||
"where " + |
||||
" dh.cur_time >= #{startTime} " + |
||||
" and dh.cur_time <= #{endTime} " + |
||||
" and dm.grade < 40 " + |
||||
" and dm.device_type like '%meter%' " + |
||||
" and dm.status = 0" + |
||||
"<if test='paramType != null and paramType != \"\"'>" + |
||||
" and dh.device_type = #{paramType}" + |
||||
"</if>" + |
||||
"</script>") |
||||
List<DeviceTypeEnergyEntity> 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("<script>" + |
||||
"select " + |
||||
" dh.calc_value, " + |
||||
" dh.device_num, " + |
||||
" dm.remark, " + |
||||
" dm.grade " + |
||||
"from " + |
||||
" ${lastTableName} dh " + |
||||
"join device_manage dm " + |
||||
"on " + |
||||
" dh.device_num = dm.device_num " + |
||||
"where " + |
||||
" dh.cur_time >= #{startTime} " + |
||||
" and dh.cur_time <= #{endTime} " + |
||||
" and dm.grade < 40 " + |
||||
" and dm.device_type like '%meter%' " + |
||||
" and dm.status = 0" + |
||||
"<if test='paramType != null and paramType != \"\"'>" + |
||||
" and dh.device_type = #{paramType}" + |
||||
"</if>" + |
||||
"</script>") |
||||
List<DeviceTypeEnergyEntity> 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("<script>" + |
||||
"select " + |
||||
" sum(isnull(dh.calc_value, 0)) as calcValue, " + |
||||
" convert(varchar(${len}), " + |
||||
" dh.cur_time, " + |
||||
" 120) as timeStr, " + |
||||
" dh.device_num as deviceNum, " + |
||||
" dm.remark " + |
||||
"from " + |
||||
" ${lastTableName} dh " + |
||||
"join device_manage dm " + |
||||
"on " + |
||||
" dh.device_num = dm.device_num " + |
||||
"where " + |
||||
" dh.cur_time >= #{startTime} " + |
||||
" and dh.cur_time <= #{endTime} " + |
||||
" and dh.device_num in " + |
||||
"<foreach collection='params' item='item' open='(' separator=',' close=')'>" + |
||||
" #{item.value} " + |
||||
"</foreach>" + |
||||
"group by " + |
||||
" convert(varchar(${len}), " + |
||||
" dh.cur_time, " + |
||||
" 120), " + |
||||
" dh.device_num, " + |
||||
" dm.remark " + |
||||
"</script>" ) |
||||
List<DeviceTypeEnergyEntity> queryDeviceOneTable(@Param("startTime") String startTime, |
||||
@Param("endTime") String endTime, |
||||
@Param("lastTableName") String lastTableName, |
||||
@Param("curTableName") String curTableName, |
||||
@Param("len") String dateLen, |
||||
@Param("params") List<ColumnFilter> params); |
||||
|
||||
@Select("<script>" + |
||||
"select " + |
||||
" sum(isnull(dh.calc_value, 0)) as calcValue, " + |
||||
" convert(varchar(${len}), " + |
||||
" dh.cur_time, " + |
||||
" 120) as timeStr, " + |
||||
" dh.device_num as deviceNum, " + |
||||
" dm.remark " + |
||||
"from " + |
||||
" ${lastTableName} dh " + |
||||
"join device_manage dm " + |
||||
"on " + |
||||
" dh.device_num = dm.device_num " + |
||||
"where " + |
||||
" dh.cur_time >= #{startTime} " + |
||||
" and dh.cur_time <= #{endTime} " + |
||||
" and dh.device_num in " + |
||||
"<foreach collection='params' item='item' open='(' separator=',' close=')'>" + |
||||
" #{item.value} " + |
||||
"</foreach>" + |
||||
"group by " + |
||||
" convert(varchar(${len}), " + |
||||
" dh.cur_time, " + |
||||
" 120), " + |
||||
" dh.device_num, " + |
||||
" dm.remark " + |
||||
"union all " + |
||||
"select " + |
||||
" sum(isnull(dh.calc_value, 0)) as calcValue, " + |
||||
" convert(varchar(${len}), " + |
||||
" dh.cur_time, " + |
||||
" 120) as timeStr, " + |
||||
" dh.device_num as deviceNum, " + |
||||
" dm.remark " + |
||||
"from " + |
||||
" ${curTableName} dh " + |
||||
"join device_manage dm " + |
||||
"on " + |
||||
" dh.device_num = dm.device_num " + |
||||
"where " + |
||||
" dh.cur_time >= #{startTime} " + |
||||
" and dh.cur_time <= #{endTime} " + |
||||
" and dh.device_num in " + |
||||
"<foreach collection='params' item='item' open='(' separator=',' close=')'>" + |
||||
" #{item.value} " + |
||||
"</foreach>" + |
||||
"group by " + |
||||
" convert(varchar(${len}), " + |
||||
" dh.cur_time, " + |
||||
" 120), " + |
||||
" dh.device_num, " + |
||||
" dm.remark " + |
||||
"</script>" ) |
||||
List<DeviceTypeEnergyEntity> queryDeviceManyTable(@Param("startTime") String startTime, |
||||
@Param("endTime") String endTime, |
||||
@Param("lastTableName") String lastTableName, |
||||
@Param("curTableName") String curTableName, |
||||
@Param("len") String dateLen, |
||||
@Param("params") List<ColumnFilter> params); |
||||
} |
Loading…
Reference in new issue