From b329ab986e250bb27e46ace97cf208f3b26d145a Mon Sep 17 00:00:00 2001
From: FUYU <fuyu@prec-tech.com>
Date: 星期一, 18 十二月 2023 17:38:15 +0800
Subject: [PATCH] 修改复制(不带产品按钮)

---
 force-app/main/default/triggers/AttachmentTrigger.trigger |  220 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 214 insertions(+), 6 deletions(-)

diff --git a/force-app/main/default/triggers/AttachmentTrigger.trigger b/force-app/main/default/triggers/AttachmentTrigger.trigger
index caec12f..5244847 100644
--- a/force-app/main/default/triggers/AttachmentTrigger.trigger
+++ b/force-app/main/default/triggers/AttachmentTrigger.trigger
@@ -1,10 +1,13 @@
-trigger AttachmentTrigger on Attachment (before insert,after insert,before delete,after delete) {
+trigger AttachmentTrigger on Attachment (before insert,after insert,before delete,after delete, before update, after update) {
     List<String> rentalIds = new List<String>();
     List<String> attIds = new List<String>();
     List<String> rentaldelIds = new List<String>();
     List<String> attdelIds = new List<String>();
     List<String> rentalApplyIds = new List<String>();
     List<String> accIds = new List<String>();
+    //20221014 yc 鍖婚櫌瀹℃壒浼樺寲 start
+    List<String> accAppIds = new List<String>();
+    //20221014 yc 鍖婚櫌瀹℃壒浼樺寲 end
     // LHJ CHAN-BCLD4P 20190618 Start
     List<String> specIds = new List<String>();
     List<String> specDelIds = new List<String>();
@@ -18,7 +21,7 @@
     List<String> OpporFileIds = new List<String>();
     Map<String,String> OpporFileUndeleteMap = new Map<String,String>();
     List<String> DeleteReIds = new List<String>();
-Map<String,String> OppordeleteReUserMap = new Map<String,String>();
+    Map<String,String> OppordeleteReUserMap = new Map<String,String>();
     Map<String,Datetime> OppordeleteReTimeMap = new Map<String,Datetime>();
     //CHAN-BCNCRB ---XHL----Start-----
     List<String> opportunity_FileIds = new List<String>();
@@ -26,6 +29,14 @@
     List<String> opportunityFileOrderIds = new List<String>();
     List<String> oFOrderIds = new List<String>();
     Map<String,Boolean> oFOrderIdMap = new Map<String,Boolean>();
+
+    // sx 20230210start 澶囧搧鍊熷嚭鐢宠鏁呴殰鎶ュ憡闄勪欢鎻愬彇
+    List<String> rentalApplyFaultIds = new List<String>();
+    Map<String,List<Id>> trentalImages = new Map<String,List<Id>>();
+    List<String> rentaldeleteApplyFaultIds = new List<String>();
+    Map<String,List<Id>> deleteImages = new Map<String,List<Id>>();
+    // sx 20230210end 澶囧搧鍊熷嚭鐢宠鏁呴殰鎶ュ憡闄勪欢鎻愬彇
+
     if (Trigger.isBefore && Trigger.isInsert) {
         for (Attachment att : Trigger.new) {
             if(String.valueOf(att.ParentId).startsWith(System.Label.Oppor_sepKey)){
@@ -52,6 +63,11 @@
             if (String.valueOf(att.ParentId).startsWith('001')) {
                 accIds.add(att.ParentId);
             }
+            //20221014 yc 鍖婚櫌瀹℃壒浼樺寲 start
+            if (String.valueOf(att.ParentId).startsWith('a5C') || String.valueOf(att.ParentId).startsWith('a5A')) {
+                accAppIds.add(att.ParentId);
+            }
+            //20221014 yc 鍖婚櫌瀹℃壒浼樺寲 end
             if(String.valueOf(att.ParentId).startsWith(System.Label.Oppor_sepKey)){
                 OpporFileIds.add(att.ParentId);
             }
@@ -72,9 +88,25 @@
                 oFOrderIds.add(att.ParentId);
                 oFOrderIdMap.put(att.ParentId,true);
             }
+
+            //2023-2-10  sx澶囧搧 澶囧搧妫�娴嬪垎鏋愭姤鍛� start
+            Id cid = att.ParentId;
+            String parentLabel = cid.getSObjectType().getDescribe().getName();
+            if(parentLabel.equals('Rental_Apply_Fault__c')){
+                rentalApplyFaultIds.add(att.ParentId);
+                if(trentalImages.containskey(att.ParentId)){
+                    trentalImages.get(att.ParentId).add(att.Id);
+                }else{
+                    List<Id> temp = new List<Id>();
+                    temp.add(att.Id);
+                    trentalImages.put(att.ParentId,temp);
+                }
+            }
+            //2023-2-10  sx澶囧搧 澶囧搧妫�娴嬪垎鏋愭姤鍛� end
         }
     }
     if (Trigger.isBefore && Trigger.isDelete) {
+        List<Attachment> attDeleteList = new List<Attachment>();
         for (Attachment att : Trigger.old) {
             //if (att.Name.toUpperCase() == '鍚堝悓涔�.PDF' && String.valueOf(att.ParentId).startsWith('a0t')) {
             // 娣讳粯銇椼仧濂戠磩鏇搞伄銉曘偂銈ゃ儷鍚嶃伅鎸囧畾銇с亶銇亜
@@ -94,7 +126,32 @@
                 oFOrderIds.add(att.ParentId);
                 oFOrderIdMap.put(att.ParentId,false);
             }
+            //2023-2-10  sx澶囧搧 澶囧搧妫�娴嬪垎鏋愭姤鍛� start
+            Id cid = att.ParentId;
+            String parentLabel = cid.getSObjectType().getDescribe().getName();
+            if(parentLabel.equals('Rental_Apply_Fault__c')){
+                rentaldeleteApplyFaultIds.add(att.ParentId);
+                attDeleteList.add(att);
+                if(deleteImages.containskey(att.ParentId)){
+                    deleteImages.get(att.ParentId).add(att.Id);
+                }else{
+                    List<Id> temp = new List<Id>();
+                    temp.add(att.Id);
+                    deleteImages.put(att.ParentId,temp);
+                }
+            }
+            //2023-2-10  sx澶囧搧 澶囧搧妫�娴嬪垎鏋愭姤鍛� end
         }
+        //2023-2-10  sx澶囧搧 澶囧搧妫�娴嬪垎鏋愭姤鍛� start
+        List<Rental_Apply_Fault__c> rafList = [SELECT Id, Name FROM Rental_Apply_Fault__c WHERE Id IN: rentaldeleteApplyFaultIds AND (status__c = '宸插彂閫�' OR status__c = '宸插弽棣�')];
+        for (Rental_Apply_Fault__c raf : rafList) {
+            for (Attachment att : attDeleteList) {
+                if (att.ParentId == raf.Id) {
+                    att.addError('闈炶崏妗堜腑鐨勬娴嬪垎鏋愭姤鍛婁笉鍏佽鍒犻櫎宸蹭笂浼犵殑鏁呴殰鍥剧墖');
+                }
+            }
+        }
+        //2023-2-10  sx澶囧搧 澶囧搧妫�娴嬪垎鏋愭姤鍛� end
     }
     if (Trigger.isAfter && Trigger.isDelete) {
         for (Attachment att : Trigger.old) {
@@ -106,10 +163,64 @@
                 specIds.add(att.ParentId);
             }
             // LHJ CHAN-BCLD4P 20190618 End
+            //20221014 yc 鍖婚櫌瀹℃壒浼樺寲 start
+            if (String.valueOf(att.ParentId).startsWith('a5C') || String.valueOf(att.ParentId).startsWith('a5A')) {
+                accAppIds.add(att.ParentId);
+            }
+            //20221014 yc 鍖婚櫌瀹℃壒浼樺寲 end
         }
     }
