潘志宝
2024-11-18 328ef4264c7be7b57f24d7dd1f040e9f681b27b5
提交 | 用户 | 时间
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.*;
10 import com.iailab.module.model.mcs.pre.entity.DmModuleEntity;
11 import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity;
12 import com.iailab.module.model.mcs.pre.service.*;
ead005 13 import com.iailab.module.model.mdk.vo.ItemVO;
b368e6 14 import lombok.extern.slf4j.Slf4j;
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.util.CollectionUtils;
17 import org.springframework.validation.annotation.Validated;
18 import org.springframework.web.bind.annotation.RestController;
19
20 import java.math.BigDecimal;
21 import java.util.*;
22
23 /**
24  * @author PanZhibao
25  * @Description
26  * @createTime 2024年11月13日
27  */
28 @Slf4j
29 @RestController
30 @Validated
31 public class McsApiImpl implements McsApi {
32
33     @Autowired
34     private DmModuleService dmModuleService;
35
36     @Autowired
37     private MmPredictItemService mmPredictItemService;
38
39     @Autowired
40     private MmItemOutputService mmItemOutputService;
41
42     @Autowired
43     private MmItemResultService mmItemResultService;
44
45     @Autowired
46     private MmItemResultLastPointService mmItemResultLastPointService;
47
48     @Autowired
49     private DataPointApi dataPointApi;
50
51     private int HOUR_MINS = 60;
52
53     @Override
54     public List<PredictItemTreeDTO> getPredictItemTree() {
55         List<PredictItemTreeDTO> result = new ArrayList<>();
56
57         List<DmModuleEntity> moduleList = dmModuleService.list(new HashMap<>());
58         if (CollectionUtils.isEmpty(moduleList)) {
59             return result;
60         }
61         moduleList.forEach(item -> {
62             PredictItemTreeDTO moduleOpt = new PredictItemTreeDTO();
63             moduleOpt.setId(item.getId());
64             moduleOpt.setLabel(item.getModulename());
65             List<PredictItemTreeDTO> children = new ArrayList<>();
a4891a 66             List<ItemVO> itemList = mmPredictItemService.getByModuleId(item.getId());
b368e6 67             itemList.forEach(item1 -> {
68                 PredictItemTreeDTO chd = new PredictItemTreeDTO();
a4891a 69                 chd.setLabel(item1.getItemName());
b368e6 70                 chd.setId(item1.getId());
a4891a 71                 List<PredictItemTreeDTO> chd1 = new ArrayList<>();
72                 List<MmItemOutputEntity> outList = mmItemOutputService.getByItemid(item1.getId());
73                 if (!CollectionUtils.isEmpty(outList)) {
74                     outList.forEach(out -> {
75                         PredictItemTreeDTO chd2 = new PredictItemTreeDTO();
76                         chd2.setId(out.getId());
77                         chd2.setLabel(out.getResultstr());
78                         chd1.add(chd2);
79                     });
80                 }
81                 chd.setChildren(chd1);
b368e6 82                 children.add(chd);
83             });
84             moduleOpt.setChildren(children);
85             result.add(moduleOpt);
86         });
87         return result;
88     }
89
90     @Override
91     public PreDataBarLineRespVO getPreDataCharts(PreDataBarLineReqVO reqVO) {
92         PreDataBarLineRespVO result = new PreDataBarLineRespVO();
a4891a 93         List<String> outIds = reqVO.getOutIds();
b368e6 94         List<String> legends = new ArrayList<>();
95         List<PreDataViewRespDTO> dataViewList = new ArrayList<>();
9e844c 96         if (CollectionUtils.isEmpty(outIds)) {
b368e6 97             return result;
98         }
99         Date predictTime = reqVO.getPredictTime();
100         if (predictTime == null) {
9e844c 101             MmItemOutputEntity output = mmItemOutputService.getOutPutById(reqVO.getOutIds().get(0));
102             ItemVO predictItem = mmPredictItemService.getItemById(output.getItemid());
103             if (predictItem.getLastTime() != null) {
104                 predictTime = predictItem.getLastTime();
105             } else {
106                 Calendar calendar = Calendar.getInstance();
107                 calendar.set(Calendar.MILLISECOND, 0);
108                 calendar.set(Calendar.SECOND, 0);
109                 predictTime = calendar.getTime();
110             }
b368e6 111         }
112         Date startTime = reqVO.getStartTime();
113         if (startTime == null) {
114             Calendar calendar = Calendar.getInstance();
115             calendar.setTime(predictTime);
116             calendar.add(Calendar.HOUR_OF_DAY, -1);
117             startTime = calendar.getTime();
118         }
119         Date endTime = reqVO.getEndTime();
120         if (endTime == null) {
121             Calendar calendar = Calendar.getInstance();
122             calendar.setTime(predictTime);
123             calendar.add(Calendar.HOUR_OF_DAY, 1);
124             endTime = calendar.getTime();
125         }
126
a4891a 127         for (int i = 0; i < outIds.size(); i++) {
b368e6 128             PreDataViewRespDTO viewDto = new PreDataViewRespDTO();
a4891a 129             String outId = outIds.get(i);
130             MmItemOutputEntity output = mmItemOutputService.getOutPutById(outId);
b368e6 131             if (output == null) {
9e844c 132                 continue;
b368e6 133             }
134             legends.add(output.getResultstr());
9e844c 135             viewDto.setItemId(output.getItemid());
136             viewDto.setOutId(outId);
137             viewDto.setResultstr(output.getResultstr());
b368e6 138             viewDto.setRealData(getHisData(output.getPointid(), startTime, endTime));
139             viewDto.setPreDataN(mmItemResultService.getData(output.getId(), startTime, endTime));
140             viewDto.setPreDataL(mmItemResultLastPointService.getData(output.getId(), startTime, endTime));
141
142             List<Double> values = new ArrayList<>();
143             if (!CollectionUtils.isEmpty(viewDto.getRealData())) {
144                 List<Double> hisValues = new ArrayList<>();
145                 viewDto.getRealData().forEach(item -> {
146                     values.add(Double.parseDouble(item[1].toString()));
147                     hisValues.add(Double.parseDouble(item[1].toString()));
148                 });
149                 viewDto.setHisMax(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
150                 viewDto.setHisMin(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
151                 viewDto.setHisAvg(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).average().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
152                 viewDto.setHisCumulant(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).sum())
153                         .divide(new BigDecimal(HOUR_MINS), 2, BigDecimal.ROUND_HALF_UP));
154             }
155             if (!CollectionUtils.isEmpty(viewDto.getPreDataN())) {
156                 viewDto.getPreDataN().forEach(item -> {
157                     values.add(Double.parseDouble(item[1].toString()));
158                 });
159             }
160             if (!CollectionUtils.isEmpty(viewDto.getPreDataL())) {
161                 List<Double> preValues = new ArrayList<>();
162                 viewDto.getPreDataL().forEach(item -> {
163                     values.add(Double.parseDouble(item[1].toString()));
164                     preValues.add(Double.parseDouble(item[1].toString()));
165                 });
166                 viewDto.setPreMax(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
167                 viewDto.setPreMin(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
168                 viewDto.setPreAvg(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).average().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
169             }
170             if (!CollectionUtils.isEmpty(viewDto.getCurData())) {
171                 List<Double> preValues = new ArrayList<>();
172                 viewDto.getCurData().forEach(item -> {
173                     values.add(Double.parseDouble(item[1].toString()));
174                     preValues.add(Double.parseDouble(item[1].toString()));
175                 });
176                 viewDto.setPreCumulant(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).sum())
177                         .divide(new BigDecimal(HOUR_MINS), 2, BigDecimal.ROUND_HALF_UP));
178             }
179             if (!CollectionUtils.isEmpty(viewDto.getAdjData())) {
180                 viewDto.getAdjData().forEach(item -> {
181                     values.add(Double.parseDouble(item[1].toString()));
182                 });
183             }
184             if (!CollectionUtils.isEmpty(values)) {
185                 viewDto.setMaxValue(new BigDecimal(values.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
186                 viewDto.setMinValue(new BigDecimal(values.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
187             }
188             dataViewList.add(viewDto);
189         }
190         result.setStartTime(startTime);
191         result.setEndTime(endTime);
192         result.setPredictTime(predictTime);
193         result.setCategories(DateUtils.getTimeScale(startTime, endTime, 60));
194         result.setLegend(legends);
195         result.setDataViewList(dataViewList);
196         return result;
197     }
198
ead005 199     @Override
200     public PreDataItemChartRespVO getPreDataItemChart(PreDataItemChartReqVO reqVO) {
201         PreDataItemChartRespVO result = new PreDataItemChartRespVO();
202         ItemVO predictItem = mmPredictItemService.getItemById(reqVO.getItemId());
203         if (predictItem == null) {
204             return result;
205         }
206         if (predictItem.getLastTime() == null) {
207             return result;
208         }
328ef4 209         result.setPredictTime(predictItem.getLastTime());
ead005 210         Date startTime = reqVO.getStartTime();
211         if (startTime == null) {
212             Calendar calendar = Calendar.getInstance();
213             calendar.setTime(predictItem.getLastTime());
214             calendar.add(Calendar.HOUR_OF_DAY, -1);
215             startTime = calendar.getTime();
216         }
217         Date endTime = reqVO.getEndTime();
218         if (endTime == null) {
219             Calendar calendar = Calendar.getInstance();
220             calendar.setTime(predictItem.getLastTime());
221             calendar.add(Calendar.HOUR_OF_DAY, 1);
222             endTime = calendar.getTime();
223         }
1f9784 224         if (endTime.getTime() <= startTime.getTime()) {
225             Calendar calendar = Calendar.getInstance();
226             calendar.setTime(startTime);
227             calendar.add(Calendar.HOUR_OF_DAY, 1);
228             endTime = calendar.getTime();
229         }
230
ead005 231         List<String> categories = DateUtils.getTimeScale(startTime, endTime, predictItem.getGranularity());
232         List<String> legend = new ArrayList<>();
233         LinkedHashMap<String, PreDataSampleViewRespDTO> viewMap = new LinkedHashMap<>();
234         List<MmItemOutputEntity> outs = mmItemOutputService.getByItemid(reqVO.getItemId());
235         if (CollectionUtils.isEmpty(outs)) {
236             return result;
237         }
238         for (MmItemOutputEntity out : outs) {
239             legend.add(out.getResultstr());
240             PreDataSampleViewRespDTO viewDto = new PreDataSampleViewRespDTO();
241             viewDto.setRealData(getHisData(out.getPointid(), startTime, endTime));
242             viewDto.setPreDataN(mmItemResultService.getData(out.getId(), startTime, endTime));
243             viewMap.put(out.getResultstr(), viewDto);
244         }
245         result.setStartTime(startTime);
246         result.setEndTime(endTime);
247         result.setCategories(categories);
248         result.setLegend(legend);
249         result.setViewMap(viewMap);
250         return result;
251     }
252
328ef4 253     @Override
254     public PreDataItemChartRespVO getPreDataSingleChart(PreDataSingleChartReqVO reqVO) {
255         PreDataItemChartRespVO result = new PreDataItemChartRespVO();
256
257
258         return result;
259     }
260
b368e6 261     /**
262      * 获取真实值
263      *
264      * @param pointId
265      * @param startTime
266      * @param endTime
267      * @return
268      */
269     private List<Object[]> getHisData(String pointId, Date startTime, Date endTime) {
270         List<Object[]> result = new ArrayList<>();
271         ApiPointDTO pointDTO = dataPointApi.getInfoById(pointId);
272         ApiPointValueQueryDTO queryPointDto = new ApiPointValueQueryDTO();
273         queryPointDto.setPointNo(pointDTO.getPointNo());
274         queryPointDto.setStart(startTime);
275         queryPointDto.setEnd(endTime);
276         List<ApiPointValueDTO> valueDTOS = dataPointApi.queryPointHistoryValue(queryPointDto);
277         if (CollectionUtils.isEmpty(valueDTOS)) {
278             return result;
279         }
280         valueDTOS.forEach(item -> {
281             Object[] values = new Object[2];
214275 282             values[0] = DateUtils.format(item.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
283             values[1] = new BigDecimal(item.getV()).setScale(2, BigDecimal.ROUND_HALF_UP);
b368e6 284             result.add(values);
285         });
286         return result;
287     }
288
289     @Override
290     public Boolean createAlarmMessage(AlarmMessageRespDTO dto) {
291         return true;
292     }
293
294     @Override
295     public List<AlarmMessageRespDTO> listAlarmMessage(Map<String, Object> params) {
296         return null;
297     }
298
299     @Override
300     public Boolean createScheduleSuggest(ScheduleSuggestRespDTO dto) {
301         return true;
302     }
303
304     @Override
305     public List<ScheduleSuggestRespDTO> listScheduleSuggest(ScheduleSuggestReqDTO params) {
306         return null;
307     }
308
309     @Override
310     public Boolean modifyPredictModelSetting(List<PredictModelSettingReqDTO> dtos) {
311         return true;
312     }
313
314     @Override
315     public Boolean modifyScheduleModelSetting(List<ScheduleModelSettingReqDTO> dtos) {
316         return true;
317     }
318 }