鞍钢鲅鱼圈能源管控系统后端代码
潘志宝
2025-06-17 25bf9a00a2b369bd19ba0fdeed241b3158293700
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
package com.iailab.module.ansteel.api.controller.admin;
 
import com.iailab.framework.common.util.date.DateUtils;
import com.iailab.framework.excel.core.util.ExcelUtils;
import com.iailab.module.ansteel.api.dto.PowerFactorExcelDTO;
import com.iailab.module.ansteel.api.dto.PowerFactorExcelReqDTO;
import com.iailab.module.ansteel.api.vo.PowerExportCosVO;
import com.iailab.module.ansteel.api.vo.PowerExportPqVO;
import com.iailab.module.ansteel.common.utils.PowerUtil;
import com.iailab.module.ansteel.power.entity.PowerNetFactorEntity;
import com.iailab.module.ansteel.power.service.PowerNetFactorService;
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 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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
 
/**
 * 文件流接口
 *
 * @author PanZhibao
 * @Description
 * @createTime 2025年06月17日
 */
@Slf4j
@RestController
@RequestMapping("/ansteel/api/file")
public class FileController {
 
    @Autowired
    private PowerNetFactorService powerNetFactorService;
 
    @Autowired
    private DataPointApi dataPointApi;
 
    /**
     * 导出功率日累计
     * 月报表导出:最大值、最小值、平均值、受电累计、返送累计
     * 日功率因数二级界面:最大值、最小值、平均值、日功率因数
     * 月功率因数二级界面:最大值、最小值、平均值、月功率因数
     *
     * @param response
     * @param request
     * @param dto
     */
    @PostMapping("/net-factor/export-day")
    @Operation(summary = "功率因数-电网拓扑功率导出(功率日累计)")
    public void getPowerFactorExportDay(HttpServletResponse response, HttpServletRequest
            request, @RequestBody PowerFactorExcelReqDTO dto) throws IOException {
 
        List<PowerExportPqVO> exportPqList = new ArrayList<>();
        List<PowerExportCosVO> exportCos = new ArrayList<>();
 
        if (dto.getStartTime() == null) {
            dto.setStartTime(new Date());
        }
        if (dto.getEndTime() == null) {
            dto.setEndTime(new Date());
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dto.getStartTime());
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        Date startTime = calendar.getTime();
        calendar.setTime(dto.getEndTime());
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        Date endTime = calendar.getTime();
 
        String pointNo = "=";
        String[] pointNoArr = new String[2];
        String queryType = dto.getQueryType();
        PowerNetFactorEntity powerNetFactorEntity = powerNetFactorService.getByNodeCode(dto.getNodeCode());
        Calendar curDay = Calendar.getInstance();
        curDay.setTime(startTime);
        do {
            log.info("do excel");
            PowerFactorExcelDTO excelDTO = new PowerFactorExcelDTO();
            switch (queryType.toUpperCase()) {
                case "P":
                    pointNo = powerNetFactorEntity.getCurP();
                    break;
                case "Q":
                    pointNo = powerNetFactorEntity.getCurQ();
                    break;
                case "DAYCOS":
                    pointNoArr[0] = powerNetFactorEntity.getPDay();
                    pointNoArr[1] = powerNetFactorEntity.getQDay();
                    break;
                case "MONTHCOS":
                    pointNoArr[0] = powerNetFactorEntity.getPMon();
                    pointNoArr[1] = powerNetFactorEntity.getQMon();
                    break;
                default:
                    return;
            }
 
            ApiPointValueQueryDTO apiPointValueQueryDTO = new ApiPointValueQueryDTO();
            apiPointValueQueryDTO.setStart(curDay.getTime());
            curDay.add(Calendar.DAY_OF_YEAR, 1);
            apiPointValueQueryDTO.setEnd(curDay.getTime());
            List<Double> valueList = new ArrayList<>();
            Double lastValue = null;
            if (StringUtils.isNotBlank(pointNo)) {
                // 单点
                apiPointValueQueryDTO.setPointNo(pointNo);
                List<ApiPointValueDTO> chartData = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
                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();
                    valueList.add(pv.getV());
                }
            } else if (StringUtils.isNotBlank(pointNoArr[0]) && StringUtils.isNotBlank(pointNoArr[1])) {
                List<String> categories = new ArrayList<>();
 
                // P/Q
                apiPointValueQueryDTO.setPointNo(pointNoArr[0]);
                List<ApiPointValueDTO> chartDataP = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
                Map<String, Double> dataMapP = new HashMap<>();
                for (ApiPointValueDTO pv : chartDataP) {
                    categories.add(DateUtils.format(pv.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
                    dataMapP.put(DateUtils.format(pv.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND), pv.getV());
                }
 
                apiPointValueQueryDTO.setPointNo(pointNoArr[1]);
                List<ApiPointValueDTO> chartDataQ = dataPointApi.queryPointHistoryValue(apiPointValueQueryDTO);
                Map<String, Double> dataMapQ = new HashMap<>();
                for (ApiPointValueDTO pv : chartDataQ) {
                    dataMapQ.put(DateUtils.format(pv.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND), pv.getV());
                }
                for (String cate : categories) {
                    Double cos = PowerUtil.calculateCos(dataMapP.get(cate), dataMapQ.get(cate));
                    valueList.add(cos);
                }
 
            }
 
            double max = 0;
            double min = 0;
            double avg = 0;
            if (!CollectionUtils.isEmpty(valueList)) {
                max = valueList.stream().mapToDouble(Double::doubleValue).max().getAsDouble();
                min = valueList.stream().mapToDouble(Double::doubleValue).min().getAsDouble();
                avg = valueList.stream().mapToDouble(Double::doubleValue).average().getAsDouble();
            }
 
            if ("P".equals(queryType.toUpperCase()) || "Q".equals(queryType.toUpperCase())) {
                PowerExportPqVO evo = new PowerExportPqVO();
                double sdl = 0;
                double fsl = 0;
                if (!CollectionUtils.isEmpty(valueList)) {
                    if (powerNetFactorEntity.getCurFlag() != null && powerNetFactorEntity.getCurFlag().intValue() != 0) {
                        for ( Double val : valueList) {
                            if (new BigDecimal(val).compareTo(BigDecimal.ZERO) != powerNetFactorEntity.getCurFlag()) {
                                // 未发生返送
                                sdl += val;
                            } else {
                                // 发生返送
                                fsl += val;
                            }
                        }
                    }
                }
                evo.setSdl(new BigDecimal(sdl).setScale(2, BigDecimal.ROUND_HALF_UP));
                evo.setFsl(new BigDecimal(fsl).setScale(2, BigDecimal.ROUND_HALF_UP));
                evo.setMax(new BigDecimal(max).setScale(2, BigDecimal.ROUND_HALF_UP));
                evo.setMin(new BigDecimal(min).setScale(2, BigDecimal.ROUND_HALF_UP));
                evo.setAvg(new BigDecimal(avg).setScale(2, BigDecimal.ROUND_HALF_UP));
                evo.setDate(DateUtils.format(apiPointValueQueryDTO.getStart()));
                exportPqList.add(evo);
            } else if ("DAYCOS".equals(queryType.toUpperCase()) || "MONTHCOS".equals(queryType.toUpperCase())) {
                double dayLast = 0;
                if (!CollectionUtils.isEmpty(valueList)) {
                    dayLast = valueList.get(valueList.size() - 1);
                }
                PowerExportCosVO evo = new PowerExportCosVO();
                evo.setCos(new BigDecimal(dayLast).setScale(2, BigDecimal.ROUND_HALF_UP));
                evo.setMax(new BigDecimal(max).setScale(2, BigDecimal.ROUND_HALF_UP));
                evo.setMin(new BigDecimal(min).setScale(2, BigDecimal.ROUND_HALF_UP));
                evo.setAvg(new BigDecimal(avg).setScale(2, BigDecimal.ROUND_HALF_UP));
                evo.setDate(DateUtils.format(apiPointValueQueryDTO.getStart()));
                exportCos.add(evo);
            }
 
        } while (curDay.getTime().getTime() <= endTime.getTime());
 
        if ("P".equals(queryType.toUpperCase()) || "Q".equals(queryType.toUpperCase())) {
            String name = powerNetFactorEntity.getNodeName() +
                    queryType.toUpperCase().replace("P", "有功").replace("Q", "无功") +
                    "功率报表.xls";
            ExcelUtils.write(response, name, "功率数据", PowerExportPqVO.class, exportPqList);
        } else if ("DAYCOS".equals(queryType.toUpperCase()) || "MONTHCOS".equals(queryType.toUpperCase())) {
            String name = powerNetFactorEntity.getNodeName() +
                    queryType.toUpperCase().replace("DAYCOS", "日").replace("MONTHCOS", "月") +
                    "功率因数报表.xls";
            ExcelUtils.write(response, name, "功率数据", PowerExportPqVO.class, exportPqList);
        }
    }
}