package com.iailab.module.ansteel.job.task; import cn.hutool.core.date.DateUtil; import com.alibaba.excel.util.DateUtils; 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.checkerframework.checker.units.qual.A; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.math.BigDecimal; import java.text.ParseException; import java.util.*; import java.util.stream.Collectors; import static com.alibaba.excel.util.DateUtils.DATE_FORMAT_16_FORWARD_SLASH; /** * 峰谷平累计量计算 * * @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 { //查询峰谷平配置列表 List list = peakValleyFlatDao.selectList(new QueryWrapper<>()); //根据测点分组 Map> groupedByPointNo = list.stream() .collect(Collectors.groupingBy(PeakValleyFlatEntity::getPowerNo)); groupedByPointNo.entrySet().stream().forEach(entry -> { //计算昨日的累积量 double value = getSumValue(entry.getValue(),1); ApiPointValueWriteDTO dto = new ApiPointValueWriteDTO(); dto.setPointNo(entry.getValue().get(0).getPointNo()); dto.setValue(value); dataPointApi.writePointRealValue(dto); //计算前三十日累积量 double value30 = 0 ; for (int i = 1; i < 31; i++) { value30 = value30 + getSumValue(entry.getValue(),i); } ApiPointValueWriteDTO monthDto = new ApiPointValueWriteDTO(); monthDto.setPointNo(entry.getValue().get(0).getPointNoMonth()); monthDto.setValue(value30); dataPointApi.writePointRealValue(monthDto); }); } catch (Exception ex) { logger.error("runPeakValleyFlatTask运行异常"); ex.printStackTrace(); } } private Date getTime(String timeStr , int ago) { String[] timeSplit = timeStr.split(":"); if (timeSplit.length != 2) { throw new IllegalArgumentException("时间配置格式不合法"); } //根据配置获取startTime、endTime、 Calendar calendar = Calendar.getInstance(); 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 fillMissingData(List valueList, Date startTime, Date endTime) { // 如果原始数据为空,返回空列表 if (valueList == null || valueList.isEmpty()) { return new ArrayList<>(); } List 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) { // 如果没有数据但有最近的有效数据,则补充 ApiPointValueDTO filledData = new ApiPointValueDTO() .setT(currentTime) .setV(lastValidData.getV()); filledList.add(filledData); } else { // 如果既没有当前数据也没有最近数据,查找第一个有值的数据 ApiPointValueDTO filledData = new ApiPointValueDTO() .setT(currentTime) .setV(valueList.get(0).getV()); filledList.add(filledData); lastValidData = filledData; } } return filledList; } private ApiPointValueDTO findDataForTime(List valueList, Date time) { for (ApiPointValueDTO data : valueList) { long time1 = data.getT().getTime() / (60 * 1000); long time2 = time.getTime() / (60 * 1000); if (time1 == time2) { return data; } } return null; } private double getSumValue( List list ,int ago){ 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); Date endTime = getTime(entity.getEndTime(),ago); dto.setPointNo(entity.getPowerNo()); dto.setStart(startTime); dto.setEnd(endTime); //查找数据 List valueList = dataPointApi.queryPointHistoryValue(dto); //补全数据 valueList = fillMissingData(valueList, startTime, endTime); //累加 double sum = valueList.stream().mapToDouble(ApiPointValueDTO::getV).sum(); value = value + sum; } return value/60; } }