From 2f5864f98743fcc1b32d1eb94015adf70c1a9bf5 Mon Sep 17 00:00:00 2001
From: 潘志宝 <979469083@qq.com>
Date: 星期一, 05 五月 2025 20:11:51 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunPeakValleyFlatTask.java |  197 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 197 insertions(+), 0 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
new file mode 100644
index 0000000..ea3f8d8
--- /dev/null
+++ b/ansteel-biz/src/main/java/com/iailab/module/ansteel/job/task/RunPeakValleyFlatTask.java
@@ -0,0 +1,197 @@
+package com.iailab.module.ansteel.job.task;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.iailab.module.ansteel.power.dao.PeakValleyFlatDao;
+import com.iailab.module.ansteel.power.entity.PeakValleyFlatEntity;
+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 org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.*;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * 峰谷平累计量计算
+ *
+ * @author DongYukun
+ * @Description
+ * @createTime 2025年5月5日
+ */
+@Component("runPeakValleyFlatTask")
+public class RunPeakValleyFlatTask implements ITask {
+    private Logger logger = LoggerFactory.getLogger(getClass());
+
+    @Autowired
+    private PeakValleyFlatDao peakValleyFlatDao;
+
+    @Autowired
+    private DataPointApi dataPointApi;
+
+    @Override
+    public void run(String params) {
+        try {
+            Calendar calendar = Calendar.getInstance();
+
+            //查询峰谷平配置列表
+            List<PeakValleyFlatEntity> list = peakValleyFlatDao.selectList(new QueryWrapper<>());
+
+            //根据测点分组
+            Map<String, List<PeakValleyFlatEntity>> groupedByPointNo = list.stream()
+                    .collect(Collectors.groupingBy(PeakValleyFlatEntity::getPowerNo));
+
+            groupedByPointNo.entrySet().stream().forEach(entry -> {
+
+                //计算昨日的累积量
+                double value = getSumValue(entry.getValue(), 1, calendar);
+
+                //计算昨日总电耗
+                calendar.set(Calendar.MILLISECOND, 0);
+                calendar.set(Calendar.MINUTE, 0);
+                calendar.set(Calendar.HOUR_OF_DAY, 0);
+                Date endTime = calendar.getTime();
+                calendar.add(Calendar.DAY_OF_YEAR, -1);
+                Date startTime = calendar.getTime();
+                double totalValue = getSumValueTotal(entry.getValue().get(0).getPowerNo(), startTime,endTime);
+
+                //下发昨日占比
+                ApiPointValueWriteDTO dto = new ApiPointValueWriteDTO();
+                dto.setPointNo(entry.getValue().get(0).getPointNo());
+                dto.setValue(value/totalValue*100);
+                dataPointApi.writePointRealValue(dto);
+
+                if(entry.getValue().get(0).getPointNoMonth()!=null){
+                    //计算前三十日累积量
+                    double value30 = 0;
+                    for (int i = 1; i < 31; i++) {
+                        //计算前三十日累积量
+                        value30 = value30 + getSumValue(entry.getValue(), i, calendar);
+                    }
+
+                    //计算前三十日总电耗
+                    calendar.set(Calendar.MILLISECOND, 0);
+                    calendar.set(Calendar.MINUTE, 0);
+                    calendar.set(Calendar.HOUR_OF_DAY, 0);
+                    calendar.add(Calendar.DAY_OF_YEAR, -30);
+                    Date startTimeMonth = calendar.getTime();
+                    double totalValueMonth = getSumValueTotal(entry.getValue().get(0).getPowerNo(), startTimeMonth,endTime);
+
+                    //下发前三十日占比
+                    ApiPointValueWriteDTO monthDto = new ApiPointValueWriteDTO();
+                    monthDto.setPointNo(entry.getValue().get(0).getPointNoMonth());
+                    monthDto.setValue(value30/totalValueMonth*100);
+                    dataPointApi.writePointRealValue(monthDto);
+                }
+            });
+        } catch (Exception ex) {
+            logger.error("runPeakValleyFlatTask运行异常");
+            ex.printStackTrace();
+        }
+    }
+
+    private Date getTime(String timeStr, int ago, Calendar calendar) {
+        String[] timeSplit = timeStr.split(":");
+        if (timeSplit.length != 2) {
+            throw new IllegalArgumentException("时间配置格式不合法");
+        }
+        //根据配置获取startTime、endTime
+        calendar.set(Calendar.MILLISECOND, 0);
+        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeSplit[0]));
+        calendar.set(Calendar.MINUTE, Integer.parseInt(timeSplit[1]));
+        calendar.add(Calendar.DAY_OF_YEAR, -ago);
+        return calendar.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) {
+        long targetMinute = time.getTime() / (60 * 1000);
+        return valueList.stream()
+                .filter(Objects::nonNull)
+                .filter(d -> d.getT() != null)
+                .filter(d -> d.getT().getTime() / (60 * 1000) == targetMinute)
+                .findFirst()
+                .orElse(null);
+    }
+
+    private double getSumValue(List<PeakValleyFlatEntity> list, int ago, Calendar calendar) {
+        double value = 0;
+        for (int i = 0; i < list.size(); i++) {
+
+            PeakValleyFlatEntity 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());
+            dto.setStart(startTime);
+            dto.setEnd(endTime);
+
+            //查找数据
+            List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(dto);
+            //补全数据
+            valueList = fillMissingData(valueList, startTime, endTime);
+            //累加
+            double sum = valueList.stream().mapToDouble(ApiPointValueDTO::getV).sum();
+            value = value + sum;
+        }
+        return value / 60;
+    }
+
+    private double getSumValueTotal(String pointNo, Date startTime, Date endTime) {
+        ApiPointValueQueryDTO dto = new ApiPointValueQueryDTO();
+        dto.setPointNo(pointNo);
+        dto.setStart(startTime);
+        dto.setEnd(endTime);
+
+        //查找数据
+        List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(dto);
+        //补全数据
+        valueList = fillMissingData(valueList, startTime, endTime);
+        //累加
+        return valueList.stream().mapToDouble(ApiPointValueDTO::getV).sum()/ 60;
+    }
+}

--
Gitblit v1.9.3