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