Li Jun
2022-04-13 5c01ebbfddb6b8674430b47c22977bea2e350084
force-app/main/default/triggers/RentalApplyBeforeUpdate.trigger
@@ -23,89 +23,66 @@
        /*因为备品借出申请在业务上不会删除,所以已经分割次数+1就是本次支番号*/
        /*************************Insert 20160513 Start*************************/
        
    if(Trigger.isInsert){
        List<Rental_Apply__c> RacList = new List<Rental_Apply__c>();
        List<Rental_Apply__c> RacListElse = new List<Rental_Apply__c>();
        List<AggregateResult> GroList = new List<AggregateResult>();
        List<String> CampaignId = new List<String>();       //学会Id List
        Map<String,String> RenMap = new Map<String,String>();
        Map<String,String> RenCountMap = new Map<String,String>();
        GroList = [select count(id) countId,Old_Rental_Apply__c from Rental_Apply__c where Old_Rental_Apply__c !=null group by Old_Rental_Apply__c];
if(Trigger.isInsert){
    List<Rental_Apply__c> RacList = new List<Rental_Apply__c>();
    List<Rental_Apply__c> RacListElse = new List<Rental_Apply__c>();
    List<AggregateResult> GroList = new List<AggregateResult>();
    Map<String,String> RenMap = new Map<String,String>();
    Map<String,String> RenCountMap = new Map<String,String>();
    GroList = [select count(id) countId,Old_Rental_Apply__c from Rental_Apply__c where Old_Rental_Apply__c !=null group by Old_Rental_Apply__c];
    for(AggregateResult Ta : GroList){
        RenMap.put( string.valueOf(Ta.get('Old_Rental_Apply__c')),string.valueOf(Ta.get('Old_Rental_Apply__c') )); //旧借出申请
        RenCountMap.put( string.valueOf(Ta.get('Old_Rental_Apply__c')),string.valueOf(Ta.get('countId') ));
    }
    //ra.Old_Rental_Apply__c  List
    List<String> LoopListRaOCon = new List<String>();
    //List<String> LoopListRaOIdUnCon = new List<String>();
    Map<String,Integer> LoopListRaN = new Map<String,Integer>();
    //Map<String,Integer> LoopListRaNElse = new Map<String,Integer>();
    for(Rental_Apply__c ra : trigger.new){
        if(ra.Name ==null){
            ra.Name='*';
        }
    //
        if(RenMap.containsKey(ra.Old_Rental_Apply__c)){
            LoopListRaOCon.add(ra.Old_Rental_Apply__c);
            LoopListRaN.put(ra.Old_Rental_Apply__c,LoopListRaOCon.size()-1);
            //RacList = [select id,Old_Rental_Apply__r.Name from Rental_Apply__c where Old_Rental_Apply__c =: ra.Old_Rental_Apply__c];
            //ra.Name = RacList[0].Old_Rental_Apply__r.Name+'-'+RacList.size();
        }else{
            //RacList = [select id,name,Old_Rental_Apply__r.Name from Rental_Apply__c where id =: ra.id];
            //LoopListRaOIdUnCon.add(ra.id);
            //LoopListRaNElse.put(ra.id, LoopListRaOIdUnCon.size()-1);
            //if(RacList.size()>0){
            //  ra.Name = RacList[0].Name+'-'+1;
            //  ra.Old_Rental_Apply__c = ra.id;
            //}
        }
        
        for(AggregateResult Ta : GroList){
            RenMap.put( string.valueOf(Ta.get('Old_Rental_Apply__c')),string.valueOf(Ta.get('Old_Rental_Apply__c') )); //旧借出申请
            RenCountMap.put( string.valueOf(Ta.get('Old_Rental_Apply__c')),string.valueOf(Ta.get('countId') ));
        }
        //ra.Old_Rental_Apply__c  List
        List<String> LoopListRaOCon = new List<String>();
        //List<String> LoopListRaOIdUnCon = new List<String>();
        Map<String,Integer> LoopListRaN = new Map<String,Integer>();
        //Map<String,Integer> LoopListRaNElse = new Map<String,Integer>();
        for(Rental_Apply__c ra : trigger.new){
            if(ra.Name ==null){
                ra.Name='*';
            }
            //查裁决申请的List
            if (!String.isBlank(ra.Campaign__c)){
                CampaignId.add(ra.Campaign__c);
            }
            if(RenMap.containsKey(ra.Old_Rental_Apply__c)){
                LoopListRaOCon.add(ra.Old_Rental_Apply__c);
                LoopListRaN.put(ra.Old_Rental_Apply__c,LoopListRaOCon.size()-1);
                //RacList = [select id,Old_Rental_Apply__r.Name from Rental_Apply__c where Old_Rental_Apply__c =: ra.Old_Rental_Apply__c];
                //ra.Name = RacList[0].Old_Rental_Apply__r.Name+'-'+RacList.size();
            }else{
                //RacList = [select id,name,Old_Rental_Apply__r.Name from Rental_Apply__c where id =: ra.id];
                //LoopListRaOIdUnCon.add(ra.id);
                //LoopListRaNElse.put(ra.id, LoopListRaOIdUnCon.size()-1);
                //if(RacList.size()>0){
                //  ra.Name = RacList[0].Name+'-'+1;
                //  ra.Old_Rental_Apply__c = ra.id;
                //}
            }
        }
        //备品借出申请增加决裁信息判断,为把此时点的决裁编号和决裁状态写到决裁编号(创建)和决裁状态(创建)上
        if( CampaignId.size() > 0 ){
            Map<Id, Application_for_Conference_Adjudication__c> application_for_con_map = new Map<Id, Application_for_Conference_Adjudication__c>([
                    select id ,ProcessState__c,MeetingApprovedNo__c
                    from Application_for_Conference_Adjudication__c
                    where id in: CampaignId]);
            for( Rental_Apply__c ra : trigger.new ) {
                if(application_for_con_map != null && application_for_con_map.containsKey(ra.Campaign__c) ){
                    ra.ApprovedNo_Create__c = application_for_con_map.get(ra.Campaign__c).MeetingApprovedNo__c;
                    ra.Approved_State_Create__c = application_for_con_map.get(ra.Campaign__c).ProcessState__c;
                }
            }
        }
        // bp2
        // System.debug('测试是否进入');
        // RacList = [select id,Old_Rental_Apply__r.Name from Rental_Apply__c where Old_Rental_Apply__c in : LoopListRaOCon];
        // //RacListElse = [select id,name,Old_Rental_Apply__r.Name from Rental_Apply__c where id in : LoopListRaOIdUnCon];
        // for(Rental_Apply__c ra : trigger.new){
        //     if(RenMap.containsKey(ra.Old_Rental_Apply__c)){
        //         if(RacList.size()>0){
        //             ra.Name = RacList[LoopListRaN.get(ra.Old_Rental_Apply__c)].Old_Rental_Apply__r.Name+'-'+(RacList.size()+1);
        //             ra.Rental_Received_Day__c = null;
        //             ra.All_Delivery_Flag_c__c = false;
        //         }
        //     }else{
        //         if(ra.Old_Rental_Apply__c!=null){
        //             ra.Name = ra.Name+'-'+1;
        //             ra.Rental_Received_Day__c = null;
        //             ra.All_Delivery_Flag_c__c = false;
        //         }
        //     }
        // }
        // bp2
    }
    }
    // bp2
    // System.debug('测试是否进入');
    // RacList = [select id,Old_Rental_Apply__r.Name from Rental_Apply__c where Old_Rental_Apply__c in : LoopListRaOCon];
    // //RacListElse = [select id,name,Old_Rental_Apply__r.Name from Rental_Apply__c where id in : LoopListRaOIdUnCon];
    // for(Rental_Apply__c ra : trigger.new){
    //     if(RenMap.containsKey(ra.Old_Rental_Apply__c)){
    //         if(RacList.size()>0){
    //             ra.Name = RacList[LoopListRaN.get(ra.Old_Rental_Apply__c)].Old_Rental_Apply__r.Name+'-'+(RacList.size()+1);
    //             ra.Rental_Received_Day__c = null;
    //             ra.All_Delivery_Flag_c__c = false;
    //         }
    //     }else{
    //         if(ra.Old_Rental_Apply__c!=null){
    //             ra.Name = ra.Name+'-'+1;
    //             ra.Rental_Received_Day__c = null;
    //             ra.All_Delivery_Flag_c__c = false;
    //         }
    //     }
    // }
    // bp2
}
        /*************************Insert 20160513 End***************************/
        for(Rental_Apply__c ra : trigger.new) {
@@ -251,11 +228,11 @@
        //List<Account> accs = [SELECT Id, Province_formula__c, State_Text__c FROM Account WHERE Id IN: hospitalIds];
        Map<Id, Account> accMap = new Map<Id, Account>();
        if (hospitalIds.size() > 0) accMap.putAll([SELECT Id, Province_formula__c, State_Text__c FROM Account WHERE Id IN: hospitalIds]);
        if (hospitalIds.size() > 0) accMap.putAll([SELECT Id, Province_formula__c, State_Text__c FROM Account WHERE Id IN: hospitalIds ]);
        
        //List<Campaign> cams = [SELECT Id, EndDate FROM Campaign WHERE Id IN: campaignIds];
        Map<Id, Campaign> camMap = new Map<Id, Campaign>();
        if (campaignIds.size() > 0) camMap.putAll([SELECT Id, EndDate FROM Campaign WHERE Id IN: campaignIds]);
        if (campaignIds.size() > 0) camMap.putAll([SELECT Id, EndDate FROM Campaign WHERE Id IN: campaignIds ]);
        
        //Map<Id, Repair__c> repairMap = new Map<Id, Repair__c>();
        //if (repairIds.size() > 0) repairMap.putAll([SELECT Id FROM Repair__c WHERE Id IN: repairIds]);