From cd069965f32189683598ac1096dab66c992c517b Mon Sep 17 00:00:00 2001
From: 潘志宝 <979469083@qq.com>
Date: 星期五, 14 三月 2025 16:36:10 +0800
Subject: [PATCH] BAD点值修复

---
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/MeasureHandle.java    |    4 ----
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/PointCollector.java           |   32 ++++++++++++++++++++++++++++++--
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java |    4 +++-
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java  |   10 +++++-----
 4 files changed, 38 insertions(+), 12 deletions(-)

diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java
index 8301664..ae6045b 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java
@@ -304,7 +304,9 @@
             sql.append("'");
             InfluxQLQueryResult data = influxQLQueryApi.query(new InfluxQLQuery(sql.toString(), influxDBInstance.getBucket()));
             Object value = data.getResults().get(0).getSeries().get(0).getValues().get(0).getValueByKey("last");
-            result.put(point.getPoint(), value);
+            if (value != null) {
+                result.put(point.getPoint(), value);
+            }
         }
         return result;
     }
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/PointCollector.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/PointCollector.java
index 0ab1552..4c5eee6 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/PointCollector.java
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/PointCollector.java
@@ -1,5 +1,6 @@
 package com.iailab.module.data.point.collection;
 
+import com.alibaba.fastjson.JSONObject;
 import com.iailab.module.data.common.enums.DataSourceType;
 import com.iailab.module.data.common.utils.R;
 import com.iailab.module.data.channel.kio.collector.KingIOCollector;
@@ -100,13 +101,40 @@
             List<DaPointDTO> pointMeasureList = daPointService.getMeasurePoint(minfreq);
             pointValues.addAll(measureHandle.handle(collectTime, pointMeasureList, dataMap, listGood, listBad));
 
+            List<String> listBadNew = new ArrayList<>();
+            if (!CollectionUtils.isEmpty(listBad)) {
+                log.info("BAD点值修复");
+                List<InfluxPointValuePOJO> influxParams = new ArrayList<>();
+                for (String bad : listBad) {
+                    DaPointDTO daPointDTO = daPointService.getByNo(bad);
+                    InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
+                    pojo.setPoint(bad);
+                    pojo.setType(daPointDTO.getDataType());
+                    influxParams.add(pojo);
+                }
+                Map<String, Object> lastValue = influxDBService.queryPointsLastValue(influxParams);
+                log.info("lastValue=" + JSONObject.toJSONString(lastValue));
+                if (!CollectionUtils.isEmpty(lastValue)) {
+                    for (String bad : listBad) {
+                        if (lastValue.containsKey(bad)) {
+                            listGood.add(bad);
+                            dataMap.put(bad, lastValue.get(bad));
+                        } else {
+                            listBadNew.add(bad);
+                        }
+                    }
+                } else {
+                    listBadNew = listBad;
+                }
+            }
+
             log.info("读取计算点");
             List<DaPointDTO> pointCalculateList = daPointService.getMathPoint(minfreq);
-            pointValues.addAll(calculateHandle.handle(collectTime, pointCalculateList, dataMap, listGood, listBad));
+            pointValues.addAll(calculateHandle.handle(collectTime, pointCalculateList, dataMap, listGood, listBadNew));
 
             log.info("读取累计点");
             List<DaPointDTO> pointCumulateList = daPointService.getCumulatePoint(minfreq);
-            pointValues.addAll(cumulateHandle.handle(collectTime, pointCumulateList, listGood, listBad));
+            pointValues.addAll(cumulateHandle.handle(collectTime, pointCumulateList, listGood, listBadNew));
 
             log.info("存入时序库");
             influxDBService.asyncWritePointValues(pointValues);
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java
index 014e6cd..8b1376a 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java
@@ -13,6 +13,7 @@
 import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
+
 import javax.annotation.Resource;
 
 import org.springframework.beans.factory.annotation.Autowired;
@@ -22,7 +23,6 @@
 
 import java.math.BigDecimal;
 import java.util.*;
-import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
 /**
@@ -53,7 +53,7 @@
 
     public static final String regex = "[+\\-\\*/()\\&\\|\\>\\<]";
 
-    public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap,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("计算点处理开始");
@@ -83,7 +83,7 @@
         return result;
     }
 
-    private Object singleCompute(DaPointDTO dto, Map<String, Object> dataMap,List<String> listGood,List<String> listBad) {
+    private Object singleCompute(DaPointDTO dto, Map<String, Object> dataMap, List<String> listGood, List<String> listBad) {
         String expression = dto.getExpression();
         log.info("PointNo=" + dto.getPointNo() + ";SourceExpression=" + expression);
         String[] arr = expression.split(regex);
@@ -166,10 +166,10 @@
             dataMap.putAll(constantHandle.getCurrent(pointNos));
             if (dataMap.get(s) == null) {
                 log.info("计算点数据异常");
-                log.info("pointNo=" + dto.getPointNo() +";dataMap.key=" + s);
+                log.info("pointNo=" + dto.getPointNo() + ";dataMap.key=" + s);
                 return CommonConstant.BAD_VALUE;
             }
-            String valueStr =  dataMap.get(s).toString();
+            String valueStr = dataMap.get(s).toString();
             if (StrUtils.isNumeric(valueStr) && new BigDecimal(valueStr).compareTo(CommonConstant.BAD_VALUE) == 0) {
                 log.info("BAD_VALUE:" + s);
             }
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/MeasureHandle.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/MeasureHandle.java
index f5a40de..6002f45 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/MeasureHandle.java
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/MeasureHandle.java
@@ -41,10 +41,6 @@
 @Component
 public class MeasureHandle {
 
-    private BigDecimal maxValue = new BigDecimal("1000000000");
-
-    private BigDecimal minValue = new BigDecimal("0");
-
     @Resource
     private ModBusCollector modBusCollector;
 

--
Gitblit v1.9.3