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