Selaa lähdekoodia

处理生产消耗数据统计时,数值跟占比出现负数的情况

lingpeng.li 6 kuukautta sitten
vanhempi
sitoutus
63e0efe301

+ 429 - 124
zgztBus/jeecg-module-lesm/src/main/java/org/jeecg/modules/productionConsume/service/impl/ProductionConsumeServiceImpl.java

@@ -10,7 +10,6 @@ import lombok.extern.slf4j.Slf4j;
 import org.jeecg.common.util.oConvertUtils;
 import org.jeecg.modules.deviceLesm.mapper.DeviceInformationMapper;
 import org.jeecg.modules.deviceLesm.mapper.DeviceRegionMapper;
-import org.jeecg.modules.fpgLeanModel.entity.ElectricityControlReport;
 import org.jeecg.modules.fpgLeanModel.entity.ExportQueryDTO;
 import org.jeecg.modules.productionConsume.entity.AggregatedData;
 import org.jeecg.modules.productionConsume.entity.ProductionConsume;
@@ -145,16 +144,29 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems.isEmpty()) {
                             ProductionConsume first = rangeItems.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff = totalActivePowerDiff.add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems.get(rangeItems.size() - 1); // 最后一条数据
-
-                                totalActivePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+                                // 从最后一条记录开始向前遍历
+                                for (int i = rangeItems.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems.get(i); // 当前的 "最后一条" 数据
+                                    BigDecimal diff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    // 检查差值是否满足条件(大于等于0 且小于等于 100000000000)
+                                    if (diff.compareTo(BigDecimal.ZERO) >= 0 && diff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        totalActivePowerDiff = diff; // 找到合法差值,直接设置
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        totalActivePowerDiff = totalActivePowerDiff.add(BigDecimal.ZERO);
+                                    }
+                                }
                             }
-
                         }
