工业互联网平台2.0版本后端代码
潘志宝
2025-05-29 ca103ae3bbb52f6b3a9edb93c8bebcdddb96608a
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CumulateHandle.java
@@ -23,6 +23,7 @@
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
 * 累计点处理
@@ -45,7 +46,7 @@
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, List<String> listGood, List<String> listBad) {
    public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap, List<String> listGood, List<String> listBad) {
        List<InfluxPointValuePOJO> result = new ArrayList<>();
        try {
            log.info("累计点处理开始");
@@ -54,9 +55,12 @@
            }
            dtos.forEach(dto -> {
                try {
                    Object value = singleCompute(dto, collectTime, listGood, listBad);
                    InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, value);
                    Object rawValue = singleCompute(dto, collectTime, listGood, listBad);
                    BigDecimal coefficient = dto.getUnittransfactor() == null ? BigDecimal.ONE : dto.getUnittransfactor();
                    BigDecimal calValue = new BigDecimal(rawValue.toString()).multiply(coefficient);
                    InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, calValue);
                    pojo.setTimestamp(GenInfluxPointValueUtils.getByMin(collectTime, DataPointFreqEnum.getEumByCode(dto.getMinfreqid())));
                    dataMap.put(dto.getPointNo(), calValue);
                    result.add(pojo);
                } catch (Exception ex) {
                    ex.printStackTrace();
@@ -79,12 +83,18 @@
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 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());
                Object rawValue = singleCompute(item, calendar.getTime());
                BigDecimal coefficient = item.getUnittransfactor() == null ? BigDecimal.ONE : item.getUnittransfactor();
                value = new BigDecimal(rawValue.toString()).multiply(coefficient);
                // 写入缓存
                redisTemplate.opsForValue().set(PointCollector.PV + item.getPointNo(),
                        new BigDecimal(value.toString()).doubleValue(), PointCollector.offset, TimeUnit.SECONDS);
            }
            data.put(item.getPointNo(), value);
        });
@@ -118,7 +128,18 @@
        queryDto.setEnd(endTime);
        queryDto.setPointNo(dto.getMomentPoint());
        log.info("queryDto=" + JSONObject.toJSONString(queryDto));
        List<ApiPointValueDTO> dataList = dataPointApi.queryPointHistoryValue(queryDto);
        List<ApiPointValueDTO> dataList = new ArrayList<>();
        List<ApiPointValueDTO> dataListTemp = dataPointApi.queryPointHistoryValue(queryDto);
        if (dto.getIsCumuNeg() != null && dto.getIsCumuNeg().equals(0)) {
            for (ApiPointValueDTO item : dataListTemp) {
                if (item.getV() > 0) {
                    dataList.add(item);
                }
            }
        } else {
            dataList = dataListTemp;
        }
        if (CollectionUtils.isEmpty(dataList)) {
            log.info("dataList is empty");
            if (listGood != null) {
@@ -127,7 +148,7 @@
            return BigDecimal.ZERO;
        } else if (dataList.size() < dto.getLength()) {
            log.info("补全数据,dataList.size()=" + dataList.size());
            dataList = completionData(dto.getLength(), dataList, startTime, endTime, pointDTO);
            dataList = completionData(dto.getLength(), dataList, startTime, endTime, momentPoint.getMinfreqid());
        }
        double total = dataList.stream().mapToDouble(ApiPointValueDTO::getV).sum();
        if (listGood != null) {
@@ -136,7 +157,7 @@
        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) {
    private List<ApiPointValueDTO> completionData(int length, List<ApiPointValueDTO> dataList, Date startTime, Date endTime, String minfreqid) {
        if (CollectionUtils.isEmpty(dataList) || length == dataList.size()) {
            return dataList;
        } else if (length < dataList.size()) {
@@ -146,8 +167,8 @@
        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;
        long oneMin = 1000L * DataPointFreqEnum.getEumByCode(minfreqid).getValue();
        long mins = ((end - start) / oneMin) + 1;
        //找出缺少项
        Map<Long, Double> sourceDataMap = new HashMap<>(dataList.size());