鞍钢鲅鱼圈能源管控系统后端代码
潘志宝
4 天以前 a01eec9f761ffcc11d42da0a82595e720427dd68
ansteel-biz/src/main/java/com/iailab/module/ansteel/api/controller/admin/PowerController.java
@@ -8,11 +8,15 @@
import com.iailab.framework.common.util.date.DateUtils;
import com.iailab.framework.common.util.object.BeanUtils;
import com.iailab.framework.common.util.object.ConvertUtils;
import com.iailab.framework.excel.core.util.ExcelUtils;
import com.iailab.module.ansteel.api.dto.*;
import com.iailab.module.ansteel.api.vo.PowerCapacitorHisPageReqVO;
import com.iailab.module.ansteel.api.vo.PowerExportCosVO;
import com.iailab.module.ansteel.api.vo.PowerExportPqVO;
import com.iailab.module.ansteel.api.vo.PowerMaxDemandMainPageReqVO;
import com.iailab.module.ansteel.common.constant.CommonConstant;
import com.iailab.module.ansteel.common.enums.TransferTypeEnum;
import com.iailab.module.ansteel.common.enums.ProcessConfDataTypeEnum;
import com.iailab.module.ansteel.common.utils.CalendarUtil;
import com.iailab.module.ansteel.common.utils.DecimalUtil;
import com.iailab.module.ansteel.common.utils.PowerUtil;
import com.iailab.module.ansteel.power.entity.*;
@@ -26,19 +30,18 @@
import com.iailab.module.model.api.mcs.dto.PredictLastValueReqVO;
import com.iailab.module.model.api.mdk.MdkApi;
import com.iailab.module.model.api.mdk.dto.MdkScheduleReqDTO;
import com.iailab.module.model.api.mdk.dto.MdkScheduleRespDTO;
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.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import java.lang.reflect.Array;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static com.iailab.framework.common.pojo.CommonResult.error;
@@ -114,10 +117,10 @@
    public static final String TIME = "time";
    private String pointNo = "F0000201825";
    //private String pointNo = "F0000201825";
    @GetMapping("/net-factor/list")
    @Operation(summary = "功率因数-电网拓扑")
    @Operation(summary = "功率因数-电网拓扑列表")
    public CommonResult<List<PowerNetFactorDTO>> getPowerNetFactorList(@RequestParam Map<String, Object> params) {
        log.info("功率因数电网拓扑");
        List<PowerNetFactorDTO> result = new ArrayList<>();
@@ -300,6 +303,13 @@
            params.put("groupName", entity.getNodeName());
        }
        list = powerNetDropdownService.list(params);
        // 追加公共 2025-06-18
        Map<String, Object> paramsCom = new HashMap<>();
        paramsCom.put("groupName", "COMMON1");
        List<PowerNetDropdownEntity> comList = powerNetDropdownService.list(paramsCom);
        list.addAll(comList);
        List<String> points = list.stream().map(item -> {
            return item.getCurCos();
        }).collect(Collectors.toList());
@@ -351,7 +361,7 @@
            Map<String, Object> pointsRealValue = dataPointApi.queryPointsRealValue(points);
            if (pointsRealValue.get(entity.getCurQ()) != null) {
                BigDecimal curQ = new BigDecimal(pointsRealValue.get(entity.getCurQ()).toString());
                if (curQ.compareTo(BigDecimal.ZERO) == entity.getCurFlag()) {
                if (entity.getCurFlag() != null && curQ.compareTo(BigDecimal.ZERO) == entity.getCurFlag()) {
                    message = entity.getNodeName() + "发生无功返送;";
                }
            }
@@ -704,6 +714,8 @@
                if (maxdemandMainEntity != null) {
                    demandDTO.setMaxDemand(maxdemandMainEntity.getMaxDemand());
                    demandDTO.setOccurTime(maxdemandMainEntity.getOccurTime());
                } else {
                    demandDTO.setMaxDemand(BigDecimal.ZERO);
                }
            }
            result.add(demandDTO);
