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