From d200d0bb4293ea3232679ecdf5c19f4b836add71 Mon Sep 17 00:00:00 2001
From: zhanghl <253316343@qq.com>
Date: 星期二, 29 四月 2025 07:53:34 +0800
Subject: [PATCH] 气体管路

---
 aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsGasPipingRouteStatServiceImpl.java |  178 +++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 155 insertions(+), 23 deletions(-)

diff --git a/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsGasPipingRouteStatServiceImpl.java b/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsGasPipingRouteStatServiceImpl.java
index 6535423..72ab84e 100644
--- a/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsGasPipingRouteStatServiceImpl.java
+++ b/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsGasPipingRouteStatServiceImpl.java
@@ -26,12 +26,11 @@
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.text.SimpleDateFormat;
-import java.time.LocalDate;
-import java.time.LocalDateTime;
-import java.time.YearMonth;
-import java.time.ZoneId;
+import java.time.*;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
+
+import static java.util.stream.Collectors.groupingBy;
 
 /**
  * 姘斾綋绠¤矾浜ц兘璐熻浇缁熻Service涓氬姟灞傚鐞�
@@ -148,6 +147,7 @@
                 apsProcessRoutes.sort((a, b)->a.getProcessNumber().compareTo(b.getProcessNumber()));
                 //鏄惁鎵惧埌褰撳墠宸ュ簭
                 boolean isFind = false;
+                ApsProcessRoute preApsProcessRoute = null;
                 for (int i=0;i<apsProcessRoutes.size();i++){
                     ApsProcessRoute apsProcessRoute = apsProcessRoutes.get(i);
                     ApsGasPipingRouteStat apsGasPipingRouteStat = new ApsGasPipingRouteStat();
@@ -156,7 +156,7 @@
                     //鏂欏彿
                     apsGasPipingRouteStat.setItemNumber(apsGasPipingPlan.getItemNumber());
                     //褰撳墠宸ュ簭鍙�
-                    apsGasPipingRouteStat.setCurrentProcessNumber(apsGasPipingPlan.getProcessNumber());
+                    apsGasPipingRouteStat.setCurrentProcessNumber(new BigDecimal(apsGasPipingPlan.getProcessNumber()));
                     //鐢熶骇鏁伴噺
                     apsGasPipingRouteStat.setProductionQuantity(apsGasPipingPlan.getProductionQuantity());
                     //宸ュ簭鍚嶇О
@@ -186,8 +186,8 @@
                     }
                     // 涓婁竴閬撳伐搴忕殑缁撴潫鏃堕棿 = 涓婁竴閬撳伐搴忕殑寮�濮嬫椂闂� + 涓婁竴閬撳伐搴忕殑鎬诲伐鏃�
                     if(apsGasPipingRouteStat.getProcessPlanStartDay()==null){
-                        LocalDateTime previousProcessPlanStartDay = apsProcessRoutes.get(i - 1).getProcessPlanStartDay().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
-                        long seconds = apsGasPipingRouteStatList.get(i - 1).getProcessTotalTime().multiply(new BigDecimal(60)).multiply(new BigDecimal(60)).longValue();
+                        LocalDateTime previousProcessPlanStartDay = preApsProcessRoute.getProcessPlanStartDay().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
+                        long seconds = preApsProcessRoute.getStandardTime().multiply(apsGasPipingPlan.getProductionQuantity()).multiply(new BigDecimal(60)).multiply(new BigDecimal(60)).longValue();
                         LocalDateTime currentProcessPlanStartDay = previousProcessPlanStartDay.plusSeconds(seconds);
                         apsGasPipingRouteStat.setProcessPlanStartDay(Date.from(currentProcessPlanStartDay.atZone(ZoneId.systemDefault()).toInstant()));
                     }
@@ -203,14 +203,15 @@
 //                    apsGasMaterialUsage.setItemNumber(apsGasPipingPlan.getItemNumber());
 //                    apsGasMaterialUsage.setProcessName(apsProcessRoute.getProcessName());
 //                    List<ApsGasMaterialUsage> apsGasMaterialUsageList = apsGasMaterialUsageService.selectApsGasMaterialUsageList(apsGasMaterialUsage);
-                    apsGasPipingRouteStat.setStandardDosage(apsProcessRoute.getStandardTime().multiply(apsGasPipingPlan.getProductionQuantity()));
+//                    apsGasPipingRouteStat.setStandardDosage(apsProcessRoute.getStandardTime().multiply(apsGasPipingPlan.getProductionQuantity()));
                     //宸ュ簭鎬荤敤閲� = 鏍囧噯鐢ㄩ噺*鐢熶骇鏁伴噺
-                    apsGasPipingRouteStat.setProcessTotalDosage(apsGasPipingRouteStat.getStandardDosage().multiply(apsGasPipingPlan.getProductionQuantity()));
+//                    apsGasPipingRouteStat.setProcessTotalDosage(apsGasPipingRouteStat.getStandardDosage().multiply(apsGasPipingPlan.getProductionQuantity()));
                     apsGasPipingRouteStat.setCreateTime(DateUtils.getNowDate());
                     apsGasPipingRouteStat.setCreateBy("auto");
                     apsGasPipingRouteStat.setBatchNumber(batchNum);
                     apsGasPipingRouteStat.setId(IdUtils.fastSimpleUUID());
                     apsGasPipingRouteStatList.add(apsGasPipingRouteStat);
+                    preApsProcessRoute = apsProcessRoute;
                 }
             });
             List<ApsGasPipingRouteStat> tempInsertList = new ArrayList<>();
@@ -234,7 +235,9 @@
     @Override
     public JSONObject getCapacityPlanData(ApsGasPipingRouteStat apsGasPipingRouteStat) {
         JSONObject result = new JSONObject();
-        HashSet<String> startPlanTimeSet = new HashSet<>();
+        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<>();
@@ -249,7 +252,24 @@
             }
             //鑾峰彇宸ュ簭璁″垝浜ц兘鏁版嵁
             HashMap<String, ApsGasPipelineCapacityPlan> apsGasPipingPlanMap = new HashMap<>();
-            List<ApsGasPipelineCapacityPlan> apsGasPipelineCapacityPlanList = apsGasPipelineCapacityPlanMapper.selectApsGasPipelineCapacityPlanList(new ApsGasPipelineCapacityPlan());
+            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);
+                for (String yearMonthStr : yearMonths) {
+                    startPlanTimeSet.add(yearMonthStr);
+                }
+            }
+            List<ApsGasPipelineCapacityPlan> apsGasPipelineCapacityPlanList = apsGasPipelineCapacityPlanMapper.selectApsGasPipelineCapacityPlanList(searchCapacityPlan);
             apsGasPipelineCapacityPlanList.forEach(apsGasPipelineCapacityPlan -> {
                 apsGasPipingPlanMap.put(apsGasPipelineCapacityPlan.getProcessName(),apsGasPipelineCapacityPlan);
             });
@@ -257,8 +277,6 @@
             DateTimeFormatter formatter = null;
             List<ApsGasPipingRouteStat> apsGasPipingRouteStats;
             SimpleDateFormat simpleDateFormat = null;
-            YearMonth yearMonth = YearMonth.parse(apsGasPipingRouteStat.getSearchEndDate());
-            int daysInMonth = yearMonth.lengthOfMonth();
             apsGasPipingRouteStat.setSearchStartDate(apsGasPipingRouteStat.getSearchStartDate()+"-01 00:00:00");
             apsGasPipingRouteStat.setSearchEndDate(apsGasPipingRouteStat.getSearchEndDate()+"-"+ daysInMonth +" 23:59:59");
             if("day".equals(apsGasPipingRouteStat.getSearchType())){
@@ -304,7 +322,7 @@
             processMap.forEach((processName, apsResourceDateStatList) -> {
                 LinkedHashMap<String, ApsResourceDateStat> dayMap = new LinkedHashMap<>();
                 apsResourceDateStatList.forEach(apsResourceDateStat -> {
-                    startPlanTimeSet.add(apsResourceDateStat.getPlanDay().toString());
+//                    startPlanTimeSet.add(apsResourceDateStat.getPlanDay().toString());
                     if(dayMap.containsKey(apsResourceDateStat.getPlanDay().toString())){
                         ApsResourceDateStat apsResourceDateStatTemp = dayMap.get(apsResourceDateStat.getPlanDay().toString());
                         if("month".equals(apsGasPipingRouteStat.getSearchType())){
@@ -314,8 +332,8 @@
                         }
                         apsResourceDateStatTemp.setRequireTimes(apsResourceDateStatTemp.getRequireTimes().add(apsResourceDateStat.getRequireTimes()));
                         if(apsResourceDateStatTemp.getDesignTimes().compareTo(BigDecimal.ZERO) > 0){
-                            apsResourceDateStat.setCapacityLoad(apsResourceDateStat.getRequireTimes()
-                                    .divide(apsResourceDateStat.getDesignTimes(), 2, RoundingMode.HALF_UP)
+                            apsResourceDateStatTemp.setCapacityLoad(apsResourceDateStatTemp.getRequireTimes()
+                                    .divide(apsResourceDateStatTemp.getDesignTimes(), 2, RoundingMode.HALF_UP)
                                     .multiply(new BigDecimal(100)));
                         }else{
                             apsResourceDateStatTemp.setCapacityLoad(new BigDecimal(0));
@@ -342,22 +360,33 @@
                 HashMap<String, List<ApsResourceDateStat>> temp = processList.get(i);
                 for (Map.Entry<String, List<ApsResourceDateStat>> entry : temp.entrySet()){
                     List<ApsResourceDateStat> apsResourceDateStatList = entry.getValue();
-                    if(apsResourceDateStatList.size()<=0){
-                        for(String tempTime:sortedStartPlanTimeList){
+                    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));
-                            if("month".equals(apsGasPipingRouteStat.getSearchType())){
-                                apsResourceDateStat.setDesignTimes(apsGasPipingPlanMap.get(entry.getKey())!=null?apsGasPipingPlanMap.get(entry.getKey()).getMonthProduceAllNum():new BigDecimal(0));
-                            }else{
-                                apsResourceDateStat.setDesignTimes(apsGasPipingPlanMap.get(entry.getKey())!=null?apsGasPipingPlanMap.get(entry.getKey()).getDayProduceAllNum():new BigDecimal(0));
+                            if ("month".equals(apsGasPipingRouteStat.getSearchType())) {
+                                apsResourceDateStat.setDesignTimes(apsGasPipingPlanMap.get(entry.getKey()) != null ? apsGasPipingPlanMap.get(entry.getKey()).getMonthProduceAllNum() : new BigDecimal(0));
+                            } else {
+                                apsResourceDateStat.setDesignTimes(apsGasPipingPlanMap.get(entry.getKey()) != null ? apsGasPipingPlanMap.get(entry.getKey()).getDayProduceAllNum() : new BigDecimal(0));
                             }
                             apsResourceDateStat.setRequireTimes(new BigDecimal(0));
                             apsResourceDateStat.setCapacityLoad(new BigDecimal(0));
                             apsResourceDateStat.setResourceName(entry.getKey());
                             apsResourceDateStat.setResourceGroupName(entry.getKey());
                             apsResourceDateStatList.add(apsResourceDateStat);
+                            crtList.add(apsResourceDateStat);
                         }
-                        temp.put(entry.getKey(), apsResourceDateStatList);
+                        temp.put(entry.getKey(), crtList);
                         processList.set(i, temp);
                     }
                 }
@@ -463,4 +492,107 @@
         styles.put("title", style);
         return styles;
     }
+
+    public static List<String> getYearMonthsInRange(YearMonth start, YearMonth end) {
+        List<String> yearMonths = new ArrayList<>();
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
+
+        while (!start.isAfter(end)) {
+            yearMonths.add(start.format(formatter));
+            start = start.plusMonths(1);
+        }
+
+        return yearMonths;
+    }
+
+    @Override
+    public void saveGasPipingProcessStat(){
+        try {
+            String batchNum = IdUtils.fastSimpleUUID();
+            List<ApsGasPipingRouteStat> tempList = apsGasPipingRouteStatMapper.queryTempStat();
+            Map<String, List<ApsGasPipingRouteStat>> groupByOrderNo = tempList.stream().collect(groupingBy(ApsGasPipingRouteStat::getWorkOrderNo));
+            Boolean hasBefore = false;
+            LocalDateTime now = LocalDateTime.now();
+            for (Map.Entry<String, List<ApsGasPipingRouteStat>> entry : groupByOrderNo.entrySet()) {
+                List<ApsGasPipingRouteStat> statPerOrder = entry.getValue();
+                /*num 涓烘牴鎹畬宸ユ椂闂存帓搴忓嚭鐨勫簭鍙凤紝鎸夋鎺掑簭锛屽彲淇濊瘉鏄寜瀹屽伐鏃堕棿鍊掑彊鎺掑垪*/
+                statPerOrder.sort((a, b)->a.getNum().compareTo(b.getNum()));
+                ApsGasPipingRouteStat last=null;
+                for (int i = 0; i <statPerOrder.size(); i++) {
+                    ApsGasPipingRouteStat stat = statPerOrder.get(i);
+                    stat.setId(IdUtils.fastSimpleUUID());
+                    stat.setBatchNumber(batchNum);
+                    stat.setCreateBy(SecurityUtils.getUsername());
+                    stat.setWarning(false);
+                    if(i==0){
+                        Date orderPlanEndDay = stat.getOrderPlanEndDay();
+                        LocalDateTime transLocalDateTime = transLocalDateTime(orderPlanEndDay);
+                        LocalTime endOfDay = LocalTime.of(23, 59, 59);
+                        LocalDateTime orderPlanEndDayLocalDateTime =  LocalDateTime.of( transLocalDateTime.toLocalDate(), endOfDay);
+                        if(orderPlanEndDayLocalDateTime.isBefore(now)){
+                            hasBefore = true;
+                            stat.setWarning(true);
+                            stat.setProcessPlanEndDay(transDate(now));
+                            stat.setProcessPlanStartDay(transDate(now));
+                        }else {
+                            /*璁″垝瀹屽伐鏃�=閽i噾璁″垝宸ュ崟瀹屾垚鏃堕棿*/
+                            stat.setProcessPlanEndDay(transDate(orderPlanEndDayLocalDateTime));
+                            /*璁″垝寮�宸ユ棩=閽i噾璁″垝宸ュ崟瀹屾垚鏃堕棿 -  宸ュ簭鎬诲伐鏃�*/
+                            long seconds = stat.getProcessTotalTime().multiply(new BigDecimal(60)).multiply(new BigDecimal(60)).longValue();
+                            LocalDateTime lastPlanStartDt = orderPlanEndDayLocalDateTime.minusSeconds(seconds);
+                            if(lastPlanStartDt.isBefore(now)){
+                                hasBefore = true;
+                                stat.setProcessPlanStartDay(transDate(now));
+                            }else {
+                                stat.setProcessPlanStartDay(transDate(lastPlanStartDt));
+                            }
+                        }
+                    }
+                    /*褰撳伐鑹哄伐搴忓彿 >= 宸ュ崟褰撳墠宸ュ簭 浠h〃鏄湭鏉ュ伐搴忥紝鎵嶈繘琛岃鍒掑紑宸ユ棩 鍜岃鍒掑畬宸ユ棩鐨勮绠�
+                     * 褰撳伐鑹哄伐搴忓彿 < 宸ュ崟褰撳墠宸ュ簭  杩囧幓宸ュ簭锛屼笉杩涜璁$畻
+                     * */
+                    if( stat.getRouteProcessNumber().compareTo(stat.getCurrentProcessNumber())>=0){
+                        /*鍊掓帓鏃� 涓嬩竴閬撳伐搴忓瓨鍦� 姣斿綋鍓嶆椂闂村皬鐨勮鍒掓椂闂达紝鍒欏綋鍓嶈鍒掑紑濮嬪拰缁撴潫鏃堕棿閮芥槸褰撳墠鏃堕棿*/
+                        if(hasBefore){
+                            stat.setWarning(true);
+                            stat.setProcessPlanEndDay(transDate(now));
+                            stat.setProcessPlanStartDay(transDate(now));
+                        }else{
+                            /*涓嬩竴閬撳伐搴忚鍒掓椂闂撮兘姝e父鏃讹紝*/
+                            if (last != null) {
+                                /*褰撳墠宸ュ簭缁撴潫鏃堕棿=涓嬩竴閬撳伐搴忕殑寮�濮嬫椂闂�*/
+                                stat.setProcessPlanEndDay(last.getProcessPlanStartDay());
+                                /*寮�濮嬫椂闂�=缁撴潫鏃堕棿-鎬诲伐鏃�*/
+                                long seconds = stat.getProcessTotalTime().multiply(new BigDecimal(60)).multiply(new BigDecimal(60)).longValue();
+                                LocalDateTime crtStartDt = transLocalDateTime(last.getProcessPlanStartDay()).minusSeconds(seconds);
+                                /*濡傛灉寮�濮嬫椂闂村皬浜庡綋鍓嶆椂闂�*/
+                                if(crtStartDt.isBefore(now)){
+                                    hasBefore=true;
+                                    stat.setWarning(true);
+                                    stat.setProcessPlanStartDay(transDate(now));
+                                }else {
+                                    stat.setProcessPlanStartDay(transDate(crtStartDt));
+                                }
+                            }
+                        }
+                    }
+                    last = stat;
+                    apsGasPipingRouteStatMapper.insertApsGasPipingRouteStat(stat);
+                }
+                hasBefore=false;
+
+            }
+            apsGasPipingRouteStatMapper.deleteApsGasPipingRouteStatByBatchNum(batchNum);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    };
+
+    private Date transDate(LocalDateTime localDateTime){
+        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
+    }
+
+    private LocalDateTime transLocalDateTime(Date date){
+        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
+    }
 }

--
Gitblit v1.9.3