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