From 2682be2d1932fd43e0226c011bb32ae3c5e643bf Mon Sep 17 00:00:00 2001
From: 李彤 <litong@prec-tech.com>
Date: 星期五, 25 三月 2022 17:17:45 +0800
Subject: [PATCH] LastBuy触发器

---
 force-app/main/default/triggers/LastbuyProductTrigger.trigger |    2 +-
 force-app/main/default/classes/LastbuyProductHandler.cls      |   35 ++++++++++++++++++++++-------------
 force-app/main/default/classes/LastbuyProductHandlerTest.cls  |    2 ++
 3 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/force-app/main/default/classes/LastbuyProductHandler.cls b/force-app/main/default/classes/LastbuyProductHandler.cls
index 3ba4f25..0fb8f6f 100644
--- a/force-app/main/default/classes/LastbuyProductHandler.cls
+++ b/force-app/main/default/classes/LastbuyProductHandler.cls
@@ -33,6 +33,10 @@
         ReservedPro();
     }
 
+    protected override void afterDelete(){
+        ReservedPro();
+    }
+
     private void ReservedPro(){
         //1.鏂板缓鎴栬�呮洿鏂颁骇鍝佸悗锛岃嫢棰勭暀浜у搧鏈夋晥锛屸�滈鐣欎骇鍝佲�濇爣璇嗕负true
         if(trigger.isInsert || trigger.isUpdate){
@@ -63,24 +67,30 @@
         //2.(1)"鏄惁鏈夋晥" 鍙樹负 false  妫�绱骇鍝佷富鏁版嵁涓嬬殑鎵�鏈夐鐣欎骇鍝侊紝鍏ㄦ棤鏁堝垯鎶婁骇鍝佷富鏁版嵁鐨�"棰勭暀浜у搧"鏍囪瘑 鍙樹负 false
         //  (2)"浜у搧"    棰勭暀浜у搧 鏇存敼 浜у搧涓绘暟鎹箣鍓嶇殑浜у搧 -- 妫�绱骇鍝佷富鏁版嵁涓嬬殑鎵�鏈夐鐣欎骇鍝侊紙娌℃湁/鍏ㄦ棤鏁堬級
         //                                                  "棰勭暀浜у搧"鏍囪瘑 鍙樹负 false
-        if(trigger.isUpdate){
+        if(trigger.isUpdate || trigger.isDelete){
             //"鏄惁鏈夋晥" 鍙樹负 "鍚�" 鏃� 鎵�瀵瑰簲鐨勪骇鍝佷富鏁版嵁Id &&棰勭暀浜у搧 鏇存敼 浜у搧涓绘暟鎹箣鍓嶇殑浜у搧
             List<Id> pfList = new List<Id>();  
 
-            for(LastbuyProduct__c lbp1 : newList){
-                LastbuyProduct__c oldLbp1 = oldMap.get(lbp1.Id);
-                if(oldLbp1.effectiveFLG__c != lbp1.effectiveFLG__c){
-                    System.debug('lt123鏃犳晥锛�'+lbp1.effectiveFLG__c);
-                    if(lbp1.effectiveFLG__c == false){
-                        System.debug('lt123鏃犳晥'+lbp1.effectiveFLG__c);
-                        pfList.add(lbp1.ProductName__c);
+            if(trigger.isUpdate){
+                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);
+                        }
+                    }
+                    //浜у搧鍙戠敓鍙樺寲鏃讹紝妫�绱袱涓骇鍝佺殑鏍囪瘑
+                    if(oldLbp1.ProductName__c != lbp1.ProductName__c){
+                        pfList.add(oldLbp1.ProductName__c);
                     }
                 }
-                //浜у搧鍙戠敓鍙樺寲鏃讹紝妫�绱袱涓骇鍝佺殑鏍囪瘑
-                if(oldLbp1.ProductName__c != lbp1.ProductName__c){
-                    pfList.add(oldLbp1.ProductName__c);
+            }
+            else if(trigger.isDelete){
+                for(LastbuyProduct__c lbp2 : oldList){
+                    pfList.add(lbp2.ProductName__c);
                 }
             }
+            
             
             System.debug('lt123闇�妫�绱㈡墍鏈夐鐣欎骇鍝佺殑浜у搧ID-pfList'+pfList);
 
@@ -90,7 +100,6 @@
                 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
@@ -125,7 +134,6 @@
                 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){
@@ -133,6 +141,7 @@
                 }
             }
         }
+
     }
 
 }
\ No newline at end of file
diff --git a/force-app/main/default/classes/LastbuyProductHandlerTest.cls b/force-app/main/default/classes/LastbuyProductHandlerTest.cls
index cd8bd02..5a53235 100644
--- a/force-app/main/default/classes/LastbuyProductHandlerTest.cls
+++ b/force-app/main/default/classes/LastbuyProductHandlerTest.cls
@@ -42,6 +42,8 @@
         lbp2.ProductName__c = pro.Id;
         update lbp2;
 
+        delete lbp1;
+
     }
 
 }
\ No newline at end of file
diff --git a/force-app/main/default/triggers/LastbuyProductTrigger.trigger b/force-app/main/default/triggers/LastbuyProductTrigger.trigger
index 4ff18a4..d48c73b 100644
--- a/force-app/main/default/triggers/LastbuyProductTrigger.trigger
+++ b/force-app/main/default/triggers/LastbuyProductTrigger.trigger
@@ -1,4 +1,4 @@
-trigger LastbuyProductTrigger on LastbuyProduct__c (before insert, after insert, after update) {
+trigger LastbuyProductTrigger on LastbuyProduct__c (before insert, after insert, after update, after delete) {
     LastbuyProductHandler handler = new LastbuyProductHandler();
     handler.run();
 }
\ No newline at end of file

--
Gitblit v1.9.1