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