dongyukun
8 天以前 0a2b23ad3f30dfb01c5d590fb98f39e93bfe1932
提交 | 用户 | 时间
b368e6 1 package com.iailab.module.model.api;
2
3 import com.iailab.framework.common.util.date.DateUtils;
8a74e9 4 import com.iailab.framework.common.util.object.ConvertUtils;
5 import com.iailab.module.data.api.plan.PlanItemApi;
6 import com.iailab.module.data.api.plan.dto.ApiPlanDataDTO;
b368e6 7 import com.iailab.module.data.api.point.DataPointApi;
8 import com.iailab.module.data.api.point.dto.ApiPointDTO;
9 import com.iailab.module.data.api.point.dto.ApiPointValueDTO;
10 import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO;
6eeac9 11 import com.iailab.module.data.api.point.dto.ApiPointValueWriteDTO;
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.*;
91343d 15 import com.iailab.module.model.common.enums.CommonConstant;
16 import com.iailab.module.model.common.enums.PreLineTypeEnum;
b368e6 17 import com.iailab.module.model.mcs.pre.entity.DmModuleEntity;
18 import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity;
c5fe30 19 import com.iailab.module.model.mcs.pre.entity.MmPredictAlarmMessageEntity;
b368e6 20 import com.iailab.module.model.mcs.pre.service.*;
c5fe30 21 import com.iailab.module.model.mcs.pre.vo.MmPredictAlarmMessageSaveReqVO;
6eeac9 22 import com.iailab.module.model.mcs.sche.entity.StScheduleModelOutEntity;
055765 23 import com.iailab.module.model.mcs.sche.entity.StScheduleSuggestEntity;
6eeac9 24 import com.iailab.module.model.mcs.sche.service.StScheduleModelOutService;
4072bc 25 import com.iailab.module.model.mcs.sche.service.StScheduleSchemeService;
055765 26 import com.iailab.module.model.mcs.sche.service.StScheduleSuggestService;
19f359 27 import com.iailab.module.model.mcs.sche.vo.StScheduleSuggestSaveReqVO;
ead005 28 import com.iailab.module.model.mdk.vo.ItemVO;
91343d 29 import com.iailab.module.model.mpk.service.ChartService;
b368e6 30 import lombok.extern.slf4j.Slf4j;
977edc 31 import org.apache.commons.lang3.StringUtils;
b368e6 32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.util.CollectionUtils;
34 import org.springframework.validation.annotation.Validated;
35 import org.springframework.web.bind.annotation.RestController;
36
37 import java.math.BigDecimal;
38 import java.util.*;
912419 39 import java.util.stream.Collectors;
b368e6 40
41 /**
42  * @author PanZhibao
43  * @Description
44  * @createTime 2024年11月13日
45  */
46 @Slf4j
47 @RestController
48 @Validated
49 public class McsApiImpl implements McsApi {
50
51     @Autowired
6eeac9 52     private DataPointApi dataPointApi;
D 53
54     @Autowired
b368e6 55     private DmModuleService dmModuleService;
56
57     @Autowired
58     private MmPredictItemService mmPredictItemService;
59
60     @Autowired
61     private MmItemOutputService mmItemOutputService;
62
63     @Autowired
64     private MmItemResultService mmItemResultService;
65
66     @Autowired
67     private MmItemResultLastPointService mmItemResultLastPointService;
91343d 68
69     @Autowired
70     private MmItemResultJsonService mmItemResultJsonService;
71
72     @Autowired
73     private ChartService chartService;
c5fe30 74
75     @Autowired
76     private MmPredictAlarmMessageService mmPredictAlarmMessageService;
055765 77
78     @Autowired
79     private StScheduleSuggestService stScheduleSuggestService;
8a74e9 80
81     @Autowired
82     private PlanItemApi planItemApi;
9587d2 83
L 84     @Autowired
85     private MmPredictAlarmConfigService mmPredictAlarmConfigService;
3a6dfc 86
4072bc 87     @Autowired
L 88     private StScheduleSchemeService stScheduleSchemeService;
6eeac9 89
D 90     @Autowired
91     private StScheduleModelOutService stScheduleModelOutService;
b368e6 92
93     private int HOUR_MINS = 60;
94
95     @Override
96     public List<PredictItemTreeDTO> getPredictItemTree() {
97         List<PredictItemTreeDTO> result = new ArrayList<>();
98
99         List<DmModuleEntity> moduleList = dmModuleService.list(new HashMap<>());
100         if (CollectionUtils.isEmpty(moduleList)) {
101             return result;
102         }
103         moduleList.forEach(item -> {
104             PredictItemTreeDTO moduleOpt = new PredictItemTreeDTO();
105             moduleOpt.setId(item.getId());
106             moduleOpt.setLabel(item.getModulename());
107             List<PredictItemTreeDTO> children = new ArrayList<>();
a4891a 108             List<ItemVO> itemList = mmPredictItemService.getByModuleId(item.getId());
b368e6 109             itemList.forEach(item1 -> {
110                 PredictItemTreeDTO chd = new PredictItemTreeDTO();
a4891a 111                 chd.setLabel(item1.getItemName());
b368e6 112                 chd.setId(item1.getId());
a4891a 113                 List<PredictItemTreeDTO> chd1 = new ArrayList<>();
114                 List<MmItemOutputEntity> outList = mmItemOutputService.getByItemid(item1.getId());
115                 if (!CollectionUtils.isEmpty(outList)) {
116                     outList.forEach(out -> {
117                         PredictItemTreeDTO chd2 = new PredictItemTreeDTO();
118                         chd2.setId(out.getId());
f283ee 119                         chd2.setLabel(out.getResultName());
a4891a 120                         chd1.add(chd2);
121                     });
122                 }
123                 chd.setChildren(chd1);
b368e6 124                 children.add(chd);
125             });
126             moduleOpt.setChildren(children);
127             result.add(moduleOpt);
128         });
129         return result;
130     }
131
132     @Override
133     public PreDataBarLineRespVO getPreDataCharts(PreDataBarLineReqVO reqVO) {
134         PreDataBarLineRespVO result = new PreDataBarLineRespVO();
a4891a 135         List<String> outIds = reqVO.getOutIds();
b368e6 136         List<String> legends = new ArrayList<>();
137         List<PreDataViewRespDTO> dataViewList = new ArrayList<>();
9e844c 138         if (CollectionUtils.isEmpty(outIds)) {
b368e6 139             return result;
140         }
141         Date predictTime = reqVO.getPredictTime();
142         if (predictTime == null) {
9e844c 143             MmItemOutputEntity output = mmItemOutputService.getOutPutById(reqVO.getOutIds().get(0));
b82ba2 144             ItemVO predictItem = mmPredictItemService.getItemByIdFromCache(output.getItemid());
9e844c 145             if (predictItem.getLastTime() != null) {
146                 predictTime = predictItem.getLastTime();
147             } else {
148                 Calendar calendar = Calendar.getInstance();
149                 calendar.set(Calendar.MILLISECOND, 0);
150                 calendar.set(Calendar.SECOND, 0);
151                 predictTime = calendar.getTime();
152             }
b368e6 153         }
154         Date startTime = reqVO.getStartTime();
155         if (startTime == null) {
156             Calendar calendar = Calendar.getInstance();
157             calendar.setTime(predictTime);
158             calendar.add(Calendar.HOUR_OF_DAY, -1);
159             startTime = calendar.getTime();
160         }
161         Date endTime = reqVO.getEndTime();
162         if (endTime == null) {
163             Calendar calendar = Calendar.getInstance();
164             calendar.setTime(predictTime);
165             calendar.add(Calendar.HOUR_OF_DAY, 1);
166             endTime = calendar.getTime();
167         }
168
a4891a 169         for (int i = 0; i < outIds.size(); i++) {
b368e6 170             PreDataViewRespDTO viewDto = new PreDataViewRespDTO();
a4891a 171             String outId = outIds.get(i);
172             MmItemOutputEntity output = mmItemOutputService.getOutPutById(outId);
b368e6 173             if (output == null) {
9e844c 174                 continue;
b368e6 175             }
f283ee 176             legends.add(output.getResultName());
9e844c 177             viewDto.setItemId(output.getItemid());
178             viewDto.setOutId(outId);
179             viewDto.setResultstr(output.getResultstr());
f283ee 180             viewDto.setResultName(output.getResultName());
b368e6 181             viewDto.setRealData(getHisData(output.getPointid(), startTime, endTime));
977edc 182             viewDto.setPreDataN(mmItemResultService.getData(output.getId(), startTime, endTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
183             viewDto.setPreDataL(mmItemResultLastPointService.getData(output.getId(), startTime, endTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
b368e6 184
185             List<Double> values = new ArrayList<>();
186             if (!CollectionUtils.isEmpty(viewDto.getRealData())) {
187                 List<Double> hisValues = new ArrayList<>();
188                 viewDto.getRealData().forEach(item -> {
189                     values.add(Double.parseDouble(item[1].toString()));
190                     hisValues.add(Double.parseDouble(item[1].toString()));
191                 });
192                 viewDto.setHisMax(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
193                 viewDto.setHisMin(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
194                 viewDto.setHisAvg(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).average().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
195                 viewDto.setHisCumulant(new BigDecimal(hisValues.stream().mapToDouble(Double::doubleValue).sum())
196                         .divide(new BigDecimal(HOUR_MINS), 2, BigDecimal.ROUND_HALF_UP));
197             }
198             if (!CollectionUtils.isEmpty(viewDto.getPreDataN())) {
199                 viewDto.getPreDataN().forEach(item -> {
200                     values.add(Double.parseDouble(item[1].toString()));
201                 });
202             }
203             if (!CollectionUtils.isEmpty(viewDto.getPreDataL())) {
204                 List<Double> preValues = new ArrayList<>();
205                 viewDto.getPreDataL().forEach(item -> {
206                     values.add(Double.parseDouble(item[1].toString()));
207                     preValues.add(Double.parseDouble(item[1].toString()));
208                 });
209                 viewDto.setPreMax(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
210                 viewDto.setPreMin(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
211                 viewDto.setPreAvg(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).average().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
212             }
213             if (!CollectionUtils.isEmpty(viewDto.getCurData())) {
214                 List<Double> preValues = new ArrayList<>();
215                 viewDto.getCurData().forEach(item -> {
216                     values.add(Double.parseDouble(item[1].toString()));
217                     preValues.add(Double.parseDouble(item[1].toString()));
218                 });
219                 viewDto.setPreCumulant(new BigDecimal(preValues.stream().mapToDouble(Double::doubleValue).sum())
220                         .divide(new BigDecimal(HOUR_MINS), 2, BigDecimal.ROUND_HALF_UP));
221             }
222             if (!CollectionUtils.isEmpty(viewDto.getAdjData())) {
223                 viewDto.getAdjData().forEach(item -> {
224                     values.add(Double.parseDouble(item[1].toString()));
225                 });
226             }
227             if (!CollectionUtils.isEmpty(values)) {
228                 viewDto.setMaxValue(new BigDecimal(values.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
229                 viewDto.setMinValue(new BigDecimal(values.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
230             }
231             dataViewList.add(viewDto);
232         }
233         result.setStartTime(startTime);
234         result.setEndTime(endTime);
235         result.setPredictTime(predictTime);
236         result.setCategories(DateUtils.getTimeScale(startTime, endTime, 60));
237         result.setLegend(legends);
238         result.setDataViewList(dataViewList);
239         return result;
240     }
241
ead005 242     @Override
243     public PreDataItemChartRespVO getPreDataItemChart(PreDataItemChartReqVO reqVO) {
244         PreDataItemChartRespVO result = new PreDataItemChartRespVO();
864b61 245         ItemVO predictItem = mmPredictItemService.getItemById(reqVO.getItemId());
ead005 246         if (predictItem == null) {
247             return result;
248         }
249         if (predictItem.getLastTime() == null) {
250             return result;
251         }
328ef4 252         result.setPredictTime(predictItem.getLastTime());
ead005 253         Date startTime = reqVO.getStartTime();
254         if (startTime == null) {
255             Calendar calendar = Calendar.getInstance();
256             calendar.setTime(predictItem.getLastTime());
91343d 257             calendar.add(Calendar.MINUTE, -1 * predictItem.getPredictLength());
ead005 258             startTime = calendar.getTime();
259         }
260         Date endTime = reqVO.getEndTime();
261         if (endTime == null) {
262             Calendar calendar = Calendar.getInstance();
263             calendar.setTime(predictItem.getLastTime());
91343d 264             calendar.add(Calendar.MINUTE, predictItem.getPredictLength());
ead005 265             endTime = calendar.getTime();
266         }
1f9784 267         if (endTime.getTime() <= startTime.getTime()) {
268             Calendar calendar = Calendar.getInstance();
269             calendar.setTime(startTime);
91343d 270             calendar.add(Calendar.MINUTE, predictItem.getPredictLength());
1f9784 271             endTime = calendar.getTime();
272         }
273
ead005 274         List<String> categories = DateUtils.getTimeScale(startTime, endTime, predictItem.getGranularity());
275         List<String> legend = new ArrayList<>();
276         LinkedHashMap<String, PreDataSampleViewRespDTO> viewMap = new LinkedHashMap<>();
277         List<MmItemOutputEntity> outs = mmItemOutputService.getByItemid(reqVO.getItemId());
278         if (CollectionUtils.isEmpty(outs)) {
279             return result;
280         }
281         for (MmItemOutputEntity out : outs) {
f283ee 282             legend.add(out.getResultName());
ead005 283             PreDataSampleViewRespDTO viewDto = new PreDataSampleViewRespDTO();
85b200 284             if (StringUtils.isNotBlank(out.getPointid())) {
D 285                 viewDto.setRealData(getHisData(out.getPointid(), startTime, endTime));
286             }
977edc 287             viewDto.setPreDataN(mmItemResultService.getData(out.getId(), startTime, endTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
f283ee 288             viewMap.put(out.getResultName(), viewDto);
ead005 289         }
290         result.setStartTime(startTime);
291         result.setEndTime(endTime);
292         result.setCategories(categories);
293         result.setLegend(legend);
294         result.setViewMap(viewMap);
295         return result;
296     }
297
328ef4 298     @Override
91343d 299     public PreDataSingleChartRespVO getPreDataSingleChart(PreDataSingleChartReqVO reqVO) {
300         PreDataSingleChartRespVO result = new PreDataSingleChartRespVO();
328ef4 301
91343d 302         Map<String, String> chartParams = chartService.getByChartCode(reqVO.getChartCode());
303         if (CollectionUtils.isEmpty(chartParams)) {
304             return result;
305         }
306         String itemCode = chartParams.get(CommonConstant.ITEM_CODE);
307         if (itemCode == null) {
308             return result;
309         }
310         String resultStr = chartParams.get(CommonConstant.RESULT_STR);
311         if (resultStr == null) {
312             return result;
313         }
bd9085 314         String resultIndex = chartParams.get(CommonConstant.RESULT_INDEX);
315
91343d 316         ItemVO predictItem = mmPredictItemService.getItemByItemNo(itemCode);
317         if (predictItem == null || predictItem.getLastTime() == null) {
318             return result;
319         }
977edc 320         String timeFormat = StringUtils.isBlank(reqVO.getTimeFormat()) ? DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND : reqVO.getTimeFormat();
321         PreLineTypeEnum lineType = chartParams.get(CommonConstant.LINE_TYPE) == null ? PreLineTypeEnum.TN : PreLineTypeEnum.getEumByCode(chartParams.get(CommonConstant.LINE_TYPE));
91343d 322         BigDecimal rangeH = chartParams.get(CommonConstant.RANGE_H) == null ? BigDecimal.ZERO : new BigDecimal(chartParams.get(CommonConstant.RANGE_H));
323         BigDecimal rangeL = chartParams.get(CommonConstant.RANGE_L) == null ? BigDecimal.ZERO : new BigDecimal(chartParams.get(CommonConstant.RANGE_L));
324         BigDecimal limitH = chartParams.get(CommonConstant.LIMIT_H) == null ? BigDecimal.ZERO : new BigDecimal(chartParams.get(CommonConstant.LIMIT_H));
325         BigDecimal limitL = chartParams.get(CommonConstant.LIMIT_L) == null ? BigDecimal.ZERO : new BigDecimal(chartParams.get(CommonConstant.LIMIT_L));
326         int lengthLeft = chartParams.get(CommonConstant.LENGTH_LEFT) == null ? predictItem.getPredictLength() : new BigDecimal(chartParams.get(CommonConstant.LENGTH_LEFT)).intValue();
327         int lengthRight = chartParams.get(CommonConstant.LENGTH_RIGHT) == null ? predictItem.getPredictLength() : new BigDecimal(chartParams.get(CommonConstant.LENGTH_RIGHT)).intValue();
8a74e9 328
329         Date[] timeArray = calResultTime(predictItem, reqVO.getStartTime(), reqVO.getEndTime(), lengthLeft, lengthRight);
330         Date predictTime = timeArray[0];
331         Date startTime = timeArray[1];
332         Date endTime = timeArray[2];
333
977edc 334         List<String> categories = DateUtils.getTimeScale(startTime, endTime, predictItem.getGranularity(), timeFormat);
91343d 335         List<String> legend = new ArrayList<>();
bdab69 336         MmItemOutputEntity outPut = mmItemOutputService.getByItemid(predictItem.getId(), resultStr, resultIndex);
91343d 337         PreDataViewRespDTO dataView = new PreDataViewRespDTO();
338         dataView.setItemId(predictItem.getId());
339         dataView.setItemName(predictItem.getItemName());
340         dataView.setResultstr(resultStr);
341         dataView.setRangeH(rangeH);
342         dataView.setRangeL(rangeL);
343         dataView.setLimitH(limitH);
344         dataView.setLimitL(limitL);
b6bfe8 345         if (StringUtils.isNotBlank(outPut.getPointid())) {
D 346             dataView.setRealData(getHisData(outPut.getPointid(), startTime, endTime, timeFormat));
347         } else {
348             dataView.setRealData(new ArrayList<>());
349         }
977edc 350         dataView.setCurData(mmItemResultJsonService.getData(outPut.getId(), predictTime, timeFormat));
91343d 351         switch (lineType) {
352             case TN:
977edc 353                 dataView.setPreDataN(mmItemResultService.getData(outPut.getId(), startTime, endTime, timeFormat));
91343d 354                 break;
355             case TL:
977edc 356                 dataView.setPreDataN(mmItemResultService.getData(outPut.getId(), predictTime, endTime, timeFormat));
357                 dataView.setPreDataL(mmItemResultLastPointService.getData(outPut.getId(), startTime, endTime, timeFormat));
91343d 358                 break;
359             default:
360                 break;
361         }
912419 362
363         if (!CollectionUtils.isEmpty(dataView.getCurData())) {
364             List<Double> curList = dataView.getCurData().stream().map(t -> {
365                 return new Double(t[1].toString());
366             }).collect(Collectors.toList());
367             dataView.setPreMax(new BigDecimal(curList.stream().mapToDouble(Double::doubleValue).max().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
368             dataView.setPreMin(new BigDecimal(curList.stream().mapToDouble(Double::doubleValue).min().getAsDouble()).setScale(2, BigDecimal.ROUND_HALF_UP));
369         }
370
8a74e9 371         result.setPredictTime(predictTime);
91343d 372         result.setStartTime(startTime);
373         result.setEndTime(endTime);
374         result.setCategories(categories);
375         result.setLegend(legend);
376         result.setDataView(dataView);
8a74e9 377         return result;
378     }
379
380     @Override
d9fe8f 381     public Map<String, List<Object[]>> getPreDataCur(PreDataJsonReqVO reqVO) {
382         Map<String, List<Object[]>> result = new HashMap<>();
383         if (reqVO == null || reqVO.getPredictTime() == null || CollectionUtils.isEmpty(reqVO.getOutputIdList())) {
384             return result;
385         }
386         reqVO.getOutputIdList().forEach(outPutId -> {
387             result.put(outPutId, mmItemResultJsonService.getData(outPutId, reqVO.getPredictTime(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
388         });
389         return result;
390     }
391
392     @Override
8a74e9 393     public PlanDataSingleChartRespVO getPlanDataSingleChart(PreDataSingleChartReqVO reqVO) {
394         PlanDataSingleChartRespVO result = new PlanDataSingleChartRespVO();
395         Map<String, String> chartParams = chartService.getByChartCode(reqVO.getChartCode());
396         if (CollectionUtils.isEmpty(chartParams)) {
397             return result;
398         }
399         String itemCode = chartParams.get(CommonConstant.ITEM_CODE);
400         if (itemCode == null) {
401             return result;
402         }
403         String planItemStr = chartParams.get(CommonConstant.PLAN_ITEM_LIST);
404         if (planItemStr == null) {
405             return result;
406         }
407         List<String> planItemCodeList = Arrays.asList(planItemStr.split(","));
408
409         ItemVO predictItem = mmPredictItemService.getItemByItemNo(itemCode);
410         if (predictItem == null || predictItem.getLastTime() == null) {
411             return result;
412         }
413         String timeFormat = StringUtils.isBlank(reqVO.getTimeFormat()) ? DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND : reqVO.getTimeFormat();
414         int lengthLeft = chartParams.get(CommonConstant.LENGTH_LEFT) == null ? predictItem.getPredictLength() : new BigDecimal(chartParams.get(CommonConstant.LENGTH_LEFT)).intValue();
415         int lengthRight = chartParams.get(CommonConstant.LENGTH_RIGHT) == null ? predictItem.getPredictLength() : new BigDecimal(chartParams.get(CommonConstant.LENGTH_RIGHT)).intValue();
416         Date[] timeArray = calResultTime(predictItem, reqVO.getStartTime(), reqVO.getEndTime(), lengthLeft, lengthRight);
417         Date predictTime = timeArray[0];
418         Date startTime = timeArray[1];
419         Date endTime = timeArray[2];
420         List<String> categories = DateUtils.getTimeScale(startTime, endTime, predictItem.getGranularity(), timeFormat);
421         List<String> legend = new ArrayList<>();
422         LinkedHashMap<String, List<PlanDataRecordDTO>> record = new LinkedHashMap<>();
423         ApiDataQueryDTO queryDTO = new ApiDataQueryDTO();
424         queryDTO.setItemNos(planItemCodeList);
425         queryDTO.setStart(startTime);
426         queryDTO.setEnd(endTime);
427         LinkedHashMap<String, List<ApiPlanDataDTO>> planData = planItemApi.queryPlanItemRecordValue(queryDTO);
428         if (CollectionUtils.isEmpty(planData)) {
429             planData.forEach((key, value) -> {
430                 record.put(key, ConvertUtils.sourceToTarget(value, PlanDataRecordDTO.class));
431             });
432         }
433         result.setPredictTime(predictTime);
434         result.setStartTime(startTime);
435         result.setEndTime(endTime);
436         result.setCategories(categories);
437         result.setLegend(legend);
438         result.setRecord(record);
328ef4 439         return result;
440     }
441
b368e6 442     /**
443      * 获取真实值
444      *
445      * @param pointId
446      * @param startTime
447      * @param endTime
448      * @return
449      */
450     private List<Object[]> getHisData(String pointId, Date startTime, Date endTime) {
451         List<Object[]> result = new ArrayList<>();
452         ApiPointDTO pointDTO = dataPointApi.getInfoById(pointId);
453         ApiPointValueQueryDTO queryPointDto = new ApiPointValueQueryDTO();
454         queryPointDto.setPointNo(pointDTO.getPointNo());
455         queryPointDto.setStart(startTime);
456         queryPointDto.setEnd(endTime);
457         List<ApiPointValueDTO> valueDTOS = dataPointApi.queryPointHistoryValue(queryPointDto);
458         if (CollectionUtils.isEmpty(valueDTOS)) {
459             return result;
460         }
461         valueDTOS.forEach(item -> {
462             Object[] values = new Object[2];
214275 463             values[0] = DateUtils.format(item.getT(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);
464             values[1] = new BigDecimal(item.getV()).setScale(2, BigDecimal.ROUND_HALF_UP);
b368e6 465             result.add(values);
466         });
467         return result;
468     }
469
977edc 470     /**
471      * 获取真实值
472      *
473      * @param pointId
474      * @param startTime
475      * @param endTime
476      * @param timeFormat
477      * @return
478      */
479     private List<Object[]> getHisData(String pointId, Date startTime, Date endTime, String timeFormat) {
480         List<Object[]> result = new ArrayList<>();
481         ApiPointDTO pointDTO = dataPointApi.getInfoById(pointId);
482         ApiPointValueQueryDTO queryPointDto = new ApiPointValueQueryDTO();
483         queryPointDto.setPointNo(pointDTO.getPointNo());
484         queryPointDto.setStart(startTime);
485         queryPointDto.setEnd(endTime);
486         List<ApiPointValueDTO> valueDTOS = dataPointApi.queryPointHistoryValue(queryPointDto);
487         if (CollectionUtils.isEmpty(valueDTOS)) {
488             return result;
489         }
490         valueDTOS.forEach(item -> {
491             Object[] values = new Object[2];
492             values[0] = DateUtils.format(item.getT(), timeFormat);
493             values[1] = new BigDecimal(item.getV()).setScale(2, BigDecimal.ROUND_HALF_UP);
494             result.add(values);
495         });
496         return result;
497     }
498
c5fe30 499     /**
500      * 新增预警信息
501      *
502      * @param dto
503      * @return
504      */
b368e6 505     @Override
506     public Boolean createAlarmMessage(AlarmMessageRespDTO dto) {
c5fe30 507         try {
508             mmPredictAlarmMessageService.create(ConvertUtils.sourceToTarget(dto, MmPredictAlarmMessageSaveReqVO.class));
509             return true;
510         } catch (Exception e) {
511             return false;
512         }
b368e6 513     }
514
515     @Override
516     public List<AlarmMessageRespDTO> listAlarmMessage(Map<String, Object> params) {
517         return null;
518     }
519
520     @Override
9587d2 521     public List<AlarmConfigRespDTO> listAlarmConfig(Map<String, Object> params) {
L 522         return mmPredictAlarmConfigService.list(params);
523     }
524
525     @Override
c5fe30 526     public AlarmMessageRespDTO getLastAlarmMessage(String alarmObj) {
527         MmPredictAlarmMessageEntity entity = mmPredictAlarmMessageService.getLast(alarmObj);
528         return ConvertUtils.sourceToTarget(entity, AlarmMessageRespDTO.class);
529     }
530
531     @Override
b368e6 532     public Boolean createScheduleSuggest(ScheduleSuggestRespDTO dto) {
19f359 533         stScheduleSuggestService.create(ConvertUtils.sourceToTarget(dto, StScheduleSuggestSaveReqVO.class));
b368e6 534         return true;
535     }
536
537     @Override
4072bc 538     public List<ScheduleSuggestRespDTO> listScheduleSuggest(Map<String, Object> params) {
3a6dfc 539         return Collections.emptyList();
L 540     }
541
542     @Override
055765 543     public List<ScheduleSuggestRespDTO> getLastLimitScheduleSuggest(String scheduleObj, Integer limit) {
544         List<StScheduleSuggestEntity> list = stScheduleSuggestService.getList(scheduleObj, limit);
545         return ConvertUtils.sourceToTarget(list, ScheduleSuggestRespDTO.class);
b368e6 546     }
547
548     @Override
549     public Boolean modifyPredictModelSetting(List<PredictModelSettingReqDTO> dtos) {
550         return true;
551     }
552
553     @Override
554     public Boolean modifyScheduleModelSetting(List<ScheduleModelSettingReqDTO> dtos) {
555         return true;
556     }
8a74e9 557
4072bc 558     @Override
ed4107 559     public List<StScheduleSchemeDTO> listScheduleScheme(String triggerMethod, String triggerCondition) {
560         Map<String, Object> params = new HashMap<>();
561         params.put("triggerMethod", triggerMethod);
562         params.put("triggerCondition", triggerCondition);
4072bc 563         return stScheduleSchemeService.list(params);
L 564     }
565
6eeac9 566     @Override
D 567     public Boolean scheduleModelOut(MdkScheduleRespDTO dto) {
568         String modelId = stScheduleSchemeService.getByCode(dto.getScheduleCode()).getModelId();
569         Map<String,Object> result =  dto.getResult();
570         List<StScheduleModelOutDTO> list =  stScheduleModelOutService.list(modelId);
571         try{
572         list.forEach(
573                 item -> {
574                     double value = 0;
575                     //判断点位是否下发以及返回结果是否存在
576                     if(item.getIsWrite()==1){
577                         if(result.get(item.getResultKey())==null){
578                             log.error(result.get(item.getResultKey()) + "resultKey匹配失败");
579                         }
580                         Object resultValue = result.get(item.getResultKey());
581                         //判断解析方式
582                         if(item.getResultType().equals("double")){
583                             if(resultValue instanceof Double) {
584                                 value = (Double) resultValue;
585                             }
586                         }else if(item.getResultType().equals("double[]")){
587                             ArrayList<Double> doubleList = (ArrayList<Double>) resultValue;
588                             double[] array = new double[doubleList.size()];
589                             for (int i = 0; i < doubleList.size(); i++) {
590                                 array[i] = doubleList.get(i);
591                             }
592                             if(array!= null && item.getResultPort() < array.length){
593                                 value = array[item.getResultPort()];
594                             }else{
595                                 log.error(result.get(item.getResultKey()) + "下角标超限");
596                             }
597
598                         }else if(item.getResultType().equals("double[][]")){
599                             if (item.getResultType().equals("double[][]")) {
600                                 if (resultValue instanceof ArrayList) {
601                                     ArrayList<ArrayList<Double>> doubleListList = (ArrayList<ArrayList<Double>>) resultValue;
602                                     double[][] array = new double[doubleListList.size()][];
603                                     for (int i = 0; i < doubleListList.size(); i++) {
604                                         ArrayList<Double> doubleList = doubleListList.get(i);
605                                         array[i] = new double[doubleList.size()];
606                                         for (int j = 0; j < doubleList.size(); j++) {
607                                             array[i][j] = doubleList.get(j);
608                                         }
609                                     }
610                                     if (array != null && item.getResultPort() < array.length && item.getResultIndex() < array[item.getResultPort()].length) {
611                                         value = array[item.getResultPort()][item.getResultIndex()];
612                                     } else {
613                                         log.error(result.get(item.getResultKey()) + "下标超限");
614                                     }
615                                 }
616                             }
617                         }
618                         //下发到point点位
619                         ApiPointValueWriteDTO ApiPointValueWriteDTO = new ApiPointValueWriteDTO();
620                         ApiPointValueWriteDTO.setPointNo(item.getPointNo());
621                         ApiPointValueWriteDTO.setValue(value);
622                         if (!dataPointApi.writePointRealValue(ApiPointValueWriteDTO)) {
623                             log.error(result.get(item.getResultKey()) + "下发数据异常");
624                         }
625
626                     }
627                 }
628         );
629         }catch (Exception ex){
630             log.error("下发数据异常");
631             ex.printStackTrace();
632         }
633         return true;
634     }
8a74e9 635
636     private Date[] calResultTime(ItemVO predictItem, Date startTimeReq, Date endTimeReq, int lengthLeft, int lengthRight) {
637         Date[] result = new Date[3];
638         Date predictTime = predictItem.getLastTime();
639         Date startTime = startTimeReq;
640         if (startTime == null) {
641             Calendar calendar = Calendar.getInstance();
642             calendar.setTime(predictItem.getLastTime());
643             calendar.add(Calendar.MINUTE, -1 * lengthLeft);
644             startTime = calendar.getTime();
645         }
646         Date endTime = endTimeReq;
647         if (endTime == null) {
648             Calendar calendar = Calendar.getInstance();
649             calendar.setTime(predictItem.getLastTime());
650             calendar.add(Calendar.MINUTE, lengthRight);
651             endTime = calendar.getTime();
652         }
653         if (endTime.getTime() <= startTime.getTime()) {
654             Calendar calendar = Calendar.getInstance();
655             calendar.setTime(startTime);
656             calendar.add(Calendar.MINUTE, lengthRight);
657             endTime = calendar.getTime();
658         }
659         result[0] = predictTime;
660         result[1] = startTime;
661         result[2] = endTime;
662         return result;
663     }
b368e6 664 }