@@ -726,9 +738,29 @@
            log.info("PowerDemandEntity is null");
            return success(result);
        }
        List<PowerNetDropdownEntity> list = new ArrayList<>();
        Map<String, Object> params0 = new HashMap<>();
        params0.put("groupName", entity.getName());
        List<PowerNetDropdownEntity> list = powerNetDropdownService.list(params0);
        List<PowerNetDropdownEntity> list0 = powerNetDropdownService.list(params0);
        if (!CollectionUtils.isEmpty(list0)) {
            list.addAll(list0);
        }
        // 所有关口进线的二级界面(表格/曲线)均增加(CCPP/135/CDQ/TRT/余热)的选项。
        Map<String, Object> params1 = new HashMap<>();
        params1.put("groupName", "COMMON1");
        List<PowerNetDropdownEntity> list1 = powerNetDropdownService.list(params1);
        if (!CollectionUtils.isEmpty(list1)) {
            list.addAll(list1);
        }
        Map<String, Object> params2 = new HashMap<>();
        params2.put("groupName", "COMMON2");
        List<PowerNetDropdownEntity> list2 = powerNetDropdownService.list(params2);
        if (!CollectionUtils.isEmpty(list2)) {
            list.addAll(list2);
        }
        List<String> points = list.stream().map(item -> {
            return item.getCurCos();
        }).collect(Collectors.toList());
