提交 | 用户 | 时间
|
7fd198
|
1 |
package com.iailab.module.model.mcs.pre.service.impl; |
潘 |
2 |
|
|
3 |
import com.alibaba.fastjson.JSONArray; |
|
4 |
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
5c6007
|
5 |
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
b368e6
|
6 |
import com.iailab.framework.common.util.date.DateUtils; |
7fd198
|
7 |
import com.iailab.module.model.mcs.pre.dao.MmItemResultDao; |
a6e46f
|
8 |
import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity; |
7fd198
|
9 |
import com.iailab.module.model.mcs.pre.entity.MmItemResultEntity; |
潘 |
10 |
import com.iailab.module.model.mcs.pre.entity.MmItemResultJsonEntity; |
|
11 |
import com.iailab.module.model.mcs.pre.service.MmItemResultService; |
|
12 |
import com.iailab.module.model.mdk.vo.DataValueVO; |
|
13 |
import org.springframework.beans.factory.annotation.Autowired; |
|
14 |
import org.springframework.stereotype.Service; |
|
15 |
import org.springframework.util.CollectionUtils; |
|
16 |
|
|
17 |
import java.math.BigDecimal; |
|
18 |
import java.text.ParseException; |
|
19 |
import java.text.SimpleDateFormat; |
|
20 |
import java.util.*; |
|
21 |
import java.util.stream.Collectors; |
|
22 |
|
|
23 |
/** |
|
24 |
* @author PanZhibao |
|
25 |
* @date 2021年05月28日 10:34 |
|
26 |
*/ |
5c6007
|
27 |
@Service |
L |
28 |
public class MmItemResultServiceImpl extends ServiceImpl<MmItemResultDao, MmItemResultEntity> implements MmItemResultService { |
7fd198
|
29 |
|
潘 |
30 |
private final int max_group_count = 100; |
|
31 |
|
|
32 |
private final String T_MM_ITEM_RESULT = "T_MM_ITEM_RESULT"; |
|
33 |
|
|
34 |
@Autowired |
5c6007
|
35 |
private MmItemResultDao mmItemResultDao; |
7fd198
|
36 |
|
潘 |
37 |
@Override |
|
38 |
public void savePredictValue(Map<String, List<DataValueVO>> predictValueMap, int t, String nIndex, Date predictTime) { |
|
39 |
List<MmItemResultEntity> importList = new ArrayList<>(); |
|
40 |
List<MmItemResultEntity> lastList = new ArrayList<>(); |
69bd5e
|
41 |
|
7fd198
|
42 |
for (Map.Entry<String, List<DataValueVO>> entry : predictValueMap.entrySet()) { |
潘 |
43 |
for (DataValueVO dataVo : entry.getValue()) { |
|
44 |
MmItemResultEntity importData = new MmItemResultEntity(); |
|
45 |
importData.setId(String.valueOf(UUID.randomUUID())); |
|
46 |
importData.setOutputid(entry.getKey()); |
|
47 |
importData.setDatatime(dataVo.getDataTime()); |
|
48 |
importData.setDatavalue(new BigDecimal(dataVo.getDataValue())); |
|
49 |
importList.add(importData); |
|
50 |
} |
|
51 |
|
|
52 |
List<DataValueVO> lastVoList = new ArrayList<>(); |
|
53 |
int size = entry.getValue().size(); |
4f1717
|
54 |
t = Math.max(t, 0); |
7fd198
|
55 |
int n = "n".equals(nIndex) ? size : Integer.parseInt(nIndex); |
4f1717
|
56 |
int length = Math.max((n - t), 0); //预测完不变的数据长度 |
7fd198
|
57 |
if (size >= n) { |
潘 |
58 |
for (int i = 0; i < (size - length); i ++) { |
|
59 |
int index = length + i; |
|
60 |
lastVoList.add(entry.getValue().get(index)); |
|
61 |
} |
|
62 |
} else { |
|
63 |
lastVoList = entry.getValue(); |
|
64 |
} |
|
65 |
|
|
66 |
for (DataValueVO dataVo : lastVoList) { |
|
67 |
MmItemResultEntity importData = new MmItemResultEntity(); |
|
68 |
importData.setId(String.valueOf(UUID.randomUUID())); |
|
69 |
importData.setOutputid(entry.getKey()); |
|
70 |
importData.setDatatime(dataVo.getDataTime()); |
|
71 |
importData.setDatavalue(new BigDecimal(dataVo.getDataValue())); |
|
72 |
lastList.add(importData); |
|
73 |
} |
|
74 |
|
|
75 |
MmItemResultJsonEntity resultJson = new MmItemResultJsonEntity(); |
|
76 |
resultJson.setId(UUID.randomUUID().toString()); |
|
77 |
resultJson.setOutputid(entry.getKey()); |
|
78 |
resultJson.setPredicttime(predictTime); |
91343d
|
79 |
List<Double> jsonValueList = entry.getValue().stream().map(valueVO -> { |
潘 |
80 |
return valueVO.getDataValue(); |
|
81 |
}).collect(Collectors.toList()); |
|
82 |
resultJson.setJsonvalue(JSONArray.toJSONString(jsonValueList)); |
7fd198
|
83 |
Map<String, Object> map4 = new HashMap(2); |
潘 |
84 |
map4.put("TABLENAME", "T_MM_ITEM_RESULT_JSON"); |
|
85 |
map4.put("entity", resultJson); |
5c6007
|
86 |
mmItemResultDao.savePredictJsonValue(map4); |
69bd5e
|
87 |
|
D |
88 |
Map<String, Object> params = new HashMap(4); |
|
89 |
params.put("TABLENAME", T_MM_ITEM_RESULT); |
|
90 |
params.put("OUTPUTID", entry.getKey()); |
|
91 |
params.put("STARTTIME", importList.get(0).getDatatime()); |
|
92 |
params.put("ENDTIME", importList.get(importList.size() - 1).getDatatime()); |
|
93 |
mmItemResultDao.deletePredictValue(params); |
7fd198
|
94 |
} |
69bd5e
|
95 |
mmItemResultDao.insertBatch(importList,max_group_count); |
7fd198
|
96 |
|
潘 |
97 |
Map<String, Object> map3 = new HashMap<>(2); |
|
98 |
map3.put("TABLENAME", "T_MM_ITEM_RESULT_LAST_POINT"); |
|
99 |
map3.put("list", lastList); |
5c6007
|
100 |
mmItemResultDao.savePredictValue(map3); |
7fd198
|
101 |
} |
潘 |
102 |
|
|
103 |
@Override |
|
104 |
public List<DataValueVO> getPredictValue(String outputid, Date startTime, Date endTime) { |
|
105 |
List<DataValueVO> result = new ArrayList<>(); |
|
106 |
QueryWrapper<MmItemResultEntity> queryWrapper = new QueryWrapper<MmItemResultEntity>() |
|
107 |
.eq("outputid", outputid) |
|
108 |
.between("datatime", startTime, endTime) |
|
109 |
.orderByAsc("datatime"); |
5c6007
|
110 |
List<MmItemResultEntity> list = mmItemResultDao.selectList(queryWrapper); |
7fd198
|
111 |
if (CollectionUtils.isEmpty(list)) { |
潘 |
112 |
return result; |
|
113 |
} |
|
114 |
result = list.stream().map(t -> { |
|
115 |
DataValueVO dv = new DataValueVO(); |
|
116 |
dv.setDataTime(t.getDatatime()); |
|
117 |
dv.setDataValue(t.getDatavalue().doubleValue()); |
|
118 |
return dv; |
|
119 |
}).collect(Collectors.toList()); |
|
120 |
return result; |
|
121 |
} |
b368e6
|
122 |
|
潘 |
123 |
@Override |
977edc
|
124 |
public List<Object[]> getData(String outputid, Date startTime, Date endTime, String timeFormat) { |
b368e6
|
125 |
List<Object[]> result = new ArrayList<>(); |
潘 |
126 |
QueryWrapper<MmItemResultEntity> queryWrapper = new QueryWrapper<MmItemResultEntity>() |
|
127 |
.eq("outputid", outputid) |
|
128 |
.between("datatime", startTime, endTime) |
|
129 |
.orderByAsc("datatime"); |
|
130 |
List<MmItemResultEntity> list = mmItemResultDao.selectList(queryWrapper); |
|
131 |
if (CollectionUtils.isEmpty(list)) { |
|
132 |
return result; |
|
133 |
} |
|
134 |
list.forEach(item -> { |
|
135 |
Object[] dataItem = new Object[2]; |
977edc
|
136 |
dataItem[0] = DateUtils.format(item.getDatatime(), timeFormat); |
1f9784
|
137 |
dataItem[1] = item.getDatavalue().setScale(2, BigDecimal.ROUND_HALF_UP); |
b368e6
|
138 |
result.add(dataItem); |
潘 |
139 |
}); |
|
140 |
return result; |
|
141 |
} |
a6e46f
|
142 |
|
D |
143 |
@Override |
|
144 |
public void savePredictValue(Map<MmItemOutputEntity, Double> predictDoubleValues, Date predictTime) { |
|
145 |
for (Map.Entry<MmItemOutputEntity, Double> entry : predictDoubleValues.entrySet()) { |
|
146 |
MmItemResultJsonEntity resultJson = new MmItemResultJsonEntity(); |
|
147 |
resultJson.setId(UUID.randomUUID().toString()); |
|
148 |
resultJson.setOutputid(entry.getKey().getId()); |
|
149 |
resultJson.setPredicttime(predictTime); |
|
150 |
resultJson.setCumulant(String.valueOf(entry.getValue())); |
|
151 |
Map<String, Object> map4 = new HashMap(2); |
|
152 |
map4.put("TABLENAME", "T_MM_ITEM_RESULT_JSON"); |
|
153 |
map4.put("entity", resultJson); |
|
154 |
mmItemResultDao.savePredictJsonValue(map4); |
|
155 |
} |
|
156 |
} |
7fd198
|
157 |
} |