鞍钢鲅鱼圈能源管控系统后端代码
liriming
5 天以前 dbfe06e01e1dd97675b5889588ed058c1817a8c6
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
package com.iailab.module.ansteel.api.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iailab.framework.common.util.date.DateUtils;
import com.iailab.framework.common.util.object.ConvertUtils;
import com.iailab.module.ansteel.api.dao.PowerRunStateDao;
import com.iailab.module.ansteel.api.dto.*;
import com.iailab.module.ansteel.api.service.DataApi;
import com.iailab.module.ansteel.common.constant.CommonConstant;
import com.iailab.module.data.api.plan.PlanItemApi;
import com.iailab.module.data.api.plan.dto.ApiPlanDataDTO;
import com.iailab.module.data.api.point.DataPointApi;
import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO;
import com.iailab.module.data.common.ApiDataQueryDTO;
import com.iailab.module.model.api.mcs.McsApi;
import com.iailab.module.model.api.mcs.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
@Slf4j
@Service
public class DataServiceImpl implements DataApi {
 
    @Autowired
    private DataPointApi dataPointApi;
 
    @Autowired
    private McsApi mcsApi;
 
    @Autowired
    private PlanItemApi planItemApi;
 
    @Autowired
    private PowerRunStateDao powerRunStateDao;
 