@@ -843,6 +875,175 @@
        return success(result);
    }
    /**
     * 导出功率日累计
     * 月报表导出:最大值、最小值、平均值、受电累计、返送累计
     * 日功率因数二级界面:最大值、最小值、平均值、日功率因数
     * 月功率因数二级界面:最大值、最小值、平均值、月功率因数
     *
     * @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<PowerExportPqVO> exportPqList = new ArrayList<>();
        List<PowerExportCosVO> 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<Double> valueList = new ArrayList<>();
            Double lastValue = null;
            if (StringUtils.isNotBlank(pointNo)) {
                // 单点
                apiPointValueQueryDTO.setPointNo(pointNo);
                List<ApiPointValueDTO> 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<String> categories = new ArrayList<>();
                // P/Q
                apiPointValueQueryDTO.setPointNo(pointNoArr[0]);
                List<ApiPointValueDTO> chartDataP = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
                Map<String, Double> 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<ApiPointValueDTO> chartDataQ = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
                Map<String, Double> 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);
        }
    }
    @PostMapping("/net-factor/history")
    @Operation(summary = "功率因数-电网拓扑功率历史")
    public CommonResult<PowerHistoryDTO> getPowerHistoryData(@RequestBody PowerNetFactorHisReqDTO dto) {
@@ -871,6 +1072,7 @@
        log.info("开始查询,");
        ApiPointValueQueryDTO apiPointValueQueryDTO = new ApiPointValueQueryDTO();
        String pointNo = "";
        String[] pointNoArr = new String[2];
        switch (queryType.toUpperCase()) {
            case "P":
                pointNo = powerNetFactorQuery.getCurP();
@@ -881,11 +1083,22 @@
            case "COS":
                pointNo = powerNetFactorQuery.getCurCos();
                break;
            case "DAYCOS":
                pointNoArr[0] = powerNetFactorEntity.getPDay();
                pointNoArr[1] = powerNetFactorEntity.getQDay();
                break;
            case "MONTHCOS":
                pointNoArr[0] = powerNetFactorEntity.getPMon();
                pointNoArr[1] = powerNetFactorEntity.getQMon();
                break;
            default:
                break;
        }
        //查询图表
        apiPointValueQueryDTO.setPointNo(pointNo);
        List<Object[]> dataList = new ArrayList<>();
        Calendar calendar0 = Calendar.getInstance();
        calendar0.set(Calendar.MILLISECOND, 0);
        calendar0.set(Calendar.SECOND, 0);
@@ -894,16 +1107,42 @@
        Date start = dto.getStartTime() == null ? calendar0.getTime() : dto.getStartTime();
        apiPointValueQueryDTO.setEnd(end);
        apiPointValueQueryDTO.setStart(start);
        List<ApiPointValueDTO> chartData = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
        List<Object[]> dataList = new ArrayList<>();
        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();
            dataList.add(data);
        }
        List<String> categories = DateUtils.getTimeScale(start, end, 60);
        result.setCategories(categories);
        if (StringUtils.isNotBlank(pointNo)) {
            apiPointValueQueryDTO.setPointNo(pointNo);
            List<ApiPointValueDTO> 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();
                dataList.add(data);
            }
        } else if (StringUtils.isNotBlank(pointNoArr[0]) && StringUtils.isNotBlank(pointNoArr[1])) {
            apiPointValueQueryDTO.setPointNo(pointNoArr[0]);
            List<ApiPointValueDTO> chartDataP = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
            Map<String, Double> dataMapP = new HashMap<>();
            for (ApiPointValueDTO pv : chartDataP) {
                dataMapP.put(DateUtils.format(pv.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND), pv.getV());
            }
            apiPointValueQueryDTO.setPointNo(pointNoArr[1]);
            List<ApiPointValueDTO> chartDataQ = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
            Map<String, Double> 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) {
                Object[] data = new Object[2];
                data[0] = cate;
                data[1] = PowerUtil.calculateCos(dataMapP.get(cate), dataMapQ.get(cate));
                dataList.add(data);
            }
        }
        result.setDataList(dataList);
        //查询月最大最小值
@@ -925,6 +1164,153 @@
            result.setMin(new BigDecimal(minV.get(pointNo).toString()));
        }
        return success(result);
    }
    @PostMapping("/net-factor/export-history")
    @Operation(summary = "功率因数-电网拓扑功率历史导出(多code)")
    public void exportPowerHistoryList(HttpServletResponse response, HttpServletRequest
            request, @RequestBody PowerNetFactorHisReqDTO dto) throws IOException {
        log.info("exportPowerHistoryList开始");
        String firstCol = "时间";
        List<String> column = new ArrayList<>();
        column.add(firstCol);
        List<List<Object>> exportData = new ArrayList<>();
        List<String> nodeCodeList = dto.getNodeCodeList();
        if (CollectionUtils.isEmpty(nodeCodeList)) {
            return;
        }
        String queryType = dto.getQueryType();
        if (StringUtils.isBlank(queryType)) {
            return;
        }
        Calendar calendar0 = Calendar.getInstance();
        calendar0.set(Calendar.MILLISECOND, 0);
        calendar0.set(Calendar.SECOND, 0);
        Date end0 = dto.getEndTime() == null ? calendar0.getTime() : dto.getEndTime();
        calendar0.set(Calendar.MINUTE, 0);
        calendar0.set(Calendar.HOUR_OF_DAY, 0);
        Date start0 = dto.getStartTime() == null ? calendar0.getTime() : dto.getStartTime();
        Map<String, Map<String, Object>> dataAll = new HashMap<>();
        List<String> categories = DateUtils.getTimeScale(start0, end0, 60);
        for (String nodeCode : nodeCodeList) {
            PowerNetFactorQuery powerNetFactorQuery = null;
            PowerNetFactorEntity powerNetFactorEntity = powerNetFactorService.getByNodeCode(nodeCode);
            PowerNetDropdownEntity powerNetDropdownEntity = powerNetDropdownService.getByNodeCode(nodeCode);
            if (powerNetFactorEntity != null) {
                powerNetFactorQuery = ConvertUtils.sourceToTarget(powerNetFactorEntity, PowerNetFactorQuery.class);
            } else if (powerNetDropdownEntity != null) {
                powerNetFactorQuery = ConvertUtils.sourceToTarget(powerNetDropdownEntity, PowerNetFactorQuery.class);
            }
            if (powerNetFactorQuery == null) {
                log.info("powerNetFactor is null");
                continue;
            }
            column.add(powerNetFactorQuery.getNodeName());
            log.info("开始查询,");
            ApiPointValueQueryDTO apiPointValueQueryDTO = new ApiPointValueQueryDTO();
            String pointNo = "";
            String[] pointNoArr = new String[2];
            switch (queryType.toUpperCase()) {
                case "P":
                    if (StringUtils.isBlank(powerNetFactorQuery.getCurP())) {
                        continue;
                    }
                    pointNo = powerNetFactorQuery.getCurP();
                    break;
                case "Q":
                    if (StringUtils.isBlank(powerNetFactorQuery.getCurQ())) {
                        continue;
                    }
                    pointNo = powerNetFactorQuery.getCurQ();
                    break;
                case "COS":
                    if (StringUtils.isBlank(powerNetFactorQuery.getCurCos())) {
                        continue;
                    }
                    pointNo = powerNetFactorQuery.getCurCos();
                    break;
                case "DAYCOS":
                    if (powerNetFactorEntity == null ||
                            StringUtils.isBlank(powerNetFactorEntity.getPDay()) || StringUtils.isBlank(powerNetFactorEntity.getQDay())) {
                        continue;
                    }
                    pointNoArr[0] = powerNetFactorEntity.getPDay();
                    pointNoArr[1] = powerNetFactorEntity.getQDay();
                    break;
                case "MONTHCOS":
                    if (powerNetFactorEntity == null ||
                            StringUtils.isBlank(powerNetFactorEntity.getPMon()) || StringUtils.isBlank(powerNetFactorEntity.getQMon())) {
                        continue;
                    }
                    pointNoArr[0] = powerNetFactorEntity.getPMon();
                    pointNoArr[1] = powerNetFactorEntity.getQMon();
                    break;
                default:
                    break;
            }
            //查询图表
            apiPointValueQueryDTO.setEnd(end0);
            apiPointValueQueryDTO.setStart(start0);
            Map<String, Object> dataMap = new HashMap<>();
            if (StringUtils.isNotBlank(pointNo)) {
                log.info("查询功率");
                apiPointValueQueryDTO.setPointNo(pointNo);
                List<ApiPointValueDTO> chartData = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
                for (ApiPointValueDTO pv : chartData) {
                    String key = DateUtils.format(pv.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
                    Object val = pv.getV();
                    dataMap.put(key, val);
                }
            } else if (StringUtils.isNotBlank(pointNoArr[0]) && StringUtils.isNotBlank(pointNoArr[1])) {
                log.info("查询累计功率");
                apiPointValueQueryDTO.setPointNo(pointNoArr[0]);
                List<ApiPointValueDTO> chartDataP = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
                Map<String, Double> dataMapP = new HashMap<>();
                for (ApiPointValueDTO pv : chartDataP) {
                    dataMapP.put(DateUtils.format(pv.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND), pv.getV());
                }
                apiPointValueQueryDTO.setPointNo(pointNoArr[1]);
                List<ApiPointValueDTO> chartDataQ = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
                Map<String, Double> 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));
                    Object val = cos == null ? 0 : new BigDecimal(cos).setScale(2, BigDecimal.ROUND_HALF_UP);
                    dataMap.put(cate, val);
                }
            }
            dataAll.put(powerNetFactorQuery.getNodeName(), dataMap);
        }
        log.info("column=" + JSONArray.toJSONString(column));
        for (String cate : categories) {
            List<Object> row = new ArrayList<>();
            for (String col : column) {
                if (firstCol.equals(col)) {
                    row.add(cate);
                } else if (dataAll.get(col) == null) {
                    log.info(col + " is null");
                    row.add("");
                } else if (dataAll.get(col).get(cate) == null) {
                    log.info(col + cate + " is null");
                    row.add("");
                } else {
                    row.add(dataAll.get(col).get(cate));
                }
            }
            exportData.add(row);
        }
        log.info("exportData.size=" + exportData.size());
        ExcelUtils.write(response,  "历史.xlsx", "历史数据", column, exportData);
    }
    @PostMapping("/net-factor/history-list")
@@ -968,70 +1354,136 @@
            log.info("开始查询,");
            ApiPointValueQueryDTO apiPointValueQueryDTO = new ApiPointValueQueryDTO();
            String pointNo = "";
            String[] pointNoArr = new String[2];
            switch (queryType.toUpperCase()) {
                case "P":
                    if (StringUtils.isBlank(powerNetFactorQuery.getCurP())) {
                        continue;
                    }
                    pointNo = powerNetFactorQuery.getCurP();
                    break;
                case "Q":
                    if (StringUtils.isBlank(powerNetFactorQuery.getCurQ())) {
                        continue;
                    }
                    pointNo = powerNetFactorQuery.getCurQ();
                    break;
                case "COS":
                    if (StringUtils.isBlank(powerNetFactorQuery.getCurCos())) {
                        continue;
                    }
                    pointNo = powerNetFactorQuery.getCurCos();
                    break;
                case "DAYCOS":
                    if (powerNetFactorEntity == null ||
                            StringUtils.isBlank(powerNetFactorEntity.getPDay()) || StringUtils.isBlank(powerNetFactorEntity.getQDay())) {
                        continue;
                    }
                    pointNoArr[0] = powerNetFactorEntity.getPDay();
                    pointNoArr[1] = powerNetFactorEntity.getQDay();
                    break;
                case "MONTHCOS":
                    if (powerNetFactorEntity == null ||
                            StringUtils.isBlank(powerNetFactorEntity.getPMon()) || StringUtils.isBlank(powerNetFactorEntity.getQMon())) {
                        continue;
                    }
                    pointNoArr[0] = powerNetFactorEntity.getPMon();
                    pointNoArr[1] = powerNetFactorEntity.getQMon();
                    break;
                default:
                    break;
            }
            //查询图表
            apiPointValueQueryDTO.setPointNo(pointNo);
            apiPointValueQueryDTO.setEnd(end0);
            apiPointValueQueryDTO.setStart(start0);
            List<ApiPointValueDTO> chartData = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
            List<Object[]> dataList = new ArrayList<>();
            List<Double> valueList = new ArrayList<>();
            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();
                dataList.add(data);
                valueList.add(pv.getV());
            }
            powerHistoryDTO.setDataList(dataList);
            //查询月最大最小值
            /*ApiPointValueQueryDTO apiPointValueQueryDTO1 = new ApiPointValueQueryDTO();
            apiPointValueQueryDTO1.setStart(start1);
            apiPointValueQueryDTO1.setEnd(new Date());
            apiPointValueQueryDTO1.setPointNo(pointNo);
            Map<String, Object> maxV = dataPointApi.queryPointMaxValueRange(apiPointValueQueryDTO1);
            Map<String, Object> minV = dataPointApi.queryPointMaxValueRange(apiPointValueQueryDTO1);
            if (maxV != null && maxV.containsKey(pointNo)) {
                powerHistoryDTO.setMax(new BigDecimal(maxV.get(pointNo).toString()));
            }
            if (minV != null && minV.containsKey(pointNo)) {
                powerHistoryDTO.setMin(new BigDecimal(minV.get(pointNo).toString()));
            }*/
            double fsl = 0;
            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();
            String maxTime = "";
            String minTime = "";
                for (Double val : valueList) {
                    if (val < 0) {
                        fsl += val;
            if (StringUtils.isNotBlank(pointNo)) {
                apiPointValueQueryDTO.setPointNo(pointNo);
                List<ApiPointValueDTO> 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();
                    dataList.add(data);
                    valueList.add(pv.getV());
                }
                // 返送累计
                double fsl = 0;
                if (!CollectionUtils.isEmpty(valueList)) {
                    if (powerNetFactorQuery.getCurFlag() != null && powerNetFactorQuery.getCurFlag().intValue() != 0) {
                        for ( Double val : valueList) {
                            if (new BigDecimal(val).compareTo(BigDecimal.ZERO) != powerNetFactorQuery.getCurFlag()) {
                                // 未发生返送
                                continue;
                            }
                            fsl += val;
                        }
                    }
                }
                powerHistoryDTO.setFsl(new BigDecimal(fsl).setScale(2, BigDecimal.ROUND_HALF_UP));
            } else if (StringUtils.isNotBlank(pointNoArr[0]) && StringUtils.isNotBlank(pointNoArr[1])) {
                apiPointValueQueryDTO.setPointNo(pointNoArr[0]);
                List<ApiPointValueDTO> chartDataP = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
                Map<String, Double> dataMapP = new HashMap<>();
                for (ApiPointValueDTO pv : chartDataP) {
                    dataMapP.put(DateUtils.format(pv.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND), pv.getV());
                }
                apiPointValueQueryDTO.setPointNo(pointNoArr[1]);
                List<ApiPointValueDTO> chartDataQ = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
                Map<String, Double> 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));
                    if (cos == null) {
                        continue;
                    }
                    Object[] data = new Object[2];
                    data[0] = cate;
                    data[1] = cos;
                    dataList.add(data);
                    valueList.add(cos);
                }
            }
            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();
                max = valueList.get(0).doubleValue();
                min = valueList.get(0).doubleValue();
                for (Object[] dataItem : dataList) {
                    String time = (String) dataItem[0];
                    double value = (double)dataItem[1];
                    if (value > max) {
                        max = value;
                        maxTime = time;
                    }
                    if (value < min) {
                        min = value;
                        minTime = time;
                    }
                }
            }
            powerHistoryDTO.setFsl(new BigDecimal(fsl).setScale(2, BigDecimal.ROUND_HALF_UP));
            powerHistoryDTO.setMax(new BigDecimal(max).setScale(2, BigDecimal.ROUND_HALF_UP));
            powerHistoryDTO.setMin(new BigDecimal(min).setScale(2, BigDecimal.ROUND_HALF_UP));
            powerHistoryDTO.setAvg(new BigDecimal(avg).setScale(2, BigDecimal.ROUND_HALF_UP));
            powerHistoryDTO.setMaxTime(maxTime);
            powerHistoryDTO.setMinTime(minTime);
            powerHistoryDTO.setDataList(dataList);
            result.put(nodeCode, powerHistoryDTO);
        }
        return success(result);
