dengzedong
2 天以前 1178da30ca701da465bf7bc1342b539b2df03c7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.iailab.module.model.mdk.predict.impl;
 
import com.iailab.module.data.api.point.DataPointApi;
import com.iailab.module.data.api.point.dto.ApiPointDTO;
import com.iailab.module.data.enums.DataPointFreqEnum;
import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity;
import com.iailab.module.model.mcs.pre.enums.ItemRunStatusEnum;
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.enums.ItemPredictStatus;
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.DataValueVO;
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 java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @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<String, double[]> predictValueMap)
            throws ItemInvokeException {
        PredictResultVO predictResult = new PredictResultVO();
        ItemPredictStatus itemStatus = ItemPredictStatus.PREDICTING;
        String itemId = predictItemDto.getId();
        try {
            String expression = itemEntityFactory.getMergeItem(itemId).getExpression();
            int predictLength = itemEntityFactory.getItemById(itemId).getPredictLength();
            double[][] predictResultMat = new double[predictLength][1];
            String[] mathOutPutId = expression.split("[\\+ \\-]");
            ArrayList<Character> 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<String, List<DataValueVO>> predictValueMap = new HashMap<>();
//                for (String outPutId : mathOutPutId) {
//                    if (outPutId.length() > 4) {
//                        Date endTime = predictTime;
////                        ItemVO itemEntity = itemEntityFactory.getItemByItemNo(itemNo);
////                        List<MmItemOutputEntity> 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<DataValueVO> predictValueList = predictResultHandler.getPredictValueByItemNo(itemNo, predictTime, endTime);
//                        List<DataValueVO> 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][0] = sum;
                }
            }
            //是否为组合预测项
//            if (compositionItem.length > 1) {
//                Map<String, PredictResultVO> 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);
//            }
            log.info("计算预测模型结果:" + predictResultMat);
            predictResult.setPredictId(itemId);
            predictResult.setPredictMatrix(predictResultMat);
            predictResult.setPredictTime(predictTime);
            //预测项预测成功的状态
            itemStatus = ItemPredictStatus.SUCCESS;
        } catch (Exception e) {
            //预测项预测失败的状态
            itemStatus = ItemPredictStatus.FAILED;
            log.debug("merge项预测失败,itemId:" + itemId);
            throw e;
        }
        log.debug("预测完成,itemId:" + itemId + ",itemStatus:" + itemStatus.getValue());
        return predictResult;
    }
}