    @Override
    public List<PowerRunStateDTO> getPowerRunState() {
        List<PowerRunStateDTO> list = ConvertUtils.sourceToTarget(powerRunStateDao.selectList(new QueryWrapper<>()), PowerRunStateDTO.class);
        if (!CollectionUtils.isEmpty(list)) {
            list.stream().map(item -> {
                StringBuffer sb = new StringBuffer();
                sb.append(item.getQhRate());
                sb.append(",");
                sb.append(item.getFhRate());
                sb.append(",");
                sb.append(item.getGasTotal());
                sb.append(",");
                sb.append(item.getGasCost());
                sb.append(",");
                sb.append(item.getGasSurplus());
                sb.append(",");
                sb.append(item.getBfg());
                sb.append(",");
                sb.append(item.getLdg());
                sb.append(",");
                sb.append(item.getCog());
                String[] arr = sb.toString().split(",");
                Map<String, Object> adjValue = dataPointApi.queryPointsRealValue(Arrays.asList(arr));
                item.setQhRate(ObjectUtils.isEmpty(adjValue.get(arr[0])) ? "0" : new BigDecimal(adjValue.get(arr[0]).toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                item.setFhRate(ObjectUtils.isEmpty(adjValue.get(arr[1])) ? "0" : new BigDecimal(adjValue.get(arr[1]).toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                item.setGasTotal(ObjectUtils.isEmpty(adjValue.get(arr[2])) ? "0" : new BigDecimal(adjValue.get(arr[2]).toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                item.setGasCost(ObjectUtils.isEmpty(adjValue.get(arr[3])) ? "0" : new BigDecimal(adjValue.get(arr[3]).toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                item.setGasSurplus(ObjectUtils.isEmpty(adjValue.get(arr[4])) ? "0" : new BigDecimal(adjValue.get(arr[4]).toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                item.setBfg(ObjectUtils.isEmpty(adjValue.get(arr[5])) ? "0" : new BigDecimal(adjValue.get(arr[5]).toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                item.setLdg(ObjectUtils.isEmpty(adjValue.get(arr[6])) ? "0" : new BigDecimal(adjValue.get(arr[6]).toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                item.setCog(ObjectUtils.isEmpty(adjValue.get(arr[7])) ? "0" : new BigDecimal(adjValue.get(arr[7]).toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
 
                return item;
            }).collect(Collectors.toList());
        }
        return list;
    }
 
    @Override
    public PreDataChartRespVO getPreDataChart(PreDataSingleChartReqVO reqVO) {
        Map<String, String> tMap = new HashMap<>();
        List<ChartParamDTO> list = mcsApi.getChartParamList(reqVO.getChartCode());
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        list.forEach(item -> {
            tMap.put(item.getParamCode(), item.getParamValue());
        });
        String itemCode = tMap.get(CommonConstant.ITEM_CODE);
        if (itemCode == null) {
            return null;
        }
        String resultStr = tMap.get(CommonConstant.RESULT_STR);
        if (resultStr == null) {
            return null;
        }
        String resultIndex = tMap.get(CommonConstant.RESULT_INDEX);
 
        PredictItemVO predictItem = mcsApi.getPredictItemByItemNo(itemCode);
        if (predictItem == null || predictItem.getLastTime() == null) {
            return null;
        }
        String timeFormat = StringUtils.isBlank(reqVO.getTimeFormat()) ? DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND : reqVO.getTimeFormat();
        int lengthLeft = tMap.get(CommonConstant.LENGTH_LEFT) == null ? predictItem.getPredictLength() : new BigDecimal(tMap.get(CommonConstant.LENGTH_LEFT)).intValue();
        int lengthRight = tMap.get(CommonConstant.LENGTH_RIGHT) == null ? predictItem.getPredictLength() : new BigDecimal(tMap.get(CommonConstant.LENGTH_RIGHT)).intValue();
        String trendsDataH = tMap.get(CommonConstant.TRENDSDATA_H) == null ? null : tMap.get(CommonConstant.TRENDSDATA_H);
        String trendsDataL = tMap.get(CommonConstant.TRENDSDATA_L) == null ? null : tMap.get(CommonConstant.TRENDSDATA_L);
 
        Date[] timeArray = calResultTime(predictItem, reqVO.getStartTime(), reqVO.getEndTime(), lengthLeft, lengthRight);
        Date startTime = timeArray[1];
        Date endTime = timeArray[2];
 
        PreDataSingleChartRespVO resultOld = mcsApi.getPreDataSingleChart(reqVO);
        PreDataChartRespVO result = ConvertUtils.sourceToTarget(resultOld, PreDataChartRespVO.class);
        PreDataViewDTO preDataView = ConvertUtils.sourceToTarget(resultOld.getDataView(), PreDataViewDTO.class);
        if(trendsDataH != null) {
            ApiPointValueQueryDTO pointValueQueryDTO = new ApiPointValueQueryDTO();
            pointValueQueryDTO.setPointNo(trendsDataH);
            List<ApiPointValueDTO> trendsDataHList = ConvertUtils.sourceToTarget(dataPointApi.queryPointHistoryValue(pointValueQueryDTO), ApiPointValueDTO.class);
            List<Object[]> newList = new ArrayList<>();
            trendsDataHList.forEach(item -> {
                Object[] dataItem = new Object[2];
                dataItem[0] = item.getT();
                dataItem[1] = item.getV();
                newList.add(dataItem);
            });
            preDataView.setTrendsDataH(newList);
        }
        if(trendsDataL != null) {
            ApiPointValueQueryDTO pointValueQueryDTO = new ApiPointValueQueryDTO();
            pointValueQueryDTO.setPointNo(trendsDataL);
            List<ApiPointValueDTO> trendsDataLList = ConvertUtils.sourceToTarget(dataPointApi.queryPointHistoryValue(pointValueQueryDTO), ApiPointValueDTO.class);
            List<Object[]> newList = new ArrayList<>();
            trendsDataLList.forEach(item -> {
                Object[] dataItem = new Object[2];
                dataItem[0] = item.getT();
                dataItem[1] = item.getV();
                newList.add(dataItem);
            });
            preDataView.setTrendsDataL(newList);
        }
        MmItemOutputDTO outputDTO = mcsApi.getItemOutputByItemid(predictItem.getId(), resultStr, resultIndex);
 
        PreItemResultReqVO preItemResultReqVO = new PreItemResultReqVO();
        preItemResultReqVO.setOutputid(outputDTO.getId());
        preItemResultReqVO.setStartTime(startTime);
        preItemResultReqVO.setEndTime(endTime);
        preItemResultReqVO.setTimeFormat(timeFormat);
 
        preDataView.setPreData(resultOld.getDataView().getPreDataL());
 
        result.setPreDataView(preDataView);
        return result;
    }
 
    private Date[] calResultTime(PredictItemVO predictItem, Date startTimeReq, Date endTimeReq, int lengthLeft, int lengthRight) {
        Date[] result = new Date[3];
        Date predictTime = predictItem.getLastTime();
        Integer granularity = predictItem.getGranularity();
        Date startTime = startTimeReq;
        if (startTime == null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(predictItem.getLastTime());
            calendar.add(Calendar.SECOND, -1 * lengthLeft * granularity);
            startTime = calendar.getTime();
        }
        Date endTime = endTimeReq;
        if (endTime == null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(predictItem.getLastTime());
            calendar.add(Calendar.SECOND, lengthRight * granularity);
            endTime = calendar.getTime();
        }
        if (endTime.getTime() <= startTime.getTime()) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startTime);
            calendar.add(Calendar.SECOND, lengthRight * granularity);
            endTime = calendar.getTime();
        }
        result[0] = predictTime;
        result[1] = startTime;
        result[2] = endTime;
        return result;
    }
 
    private Date[] calResultTime2(Date predictTimeReq,String startTimeReq, String endTimeReq, int lengthLeft, int lengthRight) {
        Date[] result = new Date[2];
        Date predictTime = predictTimeReq;
        Date startTime = new Date();
        if (startTimeReq == null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(predictTime);
            calendar.add(Calendar.SECOND, -1 * lengthLeft * 60);
            startTime = calendar.getTime();
        }
        Date endTime = new Date();
        if (endTimeReq == null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(predictTime);
            calendar.add(Calendar.SECOND, lengthRight * 60);
            endTime = calendar.getTime();
        }
        if (endTime.getTime() <= startTime.getTime()) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startTime);
            calendar.add(Calendar.SECOND, lengthRight * 60);
            endTime = calendar.getTime();
        }
        result[0] = startTime;
        result[1] = endTime;
        return result;
    }
    /**
     *  查询原始计划、修正计划数据
     */
    @Override
    public Map<String, Object> getPlanDataChart(PlanDataChartReqVO reqVO) {
        Map<String, Object> result = new HashMap<>(2);
        Map<String, String> tMap = new HashMap<>();
        List<ChartParamDTO> list = mcsApi.getChartParamList(reqVO.getChartCode());
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        list.forEach(item -> {
            tMap.put(item.getParamCode(), item.getParamValue());
        });
        String planCode = tMap.get(CommonConstant.PLAN_CODE);
        if (planCode == null) {
            return null;
        }
        String itemCode = tMap.get(CommonConstant.ITEM_CODE);
        if (itemCode == null) {
            return null;
        }
 
        PreDataSingleChartReqVO queryDto = new PreDataSingleChartReqVO();
        queryDto.setChartCode(reqVO.getChartCode());
        PreDataSingleChartRespVO chartData = mcsApi.getPreDataSingleChart(queryDto);
 
        Date predictTime = chartData.getPredictTime();
        int lengthLeft = new BigDecimal(tMap.get(CommonConstant.LENGTH_LEFT)).intValue();
        int lengthRight = reqVO.getLengthRight();
 
        Date[] timeArray = calResultTime2(predictTime, reqVO.getStartTime(), reqVO.getEndTime(), lengthLeft, lengthRight);
        Date startTime = timeArray[0];
        Date endTime = timeArray[1];
 
        // 原始计划数据
        LinkedHashMap<String, List<String[]>> planData = getRealPlanData(planCode, startTime, endTime, predictTime);
 
        // 修正计划数据
        LinkedHashMap<String, List<String[]>> predData = new LinkedHashMap<>();
        predData.put(reqVO.getChartCode(), getPrePlanData(reqVO.getChartCode(), predictTime, endTime));
        result.put("planData", planData);
        result.put("predData", predData);
        return result;
    }
 
    //原始计划
    public LinkedHashMap<String, List<String[]>> getRealPlanData(String planCode, Date startTime, Date endTime, Date predictTime) {
        LinkedHashMap<String, List<String[]>> result = new LinkedHashMap<>();
        ApiDataQueryDTO apiDataQueryDTO = new ApiDataQueryDTO();
        List<String> itemList = Arrays.asList(planCode.toString().split(","));
        apiDataQueryDTO.setItemNos(itemList);
        apiDataQueryDTO.setStart(startTime);
        apiDataQueryDTO.setEnd(endTime);
        LinkedHashMap<String, List<ApiPlanDataDTO>> planData = planItemApi.queryPlanItemRecordValue(apiDataQueryDTO);
        planData.forEach((key, value) -> {
            List<String[]> recods = new ArrayList<>();
            if (!CollectionUtils.isEmpty(value)) {
                value.forEach(item -> {
                    String[] ri = new String[3];
                    ri[0] = DateUtils.format(DateUtils.parse(item.getStartTime(), DateUtils.DATE_TIME_PATTERN_STRING), "yyyy-MM-dd HH:mm:00");
                    ri[1] = DateUtils.format(DateUtils.parse(item.getEndTime(), DateUtils.DATE_TIME_PATTERN_STRING), "yyyy-MM-dd HH:mm:00");
                    ri[2] = "2";
                    if (DateUtils.parse(item.getStartTime(), "yyyyMMddHHmmss").getTime() < predictTime.getTime()) {
                        ri[2] = "1";
                    }
                    recods.add(ri);
                });
            }
            result.put(key, recods);
        });
        return result;
    }
 
    //修正计划
    public List<String[]> getPrePlanData(String charCode, Date startTime, Date endTime) {
        List<String[]> result = new ArrayList<>();
        PreDataSingleChartReqVO queryDto = new PreDataSingleChartReqVO();
        queryDto.setChartCode(charCode);
        queryDto.setStartTime(startTime);
        queryDto.setEndTime(endTime);
        PreDataSingleChartRespVO chartData = mcsApi.getPreDataSingleChart(queryDto);
        if (CollectionUtils.isEmpty(chartData.getDataView().getPreDataN())) {
            return result;
        }
 
        double lastValue = 0;
        for (int i = 0; i < chartData.getDataView().getPreDataN().size(); i++) {
            Object[] dataItem = chartData.getDataView().getPreDataN().get(i);
            double cv = new BigDecimal(dataItem[1].toString()).doubleValue();
            if (cv == 1 && cv != lastValue) {
                String[] sa = new String[3];
                sa[0] = (String) dataItem[0];
                sa[1] = getEndTime(chartData.getDataView().getPreDataN(), i);
                sa[2] = "2";
                result.add(sa);
            }
            lastValue = cv;
        }
        return result;
    }
 
    private String getEndTime(List<Object[]> preDataN, int startIndex) {
        String endTime = "";
        double lastValue = 1;
        for (int i = startIndex; i < preDataN.size(); i++) {
            Object[] dataItem = preDataN.get(i);
            double cv = new BigDecimal(dataItem[1].toString()).doubleValue();
            if (cv == 0 && cv != lastValue) {
                return (String) dataItem[0];
            }
            lastValue = cv;
        }
        return endTime;
    }
}