sunxia
2023-03-09 c2456f20e2b5596c1694a362a8741714a55f027e
AttachmentTriggerUpdate

AttachmentTriggerUpdate
1个文件已修改
235 ■■■■■ 已修改文件
force-app/main/default/triggers/AttachmentTrigger.trigger 235 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
force-app/main/default/triggers/AttachmentTrigger.trigger
@@ -21,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>();
@@ -29,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)){
@@ -55,7 +63,7 @@
            if (String.valueOf(att.ParentId).startsWith('001')) {
                accIds.add(att.ParentId);
            }
            //20221014 yc 医院审批优化 start  正式的叫a5A
            //20221014 yc 医院审批优化 start
            if (String.valueOf(att.ParentId).startsWith('a5C') || String.valueOf(att.ParentId).startsWith('a5A')) {
                accAppIds.add(att.ParentId);
            }
@@ -80,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')) {
            // 添付した契約書のファイル名は指定できない
@@ -102,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) {
@@ -134,8 +183,44 @@
        }
    }
    //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]) {
@@ -498,147 +583,5 @@
        }                                            
    }
   if (Test.isRunningTest()) {
        Integer i=0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }
}