提交 | 用户 | 时间
|
56dba6
|
1 |
package com.iailab.module.data.point.collection.handler; |
潘 |
2 |
|
|
3 |
import com.iailab.module.data.api.point.DataPointApi; |
|
4 |
import com.iailab.module.data.api.point.dto.ApiPointDTO; |
|
5 |
import com.iailab.module.data.api.point.dto.ApiPointValueDTO; |
|
6 |
import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO; |
|
7 |
import com.iailab.module.data.common.enums.CommonConstant; |
|
8 |
import com.iailab.module.data.enums.DataPointFreqEnum; |
|
9 |
import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO; |
781e72
|
10 |
import com.iailab.module.data.point.collection.PointCollector; |
56dba6
|
11 |
import com.iailab.module.data.point.collection.utils.GenInfluxPointValueUtils; |
潘 |
12 |
import com.iailab.module.data.point.dto.DaPointDTO; |
5bf42a
|
13 |
import com.iailab.module.data.point.service.DaPointService; |
56dba6
|
14 |
import lombok.extern.slf4j.Slf4j; |
潘 |
15 |
import org.springframework.beans.factory.annotation.Autowired; |
f25963
|
16 |
import org.springframework.context.annotation.Lazy; |
781e72
|
17 |
import org.springframework.data.redis.core.RedisTemplate; |
56dba6
|
18 |
import org.springframework.stereotype.Component; |
潘 |
19 |
import org.springframework.util.CollectionUtils; |
|
20 |
|
5bf42a
|
21 |
import javax.annotation.Resource; |
56dba6
|
22 |
import java.math.BigDecimal; |
潘 |
23 |
import java.util.*; |
|
24 |
|
|
25 |
/** |
|
26 |
* 累计点处理 |
|
27 |
* |
|
28 |
* @author PanZhibao |
|
29 |
* @Description |
|
30 |
* @createTime 2024年11月29日 |
|
31 |
*/ |
|
32 |
@Slf4j |
|
33 |
@Component |
|
34 |
public class CumulateHandle { |
5bf42a
|
35 |
|
潘 |
36 |
@Resource |
|
37 |
private DaPointService daPointService; |
56dba6
|
38 |
|
潘 |
39 |
@Autowired |
f25963
|
40 |
@Lazy |
56dba6
|
41 |
private DataPointApi dataPointApi; |
781e72
|
42 |
|
潘 |
43 |
@Autowired |
|
44 |
private RedisTemplate<String, Object> redisTemplate; |
56dba6
|
45 |
|
潘 |
46 |
public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos) { |
|
47 |
List<InfluxPointValuePOJO> result = new ArrayList<>(); |
|
48 |
try { |
|
49 |
log.info("累计点处理开始"); |
|
50 |
if (CollectionUtils.isEmpty(dtos)) { |
|
51 |
return result; |
|
52 |
} |
|
53 |
dtos.forEach(dto -> { |
|
54 |
try { |
|
55 |
Object value = singleCompute(dto, collectTime); |
|
56 |
InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, value); |
|
57 |
pojo.setTimestamp(collectTime.toInstant()); |
|
58 |
result.add(pojo); |
|
59 |
} catch (Exception ex) { |
|
60 |
ex.printStackTrace(); |
|
61 |
log.info("累计点异常!PointNo=" + dto.getPointNo()); |
|
62 |
} |
|
63 |
}); |
|
64 |
|
|
65 |
} catch (Exception ex) { |
|
66 |
ex.printStackTrace(); |
|
67 |
log.info("累计点处理异常!"); |
|
68 |
} |
|
69 |
return result; |
|
70 |
} |
|
71 |
|
5bf42a
|
72 |
public Map<String, Object> getCurrent(List<String> pointNos) { |
潘 |
73 |
Map<String, Object> data = new HashMap<>(); |
|
74 |
List<DaPointDTO> pointMathList = daPointService.getCumulatePoint(pointNos); |
|
75 |
if (CollectionUtils.isEmpty(pointMathList)) { |
|
76 |
return data; |
|
77 |
} |
|
78 |
Calendar calendar = Calendar.getInstance(); |
|
79 |
calendar.set(Calendar.MILLISECOND, 0); |
|
80 |
pointMathList.forEach(item -> { |
781e72
|
81 |
Object value = CommonConstant.BAD_VALUE; |
潘 |
82 |
if (redisTemplate.hasKey(PointCollector.PV + item.getPointNo())) { |
|
83 |
value = redisTemplate.opsForValue().get(PointCollector.PV + item.getPointNo()); |
|
84 |
} else { |
|
85 |
value = singleCompute(item, calendar.getTime()); |
|
86 |
} |
|
87 |
data.put(item.getPointNo(), value); |
5bf42a
|
88 |
}); |
潘 |
89 |
return data; |
|
90 |
} |
|
91 |
|
56dba6
|
92 |
|
潘 |
93 |
private Object singleCompute(DaPointDTO dto, Date collectTime) { |
|
94 |
ApiPointDTO pointDTO = dataPointApi.getInfoByNo(dto.getMomentPoint()); |
|
95 |
if (pointDTO == null) { |
|
96 |
return CommonConstant.BAD_VALUE; |
|
97 |
} |
|
98 |
Calendar calendar = Calendar.getInstance(); |
|
99 |
calendar.setTime(collectTime); |
|
100 |
calendar.add(Calendar.MINUTE, -1); |
|
101 |
Date endTime = calendar.getTime(); |
|
102 |
calendar.add(Calendar.MINUTE, dto.getLength() * -1); |
|
103 |
Date startTime = calendar.getTime(); |
|
104 |
ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); |
|
105 |
queryDto.setStart(startTime); |
|
106 |
queryDto.setEnd(endTime); |
|
107 |
queryDto.setPointNo(dto.getMomentPoint()); |
|
108 |
|
|
109 |
List<ApiPointValueDTO> dataList = dataPointApi.queryPointHistoryValue(queryDto); |
|
110 |
if (CollectionUtils.isEmpty(dataList)) { |
|
111 |
return BigDecimal.ZERO; |
|
112 |
} else if (dataList.size() < dto.getLength()) { |
|
113 |
// 补全数据 |
|
114 |
dataList = completionData(dto.getLength(), dataList, startTime, endTime, pointDTO); |
|
115 |
} |
|
116 |
double total = dataList.stream().mapToDouble(ApiPointValueDTO::getV).sum(); |
|
117 |
return new BigDecimal(total).divide(new BigDecimal(dto.getDivisor()), 2, BigDecimal.ROUND_HALF_UP); |
|
118 |
} |
|
119 |
|
|
120 |
private List<ApiPointValueDTO> completionData(int length, List<ApiPointValueDTO> dataList, Date startTime, Date endTime, ApiPointDTO pointDTO) { |
|
121 |
if (CollectionUtils.isEmpty(dataList) || length == dataList.size()) { |
|
122 |
return dataList; |
|
123 |
} else if (length < dataList.size()) { |
|
124 |
return dataList.subList(dataList.size() - length, dataList.size()); |
|
125 |
} |
|
126 |
|
|
127 |
List<ApiPointValueDTO> result = new ArrayList<>(); |
|
128 |
long start = startTime.getTime(); |
|
129 |
long end = endTime.getTime(); |
|
130 |
long oneMin = 1000L * DataPointFreqEnum.getEumByCode(pointDTO.getMinfreqid()).getValue(); |
|
131 |
long mins = (end - start) / oneMin; |
|
132 |
|
|
133 |
//找出缺少项 |
|
134 |
Map<Long, Double> sourceDataMap = new HashMap<>(dataList.size()); |
c844cd
|
135 |
for (ApiPointValueDTO pv : dataList) { |
潘 |
136 |
sourceDataMap.put(pv.getT().getTime(), pv.getV()); |
|
137 |
} |
|
138 |
|
56dba6
|
139 |
Map<Long, Double> dataMap = new LinkedHashMap<>(); |
潘 |
140 |
for (int i = 0; i < mins; i++) { |
|
141 |
Long key = start + oneMin * i; |
|
142 |
Double value = sourceDataMap.get(key); |
|
143 |
dataMap.put(key, value); |
|
144 |
} |
|
145 |
|
|
146 |
//补充缺少项 |
|
147 |
int k = 0; |
|
148 |
Map.Entry<Long, Double> lastItem = null; |
|
149 |
for (Map.Entry<Long, Double> item : dataMap.entrySet()) { |
|
150 |
if (k == 0 && item.getValue() == null) { |
|
151 |
item.setValue(getFirstValue(dataMap)); |
|
152 |
} else if (item.getValue() == null) { |
|
153 |
item.setValue(lastItem.getValue()); |
|
154 |
} |
|
155 |
k++; |
|
156 |
lastItem = item; |
|
157 |
|
|
158 |
ApiPointValueDTO dataEntity = new ApiPointValueDTO(); |
|
159 |
dataEntity.setT(new Date(item.getKey())); |
|
160 |
dataEntity.setV(item.getValue()); |
|
161 |
result.add(dataEntity); |
|
162 |
} |
|
163 |
return result; |
|
164 |
} |
|
165 |
|
|
166 |
private Double getFirstValue(Map<Long, Double> dataMap) { |
|
167 |
for (Map.Entry<Long, Double> item : dataMap.entrySet()) { |
|
168 |
if (item.getValue() != null) { |
|
169 |
return item.getValue(); |
|
170 |
} |
|
171 |
} |
|
172 |
return null; |
|
173 |
} |
|
174 |
} |