提交 | 用户 | 时间
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)));
3722c4 394             dataView.setPreCumulant(new BigDecimal(curList.stream().mapToDouble(Double::doubleValue).sum())
395                     .divide(new BigDecimal(HOUR_MINS), 2, BigDecimal.ROUND_HALF_UP));
912419 396         }
397
9904da 398         String alarmObj = chartParams.get(CommonConstant.ALARM_OBJ);
399         if (StringUtils.isNotBlank(alarmObj)) {
400             List<AlarmMessageRespDTO> alarmList = ConvertUtils.sourceToTarget(mmPredictAlarmMessageService.getList(alarmObj, predictTime), AlarmMessageRespDTO.class);
8ccce9 401             dataView.setAlarmList(alarmList);
8fd9f6 402             if (CollectionUtils.isEmpty(alarmList)) {
403                 dataView.setAlarmMessage("暂无预警信息");
404             } else {
405                 dataView.setAlarmMessage(alarmList.get(0).getContent());
406             }
9904da 407         }
408
8a74e9 409         result.setPredictTime(predictTime);
91343d 410         result.setStartTime(startTime);
411         result.setEndTime(endTime);
412         result.setCategories(categories);
413         result.setLegend(legend);
414         result.setDataView(dataView);
8a74e9 415         return result;
416     }
417
418     @Override
d9fe8f 419     public Map<String, List<Object[]>> getPreDataCur(PreDataJsonReqVO reqVO) {
420         Map<String, List<Object[]>> result = new HashMap<>();
421         if (reqVO == null || reqVO.getPredictTime() == null || CollectionUtils.isEmpty(reqVO.getOutputIdList())) {
422             return result;
423         }
424         reqVO.getOutputIdList().forEach(outPutId -> {
425             result.put(outPutId, mmItemResultJsonService.getData(outPutId, reqVO.getPredictTime(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
426         });
427         return result;
428     }
429
430     @Override
8a74e9 431     public PlanDataSingleChartRespVO getPlanDataSingleChart(PreDataSingleChartReqVO reqVO) {
432         PlanDataSingleChartRespVO result = new PlanDataSingleChartRespVO();
433         Map<String, String> chartParams = chartService.getByChartCode(reqVO.getChartCode());
434         if (CollectionUtils.isEmpty(chartParams)) {
435             return result;
436         }
437         String itemCode = chartParams.get(CommonConstant.ITEM_CODE);
438         if (itemCode == null) {
439             return result;
440         }
441         String planItemStr = chartParams.get(CommonConstant.PLAN_ITEM_LIST);
442         if (planItemStr == null) {
443             return result;
444         }
445         List<String> planItemCodeList = Arrays.asList(planItemStr.split(","));
446
447         ItemVO predictItem = mmPredictItemService.getItemByItemNo(itemCode);
448         if (predictItem == null || predictItem.getLastTime() == null) {
449             return result;
450         }
451         String timeFormat = StringUtils.isBlank(reqVO.getTimeFormat()) ? DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND : reqVO.getTimeFormat();
452         int lengthLeft = chartParams.get(CommonConstant.LENGTH_LEFT) == null ? predictItem.getPredictLength() : new BigDecimal(chartParams.get(CommonConstant.LENGTH_LEFT)).intValue();
453         int lengthRight = chartParams.get(CommonConstant.LENGTH_RIGHT) == null ? predictItem.getPredictLength() : new BigDecimal(chartParams.get(CommonConstant.LENGTH_RIGHT)).intValue();
454         Date[] timeArray = calResultTime(predictItem, reqVO.getStartTime(), reqVO.getEndTime(), lengthLeft, lengthRight);
455         Date predictTime = timeArray[0];
456         Date startTime = timeArray[1];
457         Date endTime = timeArray[2];
458         List<String> categories = DateUtils.getTimeScale(startTime, endTime, predictItem.getGranularity(), timeFormat);
459         List<String> legend = new ArrayList<>();
460         LinkedHashMap<String, List<PlanDataRecordDTO>> record = new LinkedHashMap<>();
461         ApiDataQueryDTO queryDTO = new ApiDataQueryDTO();
462         queryDTO.setItemNos(planItemCodeList);
463         queryDTO.setStart(startTime);
464         queryDTO.setEnd(endTime);
465         LinkedHashMap<String, List<ApiPlanDataDTO>> planData = planItemApi.queryPlanItemRecordValue(queryDTO);
466         if (CollectionUtils.isEmpty(planData)) {
467             planData.forEach((key, value) -> {
468                 record.put(key, ConvertUtils.sourceToTarget(value, PlanDataRecordDTO.class));
469             });
470         }
471         result.setPredictTime(predictTime);
472         result.setStartTime(startTime);
473         result.setEndTime(endTime);
474         result.setCategories(categories);
475         result.setLegend(legend);
476         result.setRecord(record);
328ef4 477         return result;
478     }
479
b368e6 480     /**
481      * 获取真实值
482      *
483      * @param pointId
484      * @param startTime
485      * @param endTime
486      * @return
487      */
488     private List<Object[]> getHisData(String pointId, Date startTime, Date endTime) {
489         List<Object[]> result = new ArrayList<>();
490         ApiPointDTO pointDTO = dataPointApi.getInfoById(pointId);
491         ApiPointValueQueryDTO queryPointDto = new ApiPointValueQueryDTO();
492         queryPointDto.setPointNo(pointDTO.getPointNo());
493         queryPointDto.setStart(startTime);
494         queryPointDto.setEnd(endTime);
495         List<ApiPointValueDTO> valueDTOS = dataPointApi.queryPointHistoryValue(queryPointDto);
496         if (CollectionUtils.isEmpty(valueDTOS)) {
497             return result;
498         }
499         valueDTOS.forEach(item -> {
500             Object[] values = new Object[2];
214275 501             values[0] = DateUtils.format(item.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
502             values[1] = new BigDecimal(item.getV()).setScale(2, BigDecimal.ROUND_HALF_UP);
b368e6 503             result.add(values);
504         });
505         return result;
506     }
507
977edc 508     /**
509      * 获取真实值
510      *
511      * @param pointId
512      * @param startTime
513      * @param endTime
514      * @param timeFormat
515      * @return
516      */
517     private List<Object[]> getHisData(String pointId, Date startTime, Date endTime, String timeFormat) {
518         List<Object[]> result = new ArrayList<>();
519         ApiPointDTO pointDTO = dataPointApi.getInfoById(pointId);
520         ApiPointValueQueryDTO queryPointDto = new ApiPointValueQueryDTO();
521         queryPointDto.setPointNo(pointDTO.getPointNo());
522         queryPointDto.setStart(startTime);
523         queryPointDto.setEnd(endTime);
524         List<ApiPointValueDTO> valueDTOS = dataPointApi.queryPointHistoryValue(queryPointDto);
525         if (CollectionUtils.isEmpty(valueDTOS)) {
526             return result;
527         }
528         valueDTOS.forEach(item -> {
529             Object[] values = new Object[2];
530             values[0] = DateUtils.format(item.getT(), timeFormat);
531             values[1] = new BigDecimal(item.getV()).setScale(2, BigDecimal.ROUND_HALF_UP);
532             result.add(values);
533         });
534         return result;
535     }
536
c5fe30 537     /**
538      * 新增预警信息
539      *
540      * @param dto
541      * @return
542      */
b368e6 543     @Override
544     public Boolean createAlarmMessage(AlarmMessageRespDTO dto) {
c5fe30 545         try {
546             mmPredictAlarmMessageService.create(ConvertUtils.sourceToTarget(dto, MmPredictAlarmMessageSaveReqVO.class));
547             return true;
548         } catch (Exception e) {
549             return false;
550         }
b368e6 551     }
552
553     @Override
554     public List<AlarmMessageRespDTO> listAlarmMessage(Map<String, Object> params) {
555         return null;
556     }
557
558     @Override
9587d2 559     public List<AlarmConfigRespDTO> listAlarmConfig(Map<String, Object> params) {
L 560         return mmPredictAlarmConfigService.list(params);
561     }
562
563     @Override
c5fe30 564     public AlarmMessageRespDTO getLastAlarmMessage(String alarmObj) {
565         MmPredictAlarmMessageEntity entity = mmPredictAlarmMessageService.getLast(alarmObj);
566         return ConvertUtils.sourceToTarget(entity, AlarmMessageRespDTO.class);
567     }
568
569     @Override
b368e6 570     public Boolean createScheduleSuggest(ScheduleSuggestRespDTO dto) {
19f359 571         stScheduleSuggestService.create(ConvertUtils.sourceToTarget(dto, StScheduleSuggestSaveReqVO.class));
b368e6 572         return true;
573     }
574
575     @Override
4072bc 576     public List<ScheduleSuggestRespDTO> listScheduleSuggest(Map<String, Object> params) {
3a6dfc 577         return Collections.emptyList();
L 578     }
579
580     @Override
055765 581     public List<ScheduleSuggestRespDTO> getLastLimitScheduleSuggest(String scheduleObj, Integer limit) {
582         List<StScheduleSuggestEntity> list = stScheduleSuggestService.getList(scheduleObj, limit);
583         return ConvertUtils.sourceToTarget(list, ScheduleSuggestRespDTO.class);
b368e6 584     }
585
586     @Override
587     public Boolean modifyPredictModelSetting(List<PredictModelSettingReqDTO> dtos) {
588         return true;
589     }
590
591     @Override
592     public Boolean modifyScheduleModelSetting(List<ScheduleModelSettingReqDTO> dtos) {
593         return true;
594     }
8a74e9 595
4072bc 596     @Override
ed4107 597     public List<StScheduleSchemeDTO> listScheduleScheme(String triggerMethod, String triggerCondition) {
598         Map<String, Object> params = new HashMap<>();
599         params.put("triggerMethod", triggerMethod);
600         params.put("triggerCondition", triggerCondition);
4072bc 601         return stScheduleSchemeService.list(params);
6eeac9 602     }
8a74e9 603
730d19 604     @Override
D 605     public List<ChartParamDTO> getChartParamList(String chartCode) {
606         if (StringUtils.isBlank(chartCode)) {
607             return null;
608         }
609         return chartParamService.list(chartCode);
610     }
611
0a7d0f 612     @Override
613     public PageResult<StAlarmAndSuggestRespVO> getAlarmAndSuggestPage(StAlarmAndSuggestPageReqVO reqVO) {
614         return stScheduleSchemeService.getAlarmAndSuggestPage(reqVO);
615     }
616
22e321 617     @Override
D 618     public Map<String, Map<String, Double>> getPreDoubleData(PreDoubleDataReqVO reqVO) {
619         List<MmPredictItemRespVO> doubleItemOutPuts = mmPredictItemService.getDoubleOutPutsByItemNos(reqVO.getItemNos());
620
621         //<itemNo,<resultStr,double>>
622         Map<String, Map<String, Double>> result = new HashMap<>(reqVO.getItemNos().size());
623
624         for (MmPredictItemRespVO mmPredictItemRespVO : doubleItemOutPuts) {
625             List<String> outPutIds = mmPredictItemRespVO.getOutPuts().stream().map(MmItemOutputRespVO::getId).collect(Collectors.toList());
626             InfluxModelResultByOutPutIdsPOJO pojo = new InfluxModelResultByOutPutIdsPOJO();
627             pojo.setOutPutIds(outPutIds);
628             pojo.setType(DataTypeEnum.FLOAT.getCode());
629             Map<String, List<InfluxModelResultVO>> outPutIdDoubles = influxDBService.queryModelResultsByOutPutIds(pojo, mmPredictItemRespVO.getLastTime(), mmPredictItemRespVO.getLastTime());
630             Map<String, Double> outPutIdDouble = new HashMap<>(outPutIds.size());
631             for (MmItemOutputRespVO outPut : mmPredictItemRespVO.getOutPuts()) {
632                 String outPutId = outPut.getId();
633                 if (outPutIdDoubles.containsKey(outPutId)) {
634                     List<InfluxModelResultVO> influxModelResultVOS = outPutIdDoubles.get(outPutId);
635                     if (!CollectionUtils.isEmpty(influxModelResultVOS)) {
636                         outPutIdDouble.put(outPut.getResultstr(),Double.valueOf(influxModelResultVOS.get(0).getValue().toString()));
637                     }
638                 }
639             }
640             result.put(mmPredictItemRespVO.getItemno(),outPutIdDouble);
641         }
642         return result;
643     }
644
bffae5 645     public Map<String, BigDecimal> getPredictLastValue(PredictLastValueReqVO reqVO) {
646         Map<String, BigDecimal> result = new HashMap<>(reqVO.getItemNos().size());
647         if (CollectionUtils.isEmpty(reqVO.getItemNos())) {
648             return result;
649         }
650         List<String[]> itemNos = reqVO.getItemNos();
651         for (String[] itemNo : itemNos) {
fed257 652             String key = itemNo[0] + "_" + itemNo[1] + "_" + itemNo[2];
bffae5 653             ItemVO predictItem = mmPredictItemService.getItemByItemNo(itemNo[0]);
654             if (predictItem == null || predictItem.getLastTime() == null) {
fed257 655                 result.put(key, BigDecimal.ZERO);
bffae5 656             }
657             MmItemOutputEntity outPut = mmItemOutputService.getByItemid(predictItem.getId(), itemNo[1], itemNo[2]);
658
659             Date predictTime = predictItem.getLastTime();
660             List<Object[]> curData = mmItemResultJsonService.getData(outPut.getId(), predictTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
661             if(CollectionUtils.isEmpty(curData)) {
fed257 662                 result.put(key, BigDecimal.ZERO);
bffae5 663             } else {
fed257 664                 result.put(key, new BigDecimal(curData.get(curData.size() - 1)[1].toString()));
bffae5 665             }
666         }
667         return result;
668     }
669
dec0c2 670     @Override
D 671     public Map<String,List<Object[]>> getElectricPredictData(String itemCode) {
672         ItemVO predictItem = mmPredictItemService.getItemByItemNo(itemCode);
673         if (null == predictItem) {
674             return new HashMap<>();
675         }
676         List<Integer> resultIndexs = new ArrayList<>();
677         resultIndexs.add(0);
678         resultIndexs.add(1);
679         resultIndexs.add(2);
680         List<MmItemOutputEntity> outPuts = mmItemOutputService.getByItemid(predictItem.getId(),"predictValues",resultIndexs);
681         if (CollectionUtils.isEmpty(outPuts)) {
682             return new HashMap<>();
683         }
684         InfluxModelResultByOutPutIdsPOJO pojo = new InfluxModelResultByOutPutIdsPOJO();
685         pojo.setOutPutIds(outPuts.stream().map(MmItemOutputEntity::getId).collect(Collectors.toList()));
686         pojo.setType(DataTypeEnum.FLOAT.getCode());
687         Calendar calendar = Calendar.getInstance();
688         calendar.setTime(predictItem.getLastTime());
689         calendar.add(Calendar.DAY_OF_YEAR, 7);
690         Map<String, List<InfluxModelResultVO>> outPutDatas = influxDBService.queryModelResultsByOutPutIds(pojo, predictItem.getLastTime(), calendar.getTime());
691         Map<String,List<Object[]>> result = new HashMap<>(outPuts.size());
692         for (MmItemOutputEntity outPut : outPuts) {
693             String outPutId = outPut.getId();
694             if (outPutDatas.containsKey(outPutId)) {
695                 List<InfluxModelResultVO> influxModelResultVOS = outPutDatas.get(outPutId);
696                 result.put(null == outPut.getResultIndex() ? outPut.getResultstr() : outPut.getResultstr()+"_"+outPut.getResultIndex(),influxModelResultVOS.stream().map(e -> {
697                     Object[] values = new Object[2];
698                     values[0] = DateUtils.format(Date.from(e.getTimestamp()), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
699                     values[1] = Double.valueOf(e.getValue().toString());
700                     return values;
701                 }).collect(Collectors.toList()));
702             }
703         }
704         return result;
705     }
706
707     @Override
153763 708     public List<StScheduleRecordVO> getLastScheduleData(String scheduleCode, Integer limit) {
dec0c2 709         if (StringUtils.isBlank(scheduleCode)) {
D 710             return null;
711         }
153763 712         return stScheduleRecordService.getLastScheduleResult(scheduleCode,limit);
dec0c2 713     }
D 714
715     @Override
716     public List<Map<String, Object>> getRealTimeStatus(List<Map<String, Object>> machines) {
717         List<String> pointNos = new ArrayList<>(16);
718         for (Map<String, Object> machine : machines) {
719             List<String> list = (List<String>) machine.get("pointNos");
720             pointNos.addAll(list);
721         }
722         Map<String, Object> pointValues = dataPointApi.queryPointsRealValue(pointNos);
723         List<Map<String, Object>> results = new ArrayList<>();
724         for (Map<String, Object> machine : machines) {
725             List<String> pointNoList = (List<String>) machine.get("pointNos");
726             if (!pointValues.containsKey(pointNoList.get(0)) || !pointValues.containsKey(pointNoList.get(1))) {
727                 machine.put("status",false);
728                 machine.put("num",0);
729                 results.add(machine);
730                 continue;
731             }
732             Double PValue = Double.valueOf(pointValues.get(pointNoList.get(0)).toString());
733             Double QValue = Double.valueOf(pointValues.get(pointNoList.get(1)).toString());
734             Double cos = calculateCos(PValue, QValue);
735             if (cos.equals(0.0)) {
736                 machine.put("status",false);
737                 machine.put("num",0);
738             }else {
739                 machine.put("status",true);
740                 machine.put("num",cos);
741             }
742             results.add(machine);
743         }
744         return results;
745     }
746
153763 747     @Override
D 748     public Map<String, List<Object[]>> getPredictDataItemNo(PreDataItemNoReqVO reqVO) {
749         if (StringUtils.isBlank(reqVO.getItemNo()) || null == reqVO.getStartTime() || null == reqVO.getEndTime()) {
750             return new HashMap<>();
751         }
752         ItemVO predictItem = mmPredictItemService.getItemByItemNo(reqVO.getItemNo());
753         if (null == predictItem) {
754             return new HashMap<>();
755         }
756         List<String> itemNos = new ArrayList<String>(){{
757            add(reqVO.getItemNo());
758         }};
759         List<MmPredictItemRespVO> ItemRespVOs = mmPredictItemService.getArrayOutPutsByItemNos(itemNos);
760         if (CollectionUtils.isEmpty(ItemRespVOs)) {
761             return new HashMap<>();
762         }
763         List<MmItemOutputRespVO> outPuts = ItemRespVOs.get(0).getOutPuts();
764         if (CollectionUtils.isEmpty(outPuts)) {
765             return new HashMap<>();
766         }
767
768         InfluxModelResultByOutPutIdsPOJO pojo = new InfluxModelResultByOutPutIdsPOJO();
769         pojo.setOutPutIds(outPuts.stream().map(MmItemOutputRespVO::getId).collect(Collectors.toList()));
770         pojo.setType(DataTypeEnum.FLOAT.getCode());
771         Map<String, List<InfluxModelResultVO>> outPutDatas = influxDBService.queryModelResultsByOutPutIds(pojo, reqVO.getStartTime(), reqVO.getEndTime());
772         Map<String,List<Object[]>> result = new HashMap<>(outPuts.size());
773         for (MmItemOutputRespVO outPut : outPuts) {
774             String outPutId = outPut.getId();
775             if (outPutDatas.containsKey(outPutId)) {
776                 List<InfluxModelResultVO> influxModelResultVOS = outPutDatas.get(outPutId);
777                 result.put(null == outPut.getResultIndex() ? outPut.getResultstr() : outPut.getResultstr()+"_"+outPut.getResultIndex(),influxModelResultVOS.stream().map(e -> {
778                     Object[] values = new Object[2];
779                     values[0] = DateUtils.format(Date.from(e.getTimestamp()), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
780                     values[1] = Double.valueOf(e.getValue().toString());
781                     return values;
782                 }).collect(Collectors.toList()));
783             }
784         }
785         return result;
786     }
787
dec0c2 788     /**
D 789      * 计算功率因数 p²/(根号:p²+Q²)
790      **/
791     public Double calculateCos(Double PValue,Double QValue) {
792         // 绝对值
793         PValue = Math.abs(PValue);
794         QValue = Math.abs(QValue);
795         //PValue [0,0.001] 直接判断为关闭返回0
796         if (PValue >= 0 && PValue <= 0.001) {
797             return 0.0;
798         }else {
799             BigDecimal result = new BigDecimal(PValue).divide(BigDecimal.valueOf(Math.sqrt(Math.pow(PValue, 2) + Math.pow(QValue, 2))), 2, BigDecimal.ROUND_HALF_UP);
800             return result.doubleValue();
801         }
802     }
803
8a74e9 804     private Date[] calResultTime(ItemVO predictItem, Date startTimeReq, Date endTimeReq, int lengthLeft, int lengthRight) {
805         Date[] result = new Date[3];
806         Date predictTime = predictItem.getLastTime();
dec0c2 807         Integer granularity = predictItem.getGranularity();
8a74e9 808         Date startTime = startTimeReq;
809         if (startTime == null) {
810             Calendar calendar = Calendar.getInstance();
811             calendar.setTime(predictItem.getLastTime());
dec0c2 812             calendar.add(Calendar.SECOND, -1 * lengthLeft * granularity);
8a74e9 813             startTime = calendar.getTime();
814         }
815         Date endTime = endTimeReq;
816         if (endTime == null) {
817             Calendar calendar = Calendar.getInstance();
818             calendar.setTime(predictItem.getLastTime());
dec0c2 819             calendar.add(Calendar.SECOND, lengthRight * granularity);
8a74e9 820             endTime = calendar.getTime();
821         }
822         if (endTime.getTime() <= startTime.getTime()) {
823             Calendar calendar = Calendar.getInstance();
824             calendar.setTime(startTime);
dec0c2 825             calendar.add(Calendar.SECOND, lengthRight * granularity);
8a74e9 826             endTime = calendar.getTime();
827         }
828         result[0] = predictTime;
829         result[1] = startTime;
830         result[2] = endTime;
831         return result;
832     }
b368e6 833 }