From 22ffe1f084dac84d56c18480fe1d65843b28fe31 Mon Sep 17 00:00:00 2001
From: dongyukun <1208714201@qq.com>
Date: 星期三, 07 五月 2025 17:58:18 +0800
Subject: [PATCH] 归档过滤-2

---
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/arc/service/impl/ArcDataServiceImpl.java |   47 ++++++++++++++++++++++-------------------------
 1 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/arc/service/impl/ArcDataServiceImpl.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/arc/service/impl/ArcDataServiceImpl.java
index b36fc35..cef38d6 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/arc/service/impl/ArcDataServiceImpl.java
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/arc/service/impl/ArcDataServiceImpl.java
@@ -21,6 +21,7 @@
 import java.math.BigDecimal;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.stream.Collectors;
 
 
 @Service
@@ -68,7 +69,7 @@
                     arcDataEntity.setArcValue(value);
                     arcDataDao.delete(new LambdaQueryWrapperX<ArcDataEntity>()
                             .eq(ArcDataEntity::getArcId, arcDataEntity.getArcId())
-                            .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcId())
+                            .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcTime())
                     );
                     insert(arcDataEntity);
                     log.debug("归档完成,point:"+item.getPoint());
@@ -101,7 +102,7 @@
                     arcDataEntity.setArcValue(value);
                     arcDataDao.delete(new LambdaQueryWrapperX<ArcDataEntity>()
                             .eq(ArcDataEntity::getArcId, arcDataEntity.getArcId())
-                            .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcId())
+                            .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcTime())
                     );
                     insert(arcDataEntity);
                     log.debug("归档完成,point:"+item.getPoint());
@@ -136,7 +137,7 @@
                     arcDataEntity.setCreateTime(new Date());
                     arcDataDao.delete(new LambdaQueryWrapperX<ArcDataEntity>()
                             .eq(ArcDataEntity::getArcId, arcDataEntity.getArcId())
-                            .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcId())
+                            .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcTime())
                     );
                     insert(arcDataEntity);
                     log.debug("归档完成,point:"+item.getPoint());
@@ -145,23 +146,23 @@
             case MONTH:
                 //查询对应类型的归档设置列表
                 List<ArcSettingEntity> arcMonthList = arcPointSettingService.list(params);
+                Date endTime4 = calendar.getTime();
+                calendar.add(Calendar.MONTH, -1);
+                Date startTime4 = calendar.getTime();
                 //遍历列表
                 arcMonthList.forEach(item -> {
                     log.debug("开始归档,point:"+item.getPoint());
-                    //获取开始时间
-                    calendar.add(Calendar.MONTH, -1);
-                    Date startTime = calendar.getTime();
                     //通过point编号查询数据
                     ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO();
                     queryDto.setPointNo(item.getPoint());
-                    queryDto.setStart(startTime);
-                    queryDto.setEnd(new Date());
+                    queryDto.setStart(startTime4);
+                    queryDto.setEnd(endTime4);
                     List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDto);
                     //判断根据计算类型计算
                     BigDecimal value = calculate(item.getCalculate(), valueList);
                     //归档
                     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
-                    String arcTime = dateFormat.format(startTime);
+                    String arcTime = dateFormat.format(startTime4);
                     ArcDataEntity arcDataEntity = new ArcDataEntity();
                     arcDataEntity.setId(String.valueOf(new Date().getTime()));
                     arcDataEntity.setArcTime(arcTime);
@@ -171,7 +172,7 @@
                     arcDataEntity.setCreateTime(new Date());
                     arcDataDao.delete(new LambdaQueryWrapperX<ArcDataEntity>()
                             .eq(ArcDataEntity::getArcId, arcDataEntity.getArcId())
-                            .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcId())
+                            .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcTime())
                     );
                     insert(arcDataEntity);
                     log.debug("归档完成,point:"+item.getPoint());
@@ -180,24 +181,23 @@
             case YEAR:
                 //查询对应类型的归档设置列表
                 List<ArcSettingEntity> arcYearList = arcPointSettingService.list(params);
+                Date endTime5 = calendar.getTime();
+                calendar.add(Calendar.YEAR, -1);
+                Date startTime5 = calendar.getTime();
                 //遍历列表
                 arcYearList.forEach(item -> {
                     log.debug("开始归档,point:"+item.getPoint());
-                    //获取开始时间
-                    //Calendar calendar = Calendar.getInstance();
-                    calendar.add(Calendar.YEAR, -1);
-                    Date startTime = calendar.getTime();
                     //通过point编号查询数据
                     ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO();
                     queryDto.setPointNo(item.getPoint());
-                    queryDto.setStart(startTime);
-                    queryDto.setEnd(new Date());
+                    queryDto.setStart(startTime5);
+                    queryDto.setEnd(endTime5);
                     List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDto);
                     //判断根据计算类型计算
                     BigDecimal value = calculate(item.getCalculate(), valueList);
                     //归档
                     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
-                    String arcTime = dateFormat.format(startTime);
+                    String arcTime = dateFormat.format(startTime5);
                     ArcDataEntity arcDataEntity = new ArcDataEntity();
                     arcDataEntity.setId(String.valueOf(new Date().getTime()));
                     arcDataEntity.setArcTime(arcTime);
@@ -207,7 +207,7 @@
                     arcDataEntity.setCreateTime(new Date());
                     arcDataDao.delete(new LambdaQueryWrapperX<ArcDataEntity>()
                             .eq(ArcDataEntity::getArcId, arcDataEntity.getArcId())
-                            .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcId())
+                            .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcTime())
                     );
                     insert(arcDataEntity);
                     log.debug("归档完成,point:"+item.getPoint());
@@ -218,6 +218,7 @@
     }
 
     private BigDecimal calculate(String calculate, List<ApiPointValueDTO> valueList) {
+        valueList = valueList.stream().filter(e -> !Double.valueOf(e.getV()).equals(-2.0) ).collect(Collectors.toList());
         log.debug("开始计算");
         if (valueList == null || valueList.isEmpty()) {
             throw new IllegalArgumentException("valueList 为空");
@@ -231,9 +232,7 @@
                 value = BigDecimal.valueOf(valueList.get(valueList.size() - 1).getV());
                 break;
             case SUM:
-                for (ApiPointValueDTO dto : valueList) {
-                    value = value.add(BigDecimal.valueOf(dto.getV()));
-                }
+                value = BigDecimal.valueOf(valueList.stream().mapToDouble(ApiPointValueDTO::getV).sum());
                 break;
             case DIFF:
                 if (valueList.size() < 2) {
@@ -242,16 +241,14 @@
                 BigDecimal prev = BigDecimal.valueOf(valueList.get(0).getV());
                 for (int i = 1; i < valueList.size(); i++) {
                     BigDecimal curr = BigDecimal.valueOf(valueList.get(i).getV());
-                    if(curr.compareTo(prev) > 0) {
+                    if(curr.compareTo(prev) > 0 ) {
                         value = value.add(curr.subtract(prev));
                     }
                     prev = curr;
                 }
                 break;
             case AVG:
-                for (ApiPointValueDTO dto : valueList) {
-                    value = value.add(BigDecimal.valueOf(dto.getV()));
-                }
+                value = BigDecimal.valueOf(valueList.stream().mapToDouble(ApiPointValueDTO::getV).sum());
                 value = value.divide(BigDecimal.valueOf(valueList.size()), 2, BigDecimal.ROUND_HALF_UP);
                 break;
             default:

--
Gitblit v1.9.3