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