package com.iailab.module.ansteel.api.controller.admin; import com.iailab.framework.common.util.date.DateUtils; import com.iailab.framework.excel.core.util.ExcelUtils; import com.iailab.module.ansteel.api.dto.PowerFactorExcelDTO; import com.iailab.module.ansteel.api.dto.PowerFactorExcelReqDTO; import com.iailab.module.ansteel.api.vo.PowerExportCosVO; import com.iailab.module.ansteel.api.vo.PowerExportPqVO; import com.iailab.module.ansteel.common.utils.PowerUtil; import com.iailab.module.ansteel.power.entity.PowerNetFactorEntity; import com.iailab.module.ansteel.power.service.PowerNetFactorService; import com.iailab.module.data.api.point.DataPointApi; import com.iailab.module.data.api.point.dto.ApiPointValueDTO; import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO; import io.swagger.v3.oas.annotations.Operation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; 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 javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.math.BigDecimal; import java.util.*; /** * 文件流接口 * * @author PanZhibao * @Description * @createTime 2025年06月17日 */ @Slf4j @RestController @RequestMapping("/ansteel/api/file") public class FileController { @Autowired private PowerNetFactorService powerNetFactorService; @Autowired private DataPointApi dataPointApi; /** * 导出功率日累计 * 月报表导出:最大值、最小值、平均值、受电累计、返送累计 * 日功率因数二级界面:最大值、最小值、平均值、日功率因数 * 月功率因数二级界面:最大值、最小值、平均值、月功率因数 * * @param response * @param request * @param dto */ @PostMapping("/net-factor/export-day") @Operation(summary = "功率因数-电网拓扑功率导出(功率日累计)") public void getPowerFactorExportDay(HttpServletResponse response, HttpServletRequest request, @RequestBody PowerFactorExcelReqDTO dto) throws IOException { List exportPqList = new ArrayList<>(); List exportCos = new ArrayList<>(); if (dto.getStartTime() == null) { dto.setStartTime(new Date()); } if (dto.getEndTime() == null) { dto.setEndTime(new Date()); } Calendar calendar = Calendar.getInstance(); calendar.setTime(dto.getStartTime()); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.HOUR_OF_DAY, 0); Date startTime = calendar.getTime(); calendar.setTime(dto.getEndTime()); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.HOUR_OF_DAY, 23); Date endTime = calendar.getTime(); String pointNo = "="; String[] pointNoArr = new String[2]; String queryType = dto.getQueryType(); PowerNetFactorEntity powerNetFactorEntity = powerNetFactorService.getByNodeCode(dto.getNodeCode()); Calendar curDay = Calendar.getInstance(); curDay.setTime(startTime); do { log.info("do excel"); PowerFactorExcelDTO excelDTO = new PowerFactorExcelDTO(); switch (queryType.toUpperCase()) { case "P": pointNo = powerNetFactorEntity.getCurP(); break; case "Q": pointNo = powerNetFactorEntity.getCurQ(); break; case "DAYCOS": pointNoArr[0] = powerNetFactorEntity.getPDay(); pointNoArr[1] = powerNetFactorEntity.getQDay(); break; case "MONTHCOS": pointNoArr[0] = powerNetFactorEntity.getPMon(); pointNoArr[1] = powerNetFactorEntity.getQMon(); break; default: return; } ApiPointValueQueryDTO apiPointValueQueryDTO = new ApiPointValueQueryDTO(); apiPointValueQueryDTO.setStart(curDay.getTime()); curDay.add(Calendar.DAY_OF_YEAR, 1); apiPointValueQueryDTO.setEnd(curDay.getTime()); List valueList = new ArrayList<>(); Double lastValue = null; if (StringUtils.isNotBlank(pointNo)) { // 单点 apiPointValueQueryDTO.setPointNo(pointNo); List chartData = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO); for (ApiPointValueDTO pv : chartData) { Object[] data = new Object[2]; data[0] = DateUtils.format(pv.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND); data[1] = pv.getV(); valueList.add(pv.getV()); } } else if (StringUtils.isNotBlank(pointNoArr[0]) && StringUtils.isNotBlank(pointNoArr[1])) { List categories = new ArrayList<>(); // P/Q apiPointValueQueryDTO.setPointNo(pointNoArr[0]); List chartDataP = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO); Map dataMapP = new HashMap<>(); for (ApiPointValueDTO pv : chartDataP) { categories.add(DateUtils.format(pv.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)); dataMapP.put(DateUtils.format(pv.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND), pv.getV()); } apiPointValueQueryDTO.setPointNo(pointNoArr[1]); List chartDataQ = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO); Map dataMapQ = new HashMap<>(); for (ApiPointValueDTO pv : chartDataQ) { dataMapQ.put(DateUtils.format(pv.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND), pv.getV()); } for (String cate : categories) { Double cos = PowerUtil.calculateCos(dataMapP.get(cate), dataMapQ.get(cate)); valueList.add(cos); } } double max = 0; double min = 0; double avg = 0; if (!CollectionUtils.isEmpty(valueList)) { max = valueList.stream().mapToDouble(Double::doubleValue).max().getAsDouble(); min = valueList.stream().mapToDouble(Double::doubleValue).min().getAsDouble(); avg = valueList.stream().mapToDouble(Double::doubleValue).average().getAsDouble(); } if ("P".equals(queryType.toUpperCase()) || "Q".equals(queryType.toUpperCase())) { PowerExportPqVO evo = new PowerExportPqVO(); double sdl = 0; double fsl = 0; if (!CollectionUtils.isEmpty(valueList)) { if (powerNetFactorEntity.getCurFlag() != null && powerNetFactorEntity.getCurFlag().intValue() != 0) { for ( Double val : valueList) { if (new BigDecimal(val).compareTo(BigDecimal.ZERO) != powerNetFactorEntity.getCurFlag()) { // 未发生返送 sdl += val; } else { // 发生返送 fsl += val; } } } } evo.setSdl(new BigDecimal(sdl).setScale(2, BigDecimal.ROUND_HALF_UP)); evo.setFsl(new BigDecimal(fsl).setScale(2, BigDecimal.ROUND_HALF_UP)); evo.setMax(new BigDecimal(max).setScale(2, BigDecimal.ROUND_HALF_UP)); evo.setMin(new BigDecimal(min).setScale(2, BigDecimal.ROUND_HALF_UP)); evo.setAvg(new BigDecimal(avg).setScale(2, BigDecimal.ROUND_HALF_UP)); evo.setDate(DateUtils.format(apiPointValueQueryDTO.getStart())); exportPqList.add(evo); } else if ("DAYCOS".equals(queryType.toUpperCase()) || "MONTHCOS".equals(queryType.toUpperCase())) { double dayLast = 0; if (!CollectionUtils.isEmpty(valueList)) { dayLast = valueList.get(valueList.size() - 1); } PowerExportCosVO evo = new PowerExportCosVO(); evo.setCos(new BigDecimal(dayLast).setScale(2, BigDecimal.ROUND_HALF_UP)); evo.setMax(new BigDecimal(max).setScale(2, BigDecimal.ROUND_HALF_UP)); evo.setMin(new BigDecimal(min).setScale(2, BigDecimal.ROUND_HALF_UP)); evo.setAvg(new BigDecimal(avg).setScale(2, BigDecimal.ROUND_HALF_UP)); evo.setDate(DateUtils.format(apiPointValueQueryDTO.getStart())); exportCos.add(evo); } } while (curDay.getTime().getTime() <= endTime.getTime()); if ("P".equals(queryType.toUpperCase()) || "Q".equals(queryType.toUpperCase())) { String name = powerNetFactorEntity.getNodeName() + queryType.toUpperCase().replace("P", "有功").replace("Q", "无功") + "功率报表.xls"; ExcelUtils.write(response, name, "功率数据", PowerExportPqVO.class, exportPqList); } else if ("DAYCOS".equals(queryType.toUpperCase()) || "MONTHCOS".equals(queryType.toUpperCase())) { String name = powerNetFactorEntity.getNodeName() + queryType.toUpperCase().replace("DAYCOS", "日").replace("MONTHCOS", "月") + "功率因数报表.xls"; ExcelUtils.write(response, name, "功率数据", PowerExportPqVO.class, exportPqList); } } }