package com.iailab.module.model.mdk.predict.impl; import com.alibaba.fastjson.JSON; import com.iailab.module.data.api.point.DataPointApi; import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity; import com.iailab.module.model.mcs.pre.service.MmItemOutputService; import com.iailab.module.model.mcs.pre.service.MmItemResultService; import com.iailab.module.model.mdk.common.exceptions.ItemInvokeException; import com.iailab.module.model.mdk.factory.ItemEntityFactory; import com.iailab.module.model.mdk.factory.PredictItemFactory; import com.iailab.module.model.mdk.predict.PredictItemHandler; import com.iailab.module.model.mdk.predict.PredictResultHandler; import com.iailab.module.model.mdk.vo.ItemVO; import com.iailab.module.model.mdk.vo.PredictResultVO; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.util.*; /** * @author PanZhibao * @Description * @createTime 2024年09月01日 */ @Slf4j @Component public class PredictItemMergeHandlerImpl implements PredictItemHandler { @Autowired private ItemEntityFactory itemEntityFactory; @Autowired private DataPointApi dataPointApi; @Autowired private PredictItemFactory predictItemFactory; @Autowired private PredictResultHandler predictResultHandler; @Autowired private MmItemResultService mmItemResultService; @Autowired private MmItemOutputService mmItemOutputService; /** * MergeItem预测 * * @param predictTime * @param predictItemDto * @return * @throws ItemInvokeException */ @Override public PredictResultVO predict(Date predictTime, ItemVO predictItemDto, Map predictValueMap) throws ItemInvokeException { PredictResultVO predictResult = new PredictResultVO(); String itemId = predictItemDto.getId(); try { String expression = itemEntityFactory.getMergeItem(itemId).getExpression(); int predictLength = itemEntityFactory.getItemById(itemId).getPredictLength(); double[] predictResultMat = new double[predictLength]; String[] mathOutPutId = expression.split("[\\+ \\-]"); ArrayList operator = new ArrayList<>(); for (int i = 0; i < expression.length(); i++) { if (expression.charAt(i)=='+' || expression.charAt(i)=='-'){ operator.add(expression.charAt(i)); } } // String[] compositionItem = expression.split(String.valueOf("&".toCharArray())); //是否为计算预测项 if (mathOutPutId.length > 1) { // Map> predictValueMap = new HashMap<>(); // for (String outPutId : mathOutPutId) { // if (outPutId.length() > 4) { // Date endTime = predictTime; //// ItemVO itemEntity = itemEntityFactory.getItemByItemNo(itemNo); //// List outPutList = itemEntityFactory.getOutPutByItemId(itemEntity.getId()); // MmItemOutputEntity outPut = mmItemOutputService.getOutPutById(outPutId); // ApiPointDTO pointEntity = dataPointApi.getInfoById(outPut.getPointid()); // // Calendar calendar = Calendar.getInstance(); // calendar.setTime(endTime); // calendar.add(Calendar.SECOND, (predictLength - 1) * DataPointFreqEnum.getEumByCode(pointEntity.getMinfreqid()).getValue()); // endTime = new Timestamp(calendar.getTime().getTime()); //// List predictValueList = predictResultHandler.getPredictValueByItemNo(itemNo, predictTime, endTime); // List predictValueList = mmItemResultService.getPredictValue(outPutId, predictTime, endTime); // if (predictValueList.size() != predictLength) { // log.debug("merge项融合失败:缺少子项预测数据,对应子项outPutId=" + outPutId); // return null; // } // predictValueMap.put(outPutId, predictValueList); // } // } for (Integer i = 0; i < predictLength; i++) { double sum =0.0; sum = predictValueMap.get(mathOutPutId[0])[i]; for (int j = 1; j < mathOutPutId.length; j++) { if (operator.get(j-1)=='+') {sum += predictValueMap.get(mathOutPutId[j])[i];} if (operator.get(j-1)=='-') {sum -= predictValueMap.get(mathOutPutId[j])[i];} } predictResultMat[i] = sum; } } //是否为组合预测项 // if (compositionItem.length > 1) { // Map predictResultMap = new HashMap<>(); // Integer columnTotalNumber = 0; // Integer rowNumber = 0; // for (String itemNo : compositionItem) { // PredictItemHandler predictItem = (PredictItemHandler) predictItemFactory.create(itemEntityFactory. // getItemByItemNo(itemNo).getId()); // predictResult = predictItem.predict(predictTime, predictItemDto); // columnTotalNumber += Integer.valueOf(predictResult.getPredictMatrix().length); // predictResultMap.put(itemNo, predictItem.predict(predictTime, predictItemDto)); // } // double[][] matrix = new double[columnTotalNumber][1]; // for (String itemNo : compositionItem) { // for (Integer i = 0; i < predictResultMap.get(itemNo).getPredictMatrix().length; i++) { // matrix[rowNumber][0] = predictResultMap.get(itemNo).getPredictMatrix()[i][0]; // rowNumber++; // } // } // predictResult.setPredictMatrix(matrix); // } predictResult.setPredictId(itemId); List outputServiceByItemid = mmItemOutputService.getByItemid(itemId); if (!CollectionUtils.isEmpty(outputServiceByItemid)) { Map predictMatrixs = new HashMap<>(); predictMatrixs.put(outputServiceByItemid.get(0),predictResultMat); predictResult.setPredictMatrixs(predictMatrixs); } predictResult.setPredictTime(predictTime); } catch (Exception e) { log.error("merge项预测失败,itemId:" + itemId); e.printStackTrace(); throw e; } log.info("merge项预测完成,itemId:" + itemId + ",结果:" + JSON.toJSONString(predictResult)); return predictResult; } }