From d44c9ab933cfca67d4c550e499228f90432cbb3a Mon Sep 17 00:00:00 2001
From: zhanghl <253316343@qq.com>
Date: 星期四, 15 五月 2025 11:03:11 +0800
Subject: [PATCH] 钣金工单计划:优化子件工单的读取方式 先存储内存,再从内存扣减,最后一起保存

---
 aps-modules/aps-core/src/main/resources/mapper/core/ApsPlatePlanMapper.xml                                         |   15 +++++++
 aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlateStandardRequireServiceImpl.java               |   39 ++++++++++++++-----
 aps-modules/aps-core/src/main/java/com/aps/core/mapper/ApsPlatePlanMapper.java                                     |   11 +++++
 aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlateStandardRequireBomOrderDetailServiceImpl.java |    2 
 aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlatePlanServiceImpl.java                          |   24 +++++++++--
 aps-modules/aps-core/src/main/java/com/aps/core/service/IApsPlatePlanService.java                                  |    5 ++
 6 files changed, 77 insertions(+), 19 deletions(-)

diff --git a/aps-modules/aps-core/src/main/java/com/aps/core/mapper/ApsPlatePlanMapper.java b/aps-modules/aps-core/src/main/java/com/aps/core/mapper/ApsPlatePlanMapper.java
index a456135..ded9061 100644
--- a/aps-modules/aps-core/src/main/java/com/aps/core/mapper/ApsPlatePlanMapper.java
+++ b/aps-modules/aps-core/src/main/java/com/aps/core/mapper/ApsPlatePlanMapper.java
@@ -75,7 +75,7 @@
     List<ApsPlatePlan> selectPlatePlanByPlantMajor(String plant,String professionalAffiliation);
 
     /**
-     * 鑾峰彇鏈尮閰嶇殑瀛愯鍒�
+     * 鑾峰彇涓�鏉℃湭鍖归厤鐨勫瓙璁″垝
      * */
     ApsPlatePlan selectUnMatchPlateSubPlan(String plant,String itemNumber);
 
@@ -88,4 +88,13 @@
      * 鍒濆鍖� 瀛愪欢宸ュ崟鐨� 鏈尮閰嶆暟閲�
      * */
     int initUnMatchQty();
+    /**
+     * 鑾峰彇鏈尮閰嶇殑瀛愯鍒�
+     * */
+    List<ApsPlatePlan> selectUnMatchPlateSubPlanList(String plant,String itemNumber);
+
+    /**
+     * 鏇存柊瀛愪欢宸ュ崟鏈尮閰嶆暟閲忓拰鐗堟湰
+     * */
+    int updatePlanUnMatchQtyAndVersion(ApsPlatePlan plan);
 }
diff --git a/aps-modules/aps-core/src/main/java/com/aps/core/service/IApsPlatePlanService.java b/aps-modules/aps-core/src/main/java/com/aps/core/service/IApsPlatePlanService.java
index bae0205..a923079 100644
--- a/aps-modules/aps-core/src/main/java/com/aps/core/service/IApsPlatePlanService.java
+++ b/aps-modules/aps-core/src/main/java/com/aps/core/service/IApsPlatePlanService.java
@@ -4,6 +4,7 @@
 import com.aps.core.domain.ApsPlatePlanTemp;
 import com.aps.core.domain.ApsPlateStandardRequire;
 
+import java.util.Hashtable;
 import java.util.List;
 
 /**
@@ -81,5 +82,7 @@
      */
     public List<ApsPlatePlan> selectPlateRedundantOrderList(ApsPlatePlan apsPlatePlan);
 
-   // ApsPlatePlan selectUnMatchPlateSubPlan(ApsPlateStandardRequire require);
+    ApsPlatePlan selectUnMatchPlateSubPlan(String plant, String itemNumber, Hashtable<String, List<ApsPlatePlan>> subPlans);
+
+    // ApsPlatePlan selectUnMatchPlateSubPlan(ApsPlateStandardRequire require);
 }
diff --git a/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlatePlanServiceImpl.java b/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlatePlanServiceImpl.java
index 6fda182..15d21c4 100644
--- a/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlatePlanServiceImpl.java
+++ b/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlatePlanServiceImpl.java
@@ -1,15 +1,11 @@
 package com.aps.core.service.impl;
 
 import cn.hutool.core.util.IdUtil;
-import com.alibaba.fastjson2.JSON;
-import com.alibaba.fastjson2.JSONObject;
 import com.aps.common.core.utils.DateUtils;
 import com.aps.common.core.utils.uuid.IdUtils;
 import com.aps.common.security.utils.DictUtils;
-import com.aps.core.domain.ApsMaterialStorageManagement;
 import com.aps.core.domain.ApsPlatePlan;
 import com.aps.core.domain.ApsPlatePlanTemp;
