From 8db4bed21510327d0b75d1f2c5465bd375691553 Mon Sep 17 00:00:00 2001
From: whycxzp <perryhsu@163.com>
Date: 星期二, 06 九月 2022 17:07:20 +0800
Subject: [PATCH] 产品bom关联关系更新,修复

---
 src/main/java/com/whyc/service/ProductService.java |   64 ++++++++++++++++++++++++++-----
 1 files changed, 53 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/whyc/service/ProductService.java b/src/main/java/com/whyc/service/ProductService.java
index 96d9ea6..ac9c9d0 100644
--- a/src/main/java/com/whyc/service/ProductService.java
+++ b/src/main/java/com/whyc/service/ProductService.java
@@ -288,12 +288,17 @@
         String fileUrl = product.getFileUrl();
         Date date = new Date();
         boolean isCopyCustom = false;
+        Product relatedProduct = null;
         //鍒ゆ柇鏄惁涓轰緷鎹骇鍝佸鍒跺畾鍒剁殑浜у搧
         if(bomList == null){
             bomList = pbService.getBomByProductId(product.getId());
             isCopyCustom = true;
+        }else{
+            if(product.getId() != null){ //鍏宠仈鍏崇郴浠庝骇鍝乮d缁ф壙
+                relatedProduct = getById(product.getId());
+            }
         }
-        //鏌ヨ浜у搧鏈�鏂扮殑鐗堟湰鍙�
+        //鏌ヨ鏂板浜у搧鏈�鏂扮殑鐗堟湰鍙�
         ProductHistory latestProduct = phService.getLatestVersion(parentCode, customCode);
         ProductHistory enabledProduct = phService.getEnabledByParentCodeAndCustomCode(parentCode, customCode);
         int currentVersion = 0;
@@ -308,20 +313,57 @@
             //鏌ヨ浜у搧瀵瑰簲鐨勫叧鑱斿叧绯�
             copyCustomProduct = getById(product.getId());
             List<MaterialProductHistory> relatedList = mphService.getListByParentCodeAndCustomCodeAndVersion(copyCustomProduct.getParentCode(), copyCustomProduct.getCustomCode(), copyCustomProduct.getVersion());
-            relatedList.forEach(related->related.setCustomCode(product.getCustomCode()));
-            mphService.insertBatch(relatedList);
+            relatedList.forEach(related->{
+                related.setCustomCode(product.getCustomCode());
+                related.setSVersion(nextVersion);
+                related.setEVersion(nextVersion);
+                related.setCreateTime(date);
+            });
+            if(relatedList.size()!=0){
+                mphService.insertBatch(relatedList);
+            }
         }else {
-            if (latestProduct != null && enabledProduct != null) {
-                List<MaterialProductHistory> mpList = mphService.getListByParentCodeAndCustomCodeAndVersion(parentCode, customCode, enabledProduct.getVersion());
-                if(mpList.size()!=0) { //瀛樺湪鍏宠仈鍏崇郴
-                    if (latestProduct.getVersion().intValue() == enabledProduct.getVersion()) {
-                        //鏈�鏂扮増鏈敓鏁�,鍏宠仈鍏崇郴鐗堟湰杩炵潃鐨�
-                        mphService.updateVersionBatch(mpList);
-                    } else {
-                        //鏃х増鏈敓鏁�,鍏宠仈鍏崇郴鐗堟湰涓嶈繛鐫�
+            if(relatedProduct == null
+                || (relatedProduct.getParentCode().equals(product.getParentCode())
+                && relatedProduct.getCustomCode().equals(product.getCustomCode())
+                )
+            ) { //灞炰簬鏄骇鍝佸崌绾�
+                if (latestProduct != null && enabledProduct != null) {
+                    List<MaterialProductHistory> mpList = mphService.getListByParentCodeAndCustomCodeAndVersion(parentCode, customCode, enabledProduct.getVersion());
+                    if (mpList.size() != 0) { //瀛樺湪鍏宠仈鍏崇郴
+                        //褰撳墠鐨勪骇鍝乥om鏄庣粏鍖呭惈 缁ф壙鐨勪骇鍝佸瓙浠剁墿鏂�,缁ф壙鍏崇郴淇濈暀
+                        //璁剧疆mpList鍏宠仈鐨勭墿鏂欒缁嗕俊鎭�
+                        mpList = mphService.getListWithMaterialInfo(mpList);
+                        List<String> newBomMaterialStrList = bomList.stream().map(bom -> bom.getSubCode() + "/" + bom.getSubModel()).collect(Collectors.toList());
+                        mpList = mpList.stream().filter(mp -> newBomMaterialStrList.contains(mp.getSubMaterial().getSubCode() + "/" + mp.getSubMaterial().getSubModel())).collect(Collectors.toList());
+                        if (latestProduct.getVersion().intValue() == enabledProduct.getVersion()) {
+                            //鏈�鏂扮増鏈敓鏁�,鍏宠仈鍏崇郴鐗堟湰杩炵潃鐨�
+                            mphService.updateVersionBatch(mpList);
+                        } else {
+                            //鏃х増鏈敓鏁�,鍏宠仈鍏崇郴鐗堟湰涓嶈繛鐫�
+                            mpList.forEach(mp -> {
+                                mp.setSVersion(nextVersion);
+                                mp.setEVersion(nextVersion);
+                            });
+                            mphService.insertBatch(mpList);
+                        }
+                    }
+                }
+            }else{ //灞炰簬涓嶅悓浜у搧缁ф壙鍏宠仈鍏崇郴
+                List<MaterialProductHistory> mpList = mphService.getListByParentCodeAndCustomCodeAndVersion(relatedProduct.getParentCode(), relatedProduct.getCustomCode(), relatedProduct.getVersion());
+                if(mpList.size()!=0) {
+                    //褰撳墠鐨勪骇鍝乥om鏄庣粏鍖呭惈 缁ф壙鐨勪骇鍝佸瓙浠剁墿鏂�,淇濈暀
+                    //璁剧疆mpList鍏宠仈鐨勭墿鏂欒缁嗕俊鎭�
+                    mpList = mphService.getListWithMaterialInfo(mpList);
+                    List<String> newBomMaterialStrList = bomList.stream().map(bom -> bom.getSubCode() + "/" + bom.getSubModel()).collect(Collectors.toList());
+                    mpList = mpList.stream().filter(mp -> newBomMaterialStrList.contains(mp.getSubMaterial().getSubCode() + "/" + mp.getSubMaterial().getSubModel())).collect(Collectors.toList());
+                    if(mpList.size()!=0) {
                         mpList.forEach(mp -> {
+                            mp.setParentCode(parentCode);
+                            mp.setCustomCode(customCode);
                             mp.setSVersion(nextVersion);
                             mp.setEVersion(nextVersion);
+                            mp.setCreateTime(date);
                         });
                         mphService.insertBatch(mpList);
                     }

--
Gitblit v1.9.1