From 0fdc2c45a4b4f8199da04611433e93b6d9e969ae Mon Sep 17 00:00:00 2001
From: GWY <guweiyiscp096@foxmail.com>
Date: 星期四, 10 三月 2022 17:49:35 +0800
Subject: [PATCH] 11

---
 force-app/main/default/classes/Product2TriggerHandler.cls |  193 +++++++++++++++++++++++++++++++++---------------
 1 files changed, 133 insertions(+), 60 deletions(-)

diff --git a/force-app/main/default/classes/Product2TriggerHandler.cls b/force-app/main/default/classes/Product2TriggerHandler.cls
index b5978ee..a429749 100644
--- a/force-app/main/default/classes/Product2TriggerHandler.cls
+++ b/force-app/main/default/classes/Product2TriggerHandler.cls
@@ -46,27 +46,24 @@
        }
     }
 
-    //PJ1鏂版樉寰暅闄嶇被
     public static void checkProduct2Level(List<Product2> newList, Map<Id, Product2> newMap, List<Product2> oldList, Map<Id, Product2> oldMap) {
-        //鑾峰彇浜у搧
         List<String> p2ids = new List<String>();
-        for(Product2 p2 : newList){
-            p2ids.add(p2.Id);
+        for(Product2 p2 : newList)
+        {
+            p2ids.add(p2.id);
         }
 
-        //鏌ユ敞鍐岃瘉鐨勫尰鐤楀櫒姊板垎绫�,浜у搧鍏ュ簱鏃ユ湡,鐢熶骇鏃ユ湡,绯荤粺缁存姢鏃ャ��
+        //瀛樻斁鏌ユ壘鐨剆oql鏂�
         List<Product_Register_Link__c> pro2List = new List<Product_Register_Link__c>();
-        if(p2ids.size()>0){
-            pro2List = [select Id,Product2__r.demoteer_Sap__c,Product2__r.Diedatvanink__c,Product_Register__r.Stelsedag__c,Product_Register__r.MedPrdClass__c 
-            from Product_Register_Link__c 
-            where Product2__r.Id in : p2ids];
-        }
-        
-
-        //鏍规嵁Id瀛樻斁浜у搧鎵�灞炵殑鎵�鏈夋敞鍐岃瘉涓嬬殑鍣ㄦ鍒嗙被銆�
+        pro2List = [select Product2__r.id,Product2__r.demoteer_Sap__c,Product2__r.Diedatvanink__c,Product_Register__r.Stelsedag__c,Product_Register__r.MedPrdClass__c,Product_Register__r.ValidTo__c,Product_Register__r.ValidFrom__c 
+        from Product_Register_Link__c 
+        where Product2__r.Id in : p2ids];
+        System.debug('pro2List='+pro2List);
+        //瀛樻斁浜у搧鐨刬d鍜岀瓑绾х被鍒瓧娈电殑鍊肩殑闆嗗悎
         Map<Id,List<String>> pro2Map = new Map<Id,List<String>>();
-        //瀛樻斁娉ㄥ唽璇佷笂鏈�鍓嶇殑缁存姢鏃�
+        //瀛樻斁娉ㄥ唽璇佷笂鏈�鍓嶇殑缁存姢鏃�,涓�绫昏瘉浼樺厛,鍚﹀垯浜岀被璇併��
         Map<Id,Date> prSteMap = new Map<Id,Date>();
+        Map<Id,Date> prSte1Map = new Map<Id,Date>();
         if (pro2List.size()>0) {
             for (Product_Register_Link__c prl: pro2List) {
                 if (pro2Map.containsKey(prl.Product2__r.Id)) {
@@ -75,67 +72,143 @@
                     pro2Map.put(prl.Product2__r.Id, new List<String>());
                     pro2Map.get(prl.Product2__r.Id).add(prl.Product_Register__r.MedPrdClass__c);
                 }
-                if (prSteMap.containsKey(prl.Product2__r.Id)) {
-                    if (prSteMap.get(prl.Product2__r.Id) > prl.Product_Register__r.Stelsedag__c) {
-                        prSteMap.remove(prl.Product2__r.Id);
-                        prSteMap.put(prl.Product2__r.Id,prl.Product_Register__r.Stelsedag__c);
-                    }
-                }else {
-                    prSteMap.put(prl.Product2__r.Id,prl.Product_Register__r.Stelsedag__c);
-                }
-            }
-        }
-        System.debug('pro2Map='+pro2Map);
-        System.debug('prSteMap='+prSteMap);
 
-        //浜у搧Id涓庣瓑绾х被鍒�
-        Map<Id,String> prlMap = new Map<Id,String>();
-
-        if (pro2Map.size()>0 && prSteMap.size()>0) {
-            for (Product_Register_Link__c pr: pro2List) {
-                //澶у湪鍚庡皬鍦ㄥ墠
-                if (pro2Map.containsKey(pr.Product2__r.Id) && prSteMap.containsKey(pr.Product2__r.Id)) {
-                    //鐢熶骇鏃ュ皬浜庣淮鎶ゆ棩,鍏ュ簱鏃ュ皬浜庣淮鎶ゆ棩
-                    if (pr.Product2__r.demoteer_Sap__c <= prSteMap.get(pr.Product2__r.Id) && 
-                        pr.Product2__r.Diedatvanink__c <= prSteMap.get(pr.Product2__r.Id)) {
-                        if (pro2Map.get(pr.Product2__r.Id).contains('1') || 
-                            pro2Map.get(pr.Product2__r.Id).contains('2') || 
-                            pro2Map.get(pr.Product2__r.Id).contains('3')) {
-                            prlMap.put(pr.Product2__r.Id, '浜岀被');
-                        }else {
-                            prlMap.put(pr.Product2__r.Id, '闈炵洃绠�');
+                if (prl.Product_Register__r.MedPrdClass__c == '1' && prl.Product_Register__r.Stelsedag__c <> null) {
+                    if (prSteMap.containsKey(prl.Product2__r.Id)) {
+                        if (prSteMap.get(prl.Product2__r.Id) > prl.Product_Register__r.Stelsedag__c) {
+                            prSteMap.put(prl.Product2__r.Id,prl.Product_Register__r.Stelsedag__c);
                         }
                     }else {
-                        //鍏ュ簱鏃ュぇ浜庣淮鎶ゆ棩
-                        if (pr.Product2__r.Diedatvanink__c > prSteMap.get(pr.Product2__r.Id)) {
-                            //鐢熶骇鏃ュぇ浜庣淮鎶ゆ棩
-                            if (pr.Product2__r.demoteer_Sap__c > prSteMap.get(pr.Product2__r.Id)) {
-                                prlMap.put(pr.Product2__r.Id, '涓�绫�');
-                            }else if (pr.Product2__r.demoteer_Sap__c < prSteMap.get(pr.Product2__r.Id)) {
-                                //鐢熶骇鏃ュ皬浜庣淮鎶ゆ棩
-                                if (pro2Map.get(pr.Product2__r.Id).contains('1')) {
-                                    prlMap.put(pr.Product2__r.Id, '涓�绫�');
-                                }else if (!pro2Map.get(pr.Product2__r.Id).contains('1') && 
-                                          (pro2Map.get(pr.Product2__r.Id).contains('2') || 
-                                           pro2Map.get(pr.Product2__r.Id).contains('3'))) {
-                                    prlMap.put(pr.Product2__r.Id, '浜岀被');
+                        prSteMap.put(prl.Product2__r.Id,prl.Product_Register__r.Stelsedag__c);
+                    }
+                }
+
+                if ((prl.Product_Register__r.MedPrdClass__c == '2' || prl.Product_Register__r.MedPrdClass__c == '2') && prl.Product_Register__r.Stelsedag__c <> null) {
+                    if (prSte1Map.containsKey(prl.Product2__r.Id)) {
+                        if (prSte1Map.get(prl.Product2__r.Id) > prl.Product_Register__r.Stelsedag__c) {
+                            prSte1Map.put(prl.Product2__r.Id,prl.Product_Register__r.Stelsedag__c);
+                        }
+                    }else {
+                        prSte1Map.put(prl.Product2__r.Id,prl.Product_Register__r.Stelsedag__c);
+                    }
+                }
+            }
+        }
+        
+        //瀛樻斁浜у搧id鍜屽瓧绗︿覆鍊�
+        Map<Id,String> prlMap = new Map<Id,String>();
+        Map<Id,String> prl1Map = new Map<Id,String>();
+        Map<Id,String> prl2Map = new Map<Id,String>();
+        System.debug('pro2Map='+pro2Map);
+        System.debug('prSteMap='+prSteMap);
+        System.debug('prSte1Map='+prSte1Map);
+        
+        //閫氳繃涓婇潰鐨勫惊鐜紝鎶婂叧绯昏〃杩囪幏鍙栫殑鏁版嵁璇诲嚭鏉ワ紝浼犵粰map
+        if (pro2Map.size()>0) {
+            for (Product_Register_Link__c pr: pro2List) {
+                if (pro2Map.containsKey(pr.Product2__r.Id) && (prSteMap.containsKey(pr.Product2__r.Id) || prSte1Map.containsKey(pr.Product2__r.Id))) {
+                    Date stedate = prSteMap.get(pr.Product2__r.Id)==null?prSte1Map.get(pr.Product2__r.Id)==null?null:prSte1Map.get(pr.Product2__r.Id):prSte1Map.get(pr.Product2__r.Id);
+                    if (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <> null && pr.Product2__r.Diedatvanink__c <> null) {
+                        if (pr.Product_Register__r.Stelsedag__c <> null) {
+                            if (pr.Product2__r.Diedatvanink__c <= stedate) {
+                               if (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && newMap.get(pr.Product2__r.Id).demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c
+                                    && pr.Product_Register__r.MedPrdClass__c == '2' || pr.Product_Register__r.MedPrdClass__c == '3') {
+                                    System.debug('2');
+                                    if (prlMap.get(pr.Product2__r.Id) == null) {
+                                        prlMap.put(pr.Product2__r.Id, '浜岀被');
+                                    }else {
+                                        String Lins = prlMap.get(pr.Product2__r.Id);
+                                        Lins += '浜岀被';
+                                        prlMap.put(pr.Product2__r.Id, Lins);
+                                    }
+                                }else if (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && newMap.get(pr.Product2__r.Id).demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c
+                                    && (pr.Product_Register__r.MedPrdClass__c == '1')) {
+                                    System.debug('1');
+                                        if (prlMap.get(pr.Product2__r.Id) == null) {
+                                            prlMap.put(pr.Product2__r.Id, '涓�绫�');
+                                        }else {
+                                            String Lins = prlMap.get(pr.Product2__r.Id);
+                                            Lins += '涓�绫�';
+                                            prlMap.put(pr.Product2__r.Id, Lins);
+                                        }
                                 }else {
-                                    prlMap.put(pr.Product2__r.Id, '闈炵洃绠�');
+                                    System.debug('闈�');
+                                    if (prlMap.get(pr.Product2__r.Id) == null) {
+                                        prlMap.put(pr.Product2__r.Id, '闈炵洃绠�');
+                                    }else {
+                                        String Lins = prlMap.get(pr.Product2__r.Id);
+                                        Lins += '闈炵洃绠�';
+                                        prlMap.put(pr.Product2__r.Id, Lins);
+                                    }
+                                }
+                            }else if (pr.Product2__r.Diedatvanink__c > stedate) {
+                                if (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && newMap.get(pr.Product2__r.Id).demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c
+                                    && pr.Product_Register__r.MedPrdClass__c == '1') {
+                                    if (prl1Map.get(pr.Product2__r.Id) == null) {
+                                        prl1Map.put(pr.Product2__r.Id, '涓�绫�');
+                                    }else {
+                                        String Lins = prl1Map.get(pr.Product2__r.Id);
+                                        Lins += '涓�绫�';
+                                        prl1Map.put(pr.Product2__r.Id, Lins);
+                                    }
+                                }else if (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && newMap.get(pr.Product2__r.Id).demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c
+                                    && pr.Product_Register__r.MedPrdClass__c == '2' || pr.Product_Register__r.MedPrdClass__c == '3') {
+                                    if (prl1Map.get(pr.Product2__r.Id) == null) {
+                                        prl1Map.put(pr.Product2__r.Id, '浜岀被');
+                                    }else {
+                                        String Lins = prl1Map.get(pr.Product2__r.Id);
+                                        Lins += '浜岀被';
+                                        prl1Map.put(pr.Product2__r.Id, Lins);
+                                    }
+                                }else {
+                                    if (prlMap.get(pr.Product2__r.Id) == null) {
+                                        prlMap.put(pr.Product2__r.Id, '闈炵洃绠�');
+                                    }else {
+                                        String Lins = prlMap.get(pr.Product2__r.Id);
+                                        Lins += '闈炵洃绠�';
+                                        prlMap.put(pr.Product2__r.Id, Lins);
+                                    }
                                 }
                             }
+                        }else {
+                            prl2Map.put(pr.Product2__r.Id, '绌�');
                         }
                     }
                 }
             }
         }
-        System.debug('prlMap='+prlMap);
+                System.debug('prlMap='+prlMap);
+                System.debug('prl1Map='+prl1Map);
 
+        //缁欎骇鍝佷笂鐨勭瓑绾х被鍒瓧娈佃祴鍊�
         for (Product2 pr2: newList) {
             if (prlMap.containsKey(pr2.Id)) {
-                pr2.Level_Category__c = prlMap.get(pr2.Id);
-            }else {
+                if ((prlMap.get(pr2.Id).contains('浜岀被') && prlMap.get(pr2.Id).contains('涓�绫�')) || 
+                    (!prlMap.get(pr2.Id).contains('涓�绫�') && prlMap.get(pr2.Id).contains('浜岀被'))){
+                    pr2.Level_Category__c = '浜岀被';
+                }else if (prlMap.get(pr2.Id).contains('涓�绫�') && !prlMap.get(pr2.Id).contains('浜岀被')) {
+                    pr2.Level_Category__c = '涓�绫�';
+                }else if (prlMap.get(pr2.Id).contains('闈炵洃绠�') && !prl2Map.containsKey(pr2.Id)) {
+                    pr2.Level_Category__c = '闈炵洃绠�';
+                }else {
+                    pr2.Level_Category__c = null;
+                }
+            }
+            if (prl1Map.containsKey(pr2.Id)) {
+                if (prl1Map.get(pr2.Id).contains('涓�绫�')) {
+                    pr2.Level_Category__c = '涓�绫�';
+                }else if (prl1Map.get(pr2.Id).contains('浜岀被') && !prl1Map.get(pr2.Id).contains('涓�绫�')) {
+                    pr2.Level_Category__c = '浜岀被';
+                }else if (prl1Map.get(pr2.Id).contains('闈炵洃绠�') && !prl2Map.containsKey(pr2.Id)) {
+                    pr2.Level_Category__c = '闈炵洃绠�';
+                }else {
+                    pr2.Level_Category__c = null;
+                }
+            }
+            if (pr2.demoteer_Sap__c == null || pr2.Diedatvanink__c == null) {
                 pr2.Level_Category__c = null;
             }
-        }
+            System.debug('pr2.Level_Category__c='+pr2.Level_Category__c);
+        }    
     }
 }
\ No newline at end of file

--
Gitblit v1.9.1