From dd1ba0fac25de2328ed6b92bfe5c82a7822572d7 Mon Sep 17 00:00:00 2001
From: dengzedong <dengzedong@email>
Date: 星期二, 27 五月 2025 09:01:29 +0800
Subject: [PATCH] RunBatTask 峰谷平耗电量关联 峰谷平时段表

---
 ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunPeakValleyFlatTask.java |  139 +++++++++++++++++++---------------------------
 1 files changed, 58 insertions(+), 81 deletions(-)

diff --git a/ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunPeakValleyFlatTask.java b/ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunPeakValleyFlatTask.java
index b61a29e..6c4a13b 100644
--- a/ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunPeakValleyFlatTask.java
+++ b/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();
-                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();
+            Calendar cal = (Calendar) calendar.clone();
+            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);
 
         //累加

--
Gitblit v1.9.3