提交 | 用户 | 时间
|
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; |
|
5 |
import com.iailab.framework.common.service.impl.BaseServiceImpl; |
|
6 |
import com.iailab.module.data.api.point.dto.ApiPointDTO; |
|
7 |
import com.iailab.module.model.mcs.pre.dao.MmItemResultDao; |
|
8 |
import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity; |
|
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.MmItemOutputService; |
|
12 |
import com.iailab.module.model.mcs.pre.service.MmItemResultService; |
|
13 |
import com.iailab.module.model.mdk.vo.DataValueVO; |
|
14 |
import org.springframework.beans.factory.annotation.Autowired; |
|
15 |
import org.springframework.stereotype.Service; |
|
16 |
import org.springframework.util.CollectionUtils; |
|
17 |
|
|
18 |
import java.math.BigDecimal; |
|
19 |
import java.text.ParseException; |
|
20 |
import java.text.SimpleDateFormat; |
|
21 |
import java.util.*; |
|
22 |
import java.util.stream.Collectors; |
|
23 |
|
|
24 |
/** |
|
25 |
* @author PanZhibao |
|
26 |
* @date 2021年05月28日 10:34 |
|
27 |
*/ |
|
28 |
@Service("mmItemResultService") |
|
29 |
public class MmItemResultServiceImpl extends BaseServiceImpl<MmItemResultDao, MmItemResultEntity> |
|
30 |
implements MmItemResultService { |
|
31 |
|
|
32 |
private final int max_group_count = 100; |
|
33 |
|
|
34 |
private final String T_MM_ITEM_RESULT = "T_MM_ITEM_RESULT"; |
|
35 |
|
|
36 |
@Autowired |
|
37 |
private MmItemOutputService mmItemOutputService; |
|
38 |
|
|
39 |
@Override |
|
40 |
public List<MmItemResultEntity> getListByOutputId(String outputid, Map<String, Object> params) { |
|
41 |
|
|
42 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
43 |
Date startDateParam = null; |
|
44 |
try { |
|
45 |
startDateParam = sdf.parse((String)params.get("startTime")); |
|
46 |
} catch (ParseException e) { |
|
47 |
e.printStackTrace(); |
|
48 |
} |
|
49 |
Date endDateParam = null; |
|
50 |
try { |
|
51 |
endDateParam = sdf.parse((String)params.get("endTime")); |
|
52 |
} catch (ParseException e) { |
|
53 |
e.printStackTrace(); |
|
54 |
} |
|
55 |
|
|
56 |
List<MmItemResultEntity> list = baseDao.selectList( |
|
57 |
new QueryWrapper<MmItemResultEntity>() |
|
58 |
.eq("outputid", outputid) |
|
59 |
.between("datatime", startDateParam, endDateParam) |
|
60 |
.orderByAsc("datatime") |
|
61 |
); |
|
62 |
return list; |
|
63 |
} |
|
64 |
|
|
65 |
@Override |
|
66 |
public void savePredictValue(Map<String, List<DataValueVO>> predictValueMap, int t, String nIndex, Date predictTime) { |
|
67 |
List<MmItemResultEntity> importList = new ArrayList<>(); |
|
68 |
List<MmItemResultEntity> lastList = new ArrayList<>(); |
|
69 |
for (Map.Entry<String, List<DataValueVO>> entry : predictValueMap.entrySet()) { |
|
70 |
for (DataValueVO dataVo : entry.getValue()) { |
|
71 |
MmItemResultEntity importData = new MmItemResultEntity(); |
|
72 |
importData.setId(String.valueOf(UUID.randomUUID())); |
|
73 |
importData.setOutputid(entry.getKey()); |
|
74 |
importData.setDatatime(dataVo.getDataTime()); |
|
75 |
importData.setDatavalue(new BigDecimal(dataVo.getDataValue())); |
|
76 |
importList.add(importData); |
|
77 |
} |
|
78 |
|
|
79 |
List<DataValueVO> lastVoList = new ArrayList<>(); |
|
80 |
int size = entry.getValue().size(); |
|
81 |
t = t > 0 ? t : 0; |
|
82 |
int n = "n".equals(nIndex) ? size : Integer.parseInt(nIndex); |
|
83 |
int length = (n - t) > 0 ? (n - t) : 0; //预测完不变的数据长度 |
|
84 |
if (size >= n) { |
|
85 |
for (int i = 0; i < (size - length); i ++) { |
|
86 |
int index = length + i; |
|
87 |
lastVoList.add(entry.getValue().get(index)); |
|
88 |
} |
|
89 |
} else { |
|
90 |
lastVoList = entry.getValue(); |
|
91 |
} |
|
92 |
|
|
93 |
for (DataValueVO dataVo : lastVoList) { |
|
94 |
MmItemResultEntity importData = new MmItemResultEntity(); |
|
95 |
importData.setId(String.valueOf(UUID.randomUUID())); |
|
96 |
importData.setOutputid(entry.getKey()); |
|
97 |
importData.setDatatime(dataVo.getDataTime()); |
|
98 |
importData.setDatavalue(new BigDecimal(dataVo.getDataValue())); |
|
99 |
lastList.add(importData); |
|
100 |
} |
|
101 |
|
|
102 |
MmItemResultJsonEntity resultJson = new MmItemResultJsonEntity(); |
|
103 |
resultJson.setId(UUID.randomUUID().toString()); |
|
104 |
resultJson.setOutputid(entry.getKey()); |
|
105 |
resultJson.setPredicttime(predictTime); |
|
106 |
resultJson.setJsonvalue(JSONArray.toJSONString(entry.getValue())); |
|
107 |
Map<String, Object> map4 = new HashMap(2); |
|
108 |
map4.put("TABLENAME", "T_MM_ITEM_RESULT_JSON"); |
|
109 |
map4.put("entity", resultJson); |
|
110 |
baseDao.savePredictJsonValue(map4); |
|
111 |
} |
|
112 |
|
|
113 |
Map<String, Object> params = new HashMap(4); |
|
114 |
params.put("TABLENAME", T_MM_ITEM_RESULT); |
|
115 |
params.put("OUTPUTID", importList.get(0).getOutputid()); |
|
116 |
params.put("STARTTIME", importList.get(0).getDatatime()); |
|
117 |
params.put("ENDTIME", importList.get(importList.size() - 1).getDatatime()); |
|
118 |
baseDao.deletePredictValue(params); |
|
119 |
|
|
120 |
int num1 = importList.size() / max_group_count; |
|
121 |
int num2 = importList.size() % max_group_count; |
|
122 |
if (num2 != 0) { |
|
123 |
num1++; |
|
124 |
} |
|
125 |
|
|
126 |
List<MmItemResultEntity> tempList; |
|
127 |
//先删除已经存在的数据,再插入新数据 |
|
128 |
for (int i = 0; i < num1; i++) { |
|
129 |
int startIndex = max_group_count * i; |
|
130 |
int count = max_group_count; |
|
131 |
if (num2!=0 && i == num1 - 1) { |
|
132 |
count = num2; |
|
133 |
} |
|
134 |
tempList = new ArrayList<>(); |
|
135 |
//获取某个索引范围内的对象集合 |
|
136 |
for (int j = startIndex; j < startIndex + count; j++) { |
|
137 |
tempList.add(importList.get(j)); |
|
138 |
} |
|
139 |
Map<String, Object> map2 = new HashMap<>(2); |
|
140 |
map2.put("TABLENAME", T_MM_ITEM_RESULT); |
|
141 |
map2.put("list", tempList); |
|
142 |
baseDao.savePredictValue(map2); |
|
143 |
} |
|
144 |
|
|
145 |
Map<String, Object> map3 = new HashMap<>(2); |
|
146 |
map3.put("TABLENAME", "T_MM_ITEM_RESULT_LAST_POINT"); |
|
147 |
map3.put("list", lastList); |
|
148 |
baseDao.savePredictValue(map3); |
|
149 |
} |
|
150 |
|
|
151 |
@Override |
|
152 |
public List<DataValueVO> getPredictValue(String outputid, Date startTime, Date endTime) { |
|
153 |
List<DataValueVO> result = new ArrayList<>(); |
|
154 |
QueryWrapper<MmItemResultEntity> queryWrapper = new QueryWrapper<MmItemResultEntity>() |
|
155 |
.eq("outputid", outputid) |
|
156 |
.between("datatime", startTime, endTime) |
|
157 |
.orderByAsc("datatime"); |
|
158 |
List<MmItemResultEntity> list = baseDao.selectList(queryWrapper); |
|
159 |
if (CollectionUtils.isEmpty(list)) { |
|
160 |
return result; |
|
161 |
} |
|
162 |
result = list.stream().map(t -> { |
|
163 |
DataValueVO dv = new DataValueVO(); |
|
164 |
dv.setDataTime(t.getDatatime()); |
|
165 |
dv.setDataValue(t.getDatavalue().doubleValue()); |
|
166 |
return dv; |
|
167 |
}).collect(Collectors.toList()); |
|
168 |
return result; |
|
169 |
} |
|
170 |
} |