潘志宝
2024-11-21 d338b50afd6504a9676f0a26b3ecbcc844483e7c
提交 | 用户 | 时间
b368e6 1 package com.iailab.module.model.api;
2
3 import com.iailab.framework.common.util.date.DateUtils;
4 import com.iailab.module.data.api.point.DataPointApi;
5 import com.iailab.module.data.api.point.dto.ApiPointDTO;
6 import com.iailab.module.data.api.point.dto.ApiPointValueDTO;
7 import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO;
8 import com.iailab.module.model.api.mcs.McsApi;
9 import com.iailab.module.model.api.mcs.dto.*;
91343d 10 import com.iailab.module.model.common.enums.CommonConstant;
11 import com.iailab.module.model.common.enums.PreLineTypeEnum;
b368e6 12 import com.iailab.module.model.mcs.pre.entity.DmModuleEntity;
13 import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity;
14 import com.iailab.module.model.mcs.pre.service.*;
ead005 15 import com.iailab.module.model.mdk.vo.ItemVO;
91343d 16 import com.iailab.module.model.mpk.service.ChartService;
b368e6 17 import lombok.extern.slf4j.Slf4j;
977edc 18 import org.apache.commons.lang3.StringUtils;
b368e6 19 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.util.CollectionUtils;
21 import org.springframework.validation.annotation.Validated;
22 import org.springframework.web.bind.annotation.RestController;
23
24 import java.math.BigDecimal;
25 import java.util.*;
912419 26 import java.util.stream.Collectors;
b368e6 27
28 /**
29  * @author PanZhibao
30  * @Description
31  * @createTime 2024年11月13日
32  */
33 @Slf4j
34 @RestController
35 @Validated
36 public class McsApiImpl implements McsApi {
37
38     @Autowired
39     private DmModuleService dmModuleService;
40
41     @Autowired
42     private MmPredictItemService mmPredictItemService;
43
44     @Autowired
45     private MmItemOutputService mmItemOutputService;
46
47     @Autowired
48     private MmItemResultService mmItemResultService;
49
50     @Autowired
51     private MmItemResultLastPointService mmItemResultLastPointService;
52
53     @Autowired
54     private DataPointApi dataPointApi;
91343d 55
56     @Autowired
57     private MmItemResultJsonService mmItemResultJsonService;
58
59     @Autowired
60     private ChartService chartService;
b368e6 61
62     private int HOUR_MINS = 60;
63
64     @Override
65     public List<PredictItemTreeDTO> getPredictItemTree() {
66         List<PredictItemTreeDTO> result = new ArrayList<>();
67
68         List<DmModuleEntity> moduleList = dmModuleService.list(new HashMap<>());
69         if (CollectionUtils.isEmpty(moduleList)) {
70             return result;
71         }
72         moduleList.forEach(item -> {
73             PredictItemTreeDTO moduleOpt = new PredictItemTreeDTO();
74             moduleOpt.setId(item.getId());
75             moduleOpt.setLabel(item.getModulename());
76             List<PredictItemTreeDTO> children = new ArrayList<>();
a4891a 77             List<ItemVO> itemList = mmPredictItemService.getByModuleId(item.getId());
b368e6 78             itemList.forEach(item1 -> {
79                 PredictItemTreeDTO chd = new PredictItemTreeDTO();
a4891a 80                 chd.setLabel(item1.getItemName());
b368e6 81                 chd.setId(item1.getId());
a4891a 82                 List<PredictItemTreeDTO> chd1 = new ArrayList<>();
83                 List<MmItemOutputEntity> outList = mmItemOutputService.getByItemid(item1.getId());
84                 if (!CollectionUtils.isEmpty(outList)) {
85                     outList.forEach(out -> {
86                         PredictItemTreeDTO chd2 = new PredictItemTreeDTO();
87                         chd2.setId(out.getId());
88                         chd2.setLabel(out.getResultstr());
89                         chd1.add(chd2);
90                     });
91                 }
92                 chd.setChildren(chd1);
b368e6 93                 children.add(chd);
94             });
95             moduleOpt.setChildren(children);
96             result.add(moduleOpt);
97         });
98         return result;
99     }
100
101     @Override
102     public PreDataBarLineRespVO getPreDataCharts(PreDataBarLineReqVO reqVO) {
103         PreDataBarLineRespVO result = new PreDataBarLineRespVO();
a4891a 104         List<String> outIds = reqVO.getOutIds();
b368e6 105         List<String> legends = new ArrayList<>();
106         List<PreDataViewRespDTO> dataViewList = new ArrayList<>();
9e844c 107         if (CollectionUtils.isEmpty(outIds)) {
b368e6 108             return result;
109         }
110         Date predictTime = reqVO.getPredictTime();
111         if (predictTime == null) {
9e844c 112             MmItemOutputEntity output = mmItemOutputService.getOutPutById(reqVO.getOutIds().get(0));
113             ItemVO predictItem = mmPredictItemService.getItemById(output.getItemid());
114             if (predictItem.getLastTime() != null) {
115                 predictTime = predictItem.getLastTime();
116             } else {
117                 Calendar calendar = Calendar.getInstance();
118                 calendar.set(Calendar.MILLISECOND, 0);
119                 calendar.set(Calendar.SECOND, 0);
120                 predictTime = calendar.getTime();
121             }
b368e6 122         }
123         Date startTime = reqVO.getStartTime();
124         if (startTime == null) {
125             Calendar calendar = Calendar.getInstance();
126             calendar.setTime(predictTime);
127             calendar.add(Calendar.HOUR_OF_DAY, -1);
128             startTime = calendar.getTime();
129         }
130         Date endTime = reqVO.getEndTime();
131         if (endTime == null) {
132             Calendar calendar = Calendar.getInstance();
133             calendar.setTime(predictTime);
134             calendar.add(Calendar.HOUR_OF_DAY, 1);
135             endTime = calendar.getTime();
136         }
137
a4891a 138         for (int i = 0; i < outIds.size(); i++) {
b368e6 139             PreDataViewRespDTO viewDto = new PreDataViewRespDTO();
a4891a 140             String outId = outIds.get(i);
141             MmItemOutputEntity output = mmItemOutputService.getOutPutById(outId);
b368e6 142             if (output == null) {
9e844c 143                 continue;
b368e6 144             }
145             legends.add(output.getResultstr());
9e844c 146             viewDto.setItemId(output.getItemid());
147             viewDto.setOutId(outId);
148             viewDto.setResultstr(output.getResultstr());
b368e6 149             viewDto.setRealData(getHisData(output.getPointid(), startTime, endTime));
977edc 150             viewDto.setPreDataN(mmItemResultService.getData(output.getId(), startTime, endTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
151             viewDto.setPreDataL(mmItemResultLastPointService.getData(output.getId(), startTime, endTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
b368e6 152
153             List<Double> values = new ArrayList<>();
154             if (!CollectionUtils.isEmpty(viewDto.getRealData())) {
155                 List<Double> hisValues = new ArrayList<>();
156                 viewDto.getRealData().forEach(item -> {
157                     values.add(Double.parseDouble(item[1].toString()));
158                     hisValues.add(Double.parseDouble(item[1].toString()));
159                 });
160                 viewDto.setHisMax(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
161                 viewDto.setHisMin(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
162                 viewDto.setHisAvg(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).average().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
163                 viewDto.setHisCumulant(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).sum())
164                         .divide(new BigDecimal(HOUR_MINS), 2, BigDecimal.ROUND_HALF_UP));
165             }
166             if (!CollectionUtils.isEmpty(viewDto.getPreDataN())) {
167                 viewDto.getPreDataN().forEach(item -> {
168                     values.add(Double.parseDouble(item[1].toString()));
169                 });
170             }
171             if (!CollectionUtils.isEmpty(viewDto.getPreDataL())) {
172                 List<Double> preValues = new ArrayList<>();
173                 viewDto.getPreDataL().forEach(item -> {
174                     values.add(Double.parseDouble(item[1].toString()));
175                     preValues.add(Double.parseDouble(item[1].toString()));
176                 });
177                 viewDto.setPreMax(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
178                 viewDto.setPreMin(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
179                 viewDto.setPreAvg(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).average().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
180             }
181             if (!CollectionUtils.isEmpty(viewDto.getCurData())) {
182                 List<Double> preValues = new ArrayList<>();
183                 viewDto.getCurData().forEach(item -> {
184                     values.add(Double.parseDouble(item[1].toString()));
185                     preValues.add(Double.parseDouble(item[1].toString()));
186                 });
187                 viewDto.setPreCumulant(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).sum())
188                         .divide(new BigDecimal(HOUR_MINS), 2, BigDecimal.ROUND_HALF_UP));
189             }
190             if (!CollectionUtils.isEmpty(viewDto.getAdjData())) {
191                 viewDto.getAdjData().forEach(item -> {
192                     values.add(Double.parseDouble(item[1].toString()));
193                 });
194             }
195             if (!CollectionUtils.isEmpty(values)) {
196                 viewDto.setMaxValue(new BigDecimal(values.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
197                 viewDto.setMinValue(new BigDecimal(values.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
198             }
199             dataViewList.add(viewDto);
200         }
201         result.setStartTime(startTime);
202         result.setEndTime(endTime);
203         result.setPredictTime(predictTime);
204         result.setCategories(DateUtils.getTimeScale(startTime, endTime, 60));
205         result.setLegend(legends);
206         result.setDataViewList(dataViewList);
207         return result;
208     }
209
ead005 210     @Override
211     public PreDataItemChartRespVO getPreDataItemChart(PreDataItemChartReqVO reqVO) {
212         PreDataItemChartRespVO result = new PreDataItemChartRespVO();
213         ItemVO predictItem = mmPredictItemService.getItemById(reqVO.getItemId());
214         if (predictItem == null) {
215             return result;
216         }
217         if (predictItem.getLastTime() == null) {
218             return result;
219         }
328ef4 220         result.setPredictTime(predictItem.getLastTime());
ead005 221         Date startTime = reqVO.getStartTime();
222         if (startTime == null) {
223             Calendar calendar = Calendar.getInstance();
224             calendar.setTime(predictItem.getLastTime());
91343d 225             calendar.add(Calendar.MINUTE, -1 * predictItem.getPredictLength());
ead005 226             startTime = calendar.getTime();
227         }
228         Date endTime = reqVO.getEndTime();
229         if (endTime == null) {
230             Calendar calendar = Calendar.getInstance();
231             calendar.setTime(predictItem.getLastTime());
91343d 232             calendar.add(Calendar.MINUTE, predictItem.getPredictLength());
ead005 233             endTime = calendar.getTime();
234         }
1f9784 235         if (endTime.getTime() <= startTime.getTime()) {
236             Calendar calendar = Calendar.getInstance();
237             calendar.setTime(startTime);
91343d 238             calendar.add(Calendar.MINUTE, predictItem.getPredictLength());
1f9784 239             endTime = calendar.getTime();
240         }
241
ead005 242         List<String> categories = DateUtils.getTimeScale(startTime, endTime, predictItem.getGranularity());
243         List<String> legend = new ArrayList<>();
244         LinkedHashMap<String, PreDataSampleViewRespDTO> viewMap = new LinkedHashMap<>();
245         List<MmItemOutputEntity> outs = mmItemOutputService.getByItemid(reqVO.getItemId());
246         if (CollectionUtils.isEmpty(outs)) {
247             return result;
248         }
249         for (MmItemOutputEntity out : outs) {
250             legend.add(out.getResultstr());
251             PreDataSampleViewRespDTO viewDto = new PreDataSampleViewRespDTO();
252             viewDto.setRealData(getHisData(out.getPointid(), startTime, endTime));
977edc 253             viewDto.setPreDataN(mmItemResultService.getData(out.getId(), startTime, endTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
ead005 254             viewMap.put(out.getResultstr(), viewDto);
255         }
256         result.setStartTime(startTime);
257         result.setEndTime(endTime);
258         result.setCategories(categories);
259         result.setLegend(legend);
260         result.setViewMap(viewMap);
261         return result;
262     }
263
328ef4 264     @Override
91343d 265     public PreDataSingleChartRespVO getPreDataSingleChart(PreDataSingleChartReqVO reqVO) {
266         PreDataSingleChartRespVO result = new PreDataSingleChartRespVO();
328ef4 267
91343d 268         Map<String, String> chartParams = chartService.getByChartCode(reqVO.getChartCode());
269         if (CollectionUtils.isEmpty(chartParams)) {
270             return result;
271         }
272         String itemCode = chartParams.get(CommonConstant.ITEM_CODE);
273         if (itemCode == null) {
274             return result;
275         }
276         String resultStr = chartParams.get(CommonConstant.RESULT_STR);
277         if (resultStr == null) {
278             return result;
279         }
280         ItemVO predictItem = mmPredictItemService.getItemByItemNo(itemCode);
281         if (predictItem == null || predictItem.getLastTime() == null) {
282             return result;
283         }
977edc 284         String timeFormat = StringUtils.isBlank(reqVO.getTimeFormat()) ? DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND : reqVO.getTimeFormat();
285         PreLineTypeEnum lineType = chartParams.get(CommonConstant.LINE_TYPE) == null ? PreLineTypeEnum.TN : PreLineTypeEnum.getEumByCode(chartParams.get(CommonConstant.LINE_TYPE));
91343d 286         BigDecimal rangeH = chartParams.get(CommonConstant.RANGE_H) == null ? BigDecimal.ZERO : new BigDecimal(chartParams.get(CommonConstant.RANGE_H));
287         BigDecimal rangeL = chartParams.get(CommonConstant.RANGE_L) == null ? BigDecimal.ZERO : new BigDecimal(chartParams.get(CommonConstant.RANGE_L));
288         BigDecimal limitH = chartParams.get(CommonConstant.LIMIT_H) == null ? BigDecimal.ZERO : new BigDecimal(chartParams.get(CommonConstant.LIMIT_H));
289         BigDecimal limitL = chartParams.get(CommonConstant.LIMIT_L) == null ? BigDecimal.ZERO : new BigDecimal(chartParams.get(CommonConstant.LIMIT_L));
290         int lengthLeft = chartParams.get(CommonConstant.LENGTH_LEFT) == null ? predictItem.getPredictLength() : new BigDecimal(chartParams.get(CommonConstant.LENGTH_LEFT)).intValue();
291         int lengthRight = chartParams.get(CommonConstant.LENGTH_RIGHT) == null ? predictItem.getPredictLength() : new BigDecimal(chartParams.get(CommonConstant.LENGTH_RIGHT)).intValue();
292         result.setPredictTime(predictItem.getLastTime());
293         Date predictTime = predictItem.getLastTime();
294         Date startTime = reqVO.getStartTime();
295         if (startTime == null) {
296             Calendar calendar = Calendar.getInstance();
297             calendar.setTime(predictItem.getLastTime());
298             calendar.add(Calendar.MINUTE, -1 * lengthLeft);
299             startTime = calendar.getTime();
300         }
301         Date endTime = reqVO.getEndTime();
302         if (endTime == null) {
303             Calendar calendar = Calendar.getInstance();
304             calendar.setTime(predictItem.getLastTime());
305             calendar.add(Calendar.MINUTE, lengthRight);
306             endTime = calendar.getTime();
307         }
308         if (endTime.getTime() <= startTime.getTime()) {
309             Calendar calendar = Calendar.getInstance();
310             calendar.setTime(startTime);
311             calendar.add(Calendar.MINUTE, lengthRight);
312             endTime = calendar.getTime();
313         }
977edc 314         List<String> categories = DateUtils.getTimeScale(startTime, endTime, predictItem.getGranularity(), timeFormat);
91343d 315         List<String> legend = new ArrayList<>();
316         MmItemOutputEntity outPut = mmItemOutputService.getByItemid(predictItem.getId(), resultStr);
317         PreDataViewRespDTO dataView = new PreDataViewRespDTO();
318         dataView.setItemId(predictItem.getId());
319         dataView.setItemName(predictItem.getItemName());
320         dataView.setResultstr(resultStr);
321         dataView.setRangeH(rangeH);
322         dataView.setRangeL(rangeL);
323         dataView.setLimitH(limitH);
324         dataView.setLimitL(limitL);
977edc 325         dataView.setRealData(getHisData(outPut.getPointid(), startTime, endTime, timeFormat));
326         dataView.setCurData(mmItemResultJsonService.getData(outPut.getId(), predictTime, timeFormat));
91343d 327         switch (lineType) {
328             case TN:
977edc 329                 dataView.setPreDataN(mmItemResultService.getData(outPut.getId(), startTime, endTime, timeFormat));
91343d 330                 break;
331             case TL:
977edc 332                 dataView.setPreDataN(mmItemResultService.getData(outPut.getId(), predictTime, endTime, timeFormat));
333                 dataView.setPreDataL(mmItemResultLastPointService.getData(outPut.getId(), startTime, endTime, timeFormat));
91343d 334                 break;
335             default:
336                 break;
337         }
912419 338
339         if (!CollectionUtils.isEmpty(dataView.getCurData())) {
340             List<Double> curList = dataView.getCurData().stream().map(t -> {
341                 return new Double(t[1].toString());
342             }).collect(Collectors.toList());
343             dataView.setPreMax(new BigDecimal(curList.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
344             dataView.setPreMin(new BigDecimal(curList.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
345         }
346
91343d 347         result.setStartTime(startTime);
348         result.setEndTime(endTime);
349         result.setCategories(categories);
350         result.setLegend(legend);
351         result.setDataView(dataView);
328ef4 352         return result;
353     }
354
b368e6 355     /**
356      * 获取真实值
357      *
358      * @param pointId
359      * @param startTime
360      * @param endTime
361      * @return
362      */
363     private List<Object[]> getHisData(String pointId, Date startTime, Date endTime) {
364         List<Object[]> result = new ArrayList<>();
365         ApiPointDTO pointDTO = dataPointApi.getInfoById(pointId);
366         ApiPointValueQueryDTO queryPointDto = new ApiPointValueQueryDTO();
367         queryPointDto.setPointNo(pointDTO.getPointNo());
368         queryPointDto.setStart(startTime);
369         queryPointDto.setEnd(endTime);
370         List<ApiPointValueDTO> valueDTOS = dataPointApi.queryPointHistoryValue(queryPointDto);
371         if (CollectionUtils.isEmpty(valueDTOS)) {
372             return result;
373         }
374         valueDTOS.forEach(item -> {
375             Object[] values = new Object[2];
214275 376             values[0] = DateUtils.format(item.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
377             values[1] = new BigDecimal(item.getV()).setScale(2, BigDecimal.ROUND_HALF_UP);
b368e6 378             result.add(values);
379         });
380         return result;
381     }
382
977edc 383     /**
384      * 获取真实值
385      *
386      * @param pointId
387      * @param startTime
388      * @param endTime
389      * @param timeFormat
390      * @return
391      */
392     private List<Object[]> getHisData(String pointId, Date startTime, Date endTime, String timeFormat) {
393         List<Object[]> result = new ArrayList<>();
394         ApiPointDTO pointDTO = dataPointApi.getInfoById(pointId);
395         ApiPointValueQueryDTO queryPointDto = new ApiPointValueQueryDTO();
396         queryPointDto.setPointNo(pointDTO.getPointNo());
397         queryPointDto.setStart(startTime);
398         queryPointDto.setEnd(endTime);
399         List<ApiPointValueDTO> valueDTOS = dataPointApi.queryPointHistoryValue(queryPointDto);
400         if (CollectionUtils.isEmpty(valueDTOS)) {
401             return result;
402         }
403         valueDTOS.forEach(item -> {
404             Object[] values = new Object[2];
405             values[0] = DateUtils.format(item.getT(), timeFormat);
406             values[1] = new BigDecimal(item.getV()).setScale(2, BigDecimal.ROUND_HALF_UP);
407             result.add(values);
408         });
409         return result;
410     }
411
b368e6 412     @Override
413     public Boolean createAlarmMessage(AlarmMessageRespDTO dto) {
414         return true;
415     }
416
417     @Override
418     public List<AlarmMessageRespDTO> listAlarmMessage(Map<String, Object> params) {
419         return null;
420     }
421
422     @Override
423     public Boolean createScheduleSuggest(ScheduleSuggestRespDTO dto) {
424         return true;
425     }
426
427     @Override
428     public List<ScheduleSuggestRespDTO> listScheduleSuggest(ScheduleSuggestReqDTO params) {
429         return null;
430     }
431
432     @Override
433     public Boolean modifyPredictModelSetting(List<PredictModelSettingReqDTO> dtos) {
434         return true;
435     }
436
437     @Override
438     public Boolean modifyScheduleModelSetting(List<ScheduleModelSettingReqDTO> dtos) {
439         return true;
440     }
441 }