public with sharing class MeetingDelayApplyTriggerHandler extends Oly_TriggerHandler {

    private List<meeting_delay_apply__c> newList;
    private Map<Id,meeting_delay_apply__c> oldMap;
    private Map<Id,meeting_delay_apply__c> newMap;

    public MeetingDelayApplyTriggerHandler() {
        this.newList = (List<meeting_delay_apply__c>) Trigger.new;
        this.oldMap = (Map<Id,meeting_delay_apply__c>) Trigger.oldMap;
        this.newMap = (Map<Id,meeting_delay_apply__c>) Trigger.newMap;
    }


    protected override void beforeUpdate() {
        setColumn();
    }

    protected override void afterUpdate() {
        addColumn();

    }

    //当审批流程走完时 更新 市场活动相关字段
    private void addColumn(){
        List<Campaign> clist = new List<Campaign>();
        Map<Id,Campaign> cmap = new Map<Id,Campaign>();
        List<Id> cids = new List<Id>();


        for (meeting_delay_apply__c mda :newList ) {
            //判断更新前后的状态是否不同 若不同,则判断更新后的状态是否为'已完成'
            if ((mda.status__c != oldMap.get(mda.Id).status__c) && mda.status__c == '已完成' ) {
                String camId = mda.campaign__c  ;

                Campaign cam = new Campaign();

                cam.Id = camId;
                cam.StartDate = mda.meeting_start_date__c;
                cam.EndDate = mda.meeting_end_date__c;
                cam.PlanBackData__c = mda.dismantling_date__c;
                cam.Meeting_Cooperate__c = mda.Meeting_Cooperate1__c;
                cam.Meeting_Type__c = mda.Meeting_Type__c;
                cam.HostName__c = mda.HostName__c;
                cam.cooperatorCompany__c = mda.cooperatorCompany__c;
                cam.Meeting_Cooperate2__c = mda.Meeting_Cooperate2__c;
                cam.Meeting_Cooperate3__c = mda.Meeting_Cooperate3__c;
                cam.Meeting_Cooperate4__c = mda.Meeting_Cooperate4__c;
                cam.Status = mda.campaign_status__c;
                //OBPM LY 20211221 start
                cam.IF_Approved__c = mda.IF_Approved__c;//是否申请决裁
                cam.Meeting_Approved_No__c = mda.MeetingApproved__c;//会议决裁申请
                if (String.isBlank(mda.MeetingApproved__c)) {
                    cam.MeetingApprovedNo__c = null;
                } else {
                    cam.MeetingApprovedNo__c = mda.MeetingApproved__r.MeetingApprovedNo__c;
                }
                // cam.Test_Status__c = mda.campaign_status__c;
                //OBPM LY 20211221 end
                clist.add(cam);

            }
            //update by rentx 2021-01-20 SWAG-BXF5JV start  在提交申请后撤回时,回滚学会的状态
            // else if ((mda.status__c != oldMap.get(mda.Id).status__c) && mda.status__c == '驳回' ) {
            else if ((mda.status__c != oldMap.get(mda.Id).status__c) && (mda.status__c == '驳回'  || mda.status__c == '草案中')) {
            //update by rentx 2021-01-20 SWAG-BXF5JV end

                Campaign cam = new Campaign();
                cam.Id = mda.campaign__c;
                cam.Status = mda.campaign_status__c;
                clist.add(cam);
            }
        }

        if (clist.size() > 0) {
            update clist;
        }
    }


    //设置字段  经理 & 部长
    //提交时设置old字段的值
    private void setColumn(){
        List<String> camIds = new List<String>();
        List<meeting_delay_apply__c> mlist = new List<meeting_delay_apply__c>();
        List<Id> uids = new List<Id>();
        for (meeting_delay_apply__c mda :newList ) {
            //判断更新前后的状态是否不同 若不同,则判断更新后的状态是否为'审批中'
            if ((mda.status__c != oldMap.get(mda.Id).status__c) && mda.status__c == '审批中' ) {
                //获取当前会议延期申请对象的对应人
                uids.add(mda.CreatedById);
                mlist.add(mda);
                camIds.add(mda.campaign__c  );
            }
        }
        if (camIds.size() == 0) {
            return;
        }else{
                Map<String,Campaign> camMap = new Map<String,Campaign>();
                List<Campaign> statusList = [SELECT Id,
                                                Status,StartDate,EndDate,PlanBackData__c,
                                                Meeting_Cooperate__c,Meeting_Type__c,HostName__c,
                                                cooperatorCompany__c,Meeting_Cooperate2__c,Meeting_Cooperate3__c,Meeting_Cooperate4__c,
                                                IF_Approved__c,Meeting_Approved_No__c 
                                                 FROM Campaign WHERE id in :camIds];
                for (Campaign c :statusList ) {
                    camMap.put(c.Id, c);
                }
                
                Map<Id, User> userMap = new Map<Id, User>();
                List<user> userList = New List<user>();

                userList = [SELECT Id, SalesManager__c, BuchangApprovalManagerSales__c FROM user WHERE id in :uids];

                for (User u : userList) {
                    userMap.put(u.Id, u);
                }


                for (meeting_delay_apply__c mda : mlist) {
                    mda.meeting_start_date_Old__c = camMap.get(mda.campaign__c).StartDate;
                    mda.meeting_end_date_Old__c = camMap.get(mda.campaign__c).EndDate;
                    mda.dismantling_date_Old__c = camMap.get(mda.campaign__c).PlanBackData__c;
                    mda.Meeting_Cooperate1_Old__c = camMap.get(mda.campaign__c).Meeting_Cooperate__c;
                    mda.Meeting_Type_Old__c = camMap.get(mda.campaign__c).Meeting_Type__c == '主办会' ? '自办会' : camMap.get(mda.campaign__c).Meeting_Type__c;
                    mda.HostName_Old__c = camMap.get(mda.campaign__c).HostName__c;
                    mda.cooperatorCompany_Old__c = camMap.get(mda.campaign__c).cooperatorCompany__c;
                    mda.Meeting_Cooperate2_Old__c = camMap.get(mda.campaign__c).Meeting_Cooperate2__c;
                    mda.Meeting_Cooperate3_Old__c = camMap.get(mda.campaign__c).Meeting_Cooperate3__c;
                    mda.Meeting_Cooperate4_Old__c = camMap.get(mda.campaign__c).Meeting_Cooperate4__c;
                    mda.IF_Approved_Old__c = camMap.get(mda.campaign__c).IF_Approved__c;
                    mda.MeetingApproved_Old__c = camMap.get(mda.campaign__c).Meeting_Approved_No__c;

                    if (String.isNotBlank(mda.CreatedById)) {
                        mda.manager__c = userMap.get(mda.CreatedById).SalesManager__c == null ? mda.CreatedById : userMap.get(mda.CreatedById).SalesManager__c;
                        mda.minister__c = userMap.get(mda.CreatedById).BuchangApprovalManagerSales__c == null ? mda.CreatedById : userMap.get(mda.CreatedById).BuchangApprovalManagerSales__c;
                    }
                    mda.campaign_status__c = camMap.get(mda.campaign__c).Status;
                }

                /*if (shareList.size() > 0) {
                    insert shareList;   
                }*/

                for (Campaign c : statusList) {
                    c.Status = '变更申请中';
                }
            update statusList;   
        }

    }
}