/**
 * 2022-01-20 create
 * 
 * 1.SFDC先创建会议再发送OBPM时，705接口处理了决裁查找学会的操作，trigger里需要检索并更新学会上的查找决裁
 * 2.决裁状态更新为不可用的时候（驳回、删除），清除学会上的决裁信息。
 */
public without sharing class ApplicationForCAHandler extends Oly_TriggerHandler  {
    private Map<Id, Application_for_Conference_Adjudication__c> newMap;
    private Map<Id, Application_for_Conference_Adjudication__c> oldMap;
    private List<Application_for_Conference_Adjudication__c> newList;
    private List<Application_for_Conference_Adjudication__c> oldList;
    
    public ApplicationForCAHandler() {
        this.newMap = (Map<Id, Application_for_Conference_Adjudication__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Application_for_Conference_Adjudication__c>) Trigger.oldMap;
        this.newList = (List<Application_for_Conference_Adjudication__c>) Trigger.new;
        this.oldList = (List<Application_for_Conference_Adjudication__c>) Trigger.old;
    }

    protected override void beforeUpdate() {
        clean_campaign();
        clean_application();
    }

    protected override void afterUpdate() {

    }

    // 审批不通过的时候 清空会议上的决裁信息
    private void clean_campaign() {
        List<String> approved_nos = new List<String>();
        for (Application_for_Conference_Adjudication__c app : this.newList) {
            if (app.ProcessState__c == '驳回' || 
                app.ProcessState__c == '终止申请' || 
                app.ProcessState__c == '取消') {
                    approved_nos.add(app.Id);
            }
        }
        if (approved_nos.size() > 0) {
            List<Campaign> campaigns = [select Id, MeetingApprovedNo__c, Meeting_Approved_No__c from Campaign where Meeting_Approved_No__c in :approved_nos];
            if (campaigns != null && campaigns.size() > 0) {
                for (Campaign cam : campaigns) {
                    cam.MeetingApprovedNo__c = null;
                    cam.Meeting_Approved_No__c = null;
                }
                update campaigns;
            }
        }
    }

    // 审批不通过的时候 清空决裁上的会议信息
    private void clean_application() {
        List<String> campaign_ids = new List<String>();
        for (Application_for_Conference_Adjudication__c app : this.newList) {
            if (app.ProcessState__c == '驳回' || 
                app.ProcessState__c == '终止申请' || 
                app.ProcessState__c == '取消') {
                app.Num__c = null;
            }
        }
    }
}