鞍钢鲅鱼圈能源管控系统后端代码
dengzedong
2025-05-27 dd1ba0fac25de2328ed6b92bfe5c82a7822572d7
RunBatTask
峰谷平耗电量关联 峰谷平时段表
已修改3个文件
已添加1个文件
193 ■■■■■ 文件已修改
ansteel-biz/db/mysql.sql 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunBatTask.java 43 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunPeakValleyFlatTask.java 125 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ansteel-biz/src/main/java/com/iailab/module/ansteel/power/entity/PeakValleyFlatEntity.java 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ansteel-biz/db/mysql.sql
@@ -868,3 +868,18 @@
    PRIMARY KEY (`id`) USING BTREE,
    key       idx_main_id (main_id)
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC COMMENT '峰谷平电价时段表';
-- ----------------------------
-- 峰谷平累计计算配置
-- ----------------------------
CREATE TABLE `t_peak_valley_flat` (
                                      `id` varchar(36) NOT NULL COMMENT 'id',
                                      `name` varchar(36) DEFAULT NULL COMMENT '名称',
                                      `type` varchar(36) DEFAULT NULL COMMENT '类型(peak/vallay/flat)',
                                      `point_no` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '下发测点编码_日_占比',
                                      `point_no_month` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '下发测点编码_月_占比',
                                      `point_no_total` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '下发测点编码_日_峰/谷耗电量',
                                      `power_no` varchar(36) DEFAULT NULL COMMENT '功率点位',
                                      `create_time` datetime DEFAULT NULL COMMENT '创建时间',
                                      PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='峰谷平累计计算配置';
ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunBatTask.java
对比新文件
@@ -0,0 +1,43 @@
package com.iailab.module.ansteel.job.task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 * 30 0 4 * * ?
 *
 * @author PanZhibao
 * @Description
 * @createTime 2025年03月09日
 */
@Component("runBatTask")
public class RunBatTask implements ITask {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public void run(String params) {
        logger.info("runBatTask定时任务正在执行,参数为:{}", params);
        try {
            String command = "cmd /c start " + params;
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                logger.info(line);
            }
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("runBatTask运行完成");
    }
}
ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunPeakValleyFlatTask.java
@@ -1,16 +1,21 @@
package com.iailab.module.ansteel.job.task;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iailab.module.ansteel.api.dto.PowerPriceDetDTO;
import com.iailab.module.ansteel.api.dto.PowerPriceMainDTO;
import com.iailab.module.ansteel.power.dao.PeakValleyFlatDao;
import com.iailab.module.ansteel.power.entity.PeakValleyFlatEntity;
import com.iailab.module.ansteel.power.service.PowerPriceMainService;
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 com.iailab.module.data.api.point.dto.ApiPointValueWriteDTO;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.List;
@@ -23,6 +28,7 @@
 * @Description
 * @createTime 2025年5月5日
 */
@Slf4j
@Component("runPeakValleyFlatTask")
public class RunPeakValleyFlatTask implements ITask {
    private Logger logger = LoggerFactory.getLogger(getClass());
@@ -33,66 +39,82 @@
    @Autowired
    private DataPointApi dataPointApi;
    @Autowired
    private PowerPriceMainService powerPriceMainService;
    @Override
    public void run(String params) {
        try {
            Calendar calendar = Calendar.getInstance();
            //查询峰谷平配置列表
            List<PeakValleyFlatEntity> list = peakValleyFlatDao.selectList(new QueryWrapper<>());
            List<PeakValleyFlatEntity> configList = peakValleyFlatDao.selectList(new QueryWrapper<>());
            if (CollectionUtils.isEmpty(configList)) {
                log.error("峰谷平累计量计算失败:峰谷平配置列表configList为空");
                return;
            }
            // 峰谷平时段
            List<PowerPriceMainDTO> powerPriceMainDTOList = powerPriceMainService.list(new HashMap<>());
            List<PowerPriceDetDTO> detList = new ArrayList<>();
            for (PowerPriceMainDTO powerPriceMainDTO : powerPriceMainDTOList) {
                detList.addAll(powerPriceMainDTO.getDetList());
            }
            if (CollectionUtils.isEmpty(detList)) {
                log.error("峰谷平累计量计算失败:时段detList为空");
                return;
            }
            Map<String, List<PowerPriceDetDTO>> typePriceDetMap = detList.stream().collect(Collectors.groupingBy(PowerPriceDetDTO::getName));
            // 当天零点
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.MILLISECOND, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            //根据测点分组
            Map<String, List<PeakValleyFlatEntity>> groupedByPointNo = list.stream()
                    .collect(Collectors.groupingBy(PeakValleyFlatEntity::getPointNo));
            groupedByPointNo.entrySet().stream().forEach(entry -> {
                Calendar cal = (Calendar) calendar.clone();
                cal.set(Calendar.MILLISECOND, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                Date endTime = calendar.getTime();
            Date endTime = cal.getTime();
                cal.add(Calendar.DAY_OF_YEAR, -1);
                Date startTime = cal.getTime();
                cal.add(Calendar.DAY_OF_YEAR, -27);
                Date monthStartTime_29 = cal.getTime();
                cal.add(Calendar.DAY_OF_YEAR, -2);
                Date monthStartTime_30 = cal.getTime();
            for (PeakValleyFlatEntity configEntity : configList) {
                String type = configEntity.getType();
                String powerNo = configEntity.getPowerNo();
                //计算昨日的峰/谷累积量
                double value = getSumValue(entry.getValue(), 1, calendar);
                double value = getSumValue(typePriceDetMap.get(type), 1, calendar, powerNo);
                //计算昨日总电耗
                double totalValue = getSumValueTotal(entry.getValue().get(0).getPowerNo(), startTime, endTime,1440,60);
                double totalValue = getSumValueTotal(powerNo, startTime, endTime,1440,60);
                //计算前29日峰/谷累积量 + 今日
                double valueMonth = getSumValueTotal(entry.getValue().get(0).getPointNoTotal(), monthStartTime_29, endTime,29,1)+value;
                double valueMonth = getSumValueTotal(configEntity.getPointNoTotal(), monthStartTime_29, endTime,29,1)+value;
                //计算前三十日总电耗
                double totalValueMonth = getSumValueTotal(entry.getValue().get(0).getPowerNo(), monthStartTime_30, endTime,1440 * 30,60);
                double totalValueMonth = getSumValueTotal(configEntity.getPowerNo(), monthStartTime_30, endTime,1440 * 30,60);
                logger.info("name:"+entry.getValue().get(0).getName()+";value:"+value+";totalValue:"+totalValue+";valueMonth:"+valueMonth+";totalValueMonth:"+totalValueMonth);
                logger.info("name:"+configEntity.getName()+";value:"+value+";totalValue:"+totalValue+";valueMonth:"+valueMonth+";totalValueMonth:"+totalValueMonth);
                //下发昨日占比
                ApiPointValueWriteDTO percentDto = new ApiPointValueWriteDTO();
                percentDto.setPointNo(entry.getValue().get(0).getPointNo());
                percentDto.setPointNo(configEntity.getPointNo());
                double percent = totalValue == 0 ? 0 : value / totalValue * 100;
                percentDto.setValue(percent);
                dataPointApi.writePointRealValue(percentDto);
                //下发昨日峰/谷累积量
                ApiPointValueWriteDTO totalDto = new ApiPointValueWriteDTO();
                totalDto.setPointNo(entry.getValue().get(0).getPointNoTotal());
                totalDto.setPointNo(configEntity.getPointNoTotal());
                totalDto.setValue(value);
                dataPointApi.writePointRealValue(totalDto);
                //下发前三十日占比
                ApiPointValueWriteDTO monthDto = new ApiPointValueWriteDTO();
                monthDto.setPointNo(entry.getValue().get(0).getPointNoMonth());
                monthDto.setPointNo(configEntity.getPointNoMonth());
                double percentMonth = totalValueMonth == 0 ? 0 : valueMonth / totalValueMonth * 100;
                monthDto.setValue(percentMonth);
                dataPointApi.writePointRealValue(monthDto);
            });
            }
        } catch (Exception ex) {
            logger.error("runPeakValleyFlatTask运行异常", ex);
        }
@@ -105,52 +127,10 @@
            throw new IllegalArgumentException("时间配置格式不合法");
        }
        //根据配置获取startTime、endTime
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeSplit[0]));
        cal.set(Calendar.MINUTE, Integer.parseInt(timeSplit[1]));
        cal.add(Calendar.DAY_OF_YEAR, -ago);
        return cal.getTime();
    }
    private List<ApiPointValueDTO> fillMissingData(List<ApiPointValueDTO> valueList,
                                                   Date startTime,
                                                   Date endTime) {
        if (valueList == null || valueList.isEmpty()) {
            return new ArrayList<>();
        }
        // 过滤掉null元素
        valueList = valueList.stream().filter(Objects::nonNull).collect(Collectors.toList());
        if (valueList.isEmpty()) {
            return new ArrayList<>();
        }
        List<ApiPointValueDTO> filledList = new ArrayList<>();
        ApiPointValueDTO lastValidData = null;
        long totalMinutes = (endTime.getTime() - startTime.getTime()) / (60 * 1000);
        for (int i = 0; i <= totalMinutes; i++) {
            Date currentTime = new Date(startTime.getTime() + i * 60 * 1000);
            ApiPointValueDTO currentData = findDataForTime(valueList, currentTime);
            if (currentData != null) {
                filledList.add(currentData);
                lastValidData = currentData;
            } else if (lastValidData != null) {
                filledList.add(new ApiPointValueDTO()
                        .setT(currentTime)
                        .setV(lastValidData.getV()));
            } else {
                // 使用第一个非null数据的值
                filledList.add(new ApiPointValueDTO()
                        .setT(currentTime)
                        .setV(valueList.get(0).getV()));
            }
        }
        return filledList;
    }
    private ApiPointValueDTO findDataForTime(List<ApiPointValueDTO> valueList, Date time) {
@@ -163,19 +143,19 @@
                .orElse(null);
    }
    private double getSumValue(List<PeakValleyFlatEntity> list, int ago, Calendar calendar) {
    private double getSumValue(List<PowerPriceDetDTO> list, int ago, Calendar calendar,String powerNo) {
        double value = 0;
        for (int i = 0; i < list.size(); i++) {
            PeakValleyFlatEntity entity = list.get(i);
            PowerPriceDetDTO entity = list.get(i);
            ApiPointValueQueryDTO dto = new ApiPointValueQueryDTO();
            Date startTime = getTime(entity.getStartTime(), ago, calendar);
            Date endTime = getTime(entity.getEndTime(), ago, calendar);
            dto.setPointNo(entity.getPowerNo());
            Date startTime = getTime(entity.getStart(), ago, calendar);
            Date endTime = getTime(entity.getEnd(), ago, calendar);
            dto.setPointNo(powerNo);
            dto.setStart(startTime);
            dto.setEnd(endTime);
            logger.info("开始查询,测点:" + entity.getPowerNo() + "startTime:" + startTime + "endTime:" + endTime);
            logger.info("开始查询,测点:" + powerNo + "startTime:" + startTime + "endTime:" + endTime);
            List<ApiPointValueDTO> valueList;
            //查找数据
            try {
@@ -183,8 +163,6 @@
            } catch (Exception e) {
                throw new RuntimeException("查询测点异常");
            }
            //补全数据
//            valueList = fillMissingData(valueList, startTime, endTime);
            // 计算 startTime和endTime中有多少个点
            long total = (endTime.getTime() - startTime.getTime()) / (60 * 1000);
            double sum = valueList.stream().filter(e -> Double.compare(e.getV(), 0.0) > 0).mapToDouble(ApiPointValueDTO::getV).average().orElse(0.0) * Long.valueOf(total).doubleValue();
@@ -203,8 +181,7 @@
        //查找数据
        List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(dto);
        //补全数据
//        valueList = fillMissingData(valueList, startTime, endTime);
        // 平均值 * 总数
        double value = valueList.stream().filter(e -> Double.compare(e.getV(), 0.0) > 0).mapToDouble(ApiPointValueDTO::getV).average().orElse(0.0) * Double.valueOf(total);
        //累加
ansteel-biz/src/main/java/com/iailab/module/ansteel/power/entity/PeakValleyFlatEntity.java
@@ -29,7 +29,7 @@
     */
    private String name;
    /**
     * 类型(peak/vallay/flat)
     * 类型(尖,峰,谷,平)
     */
    private String type;
    /**
@@ -48,14 +48,6 @@
     * 功率测点编码
     */
    private String powerNo;
    /**
     * 开始时间
     */
    private String startTime;
    /**
     * 结束时间
     */
    private String endTime;
    /**
     * 创建时间
     */