-import com.aps.core.domain.ApsPlateStandardRequire;
 import com.aps.core.mapper.ApsPartPlanTempMapper;
 import com.aps.core.mapper.ApsPlatePlanMapper;
 import com.aps.core.mapper.ApsPlatePlanTempMapper;
@@ -18,9 +14,10 @@
 import org.apache.logging.log4j.util.Strings;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 
+import java.math.BigDecimal;
+import java.util.Hashtable;
 import java.util.List;
 import java.util.Optional;
 
@@ -197,4 +194,21 @@
     public List<ApsPlatePlan> selectPlateRedundantOrderList(ApsPlatePlan apsPlatePlan) {
         return apsPlatePlanMapper.selectPlateRedundantOrderList(apsPlatePlan);
     }
+
+    @Override
+    public ApsPlatePlan selectUnMatchPlateSubPlan(String plant, String itemNumber, Hashtable<String, List<ApsPlatePlan>> subPlans) {
+       if(subPlans.containsKey(itemNumber)) {
+           List<ApsPlatePlan> subPlanList = subPlans.get(itemNumber);
+           Optional<ApsPlatePlan> firstPlant = subPlanList.stream().filter(x -> x.getUnmatchedQuantity().compareTo(BigDecimal.ZERO) > 0).findFirst();
+           return firstPlant.orElse(null);
+       }else {
+           List<ApsPlatePlan> plans = apsPlatePlanMapper.selectUnMatchPlateSubPlanList(plant, itemNumber);
+           if (!plans.isEmpty()) {
+               subPlans.put(itemNumber, plans);
+               return selectUnMatchPlateSubPlan(plant, itemNumber, subPlans);
+           } else {
+               return null;
+           }
+       }
+    }
 }
diff --git a/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlateStandardRequireBomOrderDetailServiceImpl.java b/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlateStandardRequireBomOrderDetailServiceImpl.java
index 6294cea..b9e3551 100644
--- a/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlateStandardRequireBomOrderDetailServiceImpl.java
+++ b/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlateStandardRequireBomOrderDetailServiceImpl.java
@@ -107,7 +107,7 @@
     public void savePlastPlanAndBomOrderDetail(ApsPlateStandardRequire require, ApsPlatePlan platePlan, BigDecimal subtract
             , BigDecimal stock, BigDecimal netRequirement, List<ApsPlateStandardRequireBomOrderDetail> orderDetailsList) {
         platePlan.setUnmatchedQuantity(subtract);
-        apsPlatePlanMapper.updatePlanUnMatchQtyByVersion(platePlan);
+       // apsPlatePlanMapper.updatePlanUnMatchQtyByVersion(platePlan);
         /* 璁板綍宸ュ崟涓庡噣闇�姹傜殑鍖归厤鍏崇郴*/
         ApsPlateStandardRequireBomOrderDetail bomOrderDetail = ApsPlateStandardRequireBomOrderDetail.builder()
                 .id(IdUtil.getSnowflakeNextId())
diff --git a/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlateStandardRequireServiceImpl.java b/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlateStandardRequireServiceImpl.java
index 84995db..e8178ce 100644
--- a/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlateStandardRequireServiceImpl.java
+++ b/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlateStandardRequireServiceImpl.java
@@ -64,7 +64,7 @@
     ApsPlateStandardRequireBomOrderDetailMapper plateBomOrderDetailMapper;
 
     @Autowired
-    private ApsPlatePlanMapper apsPlatePlanMapper;
+    private IApsPlatePlanService apsPlatePlanService;
 
     @Resource
     IApsPlateStandardRequireBomOrderDetailService bomOrderDetailService;
@@ -176,19 +176,23 @@
         /*鑾峰彇閽i噾涓诲崟淇℃伅*/
         List<ApsPlatePlan> mainPlans = platePlanMapper.selectPlatePlanByPlantMajor(plantCode,mainOrderType);
         Hashtable<String, ApsMaterialStorageManagement> usedStorage = new Hashtable<>();
+        /*鍐呭瓨涓瓨鍌ㄥ瓙浠跺伐鍗�*/
+        Hashtable<String, List<ApsPlatePlan>> subPlans = new Hashtable<>();
         log.info("寮�濮嬬敓鎴愰渶姹傦細");
         for (ApsPlatePlan mainPlan : mainPlans) {
             String itemNumber = mainPlan.getItemNumber();
             /*鏍规嵁鏂欏彿 鑾峰彇BOM Header */
-            /*褰撳墠Bom鑺傜偣澶勭悊瀹屾垚鍚庯紝澶勭悊涓嬬骇BOM*/
+
 
             List<ApsPlateStandardRequire> requiresList=new ArrayList<>();
             List<ApsPlateStandardRequireBomStockDetail> stockDetailsList=new ArrayList<>();
             List<ApsPlateStandardRequireBomOrderDetail> orderDetailsList=new ArrayList<>();
 
+
             log.info("寮�濮嬬敓鎴愰渶姹傦細宸ュ崟鍙凤細"+mainPlan.getDocumentNumber());
+            /*褰撳墠Bom鑺傜偣澶勭悊瀹屾垚鍚庯紝澶勭悊涓嬬骇BOM*/
             getBomRequires(plantCode, "0","0",itemNumber,BigDecimal.ONE, batchNum, null
-                    , mainPlan, requiresList, 0L,stockDetailsList,orderDetailsList,usedStorage
+                    , mainPlan, requiresList, 0L,stockDetailsList,orderDetailsList,usedStorage,subPlans
             );
 
             // 鎵归噺鎻掑叆浠ユ彁楂樻�ц兘
@@ -225,6 +229,15 @@
                 storageManagementService.updateRemainderStock(sm.getId(),sm.getRemainderStock(),sm.getVersion());
             });
         }
