鞍钢鲅鱼圈能源管控系统后端代码
潘志宝
3 天以前 77b06515edaa46cbbb0e55fe5f37db09aa8991fd
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package com.iailab.module.ansteel.api.controller.admin;
 
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONObject;
import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.iailab.framework.common.pojo.CommonResult;
import com.iailab.framework.common.util.date.DateUtils;
import com.iailab.framework.common.util.object.ConvertUtils;
import com.iailab.module.ansteel.api.dto.*;
import com.iailab.module.ansteel.power.entity.*;
import com.iailab.module.ansteel.power.service.*;
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.ApiPointsValueQueryDTO;
import com.iailab.module.model.api.mcs.McsApi;
import com.iailab.module.model.api.mcs.dto.PredictLastValueReqVO;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
import static com.iailab.framework.common.pojo.CommonResult.success;
 
/**
 * 电力接口
 *
 * @author PanZhibao
 * @Description
 * @createTime 2025年04月11日
 */
@Slf4j
@RestController
@RequestMapping("/ansteel/api/power")
public class PowerController {
 
    @Autowired
    private PowerNetFactorService powerNetFactorService;
 
    @Autowired
    private PowerCapacitorStatusService powerCapacitorStatusService;
 
    @Autowired
    private PowerControlMainService powerControlMainService;
 
    @Autowired
    private PowerControlDetService powerControlDetService;
 
    @Autowired
    private PowerGenStatusDaoService powerGenStatusDaoService;
 
    @Autowired
    private PowerDemandService powerDemandService;
 
    @Autowired
    private PowerAdjustedFactorService powerAdjustedFactorService;
 
    @Resource
    private DataPointApi dataPointApi;
 
    @Resource
    private McsApi mcsApi;
 
    @GetMapping("/net-factor/list")
    @Operation(summary = "功率因数-电网拓扑")
    public CommonResult<List<PowerNetFactorDTO>> getPowerNetFactorList(@RequestParam Map<String, Object> params) {
        List<PowerNetFactorEntity> list = powerNetFactorService.list(params);
        List<PowerNetFactorDTO> result = ConvertUtils.sourceToTarget(list, PowerNetFactorDTO.class);
        if (CollectionUtils.isEmpty(result)) {
            return success(result);
        }
        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);
 