@@ -1194,6 +1646,7 @@
                    throw new IllegalArgumentException("不支持的queryType: " + queryType);
            }
            // 查询历史数据
            ApiPointValueQueryDTO query = new ApiPointValueQueryDTO();
            query.setPointNo(pointNo);
@@ -1208,10 +1661,13 @@
                    })
                    .collect(Collectors.toList());
            List<Double> valueList = chartData.stream().map(pv -> {
                return pv.getV();
            }).collect(Collectors.toList());
            powerHistoryDTO.setDataList(dataList);
            //查询月最大最小值
            ApiPointValueQueryDTO apiPointValueQueryDTO1 = new ApiPointValueQueryDTO();
            /*ApiPointValueQueryDTO apiPointValueQueryDTO1 = new ApiPointValueQueryDTO();
            apiPointValueQueryDTO1.setStart(start1);
            apiPointValueQueryDTO1.setEnd(new Date());
            apiPointValueQueryDTO1.setPointNo(pointNo);
@@ -1222,7 +1678,40 @@
            }
            if (minV != null && minV.containsKey(pointNo)) {
                powerHistoryDTO.setMin(new BigDecimal(minV.get(pointNo).toString()));
            }*/
            double max = 0;
            double min = 0;
            double avg = 0;
            String maxTime = "";
            String minTime = "";
            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();
                max = valueList.get(0).doubleValue();
                min = valueList.get(0).doubleValue();
                for (Object[] dataItem : dataList) {
                    String time = (String) dataItem[0];
                    double value = (double)dataItem[1];
                    if (value > max) {
                        max = value;
                        maxTime = time;
                    }
                    if (value < min) {
                        min = value;
                        minTime = time;
                    }
                }
            }
            powerHistoryDTO.setMax(new BigDecimal(max).setScale(2, BigDecimal.ROUND_HALF_UP));
            powerHistoryDTO.setMin(new BigDecimal(min).setScale(2, BigDecimal.ROUND_HALF_UP));
            powerHistoryDTO.setAvg(new BigDecimal(avg).setScale(2, BigDecimal.ROUND_HALF_UP));
            powerHistoryDTO.setMaxTime(maxTime);
            powerHistoryDTO.setMinTime(minTime);
            powerHistoryDTO.setDataList(dataList);
            result.put(code, powerHistoryDTO);
        }
