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