鞍钢鲅鱼圈能源管控系统后端代码
潘志宝
3 天以前 f7426b7804ad1ee1bfb3f1520ad035bcda54f852
ansteel-biz/src/main/java/com/iailab/module/ansteel/api/controller/admin/PowerController.java
@@ -7,6 +7,7 @@
import com.iailab.framework.common.util.date.DateUtils;
import com.iailab.framework.common.util.object.ConvertUtils;
import com.iailab.module.ansteel.api.dto.*;
import com.iailab.module.ansteel.common.utils.DecimalUtil;
import com.iailab.module.ansteel.power.entity.*;
import com.iailab.module.ansteel.power.service.*;
import com.iailab.module.data.api.point.DataPointApi;
@@ -22,10 +23,7 @@
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
@@ -64,11 +62,20 @@
    @Autowired
    private PowerAdjustedFactorService powerAdjustedFactorService;
    @Resource
    @Autowired
    private DataPointApi dataPointApi;
    @Resource
    @Autowired
    private McsApi mcsApi;
    @Autowired
    private PowerCapacitorHisService powerCapacitorHisService;
    @Autowired
    private PowerCapacitorDetService powerCapacitorDetService;
    @Autowired
    private PowerNetDropdownService powerNetDropdownService;
    @GetMapping("/net-factor/list")
    @Operation(summary = "功率因数-电网拓扑")
