aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsGasPipingRouteStatServiceImpl.java
@@ -32,6 +32,7 @@
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.groupingBy;
@@ -251,8 +252,7 @@
        return true;
    }
    @Override
    public JSONObject getCapacityPlanData(ApsGasPipingRouteStat apsGasPipingRouteStat) {
    public JSONObject getCapacityPlanDataBackup(ApsGasPipingRouteStat apsGasPipingRouteStat) {
        JSONObject result = new JSONObject();
        YearMonth yearMonth = YearMonth.parse(apsGasPipingRouteStat.getSearchEndDate());
        int daysInMonth = yearMonth.lengthOfMonth();
@@ -422,6 +422,291 @@
    }
    @Override
    public JSONObject getCapacityPlanData(ApsGasPipingRouteStat apsGasPipingRouteStat) {
        JSONObject result = new JSONObject();
        YearMonth yearMonth = YearMonth.parse(apsGasPipingRouteStat.getSearchEndDate());
        int daysInMonth = yearMonth.lengthOfMonth();
        LinkedHashSet<String> startPlanTimeSet = new LinkedHashSet<>();
        //工序分组统计
        LinkedHashMap<String, List<ApsResourceDateStat>> processMap = new LinkedHashMap<>();
        List<HashMap<String, List<ApsResourceDateStat>>> processList = new ArrayList<>();
        try {
            //获取标准工序名称
            ApsStandardProcess apsStandardProcess = new ApsStandardProcess();
            apsStandardProcess.setMajor(apsGasPipingRouteStat.getMajor().equals("gas")?"气柜":"管路");
            List<ApsStandardProcess> apsStandardProcessList = apsStandardProcessService.selectApsStandardProcessList(apsStandardProcess);
            apsStandardProcessList.sort((a, b)->a.getProcessName().compareTo(b.getProcessName()));
            for(ApsStandardProcess temp:apsStandardProcessList){
                processMap.put(temp.getProcessName(), new ArrayList<ApsResourceDateStat>());
            }
            //获取工序计划产能数据
            HashMap<String, ApsGasPipelineCapacityPlan> apsGasPipingPlanMap = new HashMap<>();
            ApsGasPipelineCapacityPlan searchCapacityPlan = new ApsGasPipelineCapacityPlan();
            searchCapacityPlan.setMajor(apsGasPipingRouteStat.getMajor().equals("gas")?"气柜":"管路");
            if("day".equals(apsGasPipingRouteStat.getSearchType())){
                searchCapacityPlan.setYear(yearMonth.getYear()+"");
                searchCapacityPlan.setMonth(yearMonth.getMonthValue()+"");
                for(int i=1;i<=daysInMonth;i++){
                    startPlanTimeSet.add(yearMonth +"-"+ (i<10?"0"+i:i));
                }
            }else if("month".equals(apsGasPipingRouteStat.getSearchType())){
                searchCapacityPlan.setYear(yearMonth.getYear()+"");
                YearMonth start = YearMonth.of(Integer.parseInt(apsGasPipingRouteStat.getSearchStartDate().split("-")[0]), Integer.parseInt(apsGasPipingRouteStat.getSearchStartDate().split("-")[1]));
                YearMonth end = YearMonth.of(Integer.parseInt(apsGasPipingRouteStat.getSearchEndDate().split("-")[0]), Integer.parseInt(apsGasPipingRouteStat.getSearchEndDate().split("-")[1]));
                List<String> yearMonths = getYearMonthsInRange(start, end);
                startPlanTimeSet.addAll(yearMonths);
            }
            List<ApsGasPipelineCapacityPlan> apsGasPipelineCapacityPlanList = apsGasPipelineCapacityPlanMapper.selectApsGasPipelineCapacityPlanList(searchCapacityPlan);
            apsGasPipelineCapacityPlanList.forEach(apsGasPipelineCapacityPlan -> {
                String key = apsGasPipelineCapacityPlan.getProcessName() + "-" + apsGasPipelineCapacityPlan.getOrgCode() + "-" + apsGasPipelineCapacityPlan.getYear() + "-" + (Integer.parseInt(apsGasPipelineCapacityPlan.getMonth())<10?"0"+apsGasPipelineCapacityPlan.getMonth():apsGasPipelineCapacityPlan.getMonth());
                apsGasPipingPlanMap.put(key, apsGasPipelineCapacityPlan);
            });
            //计算日产能数据
            DateTimeFormatter formatter = null;
            List<ApsGasPipingRouteStat> apsGasPipingRouteStats;
            SimpleDateFormat simpleDateFormat = null;
            apsGasPipingRouteStat.setSearchStartDate(apsGasPipingRouteStat.getSearchStartDate()+"-01 00:00:00");
            apsGasPipingRouteStat.setSearchEndDate(apsGasPipingRouteStat.getSearchEndDate()+"-"+ daysInMonth +" 23:59:59");
            if("day".equals(apsGasPipingRouteStat.getSearchType())){
                formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            }else if("month".equals(apsGasPipingRouteStat.getSearchType())){
                formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            }
            ApsGasPipingRouteStat queryStatParam = new ApsGasPipingRouteStat();
            BeanUtils.copyProperties(apsGasPipingRouteStat,queryStatParam);
            queryStatParam.setMajor("");
            apsGasPipingRouteStats = apsGasPipingRouteStatMapper.selectApsGasPipingRouteStatList(queryStatParam);
            //根据开工日进行升序排序
            apsGasPipingRouteStats.sort((a, b)->a.getProcessPlanStartDay().compareTo(b.getProcessPlanStartDay()));
            //工序开工日期
            String planStartDate = "";
            //统计所有工序对应的开工时间
            for (ApsGasPipingRouteStat apsGasPipingRouteStatTemp : apsGasPipingRouteStats) {
                if(processMap.containsKey(apsGasPipingRouteStatTemp.getProcessName())){
                    planStartDate = simpleDateFormat.format(apsGasPipingRouteStatTemp.getProcessPlanStartDay());
                    if("month".equals(apsGasPipingRouteStat.getSearchType())){
                        planStartDate = planStartDate+"-01";
                    }
                    ApsResourceDateStat apsResourceDateStat = new ApsResourceDateStat();
                    apsResourceDateStat.setPlanDay(LocalDate.parse(planStartDate, formatter));
                    apsResourceDateStat.setResourceName(apsGasPipingRouteStatTemp.getProcessName());
                    apsResourceDateStat.setPlant(apsGasPipingRouteStatTemp.getPlant());
                    //查询气柜管路产能规划表
                    String capacityKey = apsGasPipingRouteStatTemp.getProcessName() + "-" + apsGasPipingRouteStatTemp.getPlant() + "-" + planStartDate.substring(0, 7);
                    apsResourceDateStat.setDesignTimes(apsGasPipingPlanMap.get(capacityKey)!=null?apsGasPipingPlanMap.get(capacityKey).getDayProduceAllNum():new BigDecimal(0));
                    //查询料号工序产能表
                    apsResourceDateStat.setRequireTimes(apsGasPipingRouteStatTemp.getProcessTotalTime());
                    if(apsResourceDateStat.getDesignTimes().compareTo(BigDecimal.ZERO)>0){
                        apsResourceDateStat.setCapacityLoad(apsResourceDateStat.getRequireTimes()
                                .divide(apsResourceDateStat.getDesignTimes(), 2, RoundingMode.HALF_UP)
                                .multiply(new BigDecimal(100)));
                    }else{
                        apsResourceDateStat.setCapacityLoad(BigDecimal.valueOf(0));
                    }
                    List<ApsResourceDateStat> apsResourceDateStatList = processMap.get(apsGasPipingRouteStatTemp.getProcessName());
                    apsResourceDateStatList.add(apsResourceDateStat);
                    processMap.put(apsGasPipingRouteStatTemp.getProcessName(), apsResourceDateStatList);
                }
            }
            //聚合每道工序的开工时间和产能
            for (Map.Entry<String, List<ApsResourceDateStat>> entry : processMap.entrySet()) {
                String processName = entry.getKey();
                List<ApsResourceDateStat> apsResourceDateStatList = entry.getValue();
                if("day".equals(apsGasPipingRouteStat.getSearchType())) {
                    // 按天统计时保持原有逻辑,不按工厂分组
                    LinkedHashMap<String, ApsResourceDateStat> dayMap = new LinkedHashMap<>();
                    // 首先,为所有日期创建初始记录
                    for(String date : startPlanTimeSet) {
                        ApsResourceDateStat initStat = new ApsResourceDateStat();
                        initStat.setPlanDay(LocalDate.parse(date, formatter));
                        initStat.setResourceName(processName);
                        initStat.setResourceGroupName(processName);
                        initStat.setRequireTimes(new BigDecimal(0));
                        String capacityKey = processName + "-" + date.substring(0,7);
                        initStat.setDesignTimes(apsGasPipingPlanMap.get(capacityKey)!=null?apsGasPipingPlanMap.get(capacityKey).getDayProduceAllNum():new BigDecimal(0));
                        initStat.setCapacityLoad(new BigDecimal(0));
                        dayMap.put(date, initStat);
                    }
                    // 然后处理实际数据
                    for (ApsResourceDateStat apsResourceDateStat : apsResourceDateStatList) {
                        String dateKey = apsResourceDateStat.getPlanDay().toString();
                        if(dayMap.containsKey(dateKey)){
                            ApsResourceDateStat apsResourceDateStatTemp = dayMap.get(dateKey);
                            String capacityKey = processName + "-" + dateKey.substring(0,7);
                            apsResourceDateStatTemp.setDesignTimes(apsGasPipingPlanMap.get(capacityKey)!=null?apsGasPipingPlanMap.get(capacityKey).getDayProduceAllNum():new BigDecimal(0));
                            apsResourceDateStatTemp.setRequireTimes(apsResourceDateStatTemp.getRequireTimes().add(apsResourceDateStat.getRequireTimes()));
                            if(apsResourceDateStatTemp.getDesignTimes().compareTo(BigDecimal.ZERO) > 0){
                                apsResourceDateStatTemp.setCapacityLoad(apsResourceDateStatTemp.getRequireTimes()
                                        .divide(apsResourceDateStatTemp.getDesignTimes(), 2, RoundingMode.HALF_UP)
                                        .multiply(new BigDecimal(100)));
                            }else{
                                apsResourceDateStatTemp.setCapacityLoad(new BigDecimal(0));
                            }
                            apsResourceDateStatTemp.setResourceGroupName(processName);
                            apsResourceDateStatTemp.setPlanDay(apsResourceDateStat.getPlanDay());
                            dayMap.put(dateKey, apsResourceDateStatTemp);
                        }
                    }
                    List<ApsResourceDateStat> tempList = new ArrayList<>(dayMap.values());
                    HashMap<String, List<ApsResourceDateStat>> temp = new HashMap<>();
                    temp.put(processName, tempList);
                    processList.add(temp);
                } else {
                    // 按月统计时才按工厂分组
                    if (apsResourceDateStatList.isEmpty()) {
                        // 从产能规划数据中获取所有的工厂
                        Set<String> plants = apsGasPipelineCapacityPlanList.stream()
                                .filter(plan -> plan.getProcessName().equals(processName))
                                .map(ApsGasPipelineCapacityPlan::getOrgCode)
                                .filter(orgCode -> orgCode != null && !orgCode.trim().isEmpty())
                                .collect(Collectors.toSet());
                        // 如果没有找到任何有效工厂,跳过这个工序
                        if (plants.isEmpty()) {
                            continue;
                        }
                        // 为每个工厂创建空记录
                        for (String plant : plants) {
                            LinkedHashMap<String, ApsResourceDateStat> dayMap = new LinkedHashMap<>();
                            // 为每个月份创建记录
                            for(String monthDate : startPlanTimeSet) {
                                String tempTime = monthDate + "-01";
                                ApsResourceDateStat apsResourceDateStat = new ApsResourceDateStat();
                                apsResourceDateStat.setPlanDay(LocalDate.parse(tempTime, formatter));
                                apsResourceDateStat.setResourceName(processName);
                                apsResourceDateStat.setResourceGroupName(processName + "-" + plant);
                                apsResourceDateStat.setPlant(plant);
                                apsResourceDateStat.setRequireTimes(new BigDecimal(0));
                                String capacityKey = processName + "-" + plant + "-" + monthDate;
                                apsResourceDateStat.setDesignTimes(apsGasPipingPlanMap.get(capacityKey)!=null?apsGasPipingPlanMap.get(capacityKey).getMonthProduceAllNum():new BigDecimal(0));
                                apsResourceDateStat.setCapacityLoad(new BigDecimal(0));
                                dayMap.put(monthDate, apsResourceDateStat);
                            }
                            List<ApsResourceDateStat> tempList = new ArrayList<>(dayMap.values());
                            HashMap<String, List<ApsResourceDateStat>> temp = new HashMap<>();
                            temp.put(processName + "-" + plant, tempList);
                            processList.add(temp);
                        }
                    } else {
                        // 按工厂分组,并过滤掉plant为null或空字符串的记录
                        Map<String, List<ApsResourceDateStat>> plantGroups = apsResourceDateStatList.stream()
                                .filter(stat -> stat.getPlant() != null && !stat.getPlant().trim().isEmpty())
                                .collect(groupingBy(ApsResourceDateStat::getPlant));
                        // 如果过滤后没有有效的工厂数据,跳过这个工序
                        if (plantGroups.isEmpty()) {
                            continue;
                        }
                        // 对每个工厂的数据进行处理
                        for (Map.Entry<String, List<ApsResourceDateStat>> plantEntry : plantGroups.entrySet()) {
                            String plant = plantEntry.getKey();
                            // 再次确认plant不为空
                            if (plant == null || plant.trim().isEmpty()) {
                                continue;
                            }
                            List<ApsResourceDateStat> plantStats = plantEntry.getValue();
                            LinkedHashMap<String, ApsResourceDateStat> dayMap = new LinkedHashMap<>();
                            // 首先为所有月份创建初始记录
                            for(String monthDate : startPlanTimeSet) {
                                String tempTime = monthDate + "-01";
                                ApsResourceDateStat initStat = new ApsResourceDateStat();
                                initStat.setPlanDay(LocalDate.parse(tempTime, formatter));
                                initStat.setResourceName(processName);
                                initStat.setResourceGroupName(processName + "-" + plant);
                                initStat.setPlant(plant);
                                initStat.setRequireTimes(new BigDecimal(0));
                                String capacityKey = processName + "-" + plant + "-" + monthDate;
                                initStat.setDesignTimes(apsGasPipingPlanMap.get(capacityKey)!=null?apsGasPipingPlanMap.get(capacityKey).getMonthProduceAllNum():new BigDecimal(0));
                                initStat.setCapacityLoad(new BigDecimal(0));
                                dayMap.put(monthDate, initStat);
                            }
                            // 然后处理实际数据
                            for (ApsResourceDateStat stat : plantStats) {
                                String monthKey = stat.getPlanDay().toString().substring(0, 7);
                                if (dayMap.containsKey(monthKey)) {
                                    ApsResourceDateStat existingStat = dayMap.get(monthKey);
                                    String capacityKey = processName + "-" + plant + "-" + monthKey;
                                    existingStat.setDesignTimes(apsGasPipingPlanMap.get(capacityKey)!=null?apsGasPipingPlanMap.get(capacityKey).getMonthProduceAllNum():new BigDecimal(0));
                                    existingStat.setRequireTimes(existingStat.getRequireTimes().add(stat.getRequireTimes()));
                                    if(existingStat.getDesignTimes().compareTo(BigDecimal.ZERO) > 0){
                                        existingStat.setCapacityLoad(existingStat.getRequireTimes()
                                                .divide(existingStat.getDesignTimes(), 2, RoundingMode.HALF_UP)
                                                .multiply(new BigDecimal(100)));
                                    }
                                }
                            }
                            List<ApsResourceDateStat> tempList = new ArrayList<>(dayMap.values());
                            HashMap<String, List<ApsResourceDateStat>> temp = new HashMap<>();
                            temp.put(processName + "-" + plant, tempList);
                            processList.add(temp);
                        }
                    }
                }
            }
            //排序时间标题
            List<String> sortedStartPlanTimeList = new ArrayList<>(startPlanTimeSet);
            Collections.sort(sortedStartPlanTimeList);
            for (int i=0;i<processList.size();i++){
                HashMap<String, List<ApsResourceDateStat>> temp = processList.get(i);
                for (Map.Entry<String, List<ApsResourceDateStat>> entry : temp.entrySet()){
                    List<ApsResourceDateStat> apsResourceDateStatList = entry.getValue();
                    String key = entry.getKey();
                    List<ApsResourceDateStat> crtList = new ArrayList<>();
                    for(String tempTime:sortedStartPlanTimeList) {
                        if("month".equals(apsGasPipingRouteStat.getSearchType())){
                            tempTime += "-01";
                        }
                        LocalDate crtDate = LocalDate.parse(tempTime, formatter);
                        Optional<ApsResourceDateStat> first = apsResourceDateStatList.stream().filter(x -> x.getPlanDay().equals(crtDate)).findFirst();
                        if (first.isPresent()) {
                            ApsResourceDateStat apsResourceDateStat = first.get();
                            crtList.add(apsResourceDateStat);
                        } else {
                            ApsResourceDateStat apsResourceDateStat = new ApsResourceDateStat();
                            apsResourceDateStat.setPlanDay(LocalDate.parse(tempTime, formatter));
                            String[] keyParts = key.split("-");
                            String processNamePart = keyParts[0];
                            String plantPart = keyParts[1];
                            String capacityKey = processNamePart + "-" + plantPart + "-" + tempTime.substring(0,7);
                            if ("month".equals(apsGasPipingRouteStat.getSearchType())) {
                                apsResourceDateStat.setDesignTimes(apsGasPipingPlanMap.get(capacityKey) != null ? apsGasPipingPlanMap.get(capacityKey).getMonthProduceAllNum() : new BigDecimal(0));
                            } else {
                                apsResourceDateStat.setDesignTimes(apsGasPipingPlanMap.get(capacityKey) != null ? apsGasPipingPlanMap.get(capacityKey).getDayProduceAllNum() : new BigDecimal(0));
                            }
                            apsResourceDateStat.setRequireTimes(new BigDecimal(0));
                            apsResourceDateStat.setCapacityLoad(new BigDecimal(0));
                            apsResourceDateStat.setResourceName(processNamePart);
                            apsResourceDateStat.setResourceGroupName(key);
                            apsResourceDateStat.setPlant(plantPart);
                            apsResourceDateStatList.add(apsResourceDateStat);
                            crtList.add(apsResourceDateStat);
                        }
                        temp.put(entry.getKey(), crtList);
                        processList.set(i, temp);
                    }
                }
            }
            result.put("planTable", processList);
            result.put("planTitle", sortedStartPlanTimeList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    @Override
    public void exportExcel(HttpServletResponse response, ApsGasPipingRouteStat apsGasPipingRouteStat) {
        SXSSFWorkbook wb = new SXSSFWorkbook(500);
        wb.createSheet();