@@ -1271,6 +1760,11 @@
            log.info("code is blank");
            return error(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        // 默认只展示当月
        if(reqVO.getStartTime() == null) {
            reqVO.setStartTime(CalendarUtil.getMonthFirstDay());
        }
        PageResult<PowerMaxdemandMainEntity> page = powerMaxDemandMainService.page(reqVO);
        PageResult<PowerMaxDemandMainDTO> result = BeanUtils.toBean(page, PowerMaxDemandMainDTO.class);
        result.getList().forEach(dto0 -> {
@@ -1320,8 +1814,9 @@
    }
    @PostMapping("/factor-control/list")
    @Operation(summary = "功率因数管控")
    @Operation(summary = "功率因数调整结果查询")
    public CommonResult<List<PowerFactorControlDTO>> powerFactorControlList(@RequestBody PowerFactorReqVO powerFactorReqVO) {
        List<PowerFactorControlDTO> result = new ArrayList<>();
        Map<String, Object> chartMap = new HashMap<>();
        List<ChartParamDTO> chartList = mcsApi.getChartParamList(CommonConstant.POWER_CODE);
        chartList.forEach(item -> {
@@ -1345,71 +1840,95 @@
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        MdkScheduleReqDTO dto = new MdkScheduleReqDTO();
        dto.setScheduleTime(calendar.getTime());
        dto.setScheduleCode("AnSteelpowertransfer");
        dto.setScheduleCode("AnsteelOffPowerAC");
        Map<String, String> dynamicSettings = new HashMap<>();
        dynamicSettings.put("option_param", JSONArray.toJSONString(contentListNew.stream().toArray(Integer[]::new)));
        dynamicSettings.put("cos_param", JSONArray.toJSONString(powerFactorReqVO.getAdjustValueList().stream().toArray(String[]::new)));
        List<String> cosParam = new ArrayList<>();
        cosParam.add(powerFactorReqVO.getAdjustValueCcpp().toString());
        cosParam.add(powerFactorReqVO.getAdjustValue135().toString());
        cosParam.add(powerFactorReqVO.getAdjustValueTrt().toString());
        dynamicSettings.put("cos_param", JSONArray.toJSONString(cosParam));
        dto.setDynamicSettings(dynamicSettings);
        log.info("调度方案开始执行," + JSONObject.toJSONString(dto));
        MdkScheduleRespDTO mdkScheduleRespDTO = mdkApi.doSchedule(dto);
        log.info("调度方案执行完成," + mdkScheduleRespDTO);
        // MdkScheduleRespDTO mdkScheduleRespDTO = mdkApi.doSchedule(dto);
        //log.info("调度方案执行完成," + mdkScheduleRespDTO);
        String statusCode = mdkScheduleRespDTO.getStatusCode();
        /*String statusCode = mdkScheduleRespDTO.getStatusCode();
        if (!CommonConstant.MDK_STATUS_100.equals(statusCode)) {
            log.info("statusCode=" + statusCode);
            return CommonResult.error(GlobalErrorCodeConstants.UNKNOWN.getCode(), "工序异常,无计算结果");
        }
        Map<String, Object> data = mdkScheduleRespDTO.getResult();
        List<String> adjustCosList = Arrays.asList(data.get("adjust_cos").toString().split(","));//管控结果
        Map<String, Object> data = mdkScheduleRespDTO.getResult();*/
        // 调整后的功率因数
        // List<String> adjustCosList = Arrays.asList(data.get("adjust_cos").toString().split(","));
        String fakeData = "[0.39,0.98,0.98,0 91,1.0,0.93,0.86,0.89,0.98,0.9,0.97,0.89,0.84,0.91]";
        List<BigDecimal> adjustCosList = JSONArray.parseArray(fakeData, BigDecimal.class);
        PowerFactorControlDTO powerFactorControlDTO = new PowerFactorControlDTO();
        List<PowerFactorControlDTO> result = new ArrayList<>();
        List<PowerFactorControlDTO> list = powerFactorControlService.list(powerFactorControlDTO);
        // 无功管控结果
        BigDecimal back_wugong_buchang = new BigDecimal(36.92);
        adjustCosList.add(back_wugong_buchang);
        Map<Integer, BigDecimal> adjustCosMap = new HashMap<>();
        for (int i = 0; i < adjustCosList.size(); i++) {
            adjustCosMap.put(i, adjustCosList.get(i));
        }
        List<PowerFactorControlEntity> list = powerFactorControlService.list();
        if (CollectionUtils.isEmpty(list)) {
            log.info("list is empty");
            return success(result);
        }
        for (int i = 0; i < list.size(); i++) {
            PowerFactorControlDTO controlDTO = list.get(i);
            PowerFactorControlEntity entity = list.get(i);
            PowerFactorControlDTO controlDTO = new PowerFactorControlDTO();
            controlDTO.setName(entity.getName());
            controlDTO.setSort(entity.getSort());
            try {
                PredictLastValueReqVO reqVO = new PredictLastValueReqVO();
                reqVO.setPredictTime(calendar.getTime());
                List<String[]> itemNos = new ArrayList<>();
                if (StringUtils.isNotBlank(controlDTO.getPredResult())) {
                    itemNos.add(controlDTO.getPredResult().split(","));
                // 预测结果
                switch (ProcessConfDataTypeEnum.getEumByCode(entity.getDataType())) {
                    case DATAPOINT:
                        List<String> params1 = new ArrayList<>();
                        params1.add(entity.getPredResult());
                        Map<String, Object> pointValues = dataPointApi.queryPointsRealValue(params1);
                        if (!CollectionUtils.isEmpty(pointValues)) {
                            controlDTO.setPredResult(DecimalUtil.toBigDecimal(pointValues.get(entity.getPredResult())));
                        }
                        break;
                    case PREDICTVALUE:
                        PredictLastValueReqVO reqVO = new PredictLastValueReqVO();
                        calendar.set(calendar.HOUR_OF_DAY, 0);
                        reqVO.setPredictTime(calendar.getTime());
                        List<String[]> itemNos = new ArrayList<>();
                        if (StringUtils.isNotBlank(entity.getPredResult())) {
                            itemNos.add(entity.getPredResult().split(","));
                        }
                        reqVO.setItemNos(itemNos);
                        Map<String, BigDecimal> preValues = mcsApi.getPredictValueByTime(reqVO);
                        if (preValues.get(entity.getPredResult()) != null) {
                            controlDTO.setPredResult(new BigDecimal(preValues.get(entity.getPredResult()).toString()));
                        }
                        break;
                    default:
                        break;
                }
                if (!CollectionUtils.isEmpty(itemNos)) {
                    reqVO.setItemNos(itemNos);
                    log.info("reqVO=" + JSONObject.toJSONString(reqVO));
                    Map<String, BigDecimal> preValues = mcsApi.getPredictValueByTime(reqVO);
                    if (!ObjectUtils.isEmpty(preValues.get(controlDTO.getPredResult()))) {
                        controlDTO.setPredResult(preValues.get(controlDTO.getPredResult()).toString());
                    }
                }
                controlDTO.setAdjustCos(adjustCosList.get(i));
                // 管控结果
                if (adjustCosMap.containsKey(entity.getCosIndex())) {
                    controlDTO.setAdjustCos(adjustCosMap.get(entity.getCosIndex()));
                }
            } catch (Exception ex) {
                log.info(controlDTO.getName() + "获取预测值异常," + ex.getMessage());
                ex.printStackTrace();
            }
            result.add(controlDTO);
        }
        List<String> points = new ArrayList<>();
        points.add(pointNo);
        Map<String, Object> pointsRealValue = dataPointApi.queryPointsRealValue(points);
        PowerFactorControlDTO controlDTO = new PowerFactorControlDTO();
        controlDTO.setName("望铁关口最大无功倒送量");
        controlDTO.setPredResult(pointsRealValue.get(pointNo).toString());
        controlDTO.setAdjustCos(data.get("back_wugong_buchang").toString());
        controlDTO.setSort(list.size() + 1);
        result.add(controlDTO);
        log.info("result===" + JSONObject.toJSONString(result));
        return success(result);
    }
}