鞍钢鲅鱼圈能源管控系统后端代码
潘志宝
5 天以前 5646e9910ecaf10df4550b4d29fb0618415551f9
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
package com.iailab.module.ansteel.job.task;
 
import com.iailab.module.ansteel.common.utils.DecimalUtil;
import com.iailab.module.ansteel.power.entity.PowerDemandEntity;
import com.iailab.module.ansteel.power.entity.PowerMaxdemandDetEntity;
import com.iailab.module.ansteel.power.entity.PowerMaxdemandMainEntity;
import com.iailab.module.ansteel.power.entity.PowerNetDropdownEntity;
import com.iailab.module.ansteel.power.service.PowerDemandService;
import com.iailab.module.ansteel.power.service.PowerMaxdemandDetService;
import com.iailab.module.ansteel.power.service.PowerMaxdemandMainService;
import com.iailab.module.ansteel.power.service.PowerNetDropdownService;
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 org.apache.commons.lang3.StringUtils;
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 javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
 
/**
 * 检查每天的需量与这个月的最大值比较
 * 如果大于,则保存
 * 每天23:59执行
 *
 * @author PanZhibao
 * @Description
 * @createTime 2025年05月08日
 */
@Component("runPowerMaxdemandTask")
public class RunPowerMaxdemandTask implements ITask {
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    @Autowired
    private PowerDemandService powerDemandService;
 
    @Autowired
    private PowerMaxdemandMainService powerMaxdemandMainService;
 
    @Autowired
    private PowerNetDropdownService powerNetDropdownService;
 
    @Autowired
    private PowerMaxdemandDetService powerMaxdemandDetService;
 
    @Resource
    private DataPointApi dataPointApi;
 
    @Override
    public void run(String params) {
        logger.info("RunPowerMaxdemandTask start");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        Date nowTime = calendar.getTime();
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        Date dayStart = calendar.getTime();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date monStart = calendar.getTime();
        try {
            Map<String, Object> params0 = new HashMap<>();
            List<PowerDemandEntity> demandList = powerDemandService.list(params0);
            if (CollectionUtils.isEmpty(demandList)) {
                logger.info("demandList is empty");
                return;
            }
            for (PowerDemandEntity demand : demandList) {
                // 查询今天的最大需量
                ApiPointValueQueryDTO pointValueQuery0 = new ApiPointValueQueryDTO();
                pointValueQuery0.setStart(dayStart);
                pointValueQuery0.setEnd(nowTime);
                pointValueQuery0.setPointNo(demand.getCurDemand());
                Map<String, Object> maxValue = dataPointApi.queryPointMaxValue(pointValueQuery0);
                BigDecimal todayMax = BigDecimal.ZERO;
                if (CollectionUtils.isEmpty(maxValue) && maxValue.containsKey(demand.getCurDemand())) {
                    todayMax = new BigDecimal(maxValue.get(demand.getCurDemand()).toString());
                }
 
                List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(pointValueQuery0);
                if (CollectionUtils.isEmpty(valueList)) {
                    logger.info("valueList is empty");
                    continue;
                }
                Date occurTime = new Date(nowTime.getTime());
                for (ApiPointValueDTO apiPointValueDTO : valueList) {
                    if (Math.abs(apiPointValueDTO.getV() - todayMax.doubleValue()) < 0.001) {
                        occurTime = apiPointValueDTO.getT();
                        break;
                    }
                }
 
                // 查询本月需量发生记录
                BigDecimal monthMax = powerMaxdemandMainService.getMax(demand.getCode(), monStart, nowTime);
 
                // 保存记录
                if (todayMax.compareTo(monthMax) <= 0) {
                    continue;
                }
                PowerMaxdemandMainEntity mainEntity = new PowerMaxdemandMainEntity();
                mainEntity.setCode(demand.getCode());
                mainEntity.setName(demand.getName());
                mainEntity.setOccurTime(occurTime);
                mainEntity.setMaxDemand(todayMax);
                String mainId = powerMaxdemandMainService.add(mainEntity);
 
                Map<String, Object> params1 = new HashMap<>();
                params1.put("pCode", demand.getCode());
                List<PowerNetDropdownEntity> dropdownList = powerNetDropdownService.list(params1);
                if (CollectionUtils.isEmpty(dropdownList)) {
                    logger.info("dropdownList is empty");
                    continue;
                }
                for (PowerNetDropdownEntity dropdown : dropdownList) {
                    if (StringUtils.isBlank(dropdown.getExt1())) {
                        continue;
                    }
                    Calendar calendar1 = Calendar.getInstance();
                    calendar1.setTime(occurTime);
                    calendar1.add(Calendar.MINUTE, 1);
                    ApiPointValueQueryDTO pointValueQuery1 = new ApiPointValueQueryDTO();
                    pointValueQuery1.setStart(occurTime);
                    pointValueQuery1.setEnd(calendar1.getTime());
                    pointValueQuery1.setPointNo(dropdown.getExt1());
                    List<ApiPointValueDTO> valueList1 = dataPointApi.queryPointHistoryValue(pointValueQuery1);
                    if (CollectionUtils.isEmpty(valueList1)) {
                        logger.info("valueList1 is empty");
                        continue;
                    }
                    PowerMaxdemandDetEntity detEntity = new PowerMaxdemandDetEntity();
                    detEntity.setRelId(mainId);
                    detEntity.setCode(dropdown.getNodeCode());
                    detEntity.setName(dropdown.getNodeName());
                    detEntity.setOccurTime(occurTime);
                    detEntity.setMaxDemand(BigDecimal.valueOf(valueList1.get(0).getV()));
                    powerMaxdemandDetService.add(detEntity);
                }
            }
            logger.info("RunPowerMaxdemandTask end");
        } catch (Exception e) {
            logger.error("RunPowerMaxdemandTask error", e);
            e.printStackTrace();
        }
    }
}