@@ -158,9 +165,38 @@
    @GetMapping("/net-factor-dropdown/list")
    @Operation(summary = "功率因数-电网拓扑下拉列表")
    public CommonResult<List<PowerNetFactorDropdownDTO>> getPowerNetFactorDropdownList(@RequestParam String nodeCode) {
        List<PowerNetFactorEntity> list = powerNetFactorService.listDropdown(nodeCode);
        List<PowerNetFactorDropdownDTO> result = ConvertUtils.sourceToTarget(list, PowerNetFactorDropdownDTO.class);
    public CommonResult<List<PowerNetDropdownDTO>> getPowerNetFactorDropdownList(@RequestParam String nodeCode) {
        log.info("nodeCode=" + nodeCode);
        List<PowerNetDropdownDTO> result = new ArrayList<>();
        PowerNetFactorEntity entity = powerNetFactorService.getByNodeCode(nodeCode);
        if (entity == null) {
            return success(result);
        }
        List<PowerNetDropdownEntity> list = new ArrayList<>();
        Map<String, Object> params = new HashMap<>();
        if ("望铁关口".equals(entity.getGroupName())) {
            params.put("groupName", entity.getGroupName());
            params.put("neNodeName", entity.getNodeName());
        } else {
            params.put("groupName", entity.getNodeName());
        }
        list = powerNetDropdownService.list(params);
        List<String> points = list.stream().map(item -> {
            return item.getCurCos();
        }).collect(Collectors.toList());
        Map<String, Object> pointsRealValue = new HashMap<>();
        if (!CollectionUtils.isEmpty(points)) {
            pointsRealValue = dataPointApi.queryPointsRealValue(points);
        }
        for (PowerNetDropdownEntity netDropdown : list) {
            PowerNetDropdownDTO dto = ConvertUtils.sourceToTarget(netDropdown, PowerNetDropdownDTO.class);
            BigDecimal curCos = BigDecimal.ZERO;
            if (pointsRealValue.get(netDropdown.getCurCos()) != null) {
                curCos = new BigDecimal(pointsRealValue.get(netDropdown.getCurCos()).toString());
            }
            dto.setCurCos(curCos);
            result.add(dto);
        }
        return success(result);
    }
@@ -245,11 +281,121 @@
        return success(result);
    }
    @PostMapping("/gen-status/history")
    @Operation(summary = "功率因数-发电机组功率历史")
    public CommonResult<PowerHistoryDTO> getPowerGenStatusHistory(@RequestBody PowerGenStatusHisReqDTO dto) {
        log.info("请求参数: {}", JSONObject.toJSONString(dto));
        // 参数校验
        if (StringUtils.isBlank(dto.getId())) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST, "id不能为空");
        }
        if (StringUtils.isBlank(dto.getQueryType())) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST, "queryType不能为空");
        }
        log.info("id={}", dto.getId());
        PowerGenStatusEntity powerGenStatus = powerGenStatusDaoService.getById(dto.getId());
        if (powerGenStatus == null) {
            log.info("未找到code对应的数据: {}", dto.getId());
            return success(new PowerHistoryDTO());
        }
        String queryType = dto.getQueryType().toUpperCase();
        log.info("queryType={}", queryType);
        String pointNo;
        switch (queryType.toUpperCase()) {
            case "P":
                pointNo = powerGenStatus.getCurP();
                break;
            case "Q":
                pointNo = powerGenStatus.getCurQ();
                break;
            case "COS":
                pointNo = powerGenStatus.getCurCos();
                break;
            default:
                throw new IllegalArgumentException("不支持的queryType: " + queryType);
        }
        log.info("开始查询,pointNo={}", pointNo);
        // 默认查最近24小时
        Date end = Optional.ofNullable(dto.getEndTime()).orElseGet(() -> {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.MILLISECOND, 0);
            cal.set(Calendar.SECOND, 0);
            return cal.getTime();
        });
        Date start = Optional.ofNullable(dto.getStartTime()).orElseGet(() -> {
            Calendar cal = Calendar.getInstance();
            cal.setTime(end);
            cal.add(Calendar.MINUTE, -1440); // 24小时前
            return cal.getTime();
        });
        // 查询历史数据
        ApiPointValueQueryDTO query = new ApiPointValueQueryDTO();
        query.setPointNo(pointNo);
        query.setStart(start);
        query.setEnd(end);
        log.info("开始查询发电机组功率历史数据,测点: {}", pointNo);
        List<ApiPointValueDTO> chartData = dataPointApi.queryPointHistoryValue(query);
        // 构建返回结果
        PowerHistoryDTO result = new PowerHistoryDTO();
        result.setCategories(DateUtils.getTimeScale(start, end, 60));
        result.setDataList(chartData.stream()
                .map(pv -> new Object[]{
                        DateUtils.format(pv.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND),
                        pv.getV()
                })
                .collect(Collectors.toList()));
        return success(result);
    }
    @GetMapping("/capacitor-status/list")
    @Operation(summary = "功率因数-电容器投运状态")
    @Operation(summary = "功率因数-电容器投退状态")
    public CommonResult<List<PowerCapacitorStatusDTO>> getPowerCapacitorStatusList(@RequestParam Map<String, Object> params) {
        List<PowerCapacitorStatusEntity> list = powerCapacitorStatusService.list(params);
        return success(ConvertUtils.sourceToTarget(list, PowerCapacitorStatusDTO.class));
    }
    @GetMapping("/capacitor-det/list")
    @Operation(summary = "功率因数-电容器投退指示灯")
    public CommonResult<List<PowerCapacitorDetDTO>> getPowerCapacitorDetList(@RequestParam Map<String, Object> params) {
        List<PowerCapacitorDetEntity> list = powerCapacitorDetService.list(params);
        log.info("list.size=" + list.size());
        List<String> points = list.stream().map(item -> {
            return item.getPointNo();
        }).collect(Collectors.toList());
        Map<String, Object> pointsRealValue = new HashMap<>();
        if (!CollectionUtils.isEmpty(points)) {
            pointsRealValue = dataPointApi.queryPointsRealValue(points);
        }
        List<PowerCapacitorDetDTO> result = new ArrayList<>();
        for (PowerCapacitorDetEntity entity : list) {
            PowerCapacitorDetDTO dto = ConvertUtils.sourceToTarget(entity, PowerCapacitorDetDTO.class);
            result.add(dto);
            dto.setStatus(0);
            if (!pointsRealValue.containsKey(entity.getPointNo()) || pointsRealValue.get(entity.getPointNo()) == null) {
                continue;
            }
            BigDecimal val = new BigDecimal(pointsRealValue.get(entity.getPointNo()).toString());
            if (val.compareTo(BigDecimal.ZERO) >= 0.1) {
                dto.setStatus(1);
            }
        }
        return success(result);
    }
    @GetMapping("/capacitor-his/list")
    @Operation(summary = "功率因数-电容器投退历史")
    public CommonResult<List<PowerCapacitorHisDTO>> getPowerCapacitorHisList(@RequestParam Map<String, Object> params) {
        List<PowerCapacitorHisEntity> list = powerCapacitorHisService.list(params);
        return success(ConvertUtils.sourceToTarget(list, PowerCapacitorHisDTO.class));
    }
    @GetMapping("/control-main/list")