        for (PowerNetFactorDTO dto : result) {
            boolean cosFlag = false;
            try {
                List<String> points = new ArrayList<>();
                if (StringUtils.isNotBlank(dto.getCurP())) {
                    points.add(dto.getCurP());
                }
                if (StringUtils.isNotBlank(dto.getCurQ())) {
                    points.add(dto.getCurQ());
                }
                if (!CollectionUtils.isEmpty(points)) {
                    Map<String, Object> pointsRealValue = dataPointApi.queryPointsRealValue(points);
                    if (pointsRealValue.get(dto.getCurP()) != null) {
                        dto.setCurP(pointsRealValue.get(dto.getCurP()).toString());
                    }
                    if (pointsRealValue.get(dto.getCurQ()) != null) {
                        dto.setCurQ(pointsRealValue.get(dto.getCurQ()).toString());
                    }
                    if (pointsRealValue.get(dto.getCurCos()) != null) {
                        dto.setCurCos(pointsRealValue.get(dto.getCurCos()).toString());
                        cosFlag = true;
                    }
                }
            } catch (Exception ex) {
                log.info(dto.getNodeName() + "获取当前值异常" + ex.getMessage());
            }
 
            try {
                PredictLastValueReqVO reqVO = new PredictLastValueReqVO();
                reqVO.setPredictTime(calendar.getTime());
                List<String[]> itemNos = new ArrayList<>();
                if (StringUtils.isNotBlank(dto.getPreP())) {
                    itemNos.add(dto.getPreP().split(","));
                }
                if (StringUtils.isNotBlank(dto.getPreQ())) {
                    itemNos.add(dto.getPreQ().split(","));
                }
                if (StringUtils.isNotBlank(dto.getPreCos())) {
                    itemNos.add(dto.getPreCos().split(","));
                }
                if (!CollectionUtils.isEmpty(itemNos)) {
                    reqVO.setItemNos(itemNos);
                    log.info("reqVO=" + JSONObject.toJSONString(reqVO));
                    Map<String, BigDecimal> preValues = mcsApi.getPredictValueByTime(reqVO);
                    if (StringUtils.isNotBlank(dto.getPreP()) && preValues.get(dto.getPreP()) != null) {
                        dto.setPreP(preValues.get(dto.getPreP()).toString());
                    }
                    if (StringUtils.isNotBlank(dto.getPreQ()) && preValues.get(dto.getPreQ()) != null) {
                        dto.setPreQ(preValues.get(dto.getPreQ()).toString());
                    }
                    if (StringUtils.isNotBlank(dto.getPreCos()) && preValues.get(dto.getPreCos()) != null) {
                        dto.setPreCos(preValues.get(dto.getPreCos()).toString());
                    }
                }
            } catch (Exception ex) {
                log.info(dto.getNodeName() + "获取预测值异常," + ex.getMessage());
            }
 
            // 设置状态
            if (cosFlag && StringUtils.isNotBlank(dto.getCurCos()) && NumberUtil.isNumber(dto.getCurCos())) {
                BigDecimal curCos = new BigDecimal(dto.getCurCos());
                if (dto.getLimitL() != null && dto.getLimitH() != null &&
                        curCos.compareTo(dto.getLimitL()) < 0 || curCos.compareTo(dto.getLimitH()) > 0) {
                    dto.setStatus(1);
                } else {
                    dto.setStatus(0);
                }
            }
        }
        return success(result);
    }
 
    @GetMapping("/net-factor-dropdown/list")
    @Operation(summary = "功率因数-电网拓扑下拉列表")
    public CommonResult<List<PowerNetFactorDropdownDTO>> getPowerNetFactorDropdownList(@RequestParam String nodeName) {
        List<PowerNetFactorEntity> list = powerNetFactorService.listDropdown(nodeName);
        List<PowerNetFactorDropdownDTO> result = ConvertUtils.sourceToTarget(list, PowerNetFactorDropdownDTO.class);
        return success(result);
    }
 
    /**
     * 判断 curCos 是否超上下限
     * {nodeName}功率因数超上限/下限。
     * <p>
     * 判断 curQ
     * {nodeName}发生无功返送
     */
    @GetMapping("/net-factor/alarm")
    @Operation(summary = "功率因数-电网拓扑预警信息")
    public CommonResult<List<String>> getPowerNetFactorAlarm(@RequestParam Map<String, Object> params) {
        List<String> result = new ArrayList<>();
        List<PowerNetFactorEntity> list = powerNetFactorService.list(params);
        List<PowerNetFactorDTO> dtoList = ConvertUtils.sourceToTarget(list, PowerNetFactorDTO.class);
        if (CollectionUtils.isEmpty(dtoList)) {
            return success(result);
        }
        for (PowerNetFactorDTO dto : dtoList) {
            List<String> points = new ArrayList<>();
            String message = "";
            if (StringUtils.isNotBlank(dto.getCurQ())) {
                points.add(dto.getCurQ());
            }
            if (StringUtils.isNotBlank(dto.getCurCos())) {
                points.add(dto.getCurCos());
            }
            if (CollectionUtils.isEmpty(points)) {
                continue;
            }
 
            Map<String, Object> pointsRealValue = dataPointApi.queryPointsRealValue(points);
            if (pointsRealValue.get(dto.getCurQ()) != null) {
                BigDecimal curQ = new BigDecimal(pointsRealValue.get(dto.getCurQ()).toString());
                if (curQ.compareTo(BigDecimal.ZERO) == dto.getCurFlag()) {
                    message = dto.getNodeName() + "发生无功返送;";
                }
            }
            if (pointsRealValue.get(dto.getCurCos()) != null) {
                BigDecimal curCos = new BigDecimal(pointsRealValue.get(dto.getCurCos()).toString());
                if (curCos.compareTo(dto.getLimitH()) > 0) {
                    message += dto.getNodeName() + "功率因数超上限";
                } else if (curCos.compareTo(dto.getLimitL()) < 0) {
                    message += dto.getNodeName() + "功率因数超下限";
                }
            }
            if (!message.isEmpty()) {
                result.add(message);
            }
        }
        return success(result);
    }
 
    @GetMapping("/gen-status/list")
    @Operation(summary = "功率因数-发电机组实时状态")
    public CommonResult<List<PowerGenStatusDTO>> getPowerGenStatusList(@RequestParam Map<String, Object> params) {
        List<PowerGenStatusEntity> list = powerGenStatusDaoService.list(params);
        List<PowerGenStatusDTO> result = ConvertUtils.sourceToTarget(list, PowerGenStatusDTO.class);
        if (CollectionUtils.isEmpty(result)) {
            return success(result);
        }
        for (PowerGenStatusDTO dto : result) {
            List<String> points = new ArrayList<>();
            if (StringUtils.isNotBlank(dto.getCurP())) {
                points.add(dto.getCurP());
            }
            if (StringUtils.isNotBlank(dto.getCurQ())) {
                points.add(dto.getCurQ());
            }
            if (!CollectionUtils.isEmpty(points)) {
                Map<String, Object> pointsRealValue = dataPointApi.queryPointsRealValue(points);
                if (pointsRealValue.get(dto.getCurP()) != null) {
                    dto.setCurP(pointsRealValue.get(dto.getCurP()).toString());
                }
                if (pointsRealValue.get(dto.getCurQ()) != null) {
                    dto.setCurQ(pointsRealValue.get(dto.getCurQ()).toString());
                }
            }
        }
 
        return success(result);
    }
 
    @GetMapping("/capacitor-status/list")
    @Operation(summary = "功率因数-电容器投运状态")
    public CommonResult<List<PowerCapacitorStatusDTO>> getPowerCapacitorStatusList(@RequestParam Map<String, Object> params) {
        List<PowerCapacitorStatusEntity> list = powerCapacitorStatusService.list(params);
        return success(ConvertUtils.sourceToTarget(list, PowerCapacitorStatusDTO.class));
    }
 
    @GetMapping("/control-main/list")
    @Operation(summary = "功率因数-管控变电站列表")
    public CommonResult<List<PowerControlMainDTO>> getPowerControlMainList(@RequestParam Map<String, Object> params) {
        List<PowerControlMainEntity> list = powerControlMainService.list(params);
        return success(ConvertUtils.sourceToTarget(list, PowerControlMainDTO.class));
    }
 
    @PostMapping("/control-main/update")
    @Operation(summary = "功率因数-管控变电站修改上下限")
    public CommonResult<Boolean> updatePowerControlMain(@RequestBody PowerControlMainDTO dto) {
        if (StringUtils.isBlank(dto.getId())) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        PowerControlMainEntity entity = new PowerControlMainEntity();
        entity.setId(dto.getId());
        entity.setLimitH(dto.getLimitH());
        entity.setLimitL(dto.getLimitL());
        powerControlMainService.update(entity);
        return success(true);
    }
 
    @GetMapping("/control-det/list")
    @Operation(summary = "功率因数-管控功率因数详情")
    public CommonResult<List<PowerControlDetDTO>> getPowerControlDetList(@RequestParam Map<String, Object> params) {
        List<PowerControlDetDTO> result = new ArrayList<>();
        String name = (String) params.get("name");
        if (StringUtils.isBlank(name)) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        PowerControlMainEntity main = powerControlMainService.getByName(name);
        if (main == null) {
            return CommonResult.error(GlobalErrorCodeConstants.NOT_FOUND);
        }
        List<PowerControlDetEntity> list = powerControlDetService.list(main.getId());
        result = ConvertUtils.sourceToTarget(list, PowerControlDetDTO.class);
 
        result.forEach(item -> {
            // 设置随机数据,0.8左右
            Random rand = new Random();
            int min = 700;
            int max = 900;
            int randomNumber = rand.nextInt(max - min + 1) + min;
            BigDecimal rv = new BigDecimal(randomNumber * 0.001).setScale(4, BigDecimal.ROUND_HALF_UP);
            item.setValue(rv);
            if (item.getLimitL() != null && rv.compareTo(item.getLimitL()) < 0) {
                item.setStatus(1);
            } else {
                item.setStatus(0);
            }
        });
        return success(result);
    }
 
    @GetMapping("/demand/list")
    @Operation(summary = "负荷移植-月最大需量,实测需量,有功功率")
    public CommonResult<List<PowerDemandDTO>> getPowerDemandList(@RequestParam Map<String, Object> params) {
        List<PowerDemandEntity> list = powerDemandService.list(params);
        List<PowerDemandDTO> result = ConvertUtils.sourceToTarget(list, PowerDemandDTO.class);
        if (CollectionUtils.isEmpty(result)) {
            return success(result);
        }
        for (PowerDemandDTO dto : result) {
            List<String> points = new ArrayList<>();
            if (StringUtils.isNotBlank(dto.getCurDemand())) {
                points.add(dto.getCurDemand());
            }
            if (StringUtils.isNotBlank(dto.getActivePower())) {
                points.add(dto.getActivePower());
            }
 
            if (!CollectionUtils.isEmpty(points)) {
 
                Map<String, Object> pointsRealValue = dataPointApi.queryPointsRealValue(points);
 
                if (pointsRealValue.get(dto.getCurDemand()) != null) {
                    dto.setCurDemand(pointsRealValue.get(dto.getCurDemand()).toString());
                }
                if (pointsRealValue.get(dto.getActivePower()) != null) {
                    dto.setActivePower(pointsRealValue.get(dto.getActivePower()).toString());
                }
            }
 
            if (!StringUtils.isEmpty(dto.getMaxDemand())) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                Date start = calendar.getTime();
                ApiPointValueQueryDTO apiPointValueQueryDTO = new ApiPointValueQueryDTO();
                apiPointValueQueryDTO.setStart(start);
                apiPointValueQueryDTO.setEnd(new Date());
                apiPointValueQueryDTO.setPointNo(dto.getMaxDemand());
 
                List<ApiPointValueDTO> monthValues = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
                double max = 0;
                for (int i = 0; i < monthValues.size() - 1; i++) {
                    if (max < monthValues.get(i).getV()) {
                        max = monthValues.get(i).getV();
                    }
                }
                dto.setMaxDemand(String.valueOf(max));
            }
 
 
        }
 
        return success(result);
    }
 
    @PostMapping("/net-factor/history")
    @Operation(summary = "功率因数-根据nodeName获取最近1440min历史数据,月最大,最小值")
    public CommonResult<PowerHistoryDTO> getPowerHistoryData(@RequestBody PowerNetFactorHisReqDTO dto) {
        log.info("PowerNetFactorHisReqDTO=" + JSONObject.toJSONString(dto));
        String nodeCode = dto.getNodeCode();
        if (StringUtils.isBlank(nodeCode)) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        String queryType = dto.getQueryType();
        if (StringUtils.isBlank(queryType)) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        PowerNetFactorEntity powerNetFactor = powerNetFactorService.getByNodeCode(nodeCode);
        PowerHistoryDTO result = new PowerHistoryDTO();
        if (powerNetFactor == null) {
            log.info("powerNetFactor is null");
            return success(result);
        }
        log.info("开始查询,");
 
        ApiPointValueQueryDTO apiPointValueQueryDTO = new ApiPointValueQueryDTO();
        String pointNo = "";
        switch (queryType.toUpperCase()) {
            case "P":
                pointNo = powerNetFactor.getCurP();
                break;
            case "Q":
                pointNo = powerNetFactor.getCurQ();
                break;
            case "COS":
                pointNo = powerNetFactor.getCurCos();
                break;
            default:
                break;
        }
        //查询图表
        apiPointValueQueryDTO.setPointNo(pointNo);
        Calendar calendar0 = Calendar.getInstance();
        calendar0.set(Calendar.MILLISECOND, 0);
        calendar0.set(Calendar.SECOND, 0);
        Date end = dto.getEndTime() == null ? calendar0.getTime() : dto.getEndTime();
        calendar0.add(Calendar.MINUTE, -1440);
        Date start = dto.getStartTime() == null ?  calendar0.getTime() : dto.getStartTime();
        apiPointValueQueryDTO.setEnd(end);
        apiPointValueQueryDTO.setStart(start);
        List<ApiPointValueDTO> chartData = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
        List<Object[]> dataList = new ArrayList<>();
        for (ApiPointValueDTO pv : chartData) {
            Object[] data = new Object[2];
            data[0] = DateUtils.format(pv.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
            data[1] = pv.getV();
            dataList.add(data);
        }
        List<String> categories = DateUtils.getTimeScale(start, end, 60);
        result.setCategories(categories);
        result.setDataList(dataList);
 
        //查询月最大最小值
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        start = calendar.getTime();
        apiPointValueQueryDTO.setStart(start);
        apiPointValueQueryDTO.setEnd(new Date());
        List<ApiPointValueDTO> monthChartData = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
        List<Double> monthValues = new ArrayList<>();
        if (CollectionUtils.isEmpty(monthChartData)) {
            monthValues =  monthChartData.stream().map(item -> {
                return item.getV();
            }).collect(Collectors.toList());
 
            result.setMax(monthValues.stream().max(Double::compareTo).get());
            result.setMin(monthValues.stream().min(Double::compareTo).get());
        }
        return success(result);
    }
 
    private double getMin(List<List<Object>> dataList) {
        double result = Double.parseDouble(dataList.get(0).get(1).toString());
        for (int i = 0; i < dataList.size() - 1; i++) {
            if (result > Double.parseDouble(dataList.get(i).get(1).toString())) {
                result = Double.parseDouble(dataList.get(i).get(1).toString());
            }
        }
        return result;
    }
 
    private double getMax(List<List<Object>> dataList) {
        double result = 0;
        for (int i = 0; i < dataList.size() - 1; i++) {
            if (result < Double.parseDouble(dataList.get(i).get(1).toString())) {
                result = Double.parseDouble(dataList.get(i).get(1).toString());
            }
        }
        return result;
    }
 
    @GetMapping("/adjust-factor")
    @Operation(summary = "功率因数-调整后的功率因数与无功倒送量")
    public CommonResult<Map<String, Double>> getPowerAdjustFactor(@RequestParam Map<String, Object> params) {
        List<PowerAdjustedFactorEntity> list = powerAdjustedFactorService.list(params);
        List<PowerAdjustedFactorDTO> dtoList = ConvertUtils.sourceToTarget(list, PowerAdjustedFactorDTO.class);
        Map<String, Double> result = new HashMap<>();
        if (CollectionUtils.isEmpty(list)) {
            return success(result);
        }
        List<String> points = new ArrayList<>();
        Map<String, Object> dataMap = new HashMap<>();
        for (PowerAdjustedFactorDTO dto : dtoList) {
            if (StringUtils.isNotBlank(dto.getPointNo())) {
                points.add(dto.getPointNo());
            }
        }
        if (!CollectionUtils.isEmpty(points)) {
            dataMap = dataPointApi.queryPointsRealValue(points);
        }
        for (PowerAdjustedFactorDTO powerAdjustedFactorDTO : dtoList) {
            Double value = dataMap.get(powerAdjustedFactorDTO.getPointNo()) == null ? 0 : Double.parseDouble(dataMap.get(powerAdjustedFactorDTO.getPointNo()).toString());
            result.put(powerAdjustedFactorDTO.getName(), value);
        }
        return success(result);
    }
}