鞍钢鲅鱼圈能源管控系统后端代码
liriming
7 天以前 2cf82d64aa7ac3b2837703a7afadcc811b03170e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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::getPointNo));
 
            groupedByPointNo.entrySet().stream().forEach(entry -> {
 
                //计算昨日的峰/谷累积量
                double value = getSumValue(entry.getValue(), 1, calendar);
 
                //计算昨日总电耗
                Calendar cal = (Calendar) calendar.clone();
                cal.set(Calendar.MILLISECOND, 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, -29);
                Date monthStartTime = cal.getTime();
                double totalValue = getSumValueTotal(entry.getValue().get(0).getPowerNo(), startTime, endTime);
 
                //下发昨日占比
                ApiPointValueWriteDTO percentDto = new ApiPointValueWriteDTO();
                percentDto.setPointNo(entry.getValue().get(0).getPointNo());
                double percent = totalValue == 0 ? 0 : value / totalValue * 100;
                percentDto.setValue(percent);
                dataPointApi.writePointRealValue(percentDto);
 
                //下发昨日峰/谷累积量
                ApiPointValueWriteDTO totalDto = new ApiPointValueWriteDTO();
                percentDto.setPointNo(entry.getValue().get(0).getPointNoTotal());
                percentDto.setValue(value);
                dataPointApi.writePointRealValue(totalDto);
 
                //计算前三十日峰/谷累积量
                double valueMonth = getSumValueTotal(entry.getValue().get(0).getPointNoTotal(), monthStartTime, endTime);
 
                //计算前三十日总电耗
                double totalValueMonth = getSumValueTotal(entry.getValue().get(0).getPowerNo(), monthStartTime, endTime);
 
                //下发前三十日占比
                ApiPointValueWriteDTO monthDto = new ApiPointValueWriteDTO();
                monthDto.setPointNo(entry.getValue().get(0).getPointNoMonth());
                double percentMonth = totalValueMonth == 0 ? 0 : valueMonth / totalValueMonth * 100;
                monthDto.setValue(percentMonth);
                dataPointApi.writePointRealValue(monthDto);
 
            });
        } catch (Exception ex) {
            logger.error("runPeakValleyFlatTask运行异常", ex);
        }
    }
 
    private Date getTime(String timeStr, int ago, Calendar calendar) {
        Calendar cal = (Calendar) calendar.clone();
        String[] timeSplit = timeStr.split(":");
        if (timeSplit.length != 2) {
            throw new IllegalArgumentException("时间配置格式不合法");
        }
        //根据配置获取startTime、endTime
        cal.set(Calendar.MILLISECOND, 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) {
        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);
            logger.info("开始查询,测点:" + entity.getPowerNo() + "startTime:" + startTime + "endTime:" + endTime);
            List<ApiPointValueDTO> valueList;
            //查找数据
            try {
                valueList = dataPointApi.queryPointHistoryValue(dto);
            } catch (Exception e) {
                throw new RuntimeException("查询测点异常");
            }
            //补全数据
            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;
    }
}