@@ -321,11 +467,8 @@
            if (StringUtils.isNotBlank(dto.getActivePower())) {
                points.add(dto.getActivePower());
            }
            if (!CollectionUtils.isEmpty(points)) {
                Map<String, Object> pointsRealValue = dataPointApi.queryPointsRealValue(points);
                if (pointsRealValue.get(dto.getCurDemand()) != null) {
                    dto.setCurDemand(pointsRealValue.get(dto.getCurDemand()).toString());
                }
@@ -344,22 +487,159 @@
                Date start = calendar.getTime();
                ApiPointValueQueryDTO apiPointValueQueryDTO = new ApiPointValueQueryDTO();
                apiPointValueQueryDTO.setStart(start);
                apiPointValueQueryDTO.setEnd(new Date());
                apiPointValueQueryDTO.setPointNo(dto.getMaxDemand());
                List<ApiPointValueDTO> monthValues = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
                double max = 0;
                for (int i = 0; i < monthValues.size() - 1; i++) {
                    if (max < monthValues.get(i).getV()) {
                        max = monthValues.get(i).getV();
                    }
                Object maxValue = dataPointApi.queryPointMaxValue(apiPointValueQueryDTO);
                dto.setMaxDemand(maxValue == null ? "" : maxValue.toString());
            }
        }
        return success(result);
    }
    @GetMapping("/demand-dropdown/list")
    @Operation(summary = "负荷移植-功率因数下拉列表")
    public CommonResult<List<PowerNetDropdownDTO>> getDemandDropdownList(@RequestParam String code) {
        if (StringUtils.isBlank(code)) {
            log.info("code isBlank");
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        log.info("code=" + code);
        List<PowerNetDropdownDTO> result = new ArrayList<>();
        PowerDemandEntity entity = powerDemandService.getByCode(code);
        if (entity == null) {
            log.info("PowerDemandEntity is null");
            return success(result);
        }
        Map<String, Object> params0 = new HashMap<>();
        params0.put("groupName", entity.getName());
        List<PowerNetDropdownEntity> list = powerNetDropdownService.list(params0);
        List<String> points = list.stream().map(item -> {
            return item.getCurCos();
        }).collect(Collectors.toList());
        Map<String, Object> pointsRealValue = new HashMap<>();
        if (!CollectionUtils.isEmpty(points)) {
            pointsRealValue = dataPointApi.queryPointsRealValue(points);
        }
        for (PowerNetDropdownEntity netDropdown : list) {
            PowerNetDropdownDTO dto = ConvertUtils.sourceToTarget(netDropdown, PowerNetDropdownDTO.class);
            BigDecimal curCos = BigDecimal.ZERO;
            if (pointsRealValue.get(netDropdown.getCurCos()) != null) {
                curCos = new BigDecimal(pointsRealValue.get(netDropdown.getCurCos()).toString());
            }
            dto.setCurCos(curCos);
            result.add(dto);
        }
        return success(result);
    }
    @PostMapping("/demand-query/list")
    @Operation(summary = "负荷移植-功率因数查询")
    public CommonResult<List<PowerNetDropdownDTO>> getDemandDropdownList(@RequestBody PowerDemandQueryDTO queryDto) {
        List<PowerNetDropdownDTO> result = new ArrayList<>();
        if (StringUtils.isBlank(queryDto.getCode())) {
            log.info("code isBlank");
        }
        log.info("code=" + queryDto.getCode());
        if (StringUtils.isBlank(queryDto.getNodeCode())) {
            log.info("NodeCode isBlank");
        }
        PowerDemandEntity entity = powerDemandService.getByCode(queryDto.getCode());
        if (entity == null) {
            log.info("PowerDemandEntity is null");
            return success(result);
        }
        Map<String, Object> params0 = new HashMap<>();
        params0.put("groupName", entity.getName());
        List<PowerNetDropdownEntity> list = powerNetDropdownService.list(params0);
        List<String> points = list.stream().map(item -> {
            return item.getCurCos();
        }).collect(Collectors.toList());
        if (queryDto.getCurCos() == null) {
            log.info("查询当前值");
            Map<String, Object> pointsRealValue = new HashMap<>();
            if (!CollectionUtils.isEmpty(points)) {
                pointsRealValue = dataPointApi.queryPointsRealValue(points);
            }
            for (PowerNetDropdownEntity netDropdown : list) {
                PowerNetDropdownDTO dto = ConvertUtils.sourceToTarget(netDropdown, PowerNetDropdownDTO.class);
                BigDecimal curCos = BigDecimal.ZERO;
                if (pointsRealValue.get(netDropdown.getCurCos()) != null) {
                    curCos = new BigDecimal(pointsRealValue.get(netDropdown.getCurCos()).toString());
                }
                dto.setMaxDemand(String.valueOf(max));
                dto.setCurCos(curCos);
                result.add(dto);
            }
        } else {
            log.info("查询历史值");
            String nodeCode = queryDto.getNodeCode();
            if (StringUtils.isBlank(nodeCode)) {
                log.info("nodeCode isBlank");
                return success(result);
            }
            PowerNetDropdownEntity powerNetDropdownEntity = powerNetDropdownService.getByNodeCode(nodeCode);
            if (powerNetDropdownEntity == null) {
                log.info("PowerNetDropdownEntity is null");
                return success(result);
            }
            ApiPointsValueQueryDTO valueQueryDTO = new ApiPointsValueQueryDTO();
            valueQueryDTO.setPointNos(points);
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.MILLISECOND, 0);
            calendar.set(Calendar.SECOND, 0);
            Date startTime = queryDto.getStartTime();
            Date endTime = queryDto.getEndTime();
            if (endTime == null) {
                endTime = calendar.getTime();
            }
            if (startTime == null) {
                calendar.add(Calendar.DAY_OF_YEAR, -1);
                startTime = calendar.getTime();
            }
            valueQueryDTO.setStart(startTime);
            valueQueryDTO.setEnd(endTime);
            Map<String, List<Map<String, Object>>> pointsHisValues = dataPointApi.queryPointsHistoryValue(valueQueryDTO);
            if (CollectionUtils.isEmpty(pointsHisValues)) {
                log.info("pointsHisValues is null");
                return success(result);
            }
            ApiPointValueQueryDTO pointValueQueryDTO = new ApiPointValueQueryDTO();
            pointValueQueryDTO.setStart(startTime);
            pointValueQueryDTO.setEnd(endTime);
            pointValueQueryDTO.setPointNo(powerNetDropdownEntity.getCurCos());
            List<ApiPointValueDTO> hisValue = dataPointApi.queryPointHistoryValue(pointValueQueryDTO);
            if (CollectionUtils.isEmpty(hisValue)) {
                log.info("hisValue is null");
            }
            ApiPointValueDTO curValue = null;
            Collections.reverse(hisValue);
            for (ApiPointValueDTO valueDTO : hisValue) {
                curValue = valueDTO;
                if ((queryDto.getCurCos().doubleValue() - valueDTO.getV()) <= 0.0001) {
                    break;
                }
            }
            if (curValue == null) {
                log.info("curValue is null");
                return success(result);
            }
            log.info("curValue=" + curValue);
            for (PowerNetDropdownEntity netDropdown : list) {
                PowerNetDropdownDTO dto = ConvertUtils.sourceToTarget(netDropdown, PowerNetDropdownDTO.class);
                if (CollectionUtils.isEmpty(pointsHisValues.get(netDropdown.getCurCos()))) {
                    continue;
                }
                Map<String, BigDecimal> pointValueMap = new HashMap<>();
                pointsHisValues.get(netDropdown.getCurCos()).forEach(item -> {
                    pointValueMap.put(item.get("time").toString(), DecimalUtil.toBigDecimal(item.get("value")));
                });
                dto.setCurCos(pointValueMap.get(DateUtils.format(curValue.getT(),DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)));
                result.add(dto);
            }
        }
        return success(result);
    }
