鞍钢鲅鱼圈能源管控系统后端代码
潘志宝
2025-06-18 169b88b3d8a7c6ae833b49c8d0fab843159485db
ansteel-biz/src/main/java/com/iailab/module/ansteel/api/controller/admin/PowerController.java
@@ -1136,6 +1136,121 @@
        return success(result);
    }
    @PostMapping("/net-factor/export-history")
    @Operation(summary = "功率因数-电网拓扑功率历史(多code)")
    public void exportPowerHistoryList(HttpServletResponse response, HttpServletRequest
            request, @RequestBody PowerNetFactorHisReqDTO dto) throws IOException {
        List<String> column = new ArrayList<>();
        column.add("时间");
        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":
                    pointNo = powerNetFactorQuery.getCurP();
                    break;
                case "Q":
                    pointNo = powerNetFactorQuery.getCurQ();
                    break;
                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.setEnd(end0);
            apiPointValueQueryDTO.setStart(start0);
            Map<String, Object> dataMap = new HashMap<>();
            if (StringUtils.isNotBlank(pointNo)) {
                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])) {
                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);
        }
        for (String cate : categories) {
            List<Object> row = new ArrayList<>();
            for (String col : column) {
                row.add(dataAll.get(col).get(cate));
            }
        }
        ExcelUtils.write(response,  "历史.xlsx", "历史数据", column, exportData);
    }
    @PostMapping("/net-factor/history-list")
    @Operation(summary = "功率因数-电网拓扑功率历史(多code)")
    public CommonResult<LinkedHashMap<String, PowerHistoryDTO>> getPowerHistoryList(@RequestBody PowerNetFactorHisReqDTO dto) {
@@ -1204,9 +1319,9 @@
            apiPointValueQueryDTO.setEnd(end0);
            apiPointValueQueryDTO.setStart(start0);
            List<Object[]> dataList = new ArrayList<>();
            List<Double> valueList = new ArrayList<>();
            if (StringUtils.isNotBlank(pointNo)) {
                List<Double> valueList = new ArrayList<>();
                apiPointValueQueryDTO.setPointNo(pointNo);
                List<ApiPointValueDTO> chartData = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
                for (ApiPointValueDTO pv : chartData) {
@@ -1249,7 +1364,41 @@
                }
            } 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));
                    Object[] data = new Object[2];
                    data[0] = cate;
                    data[1] = cos == null ? 0 : new BigDecimal(cos).setScale(2, BigDecimal.ROUND_HALF_UP);
                    dataList.add(data);
                    if (cos != null) {
                        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();
                    }
                    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.setDataList(dataList);
            result.put(nodeCode, powerHistoryDTO);