dengzedong
2025-02-27 6205c22a959fbb3b69735f34af8d3316c4082a5d
提交 | 用户 | 时间
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
2fcc1a 46     public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos,List<String> listGood,List<String> listBad) {
56dba6 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 {
2fcc1a 55                     Object value = singleCompute(dto, collectTime,listGood,listBad);
56dba6 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     private Object singleCompute(DaPointDTO dto, Date collectTime) {
2fcc1a 93         return singleCompute(dto,collectTime,null,null);
D 94     }
95
96     private Object singleCompute(DaPointDTO dto, Date collectTime,List<String> listGood,List<String> listBad) {
56dba6 97         ApiPointDTO pointDTO = dataPointApi.getInfoByNo(dto.getMomentPoint());
98         if (pointDTO == null) {
2fcc1a 99             if (listBad != null) {
D 100                 listBad.add(dto.getPointNo());
101             }
56dba6 102             return CommonConstant.BAD_VALUE;
103         }
104         Calendar calendar = Calendar.getInstance();
105         calendar.setTime(collectTime);
106         calendar.add(Calendar.MINUTE, -1);
107         Date endTime = calendar.getTime();
108         calendar.add(Calendar.MINUTE, dto.getLength() * -1);
109         Date startTime = calendar.getTime();
110         ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO();
111         queryDto.setStart(startTime);
112         queryDto.setEnd(endTime);
113         queryDto.setPointNo(dto.getMomentPoint());
114
115         List<ApiPointValueDTO> dataList = dataPointApi.queryPointHistoryValue(queryDto);
116         if (CollectionUtils.isEmpty(dataList)) {
2fcc1a 117             if (listGood != null) {
D 118                 listGood.add(dto.getPointNo());
119             }
56dba6 120             return BigDecimal.ZERO;
121         } else if (dataList.size() < dto.getLength()) {
122             // 补全数据
123             dataList = completionData(dto.getLength(), dataList, startTime, endTime, pointDTO);
124         }
125         double total = dataList.stream().mapToDouble(ApiPointValueDTO::getV).sum();
2fcc1a 126         if (listGood != null) {
D 127             listGood.add(dto.getPointNo());
128         }
56dba6 129         return new BigDecimal(total).divide(new BigDecimal(dto.getDivisor()), 2, BigDecimal.ROUND_HALF_UP);
130     }
131
132     private List<ApiPointValueDTO> completionData(int length, List<ApiPointValueDTO> dataList, Date startTime, Date endTime, ApiPointDTO pointDTO) {
133         if (CollectionUtils.isEmpty(dataList) || length == dataList.size()) {
134             return dataList;
135         } else if (length < dataList.size()) {
136             return dataList.subList(dataList.size() - length, dataList.size());
137         }
138
139         List<ApiPointValueDTO> result = new ArrayList<>();
140         long start = startTime.getTime();
141         long end = endTime.getTime();
142         long oneMin = 1000L * DataPointFreqEnum.getEumByCode(pointDTO.getMinfreqid()).getValue();
143         long mins = (end - start) / oneMin;
144
145         //找出缺少项
146         Map<Long, Double> sourceDataMap = new HashMap<>(dataList.size());
c844cd 147         for (ApiPointValueDTO pv : dataList) {
148             sourceDataMap.put(pv.getT().getTime(), pv.getV());
149         }
150
56dba6 151         Map<Long, Double> dataMap = new LinkedHashMap<>();
152         for (int i = 0; i < mins; i++) {
153             Long key = start + oneMin * i;
154             Double value = sourceDataMap.get(key);
155             dataMap.put(key, value);
156         }
157
158         //补充缺少项
159         int k = 0;
160         Map.Entry<Long, Double> lastItem = null;
161         for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
162             if (k == 0 && item.getValue() == null) {
163                 item.setValue(getFirstValue(dataMap));
164             } else if (item.getValue() == null) {
165                 item.setValue(lastItem.getValue());
166             }
167             k++;
168             lastItem = item;
169
170             ApiPointValueDTO dataEntity = new ApiPointValueDTO();
171             dataEntity.setT(new Date(item.getKey()));
172             dataEntity.setV(item.getValue());
173             result.add(dataEntity);
174         }
175         return result;
176     }
177
178     private Double getFirstValue(Map<Long, Double> dataMap) {
179         for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
180             if (item.getValue() != null) {
181                 return item.getValue();
182             }
183         }
2fcc1a 184         return 0.0;
56dba6 185     }
186 }