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