houzhongjian
2025-01-21 49148428d71415ad2b5449cb6ea2e83f1fbb6c18
提交 | 用户 | 时间
b368e6 1 package com.iailab.module.model.api;
2
0a7d0f 3 import com.iailab.framework.common.pojo.PageResult;
b368e6 4 import com.iailab.framework.common.util.date.DateUtils;
8a74e9 5 import com.iailab.framework.common.util.object.ConvertUtils;
6 import com.iailab.module.data.api.plan.PlanItemApi;
7 import com.iailab.module.data.api.plan.dto.ApiPlanDataDTO;
b368e6 8 import com.iailab.module.data.api.point.DataPointApi;
9 import com.iailab.module.data.api.point.dto.ApiPointDTO;
10 import com.iailab.module.data.api.point.dto.ApiPointValueDTO;
11 import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO;
8a74e9 12 import com.iailab.module.data.common.ApiDataQueryDTO;
b368e6 13 import com.iailab.module.model.api.mcs.McsApi;
14 import com.iailab.module.model.api.mcs.dto.*;
153763 15 import com.iailab.module.model.api.mdk.dto.StScheduleRecordVO;
22e321 16 import com.iailab.module.model.common.enums.DataTypeEnum;
91343d 17 import com.iailab.module.model.common.enums.PreLineTypeEnum;
153763 18 import com.iailab.module.model.enums.CommonConstant;
22e321 19 import com.iailab.module.model.influxdb.pojo.InfluxModelResultByOutPutIdsPOJO;
D 20 import com.iailab.module.model.influxdb.service.InfluxDBService;
21 import com.iailab.module.model.influxdb.vo.InfluxModelResultVO;
b368e6 22 import com.iailab.module.model.mcs.pre.entity.DmModuleEntity;
23 import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity;
c5fe30 24 import com.iailab.module.model.mcs.pre.entity.MmPredictAlarmMessageEntity;
b368e6 25 import com.iailab.module.model.mcs.pre.service.*;
22e321 26 import com.iailab.module.model.mcs.pre.vo.MmItemOutputRespVO;
c5fe30 27 import com.iailab.module.model.mcs.pre.vo.MmPredictAlarmMessageSaveReqVO;
22e321 28 import com.iailab.module.model.mcs.pre.vo.MmPredictItemRespVO;
055765 29 import com.iailab.module.model.mcs.sche.entity.StScheduleSuggestEntity;
dec0c2 30 import com.iailab.module.model.mcs.sche.service.StScheduleRecordService;
4072bc 31 import com.iailab.module.model.mcs.sche.service.StScheduleSchemeService;
055765 32 import com.iailab.module.model.mcs.sche.service.StScheduleSuggestService;
19f359 33 import com.iailab.module.model.mcs.sche.vo.StScheduleSuggestSaveReqVO;
ead005 34 import com.iailab.module.model.mdk.vo.ItemVO;
730d19 35 import com.iailab.module.model.mpk.service.ChartParamService;
91343d 36 import com.iailab.module.model.mpk.service.ChartService;
b368e6 37 import lombok.extern.slf4j.Slf4j;
977edc 38 import org.apache.commons.lang3.StringUtils;
b368e6 39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.util.CollectionUtils;
41 import org.springframework.validation.annotation.Validated;
42 import org.springframework.web.bind.annotation.RestController;
43
44 import java.math.BigDecimal;
45 import java.util.*;
912419 46 import java.util.stream.Collectors;
b368e6 47
48 /**
49  * @author PanZhibao
50  * @Description
51  * @createTime 2024年11月13日
52  */
53 @Slf4j
54 @RestController
55 @Validated
56 public class McsApiImpl implements McsApi {
57
58     @Autowired
6eeac9 59     private DataPointApi dataPointApi;
D 60
61     @Autowired
b368e6 62     private DmModuleService dmModuleService;
63
64     @Autowired
65     private MmPredictItemService mmPredictItemService;
66
67     @Autowired
68     private MmItemOutputService mmItemOutputService;
69
70     @Autowired
71     private MmItemResultService mmItemResultService;
72
73     @Autowired
74     private MmItemResultLastPointService mmItemResultLastPointService;
91343d 75
76     @Autowired
77     private MmItemResultJsonService mmItemResultJsonService;
78
79     @Autowired
80     private ChartService chartService;
c5fe30 81
82     @Autowired
83     private MmPredictAlarmMessageService mmPredictAlarmMessageService;
055765 84
85     @Autowired
86     private StScheduleSuggestService stScheduleSuggestService;
8a74e9 87
88     @Autowired
89     private PlanItemApi planItemApi;
9587d2 90
L 91     @Autowired
92     private MmPredictAlarmConfigService mmPredictAlarmConfigService;
3a6dfc 93
4072bc 94     @Autowired
L 95     private StScheduleSchemeService stScheduleSchemeService;
dec0c2 96
D 97     @Autowired
98     private StScheduleRecordService stScheduleRecordService;
730d19 99
D 100     @Autowired
101     private ChartParamService chartParamService;
22e321 102
D 103     @Autowired
104     private InfluxDBService influxDBService;
6eeac9 105
b368e6 106     private int HOUR_MINS = 60;
107
108     @Override
109     public List<PredictItemTreeDTO> getPredictItemTree() {
110         List<PredictItemTreeDTO> result = new ArrayList<>();
111
112         List<DmModuleEntity> moduleList = dmModuleService.list(new HashMap<>());
113         if (CollectionUtils.isEmpty(moduleList)) {
114             return result;
115         }
116         moduleList.forEach(item -> {
117             PredictItemTreeDTO moduleOpt = new PredictItemTreeDTO();
118             moduleOpt.setId(item.getId());
119             moduleOpt.setLabel(item.getModulename());
120             List<PredictItemTreeDTO> children = new ArrayList<>();
a4891a 121             List<ItemVO> itemList = mmPredictItemService.getByModuleId(item.getId());
b368e6 122             itemList.forEach(item1 -> {
123                 PredictItemTreeDTO chd = new PredictItemTreeDTO();
a4891a 124                 chd.setLabel(item1.getItemName());
b368e6 125                 chd.setId(item1.getId());
a4891a 126                 List<PredictItemTreeDTO> chd1 = new ArrayList<>();
127                 List<MmItemOutputEntity> outList = mmItemOutputService.getByItemid(item1.getId());
128                 if (!CollectionUtils.isEmpty(outList)) {
129                     outList.forEach(out -> {
130                         PredictItemTreeDTO chd2 = new PredictItemTreeDTO();
131                         chd2.setId(out.getId());
f283ee 132                         chd2.setLabel(out.getResultName());
a4891a 133                         chd1.add(chd2);
134                     });
135                 }
136                 chd.setChildren(chd1);
b368e6 137                 children.add(chd);
138             });
139             moduleOpt.setChildren(children);
140             result.add(moduleOpt);
141         });
142         return result;
143     }
144
145     @Override
146     public PreDataBarLineRespVO getPreDataCharts(PreDataBarLineReqVO reqVO) {
147         PreDataBarLineRespVO result = new PreDataBarLineRespVO();
a4891a 148         List<String> outIds = reqVO.getOutIds();
b368e6 149         List<String> legends = new ArrayList<>();
150         List<PreDataViewRespDTO> dataViewList = new ArrayList<>();
9e844c 151         if (CollectionUtils.isEmpty(outIds)) {
b368e6 152             return result;
153         }
154         Date predictTime = reqVO.getPredictTime();
155         if (predictTime == null) {
d22d56 156             MmItemOutputEntity output = null;
157             for (String outId : outIds) {
158                 output = mmItemOutputService.getOutPutById(outId);
159                 if (output != null) {
160                     break;
161                 }
162             }
b82ba2 163             ItemVO predictItem = mmPredictItemService.getItemByIdFromCache(output.getItemid());
9e844c 164             if (predictItem.getLastTime() != null) {
165                 predictTime = predictItem.getLastTime();
166             } else {
167                 Calendar calendar = Calendar.getInstance();
168                 calendar.set(Calendar.MILLISECOND, 0);
169                 calendar.set(Calendar.SECOND, 0);
170                 predictTime = calendar.getTime();
171             }
b368e6 172         }
173         Date startTime = reqVO.getStartTime();
174         if (startTime == null) {
175             Calendar calendar = Calendar.getInstance();
176             calendar.setTime(predictTime);
177             calendar.add(Calendar.HOUR_OF_DAY, -1);
178             startTime = calendar.getTime();
179         }
180         Date endTime = reqVO.getEndTime();
181         if (endTime == null) {
182             Calendar calendar = Calendar.getInstance();
183             calendar.setTime(predictTime);
184             calendar.add(Calendar.HOUR_OF_DAY, 1);
185             endTime = calendar.getTime();
186         }
187
a4891a 188         for (int i = 0; i < outIds.size(); i++) {
b368e6 189             PreDataViewRespDTO viewDto = new PreDataViewRespDTO();
a4891a 190             String outId = outIds.get(i);
191             MmItemOutputEntity output = mmItemOutputService.getOutPutById(outId);
b368e6 192             if (output == null) {
9e844c 193                 continue;
b368e6 194             }
f283ee 195             legends.add(output.getResultName());
9e844c 196             viewDto.setItemId(output.getItemid());
197             viewDto.setOutId(outId);
198             viewDto.setResultstr(output.getResultstr());
f283ee 199             viewDto.setResultName(output.getResultName());
b368e6 200             viewDto.setRealData(getHisData(output.getPointid(), startTime, endTime));
977edc 201             viewDto.setPreDataN(mmItemResultService.getData(output.getId(), startTime, endTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
202             viewDto.setPreDataL(mmItemResultLastPointService.getData(output.getId(), startTime, endTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
b368e6 203
204             List<Double> values = new ArrayList<>();
205             if (!CollectionUtils.isEmpty(viewDto.getRealData())) {
206                 List<Double> hisValues = new ArrayList<>();
207                 viewDto.getRealData().forEach(item -> {
208                     values.add(Double.parseDouble(item[1].toString()));
209                     hisValues.add(Double.parseDouble(item[1].toString()));
210                 });
211                 viewDto.setHisMax(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
212                 viewDto.setHisMin(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
213                 viewDto.setHisAvg(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).average().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
214                 viewDto.setHisCumulant(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).sum())
215                         .divide(new BigDecimal(HOUR_MINS), 2, BigDecimal.ROUND_HALF_UP));
216             }
217             if (!CollectionUtils.isEmpty(viewDto.getPreDataN())) {
218                 viewDto.getPreDataN().forEach(item -> {
219                     values.add(Double.parseDouble(item[1].toString()));
220                 });
221             }
222             if (!CollectionUtils.isEmpty(viewDto.getPreDataL())) {
223                 List<Double> preValues = new ArrayList<>();
224                 viewDto.getPreDataL().forEach(item -> {
225                     values.add(Double.parseDouble(item[1].toString()));
226                     preValues.add(Double.parseDouble(item[1].toString()));
227                 });
228                 viewDto.setPreMax(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
229                 viewDto.setPreMin(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
230                 viewDto.setPreAvg(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).average().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
231             }
232             if (!CollectionUtils.isEmpty(viewDto.getCurData())) {
233                 List<Double> preValues = new ArrayList<>();
234                 viewDto.getCurData().forEach(item -> {
235                     values.add(Double.parseDouble(item[1].toString()));
236                     preValues.add(Double.parseDouble(item[1].toString()));
237                 });
238                 viewDto.setPreCumulant(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).sum())
239                         .divide(new BigDecimal(HOUR_MINS), 2, BigDecimal.ROUND_HALF_UP));
240             }
241             if (!CollectionUtils.isEmpty(viewDto.getAdjData())) {
242                 viewDto.getAdjData().forEach(item -> {
243                     values.add(Double.parseDouble(item[1].toString()));
244                 });
245             }
246             if (!CollectionUtils.isEmpty(values)) {
247                 viewDto.setMaxValue(new BigDecimal(values.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
248                 viewDto.setMinValue(new BigDecimal(values.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
249             }
250             dataViewList.add(viewDto);
251         }
252         result.setStartTime(startTime);
253         result.setEndTime(endTime);
254         result.setPredictTime(predictTime);
255         result.setCategories(DateUtils.getTimeScale(startTime, endTime, 60));
256         result.setLegend(legends);
257         result.setDataViewList(dataViewList);
258         return result;
259     }
260
ead005 261     @Override
262     public PreDataItemChartRespVO getPreDataItemChart(PreDataItemChartReqVO reqVO) {
263         PreDataItemChartRespVO result = new PreDataItemChartRespVO();
864b61 264         ItemVO predictItem = mmPredictItemService.getItemById(reqVO.getItemId());
ead005 265         if (predictItem == null) {
266             return result;
267         }
268         if (predictItem.getLastTime() == null) {
269             return result;
270         }
328ef4 271         result.setPredictTime(predictItem.getLastTime());
ead005 272         Date startTime = reqVO.getStartTime();
273         if (startTime == null) {
274             Calendar calendar = Calendar.getInstance();
275             calendar.setTime(predictItem.getLastTime());
e691b9 276             calendar.add(Calendar.SECOND, -1 * predictItem.getPredictLength() * predictItem.getGranularity());
ead005 277             startTime = calendar.getTime();
278         }
279         Date endTime = reqVO.getEndTime();
280         if (endTime == null) {
281             Calendar calendar = Calendar.getInstance();
282             calendar.setTime(predictItem.getLastTime());
e691b9 283             calendar.add(Calendar.SECOND, predictItem.getPredictLength() * predictItem.getGranularity());
ead005 284             endTime = calendar.getTime();
285         }
1f9784 286         if (endTime.getTime() <= startTime.getTime()) {
287             Calendar calendar = Calendar.getInstance();
288             calendar.setTime(startTime);
e691b9 289             calendar.add(Calendar.SECOND, predictItem.getPredictLength() * predictItem.getGranularity());
1f9784 290             endTime = calendar.getTime();
291         }
292
ead005 293         List<String> categories = DateUtils.getTimeScale(startTime, endTime, predictItem.getGranularity());
294         List<String> legend = new ArrayList<>();
295         LinkedHashMap<String, PreDataSampleViewRespDTO> viewMap = new LinkedHashMap<>();
296         List<MmItemOutputEntity> outs = mmItemOutputService.getByItemid(reqVO.getItemId());
297         if (CollectionUtils.isEmpty(outs)) {
298             return result;
299         }
300         for (MmItemOutputEntity out : outs) {
f283ee 301             legend.add(out.getResultName());
ead005 302             PreDataSampleViewRespDTO viewDto = new PreDataSampleViewRespDTO();
85b200 303             if (StringUtils.isNotBlank(out.getPointid())) {
D 304                 viewDto.setRealData(getHisData(out.getPointid(), startTime, endTime));
305             }
977edc 306             viewDto.setPreDataN(mmItemResultService.getData(out.getId(), startTime, endTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
f283ee 307             viewMap.put(out.getResultName(), viewDto);
ead005 308         }
309         result.setStartTime(startTime);
310         result.setEndTime(endTime);
311         result.setCategories(categories);
312         result.setLegend(legend);
313         result.setViewMap(viewMap);
314         return result;
315     }
316
328ef4 317     @Override
91343d 318     public PreDataSingleChartRespVO getPreDataSingleChart(PreDataSingleChartReqVO reqVO) {
319         PreDataSingleChartRespVO result = new PreDataSingleChartRespVO();
328ef4 320
91343d 321         Map<String, String> chartParams = chartService.getByChartCode(reqVO.getChartCode());
322         if (CollectionUtils.isEmpty(chartParams)) {
323             return result;
324         }
325         String itemCode = chartParams.get(CommonConstant.ITEM_CODE);
326         if (itemCode == null) {
327             return result;
328         }
329         String resultStr = chartParams.get(CommonConstant.RESULT_STR);
330         if (resultStr == null) {
331             return result;
332         }
bd9085 333         String resultIndex = chartParams.get(CommonConstant.RESULT_INDEX);
334
91343d 335         ItemVO predictItem = mmPredictItemService.getItemByItemNo(itemCode);
336         if (predictItem == null || predictItem.getLastTime() == null) {
337             return result;
338         }
977edc 339         String timeFormat = StringUtils.isBlank(reqVO.getTimeFormat()) ? DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND : reqVO.getTimeFormat();
340         PreLineTypeEnum lineType = chartParams.get(CommonConstant.LINE_TYPE) == null ? PreLineTypeEnum.TN : PreLineTypeEnum.getEumByCode(chartParams.get(CommonConstant.LINE_TYPE));
8fd9f6 341         BigDecimal rangeH = chartParams.get(CommonConstant.RANGE_H) == null ? null : new BigDecimal(chartParams.get(CommonConstant.RANGE_H));
342         BigDecimal rangeL = chartParams.get(CommonConstant.RANGE_L) == null ? null : new BigDecimal(chartParams.get(CommonConstant.RANGE_L));
343         BigDecimal limitH = chartParams.get(CommonConstant.LIMIT_H) == null ? null : new BigDecimal(chartParams.get(CommonConstant.LIMIT_H));
344         BigDecimal limitL = chartParams.get(CommonConstant.LIMIT_L) == null ? null : new BigDecimal(chartParams.get(CommonConstant.LIMIT_L));
91343d 345         int lengthLeft = chartParams.get(CommonConstant.LENGTH_LEFT) == null ? predictItem.getPredictLength() : new BigDecimal(chartParams.get(CommonConstant.LENGTH_LEFT)).intValue();
346         int lengthRight = chartParams.get(CommonConstant.LENGTH_RIGHT) == null ? predictItem.getPredictLength() : new BigDecimal(chartParams.get(CommonConstant.LENGTH_RIGHT)).intValue();
8a74e9 347
348         Date[] timeArray = calResultTime(predictItem, reqVO.getStartTime(), reqVO.getEndTime(), lengthLeft, lengthRight);
349         Date predictTime = timeArray[0];
350         Date startTime = timeArray[1];
351         Date endTime = timeArray[2];
352
977edc 353         List<String> categories = DateUtils.getTimeScale(startTime, endTime, predictItem.getGranularity(), timeFormat);
91343d 354         List<String> legend = new ArrayList<>();
bdab69 355         MmItemOutputEntity outPut = mmItemOutputService.getByItemid(predictItem.getId(), resultStr, resultIndex);
91343d 356         PreDataViewRespDTO dataView = new PreDataViewRespDTO();
357         dataView.setItemId(predictItem.getId());
358         dataView.setItemName(predictItem.getItemName());
359         dataView.setResultstr(resultStr);
360         dataView.setRangeH(rangeH);
361         dataView.setRangeL(rangeL);
362         dataView.setLimitH(limitH);
363         dataView.setLimitL(limitL);
b6bfe8 364         if (StringUtils.isNotBlank(outPut.getPointid())) {
D 365             dataView.setRealData(getHisData(outPut.getPointid(), startTime, endTime, timeFormat));
366         } else {
367             dataView.setRealData(new ArrayList<>());
368         }
3722c4 369         dataView.setCurData(mmItemResultJsonService.getData(outPut.getId(), predictTime, timeFormat));
d9f2f1 370         dataView.setLineType(lineType.getCode());
91343d 371         switch (lineType) {
372             case TN:
977edc 373                 dataView.setPreDataN(mmItemResultService.getData(outPut.getId(), startTime, endTime, timeFormat));
91343d 374                 break;
375             case TL:
977edc 376                 dataView.setPreDataN(mmItemResultService.getData(outPut.getId(), predictTime, endTime, timeFormat));
377                 dataView.setPreDataL(mmItemResultLastPointService.getData(outPut.getId(), startTime, endTime, timeFormat));
91343d 378                 break;
379             default:
380                 break;
381         }
d9f2f1 382         if (!CollectionUtils.isEmpty(dataView.getRealData())) {
383             Object[] rdo = dataView.getRealData().get(dataView.getRealData().size() - 1);
384             dataView.setCurrValue(new BigDecimal(rdo[1].toString()));
385         }
912419 386
3722c4 387         if (!CollectionUtils.isEmpty(dataView.getCurData())) {
388             List<Double> curList = dataView.getCurData().stream().map(t -> {
912419 389                 return new Double(t[1].toString());
390             }).collect(Collectors.toList());
391             dataView.setPreMax(new BigDecimal(curList.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
392             dataView.setPreMin(new BigDecimal(curList.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
8fd9f6 393             dataView.setPreLast(new BigDecimal(curList.get(curList.size() - 1)));
2c1cf6 394             dataView.setPreCumulant(new BigDecimal(curList.stream().mapToDouble(Double::doubleValue).sum()));
912419 395         }
396
9904da 397         String alarmObj = chartParams.get(CommonConstant.ALARM_OBJ);
398         if (StringUtils.isNotBlank(alarmObj)) {
399             List<AlarmMessageRespDTO> alarmList = ConvertUtils.sourceToTarget(mmPredictAlarmMessageService.getList(alarmObj, predictTime), AlarmMessageRespDTO.class);
8ccce9 400             dataView.setAlarmList(alarmList);
8fd9f6 401             if (CollectionUtils.isEmpty(alarmList)) {
402                 dataView.setAlarmMessage("暂无预警信息");
403             } else {
404                 dataView.setAlarmMessage(alarmList.get(0).getContent());
405             }
9904da 406         }
407
8a74e9 408         result.setPredictTime(predictTime);
91343d 409         result.setStartTime(startTime);
410         result.setEndTime(endTime);
411         result.setCategories(categories);
412         result.setLegend(legend);
413         result.setDataView(dataView);
8a74e9 414         return result;
415     }
416
417     @Override
d9fe8f 418     public Map<String, List<Object[]>> getPreDataCur(PreDataJsonReqVO reqVO) {
419         Map<String, List<Object[]>> result = new HashMap<>();
420         if (reqVO == null || reqVO.getPredictTime() == null || CollectionUtils.isEmpty(reqVO.getOutputIdList())) {
421             return result;
422         }
423         reqVO.getOutputIdList().forEach(outPutId -> {
424             result.put(outPutId, mmItemResultJsonService.getData(outPutId, reqVO.getPredictTime(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
425         });
426         return result;
427     }
428
429     @Override
8a74e9 430     public PlanDataSingleChartRespVO getPlanDataSingleChart(PreDataSingleChartReqVO reqVO) {
431         PlanDataSingleChartRespVO result = new PlanDataSingleChartRespVO();
432         Map<String, String> chartParams = chartService.getByChartCode(reqVO.getChartCode());
433         if (CollectionUtils.isEmpty(chartParams)) {
434             return result;
435         }
436         String itemCode = chartParams.get(CommonConstant.ITEM_CODE);
437         if (itemCode == null) {
438             return result;
439         }
440         String planItemStr = chartParams.get(CommonConstant.PLAN_ITEM_LIST);
441         if (planItemStr == null) {
442             return result;
443         }
444         List<String> planItemCodeList = Arrays.asList(planItemStr.split(","));
445
446         ItemVO predictItem = mmPredictItemService.getItemByItemNo(itemCode);
447         if (predictItem == null || predictItem.getLastTime() == null) {
448             return result;
449         }
450         String timeFormat = StringUtils.isBlank(reqVO.getTimeFormat()) ? DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND : reqVO.getTimeFormat();
451         int lengthLeft = chartParams.get(CommonConstant.LENGTH_LEFT) == null ? predictItem.getPredictLength() : new BigDecimal(chartParams.get(CommonConstant.LENGTH_LEFT)).intValue();
452         int lengthRight = chartParams.get(CommonConstant.LENGTH_RIGHT) == null ? predictItem.getPredictLength() : new BigDecimal(chartParams.get(CommonConstant.LENGTH_RIGHT)).intValue();
453         Date[] timeArray = calResultTime(predictItem, reqVO.getStartTime(), reqVO.getEndTime(), lengthLeft, lengthRight);
454         Date predictTime = timeArray[0];
455         Date startTime = timeArray[1];
456         Date endTime = timeArray[2];
457         List<String> categories = DateUtils.getTimeScale(startTime, endTime, predictItem.getGranularity(), timeFormat);
458         List<String> legend = new ArrayList<>();
459         LinkedHashMap<String, List<PlanDataRecordDTO>> record = new LinkedHashMap<>();
460         ApiDataQueryDTO queryDTO = new ApiDataQueryDTO();
461         queryDTO.setItemNos(planItemCodeList);
462         queryDTO.setStart(startTime);
463         queryDTO.setEnd(endTime);
464         LinkedHashMap<String, List<ApiPlanDataDTO>> planData = planItemApi.queryPlanItemRecordValue(queryDTO);
465         if (CollectionUtils.isEmpty(planData)) {
466             planData.forEach((key, value) -> {
467                 record.put(key, ConvertUtils.sourceToTarget(value, PlanDataRecordDTO.class));
468             });
469         }
470         result.setPredictTime(predictTime);
471         result.setStartTime(startTime);
472         result.setEndTime(endTime);
473         result.setCategories(categories);
474         result.setLegend(legend);
475         result.setRecord(record);
328ef4 476         return result;
477     }
478
b368e6 479     /**
480      * 获取真实值
481      *
482      * @param pointId
483      * @param startTime
484      * @param endTime
485      * @return
486      */
487     private List<Object[]> getHisData(String pointId, Date startTime, Date endTime) {
488         List<Object[]> result = new ArrayList<>();
489         ApiPointDTO pointDTO = dataPointApi.getInfoById(pointId);
490         ApiPointValueQueryDTO queryPointDto = new ApiPointValueQueryDTO();
491         queryPointDto.setPointNo(pointDTO.getPointNo());
492         queryPointDto.setStart(startTime);
493         queryPointDto.setEnd(endTime);
494         List<ApiPointValueDTO> valueDTOS = dataPointApi.queryPointHistoryValue(queryPointDto);
495         if (CollectionUtils.isEmpty(valueDTOS)) {
496             return result;
497         }
498         valueDTOS.forEach(item -> {
499             Object[] values = new Object[2];
214275 500             values[0] = DateUtils.format(item.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
501             values[1] = new BigDecimal(item.getV()).setScale(2, BigDecimal.ROUND_HALF_UP);
b368e6 502             result.add(values);
503         });
504         return result;
505     }
506
977edc 507     /**
508      * 获取真实值
509      *
510      * @param pointId
511      * @param startTime
512      * @param endTime
513      * @param timeFormat
514      * @return
515      */
516     private List<Object[]> getHisData(String pointId, Date startTime, Date endTime, String timeFormat) {
517         List<Object[]> result = new ArrayList<>();
518         ApiPointDTO pointDTO = dataPointApi.getInfoById(pointId);
519         ApiPointValueQueryDTO queryPointDto = new ApiPointValueQueryDTO();
520         queryPointDto.setPointNo(pointDTO.getPointNo());
521         queryPointDto.setStart(startTime);
522         queryPointDto.setEnd(endTime);
523         List<ApiPointValueDTO> valueDTOS = dataPointApi.queryPointHistoryValue(queryPointDto);
524         if (CollectionUtils.isEmpty(valueDTOS)) {
525             return result;
526         }
527         valueDTOS.forEach(item -> {
528             Object[] values = new Object[2];
529             values[0] = DateUtils.format(item.getT(), timeFormat);
530             values[1] = new BigDecimal(item.getV()).setScale(2, BigDecimal.ROUND_HALF_UP);
531             result.add(values);
532         });
533         return result;
534     }
535
c5fe30 536     /**
537      * 新增预警信息
538      *
539      * @param dto
540      * @return
541      */
b368e6 542     @Override
543     public Boolean createAlarmMessage(AlarmMessageRespDTO dto) {
c5fe30 544         try {
545             mmPredictAlarmMessageService.create(ConvertUtils.sourceToTarget(dto, MmPredictAlarmMessageSaveReqVO.class));
546             return true;
547         } catch (Exception e) {
548             return false;
549         }
b368e6 550     }
551
552     @Override
553     public List<AlarmMessageRespDTO> listAlarmMessage(Map<String, Object> params) {
554         return null;
555     }
556
557     @Override
9587d2 558     public List<AlarmConfigRespDTO> listAlarmConfig(Map<String, Object> params) {
L 559         return mmPredictAlarmConfigService.list(params);
560     }
561
562     @Override
c5fe30 563     public AlarmMessageRespDTO getLastAlarmMessage(String alarmObj) {
564         MmPredictAlarmMessageEntity entity = mmPredictAlarmMessageService.getLast(alarmObj);
565         return ConvertUtils.sourceToTarget(entity, AlarmMessageRespDTO.class);
566     }
567
568     @Override
b368e6 569     public Boolean createScheduleSuggest(ScheduleSuggestRespDTO dto) {
19f359 570         stScheduleSuggestService.create(ConvertUtils.sourceToTarget(dto, StScheduleSuggestSaveReqVO.class));
b368e6 571         return true;
572     }
573
574     @Override
4072bc 575     public List<ScheduleSuggestRespDTO> listScheduleSuggest(Map<String, Object> params) {
3a6dfc 576         return Collections.emptyList();
L 577     }
578
579     @Override
055765 580     public List<ScheduleSuggestRespDTO> getLastLimitScheduleSuggest(String scheduleObj, Integer limit) {
581         List<StScheduleSuggestEntity> list = stScheduleSuggestService.getList(scheduleObj, limit);
582         return ConvertUtils.sourceToTarget(list, ScheduleSuggestRespDTO.class);
b368e6 583     }
584
585     @Override
586     public Boolean modifyPredictModelSetting(List<PredictModelSettingReqDTO> dtos) {
587         return true;
588     }
589
590     @Override
591     public Boolean modifyScheduleModelSetting(List<ScheduleModelSettingReqDTO> dtos) {
592         return true;
593     }
8a74e9 594
4072bc 595     @Override
ed4107 596     public List<StScheduleSchemeDTO> listScheduleScheme(String triggerMethod, String triggerCondition) {
597         Map<String, Object> params = new HashMap<>();
598         params.put("triggerMethod", triggerMethod);
599         params.put("triggerCondition", triggerCondition);
4072bc 600         return stScheduleSchemeService.list(params);
6eeac9 601     }
8a74e9 602
730d19 603     @Override
D 604     public List<ChartParamDTO> getChartParamList(String chartCode) {
605         if (StringUtils.isBlank(chartCode)) {
606             return null;
607         }
608         return chartParamService.list(chartCode);
609     }
610
0a7d0f 611     @Override
612     public PageResult<StAlarmAndSuggestRespVO> getAlarmAndSuggestPage(StAlarmAndSuggestPageReqVO reqVO) {
613         return stScheduleSchemeService.getAlarmAndSuggestPage(reqVO);
614     }
615
22e321 616     @Override
D 617     public Map<String, Map<String, Double>> getPreDoubleData(PreDoubleDataReqVO reqVO) {
618         List<MmPredictItemRespVO> doubleItemOutPuts = mmPredictItemService.getDoubleOutPutsByItemNos(reqVO.getItemNos());
619
620         //<itemNo,<resultStr,double>>
621         Map<String, Map<String, Double>> result = new HashMap<>(reqVO.getItemNos().size());
622
623         for (MmPredictItemRespVO mmPredictItemRespVO : doubleItemOutPuts) {
624             List<String> outPutIds = mmPredictItemRespVO.getOutPuts().stream().map(MmItemOutputRespVO::getId).collect(Collectors.toList());
625             InfluxModelResultByOutPutIdsPOJO pojo = new InfluxModelResultByOutPutIdsPOJO();
626             pojo.setOutPutIds(outPutIds);
627             pojo.setType(DataTypeEnum.FLOAT.getCode());
628             Map<String, List<InfluxModelResultVO>> outPutIdDoubles = influxDBService.queryModelResultsByOutPutIds(pojo, mmPredictItemRespVO.getLastTime(), mmPredictItemRespVO.getLastTime());
629             Map<String, Double> outPutIdDouble = new HashMap<>(outPutIds.size());
630             for (MmItemOutputRespVO outPut : mmPredictItemRespVO.getOutPuts()) {
631                 String outPutId = outPut.getId();
632                 if (outPutIdDoubles.containsKey(outPutId)) {
633                     List<InfluxModelResultVO> influxModelResultVOS = outPutIdDoubles.get(outPutId);
634                     if (!CollectionUtils.isEmpty(influxModelResultVOS)) {
635                         outPutIdDouble.put(outPut.getResultstr(),Double.valueOf(influxModelResultVOS.get(0).getValue().toString()));
636                     }
637                 }
638             }
639             result.put(mmPredictItemRespVO.getItemno(),outPutIdDouble);
640         }
641         return result;
642     }
643
bffae5 644     public Map<String, BigDecimal> getPredictLastValue(PredictLastValueReqVO reqVO) {
645         Map<String, BigDecimal> result = new HashMap<>(reqVO.getItemNos().size());
646         if (CollectionUtils.isEmpty(reqVO.getItemNos())) {
647             return result;
648         }
649         List<String[]> itemNos = reqVO.getItemNos();
650         for (String[] itemNo : itemNos) {
fed257 651             String key = itemNo[0] + "_" + itemNo[1] + "_" + itemNo[2];
bffae5 652             ItemVO predictItem = mmPredictItemService.getItemByItemNo(itemNo[0]);
653             if (predictItem == null || predictItem.getLastTime() == null) {
fed257 654                 result.put(key, BigDecimal.ZERO);
bffae5 655             }
656             MmItemOutputEntity outPut = mmItemOutputService.getByItemid(predictItem.getId(), itemNo[1], itemNo[2]);
657
658             Date predictTime = predictItem.getLastTime();
659             List<Object[]> curData = mmItemResultJsonService.getData(outPut.getId(), predictTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
660             if(CollectionUtils.isEmpty(curData)) {
fed257 661                 result.put(key, BigDecimal.ZERO);
bffae5 662             } else {
fed257 663                 result.put(key, new BigDecimal(curData.get(curData.size() - 1)[1].toString()));
bffae5 664             }
665         }
666         return result;
667     }
668
dec0c2 669     @Override
D 670     public Map<String,List<Object[]>> getElectricPredictData(String itemCode) {
671         ItemVO predictItem = mmPredictItemService.getItemByItemNo(itemCode);
672         if (null == predictItem) {
673             return new HashMap<>();
674         }
675         List<Integer> resultIndexs = new ArrayList<>();
676         resultIndexs.add(0);
677         resultIndexs.add(1);
678         resultIndexs.add(2);
679         List<MmItemOutputEntity> outPuts = mmItemOutputService.getByItemid(predictItem.getId(),"predictValues",resultIndexs);
680         if (CollectionUtils.isEmpty(outPuts)) {
681             return new HashMap<>();
682         }
683         InfluxModelResultByOutPutIdsPOJO pojo = new InfluxModelResultByOutPutIdsPOJO();
684         pojo.setOutPutIds(outPuts.stream().map(MmItemOutputEntity::getId).collect(Collectors.toList()));
685         pojo.setType(DataTypeEnum.FLOAT.getCode());
686         Calendar calendar = Calendar.getInstance();
687         calendar.setTime(predictItem.getLastTime());
688         calendar.add(Calendar.DAY_OF_YEAR, 7);
689         Map<String, List<InfluxModelResultVO>> outPutDatas = influxDBService.queryModelResultsByOutPutIds(pojo, predictItem.getLastTime(), calendar.getTime());
690         Map<String,List<Object[]>> result = new HashMap<>(outPuts.size());
691         for (MmItemOutputEntity outPut : outPuts) {
692             String outPutId = outPut.getId();
693             if (outPutDatas.containsKey(outPutId)) {
694                 List<InfluxModelResultVO> influxModelResultVOS = outPutDatas.get(outPutId);
695                 result.put(null == outPut.getResultIndex() ? outPut.getResultstr() : outPut.getResultstr()+"_"+outPut.getResultIndex(),influxModelResultVOS.stream().map(e -> {
696                     Object[] values = new Object[2];
697                     values[0] = DateUtils.format(Date.from(e.getTimestamp()), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
698                     values[1] = Double.valueOf(e.getValue().toString());
699                     return values;
700                 }).collect(Collectors.toList()));
701             }
702         }
703         return result;
704     }
705
706     @Override
153763 707     public List<StScheduleRecordVO> getLastScheduleData(String scheduleCode, Integer limit) {
dec0c2 708         if (StringUtils.isBlank(scheduleCode)) {
D 709             return null;
710         }
153763 711         return stScheduleRecordService.getLastScheduleResult(scheduleCode,limit);
dec0c2 712     }
D 713
714     @Override
715     public List<Map<String, Object>> getRealTimeStatus(List<Map<String, Object>> machines) {
716         List<String> pointNos = new ArrayList<>(16);
717         for (Map<String, Object> machine : machines) {
718             List<String> list = (List<String>) machine.get("pointNos");
719             pointNos.addAll(list);
720         }
721         Map<String, Object> pointValues = dataPointApi.queryPointsRealValue(pointNos);
722         List<Map<String, Object>> results = new ArrayList<>();
723         for (Map<String, Object> machine : machines) {
724             List<String> pointNoList = (List<String>) machine.get("pointNos");
725             if (!pointValues.containsKey(pointNoList.get(0)) || !pointValues.containsKey(pointNoList.get(1))) {
726                 machine.put("status",false);
727                 machine.put("num",0);
728                 results.add(machine);
729                 continue;
730             }
731             Double PValue = Double.valueOf(pointValues.get(pointNoList.get(0)).toString());
732             Double QValue = Double.valueOf(pointValues.get(pointNoList.get(1)).toString());
733             Double cos = calculateCos(PValue, QValue);
734             if (cos.equals(0.0)) {
735                 machine.put("status",false);
736                 machine.put("num",0);
737             }else {
738                 machine.put("status",true);
739                 machine.put("num",cos);
740             }
741             results.add(machine);
742         }
743         return results;
744     }
745
153763 746     @Override
D 747     public Map<String, List<Object[]>> getPredictDataItemNo(PreDataItemNoReqVO reqVO) {
748         if (StringUtils.isBlank(reqVO.getItemNo()) || null == reqVO.getStartTime() || null == reqVO.getEndTime()) {
749             return new HashMap<>();
750         }
751         ItemVO predictItem = mmPredictItemService.getItemByItemNo(reqVO.getItemNo());
752         if (null == predictItem) {
753             return new HashMap<>();
754         }
755         List<String> itemNos = new ArrayList<String>(){{
756            add(reqVO.getItemNo());
757         }};
758         List<MmPredictItemRespVO> ItemRespVOs = mmPredictItemService.getArrayOutPutsByItemNos(itemNos);
759         if (CollectionUtils.isEmpty(ItemRespVOs)) {
760             return new HashMap<>();
761         }
762         List<MmItemOutputRespVO> outPuts = ItemRespVOs.get(0).getOutPuts();
763         if (CollectionUtils.isEmpty(outPuts)) {
764             return new HashMap<>();
765         }
766
767         InfluxModelResultByOutPutIdsPOJO pojo = new InfluxModelResultByOutPutIdsPOJO();
768         pojo.setOutPutIds(outPuts.stream().map(MmItemOutputRespVO::getId).collect(Collectors.toList()));
769         pojo.setType(DataTypeEnum.FLOAT.getCode());
770         Map<String, List<InfluxModelResultVO>> outPutDatas = influxDBService.queryModelResultsByOutPutIds(pojo, reqVO.getStartTime(), reqVO.getEndTime());
771         Map<String,List<Object[]>> result = new HashMap<>(outPuts.size());
772         for (MmItemOutputRespVO outPut : outPuts) {
773             String outPutId = outPut.getId();
774             if (outPutDatas.containsKey(outPutId)) {
775                 List<InfluxModelResultVO> influxModelResultVOS = outPutDatas.get(outPutId);
776                 result.put(null == outPut.getResultIndex() ? outPut.getResultstr() : outPut.getResultstr()+"_"+outPut.getResultIndex(),influxModelResultVOS.stream().map(e -> {
777                     Object[] values = new Object[2];
778                     values[0] = DateUtils.format(Date.from(e.getTimestamp()), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
779                     values[1] = Double.valueOf(e.getValue().toString());
780                     return values;
781                 }).collect(Collectors.toList()));
782             }
783         }
784         return result;
785     }
786
dec0c2 787     /**
D 788      * 计算功率因数 p²/(根号:p²+Q²)
789      **/
790     public Double calculateCos(Double PValue,Double QValue) {
791         // 绝对值
792         PValue = Math.abs(PValue);
793         QValue = Math.abs(QValue);
794         //PValue [0,0.001] 直接判断为关闭返回0
795         if (PValue >= 0 && PValue <= 0.001) {
796             return 0.0;
797         }else {
798             BigDecimal result = new BigDecimal(PValue).divide(BigDecimal.valueOf(Math.sqrt(Math.pow(PValue, 2) + Math.pow(QValue, 2))), 2, BigDecimal.ROUND_HALF_UP);
799             return result.doubleValue();
800         }
801     }
802
8a74e9 803     private Date[] calResultTime(ItemVO predictItem, Date startTimeReq, Date endTimeReq, int lengthLeft, int lengthRight) {
804         Date[] result = new Date[3];
805         Date predictTime = predictItem.getLastTime();
dec0c2 806         Integer granularity = predictItem.getGranularity();
8a74e9 807         Date startTime = startTimeReq;
808         if (startTime == null) {
809             Calendar calendar = Calendar.getInstance();
810             calendar.setTime(predictItem.getLastTime());
dec0c2 811             calendar.add(Calendar.SECOND, -1 * lengthLeft * granularity);
8a74e9 812             startTime = calendar.getTime();
813         }
814         Date endTime = endTimeReq;
815         if (endTime == null) {
816             Calendar calendar = Calendar.getInstance();
817             calendar.setTime(predictItem.getLastTime());
dec0c2 818             calendar.add(Calendar.SECOND, lengthRight * granularity);
8a74e9 819             endTime = calendar.getTime();
820         }
821         if (endTime.getTime() <= startTime.getTime()) {
822             Calendar calendar = Calendar.getInstance();
823             calendar.setTime(startTime);
dec0c2 824             calendar.add(Calendar.SECOND, lengthRight * granularity);
8a74e9 825             endTime = calendar.getTime();
826         }
827         result[0] = predictTime;
828         result[1] = startTime;
829         result[2] = endTime;
830         return result;
831     }
b368e6 832 }