yumenghui
2023-08-07 75550e1bd16611d11ffac577f14ba743c6711338
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;
        
    }
}