@@ -367,6 +647,7 @@
    @Operation(summary = "功率因数-根据nodeName获取最近1440min历史数据,月最大,最小值")
    public CommonResult<PowerHistoryDTO> getPowerHistoryData(@RequestBody PowerNetFactorHisReqDTO dto) {
        log.info("PowerNetFactorHisReqDTO=" + JSONObject.toJSONString(dto));
        PowerHistoryDTO result = new PowerHistoryDTO();
        String nodeCode = dto.getNodeCode();
        if (StringUtils.isBlank(nodeCode)) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
@@ -375,25 +656,30 @@
        if (StringUtils.isBlank(queryType)) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        PowerNetFactorEntity powerNetFactor = powerNetFactorService.getByNodeCode(nodeCode);
        PowerHistoryDTO result = new PowerHistoryDTO();
        if (powerNetFactor == null) {
        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");
            return success(result);
        }
        log.info("开始查询,");
        ApiPointValueQueryDTO apiPointValueQueryDTO = new ApiPointValueQueryDTO();
        String pointNo = "";
        switch (queryType.toUpperCase()) {
            case "P":
                pointNo = powerNetFactor.getCurP();
                pointNo = powerNetFactorQuery.getCurP();
                break;
            case "Q":
                pointNo = powerNetFactor.getCurQ();
                pointNo = powerNetFactorQuery.getCurQ();
                break;
            case "COS":
                pointNo = powerNetFactor.getCurCos();
                pointNo = powerNetFactorQuery.getCurCos();
                break;
            default:
                break;
@@ -405,7 +691,7 @@
        calendar0.set(Calendar.SECOND, 0);
        Date end = dto.getEndTime() == null ? calendar0.getTime() : dto.getEndTime();
        calendar0.add(Calendar.MINUTE, -1440);
        Date start = dto.getStartTime() == null ?  calendar0.getTime() : dto.getStartTime();
        Date start = dto.getStartTime() == null ? calendar0.getTime() : dto.getStartTime();
        apiPointValueQueryDTO.setEnd(end);
        apiPointValueQueryDTO.setStart(start);
        List<ApiPointValueDTO> chartData = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
@@ -433,31 +719,11 @@
        List<ApiPointValueDTO> monthChartData = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
        List<Double> monthValues = new ArrayList<>();
        if (CollectionUtils.isEmpty(monthChartData)) {
            monthValues =  monthChartData.stream().map(item -> item.getV()).collect(Collectors.toList());
            monthValues = monthChartData.stream().map(item -> item.getV()).collect(Collectors.toList());
            result.setMax(monthValues.stream().max(Double::compareTo).get());
            result.setMin(monthValues.stream().min(Double::compareTo).get());
        }
        return success(result);
    }
    private double getMin(List<List<Object>> dataList) {
        double result = Double.parseDouble(dataList.get(0).get(1).toString());
        for (int i = 0; i < dataList.size() - 1; i++) {
            if (result > Double.parseDouble(dataList.get(i).get(1).toString())) {
                result = Double.parseDouble(dataList.get(i).get(1).toString());
            }
        }
        return result;
    }
    private double getMax(List<List<Object>> dataList) {
        double result = 0;
        for (int i = 0; i < dataList.size() - 1; i++) {
            if (result < Double.parseDouble(dataList.get(i).get(1).toString())) {
                result = Double.parseDouble(dataList.get(i).get(1).toString());
            }
        }
        return result;
    }
    @GetMapping("/adjust-factor")
@@ -556,4 +822,5 @@
        return success(result);
    }
}