From 2aa8da8af66aa8ae00f25831aed6bb0364176e7b Mon Sep 17 00:00:00 2001
From: 高章伟 <gaozhangwei@prec-tech.com>
Date: 星期四, 24 二月 2022 20:32:31 +0800
Subject: [PATCH] 1.15---2.24 变更代码

---
 force-app/main/default/classes/LastbuyProductHandler.cls |   89 ++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 85 insertions(+), 4 deletions(-)

diff --git a/force-app/main/default/classes/LastbuyProductHandler.cls b/force-app/main/default/classes/LastbuyProductHandler.cls
index 8f8f400..4858b68 100644
--- a/force-app/main/default/classes/LastbuyProductHandler.cls
+++ b/force-app/main/default/classes/LastbuyProductHandler.cls
@@ -24,6 +24,10 @@
         UpdateIdentification();
     }
 
+    protected override void afterUpdate(){
+        Invalid();
+    }
+
     //鏇存柊鏍囪瘑
     private void UpdateIdentification(){
     
@@ -38,10 +42,10 @@
         List<Product2> proList = [select id, LastbuyProductFLG__c from Product2 where Id in :pList];
         List<Product2> pflgList = new List<Product2>();
 
-        for(Product2 lbp : proList){
-            if(lbp.LastbuyProductFLG__c == false){
-                lbp.LastbuyProductFLG__c = true;
-                pflgList.add(lbp);
+        for(Product2 pro : proList){
+            if(pro.LastbuyProductFLG__c == false){
+                pro.LastbuyProductFLG__c = true;
+                pflgList.add(pro);
             }
         }
 
@@ -50,4 +54,81 @@
         }
         
     }
+
+    //褰�"鏄惁鏈夋晥"鍙戠敓鍙樺寲涓斿彉鎴�"鍚�"鏃讹紝妫�绱㈠悓涓�浜у搧鐨勬墍鏈夐鐣欎骇鍝侊紝
+    //濡傛灉杩欎釜浜у搧鎵�鏈夌殑棰勭暀浜у搧閮芥棤鏁堜簡锛屾妸浜у搧涓绘暟鎹笂鐨勯鐣欎骇鍝佹爣绛捐缃垚false銆�
+    //褰�"鏄惁鏈夋晥"鍙戠敓鍙樺寲涓斿彉鎴�"鏄�"鏃讹紝浜у搧涓绘暟鎹笂鐨勯鐣欎骇鍝佹爣绛捐缃垚true銆�
+    private void Invalid(){
+        List<Id> pfList = new List<Id>();  //"鏄惁鏈夋晥" 鍙樹负 "鍚�" 鏃� 鎵�瀵瑰簲鐨勪骇鍝佷富鏁版嵁Id
+        List<Id> ptList = new List<Id>();  //"鏄惁鏈夋晥" 鍙樹负 "鏄�" 鏃� 鎵�瀵瑰簲鐨勪骇鍝佷富鏁版嵁Id
+
+        for(LastbuyProduct__c lbp1 : newList){
+            LastbuyProduct__c oldLbp1 = oldMap.get(lbp1.Id);
+            if(oldLbp1.effectiveFLG__c != lbp1.effectiveFLG__c){
+                if(lbp1.effectiveFLG__c == false){
+                    pfList.add(lbp1.ProductName__c);
+                }else{  //lbp1.effectiveFLG__c == true
+                    ptList.add(lbp1.ProductName__c);
+                }
+
+            }
+        }
+        //"棰勭暀浜у搧"鏍囪瘑 鍙�"false"
+        if(pfList.size() > 0){
+            //Map<浜у搧Id锛岄鐣欎骇鍝�>
+            Map<String,LastbuyProduct__c> lbpMap = new Map<String,LastbuyProduct__c>();
+            //鍙戠敓鍙樺寲鐨勪骇鍝両d涓嬬殑鎵�鏈夐鐣欎骇鍝�
+            List<LastbuyProduct__c> lbpList = [select id, effectiveFLG__c,ProductName__c from LastbuyProduct__c where ProductName__c in :pfList];
+            System.debug('lt123鍙樺寲鐨勯鐣欎骇鍝乴bpList'+lbpList);
+            //p1List effectiveFLG__c涓簍rue 鐨勪骇鍝両d
+            List<Id> p1List = new List<Id>();
+            //p2List 杩欎釜浜у搧鎵�鏈夌殑棰勭暀浜у搧閮芥棤鏁� 鐨勪骇鍝両d
+            List<Id> p2List = new List<Id>();
+
+            for(LastbuyProduct__c lbm : lbpList){
+                lbpMap.put(lbm.ProductName__c,lbm);
+                if(lbm.effectiveFLG__c == true){
+                    p1List.add(lbm.ProductName__c);
+                }
+            }
+
+            if(p1List.size() > 0){
+                for(Id p1 : p1List){
+                    if(!lbpMap.containsKey(p1)){
+                        p2List.add(p1);
+                    }
+                }
+            }else{
+                for(LastbuyProduct__c lbm : lbpList){
+                    p2List.add(lbm.ProductName__c);
+                }
+            }
+            
+            List<Product2> pro1List = [select Id, LastbuyProductFLG__c from Product2 where Id in :p2List];
+            List<Product2> prflgList = new List<Product2>();
+            for(Product2 pro1 : pro1List){
+                pro1.LastbuyProductFLG__c = false;
+                System.debug('lt123棰勭暀浜у搧鏍囪瘑'+pro1.LastbuyProductFLG__c);
+                prflgList.add(pro1);
+            }
+            if(prflgList.size() > 0){
+                update prflgList;
+            }
+        }
+        
+        //"棰勭暀浜у搧"鏍囪瘑 鍙�"true"
+        if(ptList.size() > 0){
+            List<Product2> protList = [select Id, LastbuyProductFLG__c from Product2 where Id in :ptList];
+            List<Product2> ptflgList = new List<Product2>();
+            for(Product2 prot : protList){
+                prot.LastbuyProductFLG__c = true;
+                ptflgList.add(prot);
+            }
+            if(ptflgList.size() > 0){
+                update ptflgList;
+            }
+        }
+
+    }
+
 }
\ No newline at end of file

--
Gitblit v1.9.1