提交 | 用户 | 时间
|
7fd198
|
1 |
package com.iailab.module.model.mdk.predict; |
潘 |
2 |
|
69bd5e
|
3 |
import com.baomidou.dynamic.datasource.annotation.DSTransactional; |
4af6b1
|
4 |
import com.iailab.module.model.common.enums.CommonDict; |
69bd5e
|
5 |
import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity; |
7fd198
|
6 |
import com.iailab.module.model.mcs.pre.service.MmItemResultService; |
潘 |
7 |
import com.iailab.module.model.mdk.factory.ItemEntityFactory; |
|
8 |
import com.iailab.module.model.mdk.vo.DataValueVO; |
|
9 |
import com.iailab.module.model.mdk.vo.PredictResultVO; |
|
10 |
import org.springframework.beans.factory.annotation.Autowired; |
|
11 |
import org.springframework.scheduling.annotation.Async; |
|
12 |
import org.springframework.stereotype.Service; |
|
13 |
import org.springframework.util.CollectionUtils; |
|
14 |
|
4af6b1
|
15 |
import java.math.BigDecimal; |
7fd198
|
16 |
import java.util.*; |
潘 |
17 |
|
|
18 |
@Service |
|
19 |
public class PredictResultHandler { |
|
20 |
@Autowired |
|
21 |
private MmItemResultService mmItemResultService; |
|
22 |
|
|
23 |
@Autowired |
|
24 |
private ItemEntityFactory itemEntityFactory; |
|
25 |
|
|
26 |
/** |
|
27 |
* convertToPredictData |
|
28 |
* |
|
29 |
* @param predictResult |
|
30 |
* @return |
|
31 |
*/ |
9162d9
|
32 |
public Map<String, List<DataValueVO>> convertToPredictData(PredictResultVO predictResult) { |
7fd198
|
33 |
Map<String, List<DataValueVO>> resultMap = new HashMap<>(); |
50084d
|
34 |
// List<MmItemOutputEntity> itemOutPutList = itemEntityFactory.getOutPutByItemId(predictResult.getPredictId()); |
D |
35 |
// |
|
36 |
// if (!CollectionUtils.isEmpty(predictResult.getPredictList())) { |
|
37 |
// resultMap.put(itemOutPutList.get(0).getId(), predictResult.getPredictList()); |
|
38 |
// return resultMap; |
|
39 |
// } |
a4891a
|
40 |
Map<com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity, double[]> predictMatrixs = predictResult.getPredictMatrixs(); |
69bd5e
|
41 |
HashMap<String,List<DataValueVO>> predictLists = new HashMap<>(); |
a4891a
|
42 |
for (Map.Entry<com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity, double[]> entry : predictMatrixs.entrySet()) { |
69bd5e
|
43 |
Integer rows = entry.getValue().length; |
7fd198
|
44 |
List<DataValueVO> predictDataList = new ArrayList<>(); |
69bd5e
|
45 |
Calendar calendar = Calendar.getInstance(); |
D |
46 |
calendar.setTime(predictResult.getPredictTime()); |
|
47 |
for (Integer i = 0; i < rows; i++) { |
7fd198
|
48 |
DataValueVO predictData = new DataValueVO(); |
69bd5e
|
49 |
predictData.setDataTime(calendar.getTime()); |
D |
50 |
predictData.setDataValue(Double.valueOf(entry.getValue()[i])); |
7fd198
|
51 |
predictDataList.add(predictData); |
69bd5e
|
52 |
|
4f1717
|
53 |
calendar.add(Calendar.SECOND, predictResult.getGranularity()); |
7fd198
|
54 |
} |
69bd5e
|
55 |
resultMap.put(entry.getKey().getId(), predictDataList); |
4f1717
|
56 |
predictLists.put(entry.getKey().getResultstr(), predictDataList); |
4af6b1
|
57 |
|
D |
58 |
//处理累计计算 |
|
59 |
if (entry.getKey().getIscumulant() == 1) { |
|
60 |
resultMap.put(entry.getKey().getId() + CommonDict.CUMULANT_SUFFIX, new ArrayList<DataValueVO>(){{ |
|
61 |
DataValueVO predictData = new DataValueVO(); |
|
62 |
// 时间 预测时间+预测长度*粒度 |
|
63 |
Calendar calendar = Calendar.getInstance(); |
|
64 |
calendar.setTime(predictResult.getPredictTime()); |
|
65 |
calendar.add(Calendar.SECOND, predictResult.getGranularity() * (rows - 1)); |
|
66 |
predictData.setDataTime(calendar.getTime()); |
|
67 |
//值 所有值相加/除数 |
|
68 |
BigDecimal sum = BigDecimal.valueOf(Arrays.stream(entry.getValue()).sum()); |
|
69 |
BigDecimal divisor = BigDecimal.valueOf(entry.getKey().getCumuldivisor()); |
|
70 |
predictData.setDataValue(sum.divide(divisor, 2, BigDecimal.ROUND_HALF_UP).doubleValue()); |
|
71 |
add(predictData); |
|
72 |
}}); |
|
73 |
} |
7fd198
|
74 |
} |
69bd5e
|
75 |
predictResult.setPredictLists(predictLists); |
7fd198
|
76 |
return resultMap; |
潘 |
77 |
} |
|
78 |
|
4f1717
|
79 |
public Map<String, List<DataValueVO>> convertToPredictData2(PredictResultVO predictResult) { |
潘 |
80 |
Map<String, List<DataValueVO>> predictLists = new HashMap<>(); |
|
81 |
if (!CollectionUtils.isEmpty(predictResult.getPredictList())) { |
|
82 |
return predictLists; |
|
83 |
} |
5131f1
|
84 |
Map<MmItemOutputEntity, double[]> predictMatrixs = predictResult.getPredictMatrixs(); |
潘 |
85 |
for (Map.Entry<MmItemOutputEntity, double[]> entry : predictMatrixs.entrySet()) { |
4f1717
|
86 |
Integer rows = entry.getValue().length; |
潘 |
87 |
List<DataValueVO> predictDataList = new ArrayList<>(); |
|
88 |
Calendar calendar = Calendar.getInstance(); |
|
89 |
calendar.setTime(predictResult.getPredictTime()); |
|
90 |
for (Integer i = 0; i < rows; i++) { |
|
91 |
DataValueVO predictData = new DataValueVO(); |
|
92 |
predictData.setDataTime(calendar.getTime()); |
|
93 |
predictData.setDataValue(Double.valueOf(entry.getValue()[i])); |
|
94 |
predictDataList.add(predictData); |
|
95 |
|
|
96 |
calendar.add(Calendar.SECOND, predictResult.getGranularity()); |
|
97 |
} |
|
98 |
predictLists.put(entry.getKey().getResultstr(), predictDataList); |
|
99 |
} |
|
100 |
return predictLists; |
|
101 |
} |
|
102 |
|
7fd198
|
103 |
/** |
潘 |
104 |
* savePredictResult |
|
105 |
* |
|
106 |
* @param predictResult |
|
107 |
*/ |
69bd5e
|
108 |
@DSTransactional |
7fd198
|
109 |
public void savePredictResult(PredictResultVO predictResult) { |
潘 |
110 |
Map<String, List<DataValueVO>> resultMap = convertToPredictData(predictResult); |
|
111 |
mmItemResultService.savePredictValue(resultMap, predictResult.getLt(), "n", predictResult.getPredictTime()); |
|
112 |
} |
|
113 |
|
|
114 |
public List<DataValueVO> getPredictValueByItemNo(String itemNo, Date start, Date end) { |
|
115 |
String itemId = itemEntityFactory.getItemByItemNo(itemNo).getId(); |
a4891a
|
116 |
List<MmItemOutputEntity> outputList = itemEntityFactory.getOutPutByItemId(itemId); |
7fd198
|
117 |
return mmItemResultService.getPredictValue(outputList.get(0).getId(), start, end); |
潘 |
118 |
} |
|
119 |
|
|
120 |
public List<DataValueVO> getPredictValueByItemId(String itemId, Date start, Date end) { |
a4891a
|
121 |
List<MmItemOutputEntity> outputList = itemEntityFactory.getOutPutByItemId(itemId); |
7fd198
|
122 |
return mmItemResultService.getPredictValue(outputList.get(0).getId(), start, end); |
潘 |
123 |
} |
|
124 |
} |