From 75550e1bd16611d11ffac577f14ba743c6711338 Mon Sep 17 00:00:00 2001
From: yumenghui <953181891@qq.com>
Date: 星期一, 07 八月 2023 20:33:22 +0800
Subject: [PATCH] 按钮及附件修改

---
 force-app/main/default/classes/RentalApplyTriggerHandler.cls |  112 ++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 89 insertions(+), 23 deletions(-)

diff --git a/force-app/main/default/classes/RentalApplyTriggerHandler.cls b/force-app/main/default/classes/RentalApplyTriggerHandler.cls
index fb2a9b3..0d69918 100644
--- a/force-app/main/default/classes/RentalApplyTriggerHandler.cls
+++ b/force-app/main/default/classes/RentalApplyTriggerHandler.cls
@@ -181,45 +181,52 @@
             }
             if(!childRaList.isEmpty()) {
                 update childRaList;
-                
                 // 涓诲崟閲岄檮浠�
-                List<Attachment> attList = [SELECT Id, Body, Name, ParentId
-                                            FROM Attachment
-                                            WHERE ParentId IN: raIdSet
-                                            ];
+                List<ContentDocumentLink> attList = [SELECT Id, LinkedEntityId, ContentDocumentId, IsDeleted, 
+                                                    ContentDocument.Title
+                                                    FROM ContentDocumentLink 
+                                                    WHERE  LinkedEntityId IN: raIdSet];
                 // 浠庡崟閲岄檮浠讹紝鏀惧埌涓�璧锋煡浼氭姤limit閿�
-                attList.addAll([SELECT Id, Name, ParentId FROM Attachment WHERE ParentId IN:childRaList]);
+                List<Id> racIdList = new List<Id>();
+                for (Rental_Apply__c rac : childRaList) {
+                    racIdList.add(rac.Id);
+                }
+                List<ContentDocumentLink> attList1 =[SELECT Id, LinkedEntityId, ContentDocumentId, IsDeleted, 
+                                                    ContentDocument.Title
+                                                    FROM ContentDocumentLink 
+                                                    WHERE LinkedEntityId IN: racIdList];
+                attList.addAll(attList1);
                 if(attList.isEmpty()) {
                     return;
                 }
-                Map<Id, List<Attachment>> parentFiles = new Map<Id, List<Attachment>>();
+                Map<Id, List<ContentDocumentLink>> parentFiles = new Map<Id, List<ContentDocumentLink>>();
                 // 寰呭垹闄ら檮浠�
-                List<Attachment> deleteFiles = new List<Attachment>();
-                for(Attachment att: attList) {
-                    if(att.Name.startsWith('QRCode-')) {
+                List<ContentDocumentLink> deleteFiles = new List<ContentDocumentLink>();
+                for(ContentDocumentLink att: attList) {
+                    if(att.ContentDocument.Title.startsWith('QRCode-') || att.ContentDocument.Title.startsWith('BRCode-')) {
                         continue;
                     }
-                    if(raIdSet.contains(att.ParentId)) {
-                        List<Attachment> tempList = null;
-                        if(parentFiles.containsKey(att.ParentId)) {
-                            tempList = parentFiles.get(att.ParentId);
+                    if(raIdSet.contains(att.LinkedEntityId)) {
+                        List<ContentDocumentLink> tempList = null;
+                        if(parentFiles.containsKey(att.LinkedEntityId)) {
+                            tempList = parentFiles.get(att.LinkedEntityId);
                         }
                         else {
-                            tempList = new List<Attachment>();
+                            tempList = new List<ContentDocumentLink>();
                         }
                         tempList.add(att);
-                        parentFiles.put(att.ParentId, tempList);
+                        parentFiles.put(att.LinkedEntityId, tempList);
                     }
                     else {
                         deleteFiles.add(att);
                     }
                 }
                 // 寰呮彃鍏ョ殑闄勪欢
-                List<Attachment> newFiles = new List<Attachment>();
+                List<ContentDocumentLink> newFiles = new List<ContentDocumentLink>();
                 for(Rental_Apply__c childRa: childRaList) {
                     if(parentFiles.containsKey(childRa.Old_Rental_Apply__c)) {
-                        for(Attachment att : parentFiles.get(childRa.Old_Rental_Apply__c)){
-                            newFiles.add(new Attachment(Body = att.Body,Name = att.Name, ParentId = childRa.Id));
+                        for(ContentDocumentLink att : parentFiles.get(childRa.Old_Rental_Apply__c)){
+                            newFiles.add(new ContentDocumentLink(ContentDocumentId = att.ContentDocumentId,LinkedEntityId = childRa.Id,ShareType = 'I',Visibility = 'AllUsers'));
                         }
                     }
                 }
@@ -229,6 +236,54 @@
                 if(!newFiles.isEmpty()) {
                     insert newFiles;
                 }
+                
+                // // 涓诲崟閲岄檮浠�
+                // List<Attachment> attList = [SELECT Id, Body, Name, ParentId
+                //                             FROM Attachment
+                //                             WHERE ParentId IN: raIdSet
+                //                             ];
+                // // 浠庡崟閲岄檮浠讹紝鏀惧埌涓�璧锋煡浼氭姤limit閿�
+                // attList.addAll([SELECT Id, Name, ParentId FROM Attachment WHERE ParentId IN:childRaList]);
+                // if(attList.isEmpty()) {
+                //     return;
+                // }
+                // Map<Id, List<Attachment>> parentFiles = new Map<Id, List<Attachment>>();
+                // // 寰呭垹闄ら檮浠�
+                // List<Attachment> deleteFiles = new List<Attachment>();
+                // for(Attachment att: attList) {
+                //     if(att.Name.startsWith('QRCode-')) {
+                //         continue;
+                //     }
+                //     if(raIdSet.contains(att.ParentId)) {
+                //         List<Attachment> tempList = null;
+                //         if(parentFiles.containsKey(att.ParentId)) {
+                //             tempList = parentFiles.get(att.ParentId);
+                //         }
+                //         else {
+                //             tempList = new List<Attachment>();
+                //         }
+                //         tempList.add(att);
+                //         parentFiles.put(att.ParentId, tempList);
+                //     }
+                //     else {
+                //         deleteFiles.add(att);
+                //     }
+                // }
+                // // 寰呮彃鍏ョ殑闄勪欢
+                // List<Attachment> newFiles = new List<Attachment>();
+                // for(Rental_Apply__c childRa: childRaList) {
+                //     if(parentFiles.containsKey(childRa.Old_Rental_Apply__c)) {
+                //         for(Attachment att : parentFiles.get(childRa.Old_Rental_Apply__c)){
+                //             newFiles.add(new Attachment(Body = att.Body,Name = att.Name, ParentId = childRa.Id));
+                //         }
+                //     }
+                // }
+                // if(!deleteFiles.isEmpty()) {
+                //     delete deleteFiles;
+                // }
+                // if(!newFiles.isEmpty()) {
+                //     insert newFiles;
+                // }
             }
         }
     }
@@ -241,6 +296,7 @@
         List<Rental_Apply__Share> rasList = new List<Rental_Apply__Share>();
         List<Id> deleteOfficeAssistantShare_nObjId_List = new List<Id>();       // 鍏变韩鍒犻櫎鐢�
         List<Id> deleteApplyUserShare_nObjId_List = new List<Id>();// 20210727 ljh SFDC-C54C33 鍏变韩鍒犻櫎鐢�
+        Set<Id> shareSet = new Set<Id>(); // 20230301 ljh DB202302444522 add
         for (Rental_Apply__c nObj : newList) {
             Rental_Apply__c oObj = (null == this.oldMap) ? null : this.oldMap.get(nObj.Id);
             // 鏈嶅姟閮ㄥ鎵逛汉
@@ -296,6 +352,7 @@
                 ) {
                 deleteApplyUserShare_nObjId_List.add(nObj.Id);
                 if(nObj.SalesManager__c != null){
+                    shareSet.add(nObj.SalesManager__c);// 20230301 ljh DB202302444522 add
                     Rental_Apply__Share rasSalesManager = new Rental_Apply__Share(
                             RowCause = 'ApplyUserShare__c',
                             ParentId = nObj.Id,
@@ -305,6 +362,7 @@
                     rasList.add(rasSalesManager);
                 }
                 if(nObj.BuchangApprovalManagerSales__c != null){
+                    shareSet.add(nObj.BuchangApprovalManagerSales__c);// 20230301 ljh DB202302444522 add
                     Rental_Apply__Share rasBz = new Rental_Apply__Share(
                             RowCause = 'ApplyUserShare__c',
                             ParentId = nObj.Id,
@@ -314,6 +372,7 @@
                     rasList.add(rasBz);
                 }
                 if(nObj.ZongjianApprovalManager__c != null){
+                    shareSet.add(nObj.ZongjianApprovalManager__c);// 20230301 ljh DB202302444522 add
                     Rental_Apply__Share rasZj = new Rental_Apply__Share(
                             RowCause = 'ApplyUserShare__c',
                             ParentId = nObj.Id,
@@ -336,10 +395,18 @@
         if(deleteOfficeAssistantShare_nObjId_List.size() > 0){
             soql += ' AND (RowCause = \'Office_Assistant__c\' AND ParentId =: deleteOfficeAssistantShare_nObjId_List) ';
             if(deleteApplyUserShare_nObjId_List.size() > 0){
-                soql += ' OR (RowCause = \'ApplyUserShare__c\'AND ParentId =: deleteApplyUserShare_nObjId_List)';
+                // 20230301 ljh DB202302444522 update start
+                // soql += ' OR (RowCause = \'ApplyUserShare__c\'AND ParentId =: deleteApplyUserShare_nObjId_List)';
+                soql += ' OR (RowCause = \'ApplyUserShare__c\'AND ParentId =: deleteApplyUserShare_nObjId_List';
+                soql += ' and UserOrGroupId IN :shareSet)';
+                // 20230301 ljh DB202302444522 update end
             }
         }else if(deleteApplyUserShare_nObjId_List.size() > 0){
-            soql += ' AND (RowCause = \'ApplyUserShare__c\'AND ParentId =: deleteApplyUserShare_nObjId_List)';
+            // 20230301 ljh DB202302444522 update start
+            // soql += ' AND (RowCause = \'ApplyUserShare__c\'AND ParentId =: deleteApplyUserShare_nObjId_List)';
+            soql += ' AND (RowCause = \'ApplyUserShare__c\'AND ParentId =: deleteApplyUserShare_nObjId_List';
+            soql += ' and UserOrGroupId IN :shareSet)';
+            // 20230301 ljh DB202302444522 update end
         }
         List<Rental_Apply__Share> deleteShareList = new List<Rental_Apply__Share>();
         if(deleteOfficeAssistantShare_nObjId_List.size() > 0 || deleteApplyUserShare_nObjId_List.size() > 0){
@@ -1899,7 +1966,6 @@
 
     @testVisible
     private void testI() {
-        Integer i = 0;
-        
+       
     }
 }
\ No newline at end of file

--
Gitblit v1.9.1