hongjli
2025-05-23 26c8536e1a95b91b7763afccd3c4a7dec9e5f5dc
aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsGasPipingRouteStatServiceImpl.java
@@ -1627,13 +1627,13 @@
            for (String timePoint : timePoints) {
                Map<String, Object> pointData = new HashMap<>();
                pointData.put("planDay", timePoint);
                pointData.put("designTimes", 0); // 设计工时暂时给0
                
                // 获取该时间点的需求工时,如果不存在则设为0
                BigDecimal requireTimes = timeMap.getOrDefault(timePoint, BigDecimal.ZERO);
                pointData.put("requireTimes", requireTimes);
                
                // 计算产能负荷(暂时为0)
                // 设计工时和产能负荷稍后计算
                pointData.put("designTimes", 0);
                pointData.put("capacityLoad", 0);
                
                timeDataList.add(pointData);
@@ -1644,6 +1644,160 @@
            plantTable.add(rowEntry);
        }
        // 在返回前查询设计产能数据并计算产能负荷
        for (Map<String, Object> rowEntry : plantTable) {
            for (String rowKey : rowEntry.keySet()) {
                Map<String, Object> rowDetail = (Map<String, Object>) rowEntry.get(rowKey);
                List<Map<String, Object>> timeDataList = (List<Map<String, Object>>) rowDetail.get("timeData");
                // 获取基本信息
                String plant = rowDetail.containsKey("plant") ? (String) rowDetail.get("plant") : null;
                String major = rowDetail.containsKey("major") ? (String) rowDetail.get("major") : null;
                String workshop = rowDetail.containsKey("workshop") ? (String) rowDetail.get("workshop") : null;
                // 处理不同的rowGroupBy情况
                if ("workshop".equals(rowGroupBy) && rowDetail.containsKey("processName")) {
                    // 情况1: 按workshop聚合,需要拆分processName字段
                    String processNamesStr = (String) rowDetail.get("processName");
                    String[] processNames = processNamesStr.split(";");
                    // 优化查询 - 创建缓存,按月份缓存查询结果
                    // Key: year-month-processName, Value: 查询结果列表
                    Map<String, List<ApsGasPipelineCapacityPlan>> capacityPlanCache = new HashMap<>();
                    // 遍历每个时间点
                    for (Map<String, Object> timeData : timeDataList) {
                        String planDay = (String) timeData.get("planDay");
                        BigDecimal requireTimes = new BigDecimal(timeData.get("requireTimes").toString());
                        BigDecimal totalDesignTimes = BigDecimal.ZERO;
                        // 拆分年月日
                        String[] dateParts = planDay.split("-");
                        String year = dateParts[0];
                        String month = dateParts[1];
                        // 统一month格式为整数字符串,去掉前导零
                        month = String.valueOf(Integer.parseInt(month));
                        String yearMonth = year + "-" + month;
                        // 累加多个工序的设计产能
                        for (String processName : processNames) {
                            // 构建缓存键
                            String cacheKey = yearMonth + "-" + processName.trim();
                            // 从缓存获取或查询数据
                            List<ApsGasPipelineCapacityPlan> capacityPlans;
                            if (capacityPlanCache.containsKey(cacheKey)) {
                                capacityPlans = capacityPlanCache.get(cacheKey);
                            } else {
                                // 使用专用查询方法查询设计产能数据
                                // 按文档要求:根据多个process_name和major、plant去aps_gas_pipeline_capacity_plan表中查询
                                capacityPlans = apsGasPipelineCapacityPlanMapper.selectDesignCapacityForInterface2(
                                        processName.trim(), year, month, major, plant);
                                // 将结果存入缓存
                                capacityPlanCache.put(cacheKey, capacityPlans);
                            }
                            // 累加设计产能值
                            for (ApsGasPipelineCapacityPlan plan : capacityPlans) {
                                if ("day".equalsIgnoreCase(timeGranularity)) {
                                    // 日粒度使用日产出数量
                                    if (plan.getDayProduceAllNum() != null) {
                                        totalDesignTimes = totalDesignTimes.add(plan.getDayProduceAllNum());
                                    }
                                } else {
                                    // 月粒度使用月产出总数量
                                    if (plan.getMonthProduceAllNum() != null) {
                                        totalDesignTimes = totalDesignTimes.add(plan.getMonthProduceAllNum());
                                    }
                                }
                            }
                        }
                        // 更新设计工时
                        timeData.put("designTimes", totalDesignTimes);
                        // 计算产能负荷 = 需求产能/设计产能×100%
                        if (totalDesignTimes.compareTo(BigDecimal.ZERO) > 0) {
                            BigDecimal capacityLoad = requireTimes
                                    .divide(totalDesignTimes, 2, RoundingMode.HALF_UP)
                                    .multiply(new BigDecimal(100));
                            timeData.put("capacityLoad", capacityLoad);
                        } else {
                            timeData.put("capacityLoad", 0);
                        }
                    }
                } else {
                    // 情况2: 按processName或其他字段聚合
                    String processName = rowDetail.containsKey("processName") ?
                            (String) rowDetail.get("processName") : rowKey;
                    // 优化查询 - 创建缓存,按月份缓存查询结果
                    Map<String, List<ApsGasPipelineCapacityPlan>> capacityPlanCache = new HashMap<>();
                    // 遍历每个时间点
                    for (Map<String, Object> timeData : timeDataList) {
                        String planDay = (String) timeData.get("planDay");
                        BigDecimal requireTimes = new BigDecimal(timeData.get("requireTimes").toString());
                        // 拆分年月日
                        String[] dateParts = planDay.split("-");
                        String year = dateParts[0];
                        String month = dateParts[1];
                        // 统一month格式为整数字符串,去掉前导零
                        month = String.valueOf(Integer.parseInt(month));
                        String yearMonth = year + "-" + month;
                        // 构建缓存键
                        String cacheKey = yearMonth + "-" + processName;
                        // 从缓存获取或查询数据
                        List<ApsGasPipelineCapacityPlan> capacityPlans;
                        if (capacityPlanCache.containsKey(cacheKey)) {
                            capacityPlans = capacityPlanCache.get(cacheKey);
                        } else {
                            // 使用专用查询方法查询设计产能数据
                            // 按文档要求:根据process_name、major、plant去aps_gas_pipeline_capacity_plan表中查询
                            capacityPlans = apsGasPipelineCapacityPlanMapper.selectDesignCapacityForInterface2(
                                    processName, year, month, major, plant);
                            // 将结果存入缓存
                            capacityPlanCache.put(cacheKey, capacityPlans);
                        }
                        // 累加设计产能值
                        BigDecimal totalDesignTimes = BigDecimal.ZERO;
                        for (ApsGasPipelineCapacityPlan plan : capacityPlans) {
                            if ("day".equalsIgnoreCase(timeGranularity)) {
                                // 日粒度使用日产出数量
                                if (plan.getDayProduceAllNum() != null) {
                                    totalDesignTimes = totalDesignTimes.add(plan.getDayProduceAllNum());
                                }
                            } else {
                                // 月粒度使用月产出总数量
                                if (plan.getMonthProduceAllNum() != null) {
                                    totalDesignTimes = totalDesignTimes.add(plan.getMonthProduceAllNum());
                                }
                            }
                        }
                        // 更新设计工时
                        timeData.put("designTimes", totalDesignTimes);
                        // 计算产能负荷 = 需求产能/设计产能×100%
                        if (totalDesignTimes.compareTo(BigDecimal.ZERO) > 0) {
                            BigDecimal capacityLoad = requireTimes
                                    .divide(totalDesignTimes, 2, RoundingMode.HALF_UP)
                                    .multiply(new BigDecimal(100));
                            timeData.put("capacityLoad", capacityLoad);
                        } else {
                            timeData.put("capacityLoad", 0);
                        }
                    }
                }
            }
        }
        result.put("plantTable", plantTable);
        result.put("timePoints", timePoints);
        result.put("rowGroupBy", rowGroupBy);