+    //20221207 lt 鍖婚櫌瀹℃壒浼樺寲 start  
+    if (Trigger.isAfter && Trigger.isUpdate) {
+        for (Attachment att : Trigger.old) {
+            if (String.valueOf(att.ParentId).startsWith('a5C') || String.valueOf(att.ParentId).startsWith('a5A')) {
+                accAppIds.add(att.ParentId);
+            }
 
-    
+            if (String.valueOf(att.ParentId).startsWith('001')) {
+                accIds.add(att.ParentId);
+            }
+        }
+    }
+    //20221207 lt 鍖婚櫌瀹℃壒浼樺寲 start
+    //2023-2-10  sx澶囧搧 澶囧搧妫�娴嬪垎鏋愭姤鍛� start
+    if(rentalApplyFaultIds.size() > 0){
+        List<Rental_Apply_Fault__c> renList = [SELECT Id,AttachmentId1__c,AttachmentId2__c from Rental_Apply_Fault__c where Id in :rentalApplyFaultIds];
+
+        system.debug('updrentalApplyFaultList====='+renList);
+        for(Rental_Apply_Fault__c ren : renList){
+            if(trentalImages.containskey(ren.Id)){ 
+                if (ren.AttachmentId1__c == null) {
+                    ren.AttachmentId1__c = trentalImages.get(ren.id)[0];
+                } else if(ren.AttachmentId2__c == null) {
+                    ren.AttachmentId2__c = trentalImages.get(ren.id)[0];
+                }
+            }
+        }
+        system.debug('updrentalApplyFaultList====='+renList);
+        UPDATE renList;
+    }
+
+    if(rentaldeleteApplyFaultIds.size() > 0){
+        List<Rental_Apply_Fault__c> renList = [SELECT Id,AttachmentId1__c,AttachmentId2__c from Rental_Apply_Fault__c where Id in :rentaldeleteApplyFaultIds];
+
+        system.debug('renList====='+renList);
+        for(Rental_Apply_Fault__c ren : renList){
+            if(deleteImages.containskey(ren.Id)){
+
+                if(deleteImages.get(ren.Id).contains(ren.AttachmentId1__c)){
+                    ren.AttachmentId1__c = '';
+                }
+                if(deleteImages.get(ren.Id).contains(ren.AttachmentId2__c)){
+                    ren.AttachmentId2__c = '';
+                }
+            }
+        }
+        system.debug('renList====='+renList);
+        UPDATE renList;
+    }
+    //2023-2-10  sx澶囧搧 澶囧搧妫�娴嬪垎鏋愭姤鍛� end
+
     if (rentaldelIds.size() > 0) {
         List<Consumable_accessories_invoice__c> rentaldelList = new List<Consumable_accessories_invoice__c>();
         for (Consumable_accessories_invoice__c ra : [select Id from Consumable_accessories_invoice__c where Invoice_code__c in :rentaldelIds and Attachment_ID__c in :attdelIds]) {
@@ -146,13 +257,109 @@
             attMap.put(att.ParentId, att.ParentId);
         }
         List<Account> updAccList = new List<Account>();
+
+        //20221207 lt 鍖婚櫌瀹℃壒浼樺寲 start
+        List<Account> accList = [select id,Name,
+                                                         Is_Active__c
+                                               from Account
+                                               where id in:accIds ];
+        //20221207 lt 鍖婚櫌瀹℃壒浼樺寲 end
+
         for (String accid : accIds) {
-            Account acc = new Account(Id = accid);
-            acc.Is_upload_file__c = attMap.containsKey(accid);
-            updAccList.add(acc);
+            if(accList.size() > 0){
+                // Account acc = new Account(Id = accid);
+                //20221207 lt 鍖婚櫌瀹℃壒浼樺寲 start
+                for(Account acc : accList){                    
+                    if(Trigger.isUpdate && Trigger.isAfter){
+                        for(Attachment att : Trigger.New){
+                            System.debug('lt123---鏇存柊---鐘舵��---'+acc.Is_Active__c);
+                            if(attMap.containsKey(att.ParentID) && acc.Is_Active__c != '鑽夋涓�'){
+                                System.debug('lt123---鑽夋涓�---');
+                                att.addError('闈炶崏妗堜腑锛岄檮浠朵笉鍏佽缂栬緫銆�');
+                
+                                return;
+                            }
+                        }
+                    }
+
+                    if(Trigger.isDelete && (Trigger.isBefore || Trigger.isAfter)){
+                        for(Attachment att : Trigger.old){
+                            // if(attMap.containsKey(att.ParentID) && acc.Is_Active__c != '鑽夋涓�'){
+                            if(acc.Is_Active__c != '鑽夋涓�'){
+                                att.addError('闈炶崏妗堜腑锛岄檮浠朵笉鍏佽鍒犻櫎銆�');
+                
+                                return;
+                            }
+                        }
+                    }
+                    acc.Is_upload_file__c = attMap.containsKey(accid);
+                    updAccList.add(acc);
+                }
+            }
+            //20221207 lt 鍖婚櫌瀹℃壒浼樺寲 end
         }
+
+        // for (String accid : accIds) {
+        //     Account acc = new Account(Id = accid);
+        //     acc.Is_upload_file__c = attMap.containsKey(accid);
+        //     updAccList.add(acc);
+        // }
         if (updAccList.size() > 0) update updAccList;
     }