+        if(!subPlans.isEmpty()){
+            subPlans.forEach((key, subPlansList) -> {
+                subPlansList.forEach(x->{
+                    if (!x.getUnmatchedQuantity().equals(x.getProductionQuantity())){
+                        platePlanMapper.updatePlanUnMatchQtyAndVersion(x);
+                    }
+                });
+            });
+        }
     }
 
     /**
@@ -248,7 +261,8 @@
                                 List<ApsPlateStandardRequire> allRequires,
                                 Long level,List<ApsPlateStandardRequireBomStockDetail> stockDetailsList,
                                 List<ApsPlateStandardRequireBomOrderDetail> orderDetailsList,
-                                Hashtable<String, ApsMaterialStorageManagement> usedStorage
+                                Hashtable<String, ApsMaterialStorageManagement> usedStorage,
+                                Hashtable<String, List<ApsPlatePlan>> subPlans
     ) {
         /*鏋勫缓闇�姹備俊鎭�*/
         ApsPlateStandardRequire require = new ApsPlateStandardRequire();
@@ -367,7 +381,7 @@
         if (require.getNetRequirement().compareTo(BigDecimal.ZERO) > 0) {
             require.setMatchMode("宸ュ崟鍖归厤");
             /*浣跨敤瀛愪欢宸ュ崟杩涜闇�姹傚尮閰�*/
-            matchRequireAndSubPlan(require,orderDetailsList);
+            matchRequireAndSubPlan(require,orderDetailsList,subPlans);
         }
         allRequires.add(require);
         log.info("宸茬敓鎴愰渶姹傦細"+plan.getDocumentNumber()+"bomHeaderCode:"+bomHeaderCode+"bomLineCode:"+itemCode+"bomLevel:"+level);
@@ -379,7 +393,7 @@
                 bomLineList.forEach(line -> {
                     getBomRequires(plant, itemCode, line.getBomLineId(),line.getItemCode() ,line.getNum() , batchNum
                             , require.getStartDate(), plan, allRequires, nextLevel,stockDetailsList
-                            ,orderDetailsList,usedStorage
+                            ,orderDetailsList,usedStorage,subPlans
                     );
                 });
             }
@@ -403,12 +417,12 @@
     * 浣跨敤瀛愪欢宸ュ崟鍖归厤闇�姹備腑鐨勫噣闇�姹�
     * */
 
