dengzedong
2024-11-12 69bd5efa5e2849a560e604d0aa608d5492113915
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
package com.iailab.module.model.mdk.predict;
 
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
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.service.MmItemResultService;
import com.iailab.module.model.mdk.factory.ItemEntityFactory;
import com.iailab.module.model.mdk.vo.DataValueVO;
import com.iailab.module.model.mdk.vo.MmItemOutputVO;
import com.iailab.module.model.mdk.vo.PredictResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.*;
 
@Service
public class PredictResultHandler {
    @Autowired
    private MmItemResultService mmItemResultService;
 
    @Autowired
    private ItemEntityFactory itemEntityFactory;
 
    @Autowired
    private DataPointApi dataPointApi;
 
    /**
     * convertToPredictData
     *
     * @param predictResult
     * @return
     */
    public Map<String, List<DataValueVO>> convertToPredictData(PredictResultVO predictResult) {
        Map<String, List<DataValueVO>> resultMap = new HashMap<>();
        List<MmItemOutputVO> itemOutPutList = itemEntityFactory.getOutPutByItemId(predictResult.getPredictId());
 
        if (!CollectionUtils.isEmpty(predictResult.getPredictList())) {
            resultMap.put(itemOutPutList.get(0).getId(), predictResult.getPredictList());
            return resultMap;
        }
        Map<MmItemOutputEntity, double[]> predictMatrixs = predictResult.getPredictMatrixs();
        HashMap<String,List<DataValueVO>> predictLists = new HashMap<>();
        for (Map.Entry<MmItemOutputEntity, double[]> entry : predictMatrixs.entrySet()) {
            ApiPointDTO point = dataPointApi.getInfoById(entry.getKey().getPointid());
            Integer pointMinfreq = DataPointFreqEnum.getEumByCode(point.getMinfreqid()).getValue();
            Integer rows = entry.getValue().length;
            List<DataValueVO> predictDataList = new ArrayList<>();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(predictResult.getPredictTime());
            for (Integer i = 0; i < rows; i++) {
                DataValueVO predictData = new DataValueVO();
                predictData.setDataTime(calendar.getTime());
                predictData.setDataValue(Double.valueOf(entry.getValue()[i]));
                predictDataList.add(predictData);
 
                calendar.add(Calendar.SECOND, pointMinfreq);
            }
            resultMap.put(entry.getKey().getId(), predictDataList);
        }
        predictResult.setPredictLists(predictLists);
        return resultMap;
    }
 
    /**
     * savePredictResult
     *
     * @param predictResult
     */
    @Async
    @DSTransactional
    public void savePredictResult(PredictResultVO predictResult) {
        Map<String, List<DataValueVO>> resultMap = convertToPredictData(predictResult);
        mmItemResultService.savePredictValue(resultMap, predictResult.getLt(), "n", predictResult.getPredictTime());
    }
 
    public List<DataValueVO> getPredictValueByItemNo(String itemNo, Date start, Date end) {
        String itemId = itemEntityFactory.getItemByItemNo(itemNo).getId();
        List<MmItemOutputVO> outputList = itemEntityFactory.getOutPutByItemId(itemId);
        return mmItemResultService.getPredictValue(outputList.get(0).getId(), start, end);
    }
 
    public List<DataValueVO> getPredictValueByItemId(String itemId, Date start, Date end) {
        List<MmItemOutputVO> outputList = itemEntityFactory.getOutPutByItemId(itemId);
        return mmItemResultService.getPredictValue(outputList.get(0).getId(), start, end);
    }
}