+    //20221014 yc 鍖婚櫌瀹℃壒浼樺寲 start
+    if (accAppIds.size() > 0) {
+        Map<String, String> attMap = new Map<String, String>();
+        List<Attachment> attList = [select Id, ParentId from Attachment where ParentId = :accAppIds order by ParentId];
+        for (Attachment att : attList) {
+            attMap.put(att.ParentId, att.ParentId);
+        }
+        List<Account_Delay_Apply__c> updAccAppList = new List<Account_Delay_Apply__c>();
+        
+        //20221207 lt 鍖婚櫌瀹℃壒浼樺寲 start
+        List<Account_Delay_Apply__c> adaList = [select id,Name,
+                                                         Is_Active__c
+                                               from Account_Delay_Apply__c 
+                                               where id in:accAppIds ];
+        //20221207 lt 鍖婚櫌瀹℃壒浼樺寲 end
+
+        for (String accappid : accAppIds) {
+            if(adaList.size() > 0){
+                // Account_Delay_Apply__c accapp = new Account_Delay_Apply__c(Id = accappid);
+                //20221207 lt 鍖婚櫌瀹℃壒浼樺寲 start
+                
+                for(Account_Delay_Apply__c accapp : adaList){                    
+                    if(Trigger.isUpdate && Trigger.isAfter){
+                        for(Attachment att : Trigger.New){
+                            if(attMap.containsKey(att.ParentID) && accapp.Is_Active__c != '鑽夋涓�'){
+                                att.addError('闈炶崏妗堜腑锛岄檮浠朵笉鍏佽缂栬緫銆�');
+                
+                                return;
+                            }
+                        }
+                    }
+                    if(Trigger.isDelete){
+                        for(Attachment att : Trigger.old){
+                            // if(attMap.containsKey(att.ParentID) && accapp.Is_Active__c != '鑽夋涓�'){
+                            if(accapp.Is_Active__c != '鑽夋涓�'){
+                                att.addError('闈炶崏妗堜腑锛岄檮浠朵笉鍏佽鍒犻櫎銆�');
+                
+                                return;
+                            }
+                        }
+                    }
+                    accapp.Is_upload_file__c = attMap.containsKey(accappid);
+                    updAccAppList.add(accapp);
+                }
+                
+            }
+            
+            //20221207 lt 鍖婚櫌瀹℃壒浼樺寲 end
+            
+        }
+        if (updAccAppList.size() > 0) update updAccAppList;
+    }
+    //20221014 yc 鍖婚櫌瀹℃壒浼樺寲 end
+
     // LHJ CHAN-BCLD4P 20190618 Start
     if (specIds.size() > 0) {
         
@@ -376,4 +583,5 @@
 
         }                                            
     }
+
 }
\ No newline at end of file

--
Gitblit v1.9.1