-    private void matchRequireAndSubPlan(ApsPlateStandardRequire require, List<ApsPlateStandardRequireBomOrderDetail> orderDetailsList) {
+    private void matchRequireAndSubPlan(ApsPlateStandardRequire require, List<ApsPlateStandardRequireBomOrderDetail> orderDetailsList,Hashtable<String, List<ApsPlatePlan>> subPlans) {
         BigDecimal netRequirement = require.getNetRequirement();
         require.setMatchMode("宸ュ崟鍖归厤");
         require.setMatchState("宸插尮閰�");
         if (netRequirement.compareTo(BigDecimal.ZERO) > 0) {
-            ApsPlatePlan platePlan = apsPlatePlanMapper.selectUnMatchPlateSubPlan(require.getOrgCode(),  require.getBomLineCode());
+            ApsPlatePlan platePlan = apsPlatePlanService.selectUnMatchPlateSubPlan(require.getOrgCode(),  require.getBomLineCode(),subPlans);
             /*瀛愪欢宸ュ崟鐨勬湭鍖归厤鏁伴噺 浣滀负褰撳墠鐨勫簱瀛�*/
             while (platePlan != null && netRequirement.compareTo(BigDecimal.ZERO) > 0) {
                 BigDecimal stock = platePlan.getUnmatchedQuantity();
@@ -416,6 +430,8 @@
                     /* 搴撳瓨鏁伴噺 澶т簬 鍑�闇�姹傛暟閲�*/
                     /* 鍑�闇�姹傛暟閲�=0 锛屽瓙浠跺伐鍗曟湭鍖归厤鏁伴噺= 搴撳瓨-鍑�闇�姹�*/
                     BigDecimal subtract = stock.subtract(netRequirement);
+                    platePlan.setUnmatchedQuantity(subtract);
+                    platePlan.setVersion(platePlan.getVersion() + 1);
                     bomOrderDetailService.savePlastPlanAndBomOrderDetail(require, platePlan, subtract, stock, netRequirement,orderDetailsList);
                     netRequirement = BigDecimal.ZERO;
 
@@ -426,6 +442,8 @@
                     /*鍑�闇�姹傛暟閲� == 搴撳瓨鏁伴噺*/
 
                     BigDecimal subtract = BigDecimal.ZERO;
+                    platePlan.setUnmatchedQuantity(subtract);
+                    platePlan.setVersion(platePlan.getVersion() + 1);
                     bomOrderDetailService.savePlastPlanAndBomOrderDetail(require, platePlan, subtract, stock, netRequirement,orderDetailsList);
                     netRequirement = BigDecimal.ZERO;
 
@@ -436,11 +454,12 @@
                     /*闇�姹傚ぇ浜庡簱瀛�*/
                     /*鍑�闇�姹� 琚儴鍒嗘弧瓒� */
                     BigDecimal rest = netRequirement.subtract(stock);
-
+                    platePlan.setUnmatchedQuantity(BigDecimal.ZERO);
+                    platePlan.setVersion(platePlan.getVersion() + 1);
                     /*宸ュ崟 鏈尮閰嶆暟閲忎负0 鍏ㄩ儴鐢ㄤ簬鍖归厤闇�姹�*/
                     bomOrderDetailService.savePlastPlanAndBomOrderDetail(require, platePlan, BigDecimal.ZERO, stock, netRequirement,orderDetailsList);
                     /*鍑�闇�姹傛湭琚弧瓒筹紝闇�瑕佺户缁尮閰�*/
-                    platePlan = apsPlatePlanMapper.selectUnMatchPlateSubPlan(require.getOrgCode(),  require.getBomLineCode());
+                    platePlan = apsPlatePlanService.selectUnMatchPlateSubPlan(require.getOrgCode(),  require.getBomLineCode(),subPlans);
                     /*鍓╀綑鍑�闇�姹�*/
                     netRequirement = rest;
                     require.setUnmatchedDemandAmount(rest);
diff --git a/aps-modules/aps-core/src/main/resources/mapper/core/ApsPlatePlanMapper.xml b/aps-modules/aps-core/src/main/resources/mapper/core/ApsPlatePlanMapper.xml
index 9cb4122..c39feeb 100644
--- a/aps-modules/aps-core/src/main/resources/mapper/core/ApsPlatePlanMapper.xml
+++ b/aps-modules/aps-core/src/main/resources/mapper/core/ApsPlatePlanMapper.xml
@@ -293,7 +293,15 @@
         order by document_number asc,id asc
         limit 1
     </select>
-
+    <select id="selectUnMatchPlateSubPlanList" parameterType="com.aps.core.domain.ApsPlatePlan" resultMap="ApsPlatePlanResult">
+        select  id,document_number,main_part_number,item_number,plant,professional_affiliation,production_quantity,
+        unmatched_quantity,version
+        from aps_plate_plan
+        where  del_flag='0' and professional_affiliation ='sub' and unmatched_quantity > 0
+        <if test="plant != null  and plant !='' "> and plant = #{plant}</if>
+        <if test="itemNumber != null  and itemNumber !='' "> and item_number = #{itemNumber}</if>
+        order by document_number asc,id asc
+    </select>
 
     <update id="updatePlanUnMatchQtyByVersion" parameterType="com.aps.core.domain.ApsPlatePlan">
         update aps_plate_plan
@@ -316,4 +324,9 @@
             set unmatched_quantity=production_quantity, version=0,require_id=id
         where   del_flag='0' and professional_affiliation !='0'
     </update>
+    <update id="updatePlanUnMatchQtyAndVersion" parameterType="com.aps.core.domain.ApsPlatePlan">
+        update aps_plate_plan
+        set unmatched_quantity=#{unmatchedQuantity},version=#{version},update_time=now()
+        where id=#{id}
+    </update>
 </mapper>
\ No newline at end of file

--
Gitblit v1.9.3