高章伟
2023-03-02 e3c02c03dd2de442bbced87236f60a13a1cd154e
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>();
@@ -52,6 +55,11 @@
            if (String.valueOf(att.ParentId).startsWith('001')) {
                accIds.add(att.ParentId);
            }
            //20221014 yc 医院审批优化 start  正式的叫a5A
            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);
            }
@@ -106,8 +114,26 @@
                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
    
    if (rentaldelIds.size() > 0) {
@@ -146,13 +172,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 +498,147 @@
        }                                            
    }
   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++;
    }
}