From d29f75b84fccd06917fb8e2c2ee70e9d76f868cd Mon Sep 17 00:00:00 2001
From: chenjingwu <chenjingwu@prec-tech.com>
Date: 星期四, 11 四月 2024 20:15:54 +0800
Subject: [PATCH] 123

---
 force-app/main/default/triggers/ContentDocumentTrigger.trigger |  657 +++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 365 insertions(+), 292 deletions(-)

diff --git a/force-app/main/default/triggers/ContentDocumentTrigger.trigger b/force-app/main/default/triggers/ContentDocumentTrigger.trigger
index c61899d..46df1e0 100644
--- a/force-app/main/default/triggers/ContentDocumentTrigger.trigger
+++ b/force-app/main/default/triggers/ContentDocumentTrigger.trigger
@@ -1,117 +1,235 @@
-trigger ContentDocumentTrigger on ContentDocument (before delete) {
-    List<String> rentalIds = new List<String>();
-    List<String> attIds = new List<String>();
+trigger ContentDocumentTrigger on ContentDocument (before delete,before update) {
+    // 20231103 闄堜含姝� Lightning鏂囦欢淇敼 Start
+    if(StaticParameter.ContentDocumentTrigger == false){
+        return;
+    }
+    if((!Test.isRunningTest())&&UserInfo.getUserId()==System.Label.ByPassTrigger){
+        return;
+    }
+    
+    // LHJ CHAN-BCLD4P 20190618 Start
+    List<String> specIds = new List<String>();
+    // LHJ CHAN-BCLD4P 20190618 End
+    System.debug('enter ContentDocumentTrigger');
     List<String> rentaldelIds = new List<String>();
     List<String> attdelIds = new List<String>();
-    List<String> rentalApplyIds = new List<String>();
+    // LHJ CHAN-BCLD4P 20190618 Start
+    List<String> specDelIds = new List<String>();
+    // LHJ CHAN-BCLD4P 20190618 End
     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>();
-    // LHJ CHAN-BCLD4P 20190618 End
-
-    // MZY SWAG-BXXBKA  2021-02-19 start
-    List<String> opdIds = new List<String>();
-    Map<String,String> opdMap = new Map<String,String>();
-    // MZY SWAG-BXXBKA  2021-02-19 end
-
     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,Datetime> OppordeleteReTimeMap = new Map<String,Datetime>();
-    //CHAN-BCNCRB ---XHL----Start-----
-    List<String> opportunity_FileIds = new List<String>();
 
-    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 澶囧搧鍊熷嚭鐢宠鏁呴殰鎶ュ憡闄勪欢鎻愬彇
+
+
+    //deloitte-zhj 鍙戠エ闄勪欢鍒犻櫎 20230901 start
+    String invoiceStart = System.Label.invoiceStart;
+    List<String> rentaldelIdsZhj = new List<String>();
+    List<String> attdelIdsZhj = new List<String>();
+    Set<Id> docidList = new Set<Id>();
+    Set<Id> delDocidList = new Set<Id>();
+    if(Trigger.isDelete && Trigger.isBefore){
+        for (ContentDocument doc : Trigger.old) {
+            docidList.add(doc.Id);
+            delDocidList.add(doc.Id);
+        }
+    }
+    if(Trigger.isUpdate && Trigger.isBefore){
+        for (ContentDocument doc : Trigger.old) {
+            docidList.add(doc.Id);
+        }
+    }
+    ContentVersion ver = new ContentVersion();
+    if(docidList.size() > 0){
+        ver = [select FileType,TextPreview from ContentVersion where ContentDocumentId in: docidList limit 1];
+        System.debug('TextPreview=>' + ver.TextPreview);
+        System.debug('FileType=>' + ver.TextPreview);
+        if(ver.FileType == 'SNOTE'){
+            System.debug('鍙伓鐨勫娉紒锛侊紒');
+            return;
+        }
+    }
+    if (Trigger.isBefore && Trigger.isUpdate) {
+        List<Id> idList = new List<Id>();
+        for(ContentDocument doc: Trigger.old){
+            idList.add(doc.Id);
+        }
+        List<ContentDocumentLink> linkList = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList];
+        for (ContentDocumentLink link : linkList) {
+            if (String.valueOf(link.LinkedEntityId).startsWith('a5C') || String.valueOf(link.LinkedEntityId).startsWith('a5A')) {
+                accAppIds.add(link.LinkedEntityId);
+            }
+
+            if (String.valueOf(link.LinkedEntityId).startsWith('001')) {
+                accIds.add(link.LinkedEntityId);
+            }
+        }
+    }
+
+    if (Trigger.isBefore && Trigger.isDelete) {
+        List<Id> idList = new List<Id>();
+        for(ContentDocument doc: Trigger.old){
+            idList.add(doc.Id);
+        }
+        List<ContentDocumentLink> linkList = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList];
+        System.debug('Link鐨勬潯鏁帮細' + linkList.size());
+        for (ContentDocumentLink link : linkList) {
+            if (String.valueOf(link.LinkedEntityId).startsWith('001')) {
+                accIds.add(link.LinkedEntityId);
+            }
+            // LHJ CHAN-BCLD4P 20190618 Start
+            if(String.valueOf(link.LinkedEntityId).startsWith(System.Label.OppSpecialApply)){
+                specIds.add(link.LinkedEntityId);
+            }
+            // LHJ CHAN-BCLD4P 20190618 End
+            //20221014 yc 鍖婚櫌瀹℃壒浼樺寲 start
+            if (String.valueOf(link.LinkedEntityId).startsWith('a5C') || String.valueOf(link.LinkedEntityId).startsWith('a5A')) {
+                accAppIds.add(link.LinkedEntityId);
+            }
+            //20221014 yc 鍖婚櫌瀹℃壒浼樺寲 end
+        }
+        for (ContentDocument cd : Trigger.old) {
+            System.debug('cd.Id = ' + cd.Id);
+            List<ContentDocumentLink> cdlList = [SELECT id, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId = :cd.Id];
+            System.debug('cdlList = ' + cdlList);
+            System.debug('cd.Id = ' + cd.Id);
+            for (ContentDocumentLink cdl : cdlList) {
+                if (String.valueOf(cdl.LinkedEntityId).startsWith(invoiceStart)) {
+                    System.debug('String.valueOf(cdl.LinkedEntityId).startsWith(invoiceStart)');
+                    rentaldelIdsZhj.add(cdl.LinkedEntityId);
+                    attdelIdsZhj.add(cd.Id);
+                }
+            }
+        }
+    }
+    if (rentaldelIdsZhj.size() > 0) {
+        System.debug('rentaldelIdsZhj = ' + rentaldelIdsZhj);
+        System.debug('attdelIdsZhj = ' + attdelIdsZhj);
+        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 :rentaldelIdsZhj AND Attachment_ID__c IN :attdelIdsZhj
+        ]) {
+            rentaldelList.add(ra);
+        }
+        System.debug('rentaldelList = ' + rentaldelList);
+        ControllerUtil.delConsumableAccessoriesInvoiceSet(rentaldelList);
+    }
+    //deloitte-zhj 鍙戠エ闄勪欢鍒犻櫎 20230901 end
 
     //CHAN-BCNCRB ---XHL----End-----
    
     if (Trigger.isBefore && Trigger.isDelete) {
         // List<Attachment> attDeleteList = new List<Attachment>();
         List<ContentDocumentLink> conDeleteList = new List<ContentDocumentLink>();
+        List<Id> idList = new List<Id>();
         for (ContentDocument doc : Trigger.old) {
-            List<ContentDocumentLink> link = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId =: doc.Id limit 1];
+            idList.add(doc.Id);
+        }
+        List<ContentDocumentLink> linkList = [select ContentDocumentId,LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList];
+        List<ContentVersion> version = [select Id from ContentVersion where ContentDocumentId in: idList];
+        System.debug('Version鐨勬潯鏁帮細' + version.size());
+        Id cid;
+        ContentDocumentLink link;
+        for(ContentDocumentLink lin: linkList){
+            System.debug('...' + lin.LinkedEntityId.getSObjectType().getDescribe().getName());
+            if(lin.LinkedEntityId.getSObjectType().getDescribe().getName() != 'User'){
+                cid = lin.LinkedEntityId;
+                link = lin;
+            }
+        }
+        if(cid == null){
+            return;
+        }
+        for (ContentDocument doc : Trigger.old) {
             //if (att.Name.toUpperCase() == '鍚堝悓涔�.PDF' && String.valueOf(att.ParentId).startsWith('a0t')) {
             // 娣讳粯銇椼仧濂戠磩鏇搞伄銉曘偂銈ゃ儷鍚嶃伅鎸囧畾銇с亶銇亜
-            ContentVersion version = [select Id from ContentVersion where ContentDocumentId =: doc.Id];
-            if (String.valueOf(link[0].LinkedEntityId).startsWith('a2K')) {
-                rentaldelIds.add(link[0].LinkedEntityId);
-                attdelIds.add(version.Id);
+            if (String.valueOf(cid).startsWith('a2K')) {
+                rentaldelIds.add(cid);
+                attdelIds.add(version[0].Id);
             }
-            if(String.valueOf(link[0].LinkedEntityId).startsWith(System.Label.Oppor_sepKey)){
-                OpporFileIds.add(link[0].LinkedEntityId);
+            if(String.valueOf(cid).startsWith(System.Label.Oppor_sepKey)){
+                OpporFileIds.add(cid);
             }
             // LHJ CHAN-BCLD4P 20190618 Start
-            if(String.valueOf(link[0].LinkedEntityId).startsWith(System.Label.OppSpecialApply)){
-                specDelIds.add(link[0].LinkedEntityId);
+            if(String.valueOf(cid).startsWith(System.Label.OppSpecialApply)){
+                specDelIds.add(cid);
             }
             // LHJ CHAN-BCLD4P 20190618 End
-            if(String.valueOf(link[0].LinkedEntityId).startsWith('a3Z')){
-                oFOrderIds.add(link[0].LinkedEntityId);
-                oFOrderIdMap.put(link[0].LinkedEntityId,false);
+            if(String.valueOf(cid).startsWith('a3Z')){
+                oFOrderIds.add(cid);
+                oFOrderIdMap.put(cid,false);
             }
             //2023-2-10  sx澶囧搧 澶囧搧妫�娴嬪垎鏋愭姤鍛� start
-            Id cid = link[0].LinkedEntityId;
             String parentLabel = cid.getSObjectType().getDescribe().getName();
+            System.debug('...' + cid.getSObjectType().getDescribe().getName());
             if(parentLabel.equals('Rental_Apply_Fault__c')){
-                rentaldeleteApplyFaultIds.add(link[0].LinkedEntityId);
-                conDeleteList.add(link[0]);
-                if(deleteImages.containskey(version.Id)){
-                    deleteImages.get(link[0].LinkedEntityId).add(version.Id);
+                rentaldeleteApplyFaultIds.add(cid);
+                conDeleteList.add(link);
+                if(deleteImages.containskey(version[0].Id)){
+                    deleteImages.get(cid).add(version[0].Id);
                 }else{
                     List<Id> temp = new List<Id>();
-                    temp.add(version.Id);
-                    deleteImages.put(link[0].LinkedEntityId,temp);
+                    temp.add(version[0].Id);
+                    deleteImages.put(cid,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 = '宸插弽棣�')];
+        List<Id> docList = new List<Id>();
         for (Rental_Apply_Fault__c raf : rafList) {
-            for (ContentDocumentLink link : conDeleteList) {
-                if (link.LinkedEntityId == raf.Id) {
-                    ContentDocument doc = [select Id from ContentDocument where Id =: link.ContentDocumentId];
-                    doc.addError('闈炶崏妗堜腑鐨勬娴嬪垎鏋愭姤鍛婁笉鍏佽鍒犻櫎宸蹭笂浼犵殑鏁呴殰鍥剧墖');
+            for (ContentDocumentLink link1 : conDeleteList) {
+                if (link1.LinkedEntityId == raf.Id) {
+                    docList.add(link1.ContentDocumentId);
                 }
+            }
+        }
+        for(ContentDocument doc: Trigger.old){
+            if(docList.contains(doc.Id)){
+                doc.addError('闈炶崏妗堜腑鐨勬娴嬪垎鏋愭姤鍛婁笉鍏佽鍒犻櫎宸蹭笂浼犵殑鏁呴殰鍥剧墖');
             }
         }
         //2023-2-10  sx澶囧搧 澶囧搧妫�娴嬪垎鏋愭姤鍛� end
     }
 
-    
+    if (specIds.size() > 0) {
+        
+        Map<String, String> attMap = new Map<String, String>();
+        // List<Attachment> attList = [select Id, ParentId from Attachment where ParentId = :specIds order by ParentId];
+        List<ContentDocumentLink> linkList = [select ContentDocumentId,LinkedEntityId from ContentDocumentLink where   ContentDocument.FileType != 'SNOTE' and   LinkedEntityId =: specIds order by LinkedEntityId];
+        for (ContentDocumentLink link : linkList) {
+            // attMap.put(link.LinkedEntityId, link.LinkedEntityId);
+            if(!delDocidList.contains(link.ContentDocumentId)){
+                attMap.put(link.LinkedEntityId, link.LinkedEntityId);
+            }
+        }
+        List<OpportunitySpecialApply__c> updSpecList = new List<OpportunitySpecialApply__c>();
+        for (String specId : specIds) {
+            OpportunitySpecialApply__c spec = new OpportunitySpecialApply__c(Id = specId);
+            spec.Is_upload_file__c = attMap.containsKey(specId);
+            updSpecList.add(spec);
+        }
+        if (updSpecList.size() > 0) update updSpecList;
+    }
 
     //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];
@@ -119,13 +237,19 @@
         system.debug('renList====='+renList);
         for(Rental_Apply_Fault__c ren : renList){
             if(deleteImages.containskey(ren.Id)){
-
+                System.debug('att1' + ren.AttachmentId1__c);
+                System.debug('att2' + ren.AttachmentId2__c);
+                System.debug('ver' + deleteImages.get(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 = '';
                 }
+                // if(!(deleteImages.get(ren.Id).contains(ren.AttachmentId2__c)) && !(deleteImages.get(ren.Id).contains(ren.AttachmentId1__c))){
+                //     ren.AttachmentId2__c = '';
+                //     ren.AttachmentId1__c = '';
+                // }
             }
         }
         system.debug('renList====='+renList);
@@ -142,157 +266,7 @@
         ControllerUtil.delConsumableAccessoriesInvoiceSet(rentaldelList);
     }
     
-    if (rentalIds.size() > 0) {
-        List<Consumable_order__c> rentalList = new List<Consumable_order__c>();
-        for (Consumable_order__c ra : [select Id from Consumable_order__c where Id in :rentalIds]) {
-            ra.Consumable_pdf_insert_day__c = Date.today();
-            ra.Order_Attachment__c = attIds[0];
-            //ra.Order_status__c = '褰曞叆瀹屾垚';
-            //ra.Contract_pdf_updated__c = true;
-            rentalList.add(ra);
-        }
-        ControllerUtil.updRentalApplyList(rentalList);
-    }
-     if (rentalApplyIds.size() > 0) {
-        List<Rental_Apply__c> rentalApplyList = new List<Rental_Apply__c>();
-        for (Rental_Apply__c ra : [select Id from Rental_Apply__c where Id in :rentalApplyIds]) {
-            ra.Contract_pdf_update_day__c = Date.today();
-            ra.Contract_pdf_updated__c = true;
-            rentalApplyList.add(ra);
-        }
-        ControllerUtil.updRentalApplyList(rentalApplyList);
-    }
-
-    if (accIds.size() > 0) {
-        Map<String, String> attMap = new Map<String, String>();
-        // List<Attachment> attList = [select Id, ParentId from Attachment where ParentId = :accIds order by ParentId];
-        List<ContentDocumentLink> linkList = [select LinkedEntityId from ContentDocumentLink where LinkedEntityId =: accIds order by LinkedEntityId];
-        for (ContentDocumentLink link : linkList) {
-            attMap.put(link.LinkedEntityId, link.LinkedEntityId);
-        }
-        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) {
-            if(accList.size() > 0){
-                // Account acc = new Account(Id = accid);
-                //20221207 lt 鍖婚櫌瀹℃壒浼樺寲 start
-                for(Account acc : accList){                    
-
-                    if(Trigger.isDelete && (Trigger.isBefore || Trigger.isAfter)){
-                        for(ContentDocument doc : Trigger.old){
-                            // if(attMap.containsKey(att.ParentID) && acc.Is_Active__c != '鑽夋涓�'){
-                            if(acc.Is_Active__c != '鑽夋涓�'){
-                                doc.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];
-        List<ContentDocumentLink> linkList = [select LinkedEntityId from ContentDocumentLink where LinkedEntityId =: accAppIds order by LinkedEntityId];
-        for (ContentDocumentLink link : linkList) {
-            attMap.put(link.LinkedEntityId, link.LinkedEntityId);
-        }
-        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.isDelete){
-                        for(ContentDocument doc : Trigger.old){
-                            // if(attMap.containsKey(att.ParentID) && accapp.Is_Active__c != '鑽夋涓�'){
-                            if(accapp.Is_Active__c != '鑽夋涓�'){
-                                doc.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) {
-        
-        Map<String, String> attMap = new Map<String, String>();
-        // List<Attachment> attList = [select Id, ParentId from Attachment where ParentId = :specIds order by ParentId];
-        List<ContentDocumentLink> linkList = [select LinkedEntityId from ContentDocumentLink where LinkedEntityId =: specIds order by LinkedEntityId];
-        for (ContentDocumentLink link : linkList) {
-            attMap.put(link.LinkedEntityId, link.LinkedEntityId);
-        }
-        List<OpportunitySpecialApply__c> updSpecList = new List<OpportunitySpecialApply__c>();
-        for (String specId : specIds) {
-            OpportunitySpecialApply__c spec = new OpportunitySpecialApply__c(Id = specId);
-            spec.Is_upload_file__c = attMap.containsKey(specId);
-            updSpecList.add(spec);
-        }
-        if (updSpecList.size() > 0) update updSpecList;
-    }
-    // LHJ CHAN-BCLD4P 20190618 End
     
-    // MZY SWAG-BXXBKA  2021-02-19 start
-    System.debug('SWAG-BXXBKA start');
-    if (opdIds.size()>0) {
-        List<OPDPlan__c> updateOPDs = new List<OPDPlan__c>();
-
-        OPDPlan__c tempOPD = new OPDPlan__c();
-        for (String i :opdIds ) {
-            tempOPD.id = i;
-            tempOPD.AttachmentCertificate__c = opdMap.get(i);
-            updateOPDs.add(tempOPD);
-        }
-        System.debug('涓存椂OPD :'+tempOPD);
-        if (updateOPDs.size()>0){
-            update updateOPDs;
-        }
-    }
-    System.debug('SWAG-BXXBKA end');
-    // MZY SWAG-BXXBKA  2021-02-19 end
 
     if(OpporFileIds.size()>0){
         List<Opportunity_File__c> OpporList = [select id,Last_upload_time__c,
@@ -323,26 +297,121 @@
 
 
     if(Trigger.isDelete && Trigger.isBefore){
+        List<Id> idList = new List<Id>();
         for(ContentDocument doc : Trigger.old){
-            List<ContentDocumentLink> link = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId =: doc.Id];
-            if(OpporFileUndeleteMap.containsKey(link[0].LinkedEntityId)){
+            idList.add(doc.Id);
+        }
+        List<ContentDocumentLink> link = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList];
+        Id enId;
+        for(ContentDocumentLink lin: link){
+            if(lin.LinkedEntityId.getSObjectType().getDescribe().getName() != 'User'){
+                enId = lin.LinkedEntityId;
+            }
+        }
+        for(ContentDocument doc : Trigger.old){
+            if(OpporFileUndeleteMap.containsKey(enId)){
                 doc.addError('澶囪揣宸插鎵癸紝涓嶅彲鍒犻櫎闄勪欢');
                 return;
             }else{
+                DeleteReIds.add(enId);
             }
         }
     }
-    if(Trigger.isDelete && Trigger.isAfter){
-        for(ContentDocument doc : Trigger.old){
-            List<ContentDocumentLink> link = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId =: doc.Id];
-            if(OpporFileUndeleteMap.containsKey(link[0].LinkedEntityId)){
-                doc.addError('澶囪揣宸插鎵癸紝涓嶅彲鍒犻櫎闄勪欢');
-                return;
-            }else{
-                DeleteReIds.add(link[0].LinkedEntityId);
+    // if(Trigger.isDelete && Trigger.isAfter){
+    //     List<Id> idList = new List<Id>();
+    //     for(ContentDocument doc : Trigger.old){
+    //         idList.add(doc.Id);
+    //     }
+    //     List<ContentDocumentLink> linkList = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList];
+    //     ContentDocumentLink link;
+    //     for(ContentDocumentLink lin: linkList){
+    //         if(lin.LinkedEntityId.getSObjectType().getDescribe().getName() != 'User'){
+    //             link = lin;
+    //         }
+    //     }
+    //     for(ContentDocument doc : Trigger.old){
+    //         // List<ContentDocumentLink> link = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId =: doc.Id];
+    //         if(OpporFileUndeleteMap.containsKey(link.LinkedEntityId)){
+    //             doc.addError('澶囪揣宸插鎵癸紝涓嶅彲鍒犻櫎闄勪欢');
+    //             return;
+    //         }else{
+    //             DeleteReIds.add(link.LinkedEntityId);
+    //         }
+    //     }
+    // }
+     //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];
+        List<ContentDocumentLink> linkList = [select ContentDocumentId,LinkedEntityId from ContentDocumentLink where   ContentDocument.FileType != 'SNOTE' and   LinkedEntityId =: accAppIds order by LinkedEntityId];
+        for (ContentDocumentLink link : linkList) {
+            // attMap.put(link.LinkedEntityId, link.LinkedEntityId);
+            if(!delDocidList.contains(link.ContentDocumentId)){
+                attMap.put(link.LinkedEntityId, link.LinkedEntityId);
             }
         }
+        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.isBefore){
+                        for(ContentDocument doc : Trigger.New){
+                            System.debug('鐘舵��1=銆�' + accapp.Is_Active__c);
+                            if(accapp.Is_Active__c != '鑽夋涓�'){
+                                doc.addError('闈炶崏妗堜腑锛岄檮浠朵笉鍏佽缂栬緫銆�');
+                
+                                return;
+                            }
+                        }
+                    }
+                    if(Trigger.isDelete && Trigger.isBefore){
+                        for(ContentDocument doc : Trigger.old){
+                            // if(attMap.containsKey(att.ParentID) && accapp.Is_Active__c != '鑽夋涓�'){
+                                System.debug('鐘舵��2=銆�' + accapp.Is_Active__c);
+                            if(accapp.Is_Active__c != '鑽夋涓�'){
+                                doc.addError('闈炶崏妗堜腑锛岄檮浠朵笉鍏佽鍒犻櫎銆�');
+                
+                                return;
+                            }
+                        }
+                    }
+                }
+                
+            }
+            
+            //20221207 lt 鍖婚櫌瀹℃壒浼樺寲 end
+            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){                    
+                    accapp.Is_upload_file__c = attMap.containsKey(accappid);
+                    updAccAppList.add(accapp);
+                }
+                
+            }
+            
+        }
+        try {
+            if (updAccAppList.size() > 0) update updAccAppList; 
+        } catch (Exception e) {
+            linkList[0].addError(e.getMessage());
+        }
+        
     }
+    //20221014 yc 鍖婚櫌瀹℃壒浼樺寲 end
+
     
 
     // List<Attachment> atcList = [select id,ParentId,createddate,createdbyid from Attachment where ParentId in:DeleteReIds];
@@ -352,21 +421,28 @@
         for (ContentDocumentLink link : linkList) {
             idList.add(link.ContentDocumentId);
         }
-        List<ContentDocument> docList = [select Id,ArchivedDate,ArchivedById from ContentDocument where Id =: idList];
+        List<ContentDocument> docList = [select Id,CreatedDate,CreatedById from ContentDocument where Id =: idList];
         System.debug(DeleteReIds+'222222222222');
+        List<Id> oldList = new List<Id>();
+        for(ContentDocument doc : Trigger.old){
+            oldList.add(doc.Id);
+        }
         if(docList.size()>0){
             for(String ids : DeleteReIds){
                 for(ContentDocument doc : docList){
+                    if(oldList.contains(doc.Id)){
+                        continue;
+                    }
                     if(OppordeleteReTimeMap.containsKey(ids)){
-                        if(doc.ArchivedDate > OppordeleteReTimeMap.get(ids)){
-                            OppordeleteReTimeMap.put(ids,doc.ArchivedDate);
-                            OppordeleteReUserMap.put(ids,doc.ArchivedById);
+                        if(doc.CreatedDate > OppordeleteReTimeMap.get(ids)){
+                            OppordeleteReTimeMap.put(ids,doc.CreatedDate);
+                            OppordeleteReUserMap.put(ids,doc.CreatedById);
                         }else{
                             continue;
                         }
                     }else{
-                        OppordeleteReTimeMap.put(ids,doc.ArchivedDate);
-                        OppordeleteReUserMap.put(ids,doc.ArchivedById);
+                        OppordeleteReTimeMap.put(ids,doc.CreatedDate);
+                        OppordeleteReUserMap.put(ids,doc.CreatedById);
                     }
                 }
             }
@@ -404,72 +480,6 @@
     }
     
 
-    //CHAN-BCNCRB ---XHL----Start-----
-    if (opportunity_FileIds.size() > 0) {
-        List<Opportunity_File__c> opporList = [select id,Last_upload_time__c,
-                                                            Is_Locked__c,
-                                                            Last_upload_user__c,
-                                                            Stock_apply_time__c,
-                                                            Oppor_File_Stage__c
-                                                        from Opportunity_File__c 
-                                                        where id in:opportunity_FileIds And Oppor_File_Stage__c = 'G'];
-        List<String>  ofIds = new  List<String>();                                              
-        if (opporList.size() > 0) {
-
-            for (Opportunity_File__c oppf : opporList) {
-                ofIds.add(oppf.Id);
-            }
-
-            if (ofIds.size() > 0 ) {
-                //  List<Attachment> atcList = [select id,ParentId,createddate,createdbyid from Attachment where ParentId in:ofIds];
-                List<ContentDocumentLink> linkList = [select ContentDocumentId from ContentDocumentLink where LinkedEntityId in: ofIds];
-
-                List<Id> idList = new List<Id>();
-                for (ContentDocumentLink link : linkList) {
-                    idList.add(link.ContentDocumentId);
-                }
-                List<ContentDocument> docList = [select Id from ContentDocument where Id in: idList];
-                if (docList.size() > 0) {
-                    delete docList;
-                }
-            }
-           
-            
-        }
-    }
-    //CHAN-BCNCRB ---XHL----End-----
-    
-
-    if (opportunityFileOrderIds.size() > 0) {
-        List<OpportunityFileOrder__c> ofoList = [select id,
-                                                            OpporFileStage__c
-                                                        from OpportunityFileOrder__c 
-                                                        where Id in:opportunityFileOrderIds And OpporFileStage__c = 'G'];
-        List<String>  ofoIds = new  List<String>();                                              
-        if (ofoList.size() > 0) {
-
-            for (OpportunityFileOrder__c ofo : ofoList) {
-                ofoIds.add(ofo.Id);
-            }
-
-            if (ofoIds.size() > 0 ) {
-                //  List<Attachment> atcList = [select id,ParentId,createddate,createdbyid from Attachment where ParentId in:ofoIds];
-                List<ContentDocumentLink> linkList = [select ContentDocumentId from ContentDocumentLink where LinkedEntityId in:ofoIds];
-
-                List<Id> idList = new List<Id>();
-                for (ContentDocumentLink link : linkList) {
-                    idList.add(link.ContentDocumentId);
-                }
-                List<ContentDocument> docList = [select Id from ContentDocument where Id in: idList];
-                if (docList.size() > 0) {
-                    delete docList;
-                }
-            }
-           
-            
-        }
-    }
-
     if (oFOrderIdMap.size() > 0) {
 
         List<OpportunityFileOrder__c> oFOrderList = [select Id,Name,LatestUploadUser__c,LatestUploadTime__c
@@ -492,5 +502,68 @@
 
         }                                            
     }
+    if (accIds.size() > 0) {
+        Map<String, String> attMap = new Map<String, String>();
+        // List<Attachment> attList = [select Id, ParentId from Attachment where ParentId = :accIds order by ParentId];
+        List<ContentDocumentLink> linkList = [select ContentDocumentId,LinkedEntityId from ContentDocumentLink where   ContentDocument.FileType != 'SNOTE' and   LinkedEntityId =: accIds order by LinkedEntityId];
+        System.debug('瀹㈡埛Link鐨勬潯鏁帮細' + linkList.size());
+        for (ContentDocumentLink link : linkList) {
+            if(!delDocidList.contains(link.ContentDocumentId)){
+                attMap.put(link.LinkedEntityId, link.LinkedEntityId);
+            }
+        }
+        System.debug('attMap鐨勬潯鏁帮細' + attMap);
+        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) {
+            if(accList.size() > 0){
+                // Account acc = new Account(Id = accid);
+                //20221207 lt 鍖婚櫌瀹℃壒浼樺寲 start
+                for(Account acc : accList){  
+                    if(Trigger.isUpdate && Trigger.isBefore){
+                        for(ContentDocument link: Trigger.Old){
+                            System.debug(link + 'old');
+                        }
+                        for(ContentDocument link : Trigger.New){
+                            System.debug(link + 'new');
+                            if(attMap.containsKey(acc.Id) &&acc.Is_Active__c != '鑽夋涓�'){
+                                System.debug('lt123---鑽夋涓�---');
+                                link.addError('闈炶崏妗堜腑锛岄檮浠朵笉鍏佽缂栬緫銆�');
+                
+                                return;
+                            }
+                        }
+                    }                  
+                    if(Trigger.isDelete && (Trigger.isBefore)){
+                        for(ContentDocument link : Trigger.old){
+                            if(acc.Is_Active__c != '鑽夋涓�'){
+                                link.addError('闈炶崏妗堜腑锛岄檮浠朵笉鍏佽鍒犻櫎銆�');
+                                return;
+                            }
+                        }
+                    }
+                    acc.Is_upload_file__c = attMap.containsKey(accid);
+                    System.debug('鏄惁涓婁紶闄勪欢锛�' + 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;
+
+}
+// 20231103 闄堜含姝� Lightning鏂囦欢淇敼 End
 }
\ No newline at end of file

--
Gitblit v1.9.1