Browse Source

pms 瑞恒 井累计 年累计 逻辑调整

zhangcl 3 days ago
parent
commit
75aed9e321

+ 105 - 20
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/controller/admin/iotrhdailyreport/IotRhDailyReportController.java

@@ -723,6 +723,8 @@ public class IotRhDailyReportController {
         // 搬迁安装天数
         Map<Long, BigDecimal> relocationDaysPair = new HashMap<>();
 
+        // key部门id    value 部门相关日报列表
+        Map<Long, List<IotRhDailyReportDO>> deptReportsPair = new HashMap<>();
         // key队伍id   value 队伍当年累计注气量
         Map<Long, BigDecimal> yearGasInjectionPair = new HashMap<>();
         // key队伍id   value 队伍当年累计注水量
@@ -734,13 +736,16 @@ public class IotRhDailyReportController {
 
         // key任务井id    value 井累计注气量
         Map<Long, BigDecimal> wellGasInjectionPair = new HashMap<>();
-        // key任务井id    value 井累计注水量
+        // key任务井id    value 井相关日报列表
+        Map<Long, List<IotRhDailyReportDO>> wellReportsPair = new HashMap<>();
+        // key日报id    value 井累计注水量
         Map<Long, BigDecimal> wellWaterInjectionPair = new HashMap<>();
         // key任务井id    value 井累计用电量
         Map<Long, BigDecimal> wellPowerPair = new HashMap<>();
         // key任务井id    value 井累计油耗
         Map<Long, BigDecimal> wellFuelPair = new HashMap<>();
 
+        // 井累计 注气量 日报当天日期及之前所有日报累计注气量之和
         // 查询所有井日报数据
         IotRhDailyReportPageReqVO currentTaskReqVO = new IotRhDailyReportPageReqVO();
         currentTaskReqVO.setTaskIds(convertList(reports, IotRhDailyReportDO::getTaskId));
@@ -748,21 +753,31 @@ public class IotRhDailyReportController {
         if (CollUtil.isNotEmpty(taskDailyReports)) {
             taskDailyReports.forEach(report -> {
                 // 注气量
-                if (wellGasInjectionPair.containsKey(report.getTaskId())) {
+                /* if (wellGasInjectionPair.containsKey(report.getTaskId())) {
                     BigDecimal tempGasInjection = wellGasInjectionPair.get(report.getTaskId());
                     BigDecimal tempResult = tempGasInjection.add(report.getDailyGasInjection());
                     wellGasInjectionPair.put(report.getTaskId(), tempResult);
                 } else {
                     wellGasInjectionPair.put(report.getTaskId(), report.getDailyGasInjection());
+                } */
+                // 井相关的所有日报列表
+                if (wellReportsPair.containsKey(report.getTaskId())) {
+                    List<IotRhDailyReportDO> tempReports = wellReportsPair.get(report.getTaskId());
+                    tempReports.add(report);
+                    wellReportsPair.put(report.getTaskId(), tempReports);
+                } else {
+                    List<IotRhDailyReportDO> tempReports = new ArrayList<>();
+                    tempReports.add(report);
+                    wellReportsPair.put(report.getTaskId(), tempReports);
                 }
                 // 注水量
-                if (wellWaterInjectionPair.containsKey(report.getTaskId())) {
+                /* if (wellWaterInjectionPair.containsKey(report.getTaskId())) {
                     BigDecimal tempWaterInjection = wellWaterInjectionPair.get(report.getTaskId());
                     BigDecimal tempResult = tempWaterInjection.add(report.getDailyWaterInjection());
                     wellWaterInjectionPair.put(report.getTaskId(), tempResult);
                 } else {
                     wellWaterInjectionPair.put(report.getTaskId(), report.getDailyWaterInjection());
-                }
+                } */
                 // 电量
                 if (wellPowerPair.containsKey(report.getTaskId())) {
                     BigDecimal tempPower = wellPowerPair.get(report.getTaskId());
@@ -782,6 +797,41 @@ public class IotRhDailyReportController {
             });
         }
 
+        // 基于 wellReportsPair 计算每个日报的截止到创建时间的累计注气量
+        Map<Long, BigDecimal> reportCumulativeGasMap = new HashMap<>();
+        if (CollUtil.isNotEmpty(wellReportsPair)) {
+            for (Map.Entry<Long, List<IotRhDailyReportDO>> entry : wellReportsPair.entrySet()) {
+                List<IotRhDailyReportDO> reportList = entry.getValue();
+                // 按 createTime 升序排序(确保时间顺序)
+                reportList.sort(Comparator.comparing(IotRhDailyReportDO::getCreateTime));
+                BigDecimal cumulativeSum = BigDecimal.ZERO;
+                // 累计注水量
+                BigDecimal cumulativeSumWater = BigDecimal.ZERO;
+                // 累计用电量
+                BigDecimal cumulativeSumPower = BigDecimal.ZERO;
+                // 累计油耗
+                BigDecimal cumulativeSumFuel = BigDecimal.ZERO;
+                for (IotRhDailyReportDO report : reportList) {
+                    // 累加当前日报的注气量
+                    cumulativeSum = cumulativeSum.add(Optional.ofNullable(report.getDailyGasInjection()).orElse(BigDecimal.ZERO));
+                    // 存入 map,key=日报ID,value=截止到当前日报的累计注气量
+                    reportCumulativeGasMap.put(report.getId(), cumulativeSum);
+                    // 累计注水量
+                    cumulativeSumWater = cumulativeSumWater.add(Optional.ofNullable(report.getDailyWaterInjection()).orElse(BigDecimal.ZERO));
+                    // 存入 map,key=日报ID,value=截止到当前日报的累计注水量
+                    wellWaterInjectionPair.put(report.getId(), cumulativeSumWater);
+                    // 累计用电量
+                    cumulativeSumPower = cumulativeSumPower.add(Optional.ofNullable(report.getDailyPowerUsage()).orElse(BigDecimal.ZERO));
+                    // 存入 map,key=日报ID,value=截止到当前日报的累计用电量
+                    wellPowerPair.put(report.getId(), cumulativeSumPower);
+                    // 累计油耗
+                    cumulativeSumFuel = cumulativeSumFuel.add(Optional.ofNullable(report.getDailyOilUsage()).orElse(BigDecimal.ZERO));
+                    // 存入 map,key=日报ID,value=截止到当前日报的累计油耗
+                    wellFuelPair.put(report.getId(), cumulativeSumFuel);
+                }
+            }
+        }
+
         // 根据传递的时间筛选获取要查询的 yyyy
         if (ObjUtil.isNotEmpty(pageReqVO.getCreateTime())) {
             // 只有传递了 时间查询条件 才会查询 当年累计的工作量数据
@@ -795,39 +845,74 @@ public class IotRhDailyReportController {
                 List<IotRhDailyReportDO> dailyReports = iotRhDailyReportMapper.dailyReports(reqVO);
                 if (CollUtil.isNotEmpty(dailyReports)) {
                     dailyReports.forEach(report -> {
+                        // 组装每个部门的日报列表
+                        if (deptReportsPair.containsKey(report.getDeptId())) {
+                            List<IotRhDailyReportDO> tempReports = deptReportsPair.get(report.getDeptId());
+                            tempReports.add(report);
+                            deptReportsPair.put(report.getDeptId(), tempReports);
+                        } else {
+                            List<IotRhDailyReportDO> tempReports = new ArrayList<>();
+                            tempReports.add(report);
+                            deptReportsPair.put(report.getDeptId(), tempReports);
+                        }
                         // 注气量
-                        if (yearGasInjectionPair.containsKey(report.getDeptId())) {
+                        /* if (yearGasInjectionPair.containsKey(report.getDeptId())) {
                             BigDecimal tempGasInjection = yearGasInjectionPair.get(report.getDeptId());
                             BigDecimal tempResult = tempGasInjection.add(report.getDailyGasInjection());
                             yearGasInjectionPair.put(report.getDeptId(), tempResult);
                         } else {
                             yearGasInjectionPair.put(report.getDeptId(), report.getDailyGasInjection());
-                        }
+                        } */
                         // 注水量
-                        if (yearWaterInjectionPair.containsKey(report.getDeptId())) {
+                        /* if (yearWaterInjectionPair.containsKey(report.getDeptId())) {
                             BigDecimal tempWaterInjection = yearWaterInjectionPair.get(report.getDeptId());
                             BigDecimal tempResult = tempWaterInjection.add(report.getDailyWaterInjection());
                             yearWaterInjectionPair.put(report.getDeptId(), tempResult);
                         } else {
                             yearWaterInjectionPair.put(report.getDeptId(), report.getDailyWaterInjection());
-                        }
+                        } */
                         // 电量
-                        if (yearPowerPair.containsKey(report.getDeptId())) {
+                        /* if (yearPowerPair.containsKey(report.getDeptId())) {
                             BigDecimal tempPower = yearPowerPair.get(report.getDeptId());
                             BigDecimal tempResult = tempPower.add(report.getDailyPowerUsage());
                             yearPowerPair.put(report.getDeptId(), tempResult);
                         } else {
                             yearPowerPair.put(report.getDeptId(), report.getDailyPowerUsage());
-                        }
+                        } */
                         // 油耗
-                        if (yearFuelPair.containsKey(report.getDeptId())) {
+                        /* if (yearFuelPair.containsKey(report.getDeptId())) {
                             BigDecimal tempFuel = yearFuelPair.get(report.getDeptId());
                             BigDecimal tempResult = tempFuel.add(report.getDailyOilUsage());
                             yearFuelPair.put(report.getDeptId(), tempResult);
                         } else {
                             yearFuelPair.put(report.getDeptId(), report.getDailyOilUsage());
-                        }
+                        } */
                     });
+
+                    // 2. 对每个部门的日报列表按 createTime 排序,计算滚动累计
+                    for (Map.Entry<Long, List<IotRhDailyReportDO>> entry : deptReportsPair.entrySet()) {
+                        List<IotRhDailyReportDO> deptReports = entry.getValue();
+                        deptReports.sort(Comparator.comparing(IotRhDailyReportDO::getCreateTime));
+                        // 累计注气量
+                        BigDecimal cumGas = BigDecimal.ZERO;
+                        // 累计注水量
+                        BigDecimal cumWater = BigDecimal.ZERO;
+                        // 累计用电量
+                        BigDecimal cumPower = BigDecimal.ZERO;
+                        // 累计油耗
+                        BigDecimal cumFuel = BigDecimal.ZERO;
+                        for (IotRhDailyReportDO report : deptReports) {
+                            cumGas = cumGas.add(Optional.ofNullable(report.getDailyGasInjection()).orElse(BigDecimal.ZERO));
+                            cumWater = cumWater.add(Optional.ofNullable(report.getDailyWaterInjection()).orElse(BigDecimal.ZERO));
+                            cumPower = cumPower.add(Optional.ofNullable(report.getDailyPowerUsage()).orElse(BigDecimal.ZERO));
+                            cumFuel = cumFuel.add(Optional.ofNullable(report.getDailyOilUsage()).orElse(BigDecimal.ZERO));
+                            // 存入以日报ID为键的Map
+                            yearGasInjectionPair.put(report.getId(), cumGas);
+                            yearWaterInjectionPair.put(report.getId(), cumWater);
+                            yearPowerPair.put(report.getId(), cumPower);
+                            yearFuelPair.put(report.getId(), cumFuel);
+                        }
+                    }
                 }
             }
         }
@@ -961,41 +1046,41 @@ public class IotRhDailyReportController {
             // 2.1 拼接项目部信息
             findAndThen(projectDeptNamePair, reportVO.getDeptId(), projectDeptName -> reportVO.setProjectDeptName(projectDeptName));
             // 队伍当年累计注气量
-            findAndThen(yearGasInjectionPair, reportVO.getDeptId(), gasInjection -> {
+            findAndThen(yearGasInjectionPair, reportVO.getId(), gasInjection -> {
                 // 转换单位为 万方
                 BigDecimal gasInjectionWanFang = gasInjection
                         .divide(BigDecimal.valueOf(10000), 4, RoundingMode.HALF_UP);
                 reportVO.setYearTotalGasInjection(gasInjectionWanFang);
             });
             // 队伍当年累计注水量
-            findAndThen(yearWaterInjectionPair, reportVO.getDeptId(), waterInjection -> reportVO.setYearTotalWaterInjection(waterInjection));
+            findAndThen(yearWaterInjectionPair, reportVO.getId(), waterInjection -> reportVO.setYearTotalWaterInjection(waterInjection));
             // 队伍当年累计用电量
-            findAndThen(yearPowerPair, reportVO.getDeptId(), power -> {
+            findAndThen(yearPowerPair, reportVO.getId(), power -> {
                 // 换算单位为 MWh
                 BigDecimal powerW = power
                         .divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
                 reportVO.setYearTotalPower(powerW);
             });
             // 队伍当年累计油耗
-            findAndThen(yearFuelPair, reportVO.getDeptId(), fuel -> reportVO.setYearTotalFuel(fuel));
+            findAndThen(yearFuelPair, reportVO.getId(), fuel -> reportVO.setYearTotalFuel(fuel));
             // 井当年累计注气量
-            findAndThen(wellGasInjectionPair, reportVO.getTaskId(), gasInjection -> {
+            findAndThen(reportCumulativeGasMap, reportVO.getId(), gasInjection -> {
                 // 转换单位为 万方
                 BigDecimal gasInjectionWanFang = gasInjection
                         .divide(BigDecimal.valueOf(10000), 4, RoundingMode.HALF_UP);
                 reportVO.setWellTotalGasInjection(gasInjectionWanFang);
             });
             // 井当年累计注水量
-            findAndThen(wellWaterInjectionPair, reportVO.getTaskId(), waterInjection -> reportVO.setWellTotalWaterInjection(waterInjection));
+            findAndThen(wellWaterInjectionPair, reportVO.getId(), waterInjection -> reportVO.setWellTotalWaterInjection(waterInjection));
             // 井当年累计用电量
-            findAndThen(wellPowerPair, reportVO.getTaskId(), power -> {
+            findAndThen(wellPowerPair, reportVO.getId(), power -> {
                 // 换算单位为 MWh
                 BigDecimal powerW = power
                         .divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
                 reportVO.setWellTotalPower(powerW);
             });
             // 井当年累计油耗
-            findAndThen(wellFuelPair, reportVO.getTaskId(), fuel -> reportVO.setWellTotalFuel(fuel));
+            findAndThen(wellFuelPair, reportVO.getId(), fuel -> reportVO.setWellTotalFuel(fuel));
             // 2.2 日报关联的项目信息
             findAndThen(projectPair, reportVO.getProjectId(), contractName -> reportVO.setContractName(contractName));
             // 2.3 日报关联的任务井号