+
                         if (totalActivePowerDiff.compareTo(BigDecimal.ZERO) != 0) {
                             productionConsumeReport.setValue(totalActivePowerDiff); // 设置累加的差值
                         } else {
@@ -184,20 +196,38 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                                 .sorted(Comparator.comparing(ProductionConsume::getCreateTime))
                                 .collect(Collectors.toList());
 
+
                         if (!rangeItems.isEmpty()) {
                             ProductionConsume first = rangeItems.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems.get(rangeItems.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
+
                     } catch (ParseException e) {
                         log.error("日期解析错误", e);
                     }
@@ -242,17 +272,34 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems.isEmpty()) {
                             ProductionConsume first = rangeItems.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems.get(rangeItems.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
+
                     } catch (ParseException e) {
                         log.error("日期解析错误", e);
                     }
@@ -293,21 +340,39 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                                 .sorted(Comparator.comparing(ProductionConsume::getCreateTime))
                                 .collect(Collectors.toList());
 
+
                         if (!rangeItems1.isEmpty()) {
                             ProductionConsume first = rangeItems1.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems1.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems1.get(rangeItems1.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems1.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems1.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
 
+
                         // 第二部分:date + 23:00:00 到 date + 23:59:59
                         Date startTime2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date + " " + config.getStartTime());
                         Date endTime2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date + " 23:59:59");
@@ -320,16 +385,32 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems2.isEmpty()) {
                             ProductionConsume first = rangeItems2.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems2.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems2.get(rangeItems2.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems2.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems2.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
                     } catch (ParseException e) {
                         log.error("日期解析错误", e);
@@ -495,20 +576,32 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         productionConsumeReport.setKey1("尖");
                         productionConsumeReport.setOrderIndex(1);
 
-
                         if (!rangeItems.isEmpty()) {
                             ProductionConsume first = rangeItems.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff = totalActivePowerDiff.add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems.get(rangeItems.size() - 1); // 最后一条数据
-
-                                totalActivePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+                                // 从最后一条记录开始向前遍历
+                                for (int i = rangeItems.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems.get(i); // 当前的 "最后一条" 数据
+                                    BigDecimal diff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    // 检查差值是否满足条件(大于等于0 且小于等于 100000000000)
+                                    if (diff.compareTo(BigDecimal.ZERO) >= 0 && diff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        totalActivePowerDiff = diff; // 找到合法差值,直接设置
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        totalActivePowerDiff = totalActivePowerDiff.add(BigDecimal.ZERO);
+                                    }
+                                }
                             }
-
                         }
+
                         if (totalActivePowerDiff.compareTo(BigDecimal.ZERO) != 0) {
                             productionConsumeReport.setValue(totalActivePowerDiff); // 设置累加的差值
                         } else {
@@ -541,16 +634,32 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems.isEmpty()) {
                             ProductionConsume first = rangeItems.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems.get(rangeItems.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
                     } catch (ParseException e) {
                         log.error("日期解析错误", e);
@@ -595,17 +704,34 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems.isEmpty()) {
                             ProductionConsume first = rangeItems.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems.get(rangeItems.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
+
                     } catch (ParseException e) {
                         log.error("日期解析错误", e);
                     }
@@ -648,16 +774,32 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems1.isEmpty()) {
                             ProductionConsume first = rangeItems1.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems1.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems1.get(rangeItems1.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems1.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems1.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
 
                         // 第二部分:date + 23:00:00 到 date + 23:59:59
@@ -672,16 +814,32 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems2.isEmpty()) {
                             ProductionConsume first = rangeItems2.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems2.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems2.get(rangeItems2.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems2.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems2.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
                     } catch (ParseException e) {
                         log.error("日期解析错误", e);
@@ -708,7 +866,6 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
         }
 
 
-
         // 按月份、deviceTitle、regionTitle 和 key1 分组并累加 value,同时保留 orderIndex
         Map<String, Map<String, Map<String, Map<String, AggregatedData>>>> groupedResult = finalResult.stream()
                 .collect(Collectors.groupingBy(
@@ -767,7 +924,6 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
         });
 
 
-
         // 按设备和日期分组
         Map<String, Map<String, List<ProductionConsumeReport>>> groupedByDeviceAndDate = monthlyAggregatedResult.stream()
                 .collect(Collectors.groupingBy(ProductionConsumeReport::getDeviceTitle,
@@ -904,15 +1060,27 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems.isEmpty()) {
                             ProductionConsume first = rangeItems.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff = totalActivePowerDiff.add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems.get(rangeItems.size() - 1); // 最后一条数据
-
-                                totalActivePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+                                // 从最后一条记录开始向前遍历
+                                for (int i = rangeItems.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems.get(i); // 当前的 "最后一条" 数据
+                                    BigDecimal diff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    // 检查差值是否满足条件(大于等于0 且小于等于 100000000000)
+                                    if (diff.compareTo(BigDecimal.ZERO) >= 0 && diff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        totalActivePowerDiff = diff; // 找到合法差值,直接设置
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        totalActivePowerDiff = totalActivePowerDiff.add(BigDecimal.ZERO);
+                                    }
+                                }
                             }
-
                         }
                         if (totalActivePowerDiff.compareTo(BigDecimal.ZERO) != 0) {
                             productionConsumeReport.setValue(totalActivePowerDiff); // 设置累加的差值
@@ -946,16 +1114,32 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems.isEmpty()) {
                             ProductionConsume first = rangeItems.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems.get(rangeItems.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
                     } catch (ParseException e) {
                         log.error("日期解析错误", e);
@@ -1001,16 +1185,32 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems.isEmpty()) {
                             ProductionConsume first = rangeItems.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems.get(rangeItems.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
                     } catch (ParseException e) {
                         log.error("日期解析错误", e);
@@ -1055,16 +1255,32 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems1.isEmpty()) {
                             ProductionConsume first = rangeItems1.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems1.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems1.get(rangeItems1.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems1.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems1.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
 
                         // 第二部分:date + 23:00:00 到 date + 23:59:59
@@ -1079,16 +1295,32 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems2.isEmpty()) {
                             ProductionConsume first = rangeItems2.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems2.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems2.get(rangeItems2.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems2.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems2.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
                     } catch (ParseException e) {
                         log.error("日期解析错误", e);
@@ -1292,15 +1524,27 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems.isEmpty()) {
                             ProductionConsume first = rangeItems.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff = totalActivePowerDiff.add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems.get(rangeItems.size() - 1); // 最后一条数据
-
-                                totalActivePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+                                // 从最后一条记录开始向前遍历
+                                for (int i = rangeItems.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems.get(i); // 当前的 "最后一条" 数据
+                                    BigDecimal diff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    // 检查差值是否满足条件(大于等于0 且小于等于 100000000000)
+                                    if (diff.compareTo(BigDecimal.ZERO) >= 0 && diff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        totalActivePowerDiff = diff; // 找到合法差值,直接设置
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        totalActivePowerDiff = totalActivePowerDiff.add(BigDecimal.ZERO);
+                                    }
+                                }
                             }
-
                         }
                         if (totalActivePowerDiff.compareTo(BigDecimal.ZERO) != 0) {
                             productionConsumeReport.setValue(totalActivePowerDiff); // 设置累加的差值
@@ -1334,16 +1578,32 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems.isEmpty()) {
                             ProductionConsume first = rangeItems.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems.get(rangeItems.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
                     } catch (ParseException e) {
                         log.error("日期解析错误", e);
@@ -1388,16 +1648,32 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems.isEmpty()) {
                             ProductionConsume first = rangeItems.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems.get(rangeItems.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
                     } catch (ParseException e) {
                         log.error("日期解析错误", e);
@@ -1441,16 +1717,32 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems1.isEmpty()) {
                             ProductionConsume first = rangeItems1.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems1.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems1.get(rangeItems1.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems1.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems1.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
 
                         // 第二部分:date + 23:00:00 到 date + 23:59:59
@@ -1465,16 +1757,32 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
                         if (!rangeItems2.isEmpty()) {
                             ProductionConsume first = rangeItems2.get(0); // 第一条数据
 
-                            // 如果只有一条数据,那差值为0
                             if (rangeItems2.size() == 1) {
+                                // 如果只有一条数据,那差值为 0
                                 totalActivePowerDiff[0] = totalActivePowerDiff[0].add(BigDecimal.ZERO);
                             } else {
-                                ProductionConsume last = rangeItems2.get(rangeItems2.size() - 1); // 最后一条数据
-
-                                BigDecimal activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
-                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff); // 累加差值
+                                // 初始化差值为 0
+                                BigDecimal activePowerDiff = BigDecimal.ZERO;
+
+                                // 从最后一条记录开始向前查找
+                                for (int i = rangeItems2.size() - 1; i > 0; i--) {
+                                    ProductionConsume last = rangeItems2.get(i); // 当前记录
+                                    activePowerDiff = last.getActivePower().subtract(first.getActivePower()); // 差值计算
+
+                                    if (activePowerDiff.compareTo(BigDecimal.ZERO) >= 0 && activePowerDiff.compareTo(new BigDecimal("100000000000")) <= 0) {
+                                        // 如果找到差值大于等于 0 的记录,跳出循环
+                                        break;
+                                    }
+
+                                    // 如果循环到第一条记录仍然未找到合法差值
+                                    if (i == 1) {
+                                        activePowerDiff = BigDecimal.ZERO;
+                                    }
+                                }
+
+                                // 累加差值(合法值或 0)
+                                totalActivePowerDiff[0] = totalActivePowerDiff[0].add(activePowerDiff);
                             }
-
                         }
                     } catch (ParseException e) {
                         log.error("日期解析错误", e);
@@ -1501,9 +1809,8 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
         }
 
 
-
         Map<String, BigDecimal> dailyTotals = finalResult.stream()
-                .collect(Collectors.groupingBy(report -> report.getCreateTime().substring(0, 7)+ "_" + report.getRegionTitle(),
+                .collect(Collectors.groupingBy(report -> report.getCreateTime().substring(0, 7) + "_" + report.getRegionTitle(),
                         Collectors.mapping(ProductionConsumeReport::getValue,
                                 Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
 
@@ -1567,8 +1874,6 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
         }));
 
 
-
-
         jsonResult = JSON.parseArray(JSON.toJSONString(resultArray));
         log.info("积木报表导出总数:{}", jsonResult.size());
         result.put("data", jsonResult);