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