package com.iailab.module.data.point.collection.handler; import com.iailab.module.data.api.point.DataPointApi; import com.iailab.module.data.api.point.dto.ApiPointDTO; import com.iailab.module.data.api.point.dto.ApiPointValueDTO; import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO; import com.iailab.module.data.common.enums.CommonConstant; import com.iailab.module.data.enums.DataPointFreqEnum; import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO; import com.iailab.module.data.point.collection.PointCollector; import com.iailab.module.data.point.collection.utils.GenInfluxPointValueUtils; import com.iailab.module.data.point.dto.DaPointDTO; import com.iailab.module.data.point.service.DaPointService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.*; /** * ç´¯è®¡ç‚¹å¤„ç† * * @author PanZhibao * @Description * @createTime 2024å¹´11月29æ—¥ */ @Slf4j @Component public class CumulateHandle { @Resource private DaPointService daPointService; @Autowired @Lazy private DataPointApi dataPointApi; @Autowired private RedisTemplate<String, Object> redisTemplate; public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, List<String> listGood, List<String> listBad) { List<InfluxPointValuePOJO> result = new ArrayList<>(); try { log.info("累计点处ç†å¼€å§‹"); if (CollectionUtils.isEmpty(dtos)) { return result; } dtos.forEach(dto -> { try { Object value = singleCompute(dto, collectTime, listGood, listBad); InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, value); pojo.setTimestamp(collectTime.toInstant()); result.add(pojo); } catch (Exception ex) { ex.printStackTrace(); log.info("累计点异常ï¼PointNo=" + dto.getPointNo()); } }); } catch (Exception ex) { ex.printStackTrace(); log.info("累计点处ç†å¼‚常ï¼"); } return result; } public Map<String, Object> getCurrent(List<String> pointNos) { Map<String, Object> data = new HashMap<>(); List<DaPointDTO> pointMathList = daPointService.getCumulatePoint(pointNos); if (CollectionUtils.isEmpty(pointMathList)) { return data; } Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); pointMathList.forEach(item -> { Object value = CommonConstant.BAD_VALUE; if (redisTemplate.hasKey(PointCollector.PV + item.getPointNo())) { value = redisTemplate.opsForValue().get(PointCollector.PV + item.getPointNo()); } else { value = singleCompute(item, calendar.getTime()); } data.put(item.getPointNo(), value); }); return data; } private Object singleCompute(DaPointDTO dto, Date collectTime) { return singleCompute(dto, collectTime, null, null); } private Object singleCompute(DaPointDTO dto, Date collectTime, List<String> listGood, List<String> listBad) { ApiPointDTO pointDTO = dataPointApi.getInfoByNo(dto.getMomentPoint()); if (pointDTO == null) { if (listBad != null) { listBad.add(dto.getPointNo()); } return CommonConstant.BAD_VALUE; } Calendar calendar = Calendar.getInstance(); calendar.setTime(collectTime); calendar.add(Calendar.MINUTE, -1); Date endTime = calendar.getTime(); calendar.add(Calendar.MINUTE, dto.getLength() * -1); Date startTime = calendar.getTime(); ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); queryDto.setStart(startTime); queryDto.setEnd(endTime); queryDto.setPointNo(dto.getMomentPoint()); List<ApiPointValueDTO> dataList = dataPointApi.queryPointHistoryValue(queryDto); if (CollectionUtils.isEmpty(dataList)) { if (listGood != null) { listGood.add(dto.getPointNo()); } return BigDecimal.ZERO; } else if (dataList.size() < dto.getLength()) { // è¡¥å…¨æ•°æ® dataList = completionData(dto.getLength(), dataList, startTime, endTime, pointDTO); } double total = dataList.stream().mapToDouble(ApiPointValueDTO::getV).sum(); if (listGood != null) { listGood.add(dto.getPointNo()); } return new BigDecimal(total).divide(new BigDecimal(dto.getDivisor()), 2, BigDecimal.ROUND_HALF_UP); } private List<ApiPointValueDTO> completionData(int length, List<ApiPointValueDTO> dataList, Date startTime, Date endTime, ApiPointDTO pointDTO) { if (CollectionUtils.isEmpty(dataList) || length == dataList.size()) { return dataList; } else if (length < dataList.size()) { return dataList.subList(dataList.size() - length, dataList.size()); } List<ApiPointValueDTO> result = new ArrayList<>(); long start = startTime.getTime(); long end = endTime.getTime(); long oneMin = 1000L * DataPointFreqEnum.getEumByCode(pointDTO.getMinfreqid()).getValue(); long mins = (end - start) / oneMin; //找出缺少项 Map<Long, Double> sourceDataMap = new HashMap<>(dataList.size()); for (ApiPointValueDTO pv : dataList) { sourceDataMap.put(pv.getT().getTime(), pv.getV()); } Map<Long, Double> dataMap = new LinkedHashMap<>(); for (int i = 0; i < mins; i++) { Long key = start + oneMin * i; Double value = sourceDataMap.get(key); dataMap.put(key, value); } //补充缺少项 int k = 0; Map.Entry<Long, Double> lastItem = null; for (Map.Entry<Long, Double> item : dataMap.entrySet()) { if (k == 0 && item.getValue() == null) { item.setValue(getFirstValue(dataMap)); } else if (item.getValue() == null) { item.setValue(lastItem.getValue()); } k++; lastItem = item; ApiPointValueDTO dataEntity = new ApiPointValueDTO(); dataEntity.setT(new Date(item.getKey())); dataEntity.setV(item.getValue()); result.add(dataEntity); } return result; } private Double getFirstValue(Map<Long, Double> dataMap) { for (Map.Entry<Long, Double> item : dataMap.entrySet()) { if (item.getValue() != null) { return item.getValue(); } } return 0.0; } }