/**
 * @author 张玉山
 * @DateTime 2019-03-11T14:20:19+0800
 * 时间修改申请日期处理
 * 开班日期初始化
 */
public without sharing class CampaignTriggerBefInsHandler extends Oly_TriggerHandler {

    private Map<Id, Campaign> newMap;
    private Map<Id, Campaign> oldMap;
    private List<Campaign> newList;
    private List<Campaign> oldList;
    public CampaignTriggerBefInsHandler() {
        this.newMap = (Map<Id, Campaign>) Trigger.newMap;
        this.oldMap = (Map<Id, Campaign>) Trigger.oldMap;
        this.newList = (List<Campaign>) Trigger.new;
        this.oldList = (List<Campaign>) Trigger.old;

    }

    private static Map<String, String> states;
    private static Map<String, String> cities;

    protected override void beforeInsert() {
        for (Campaign campaign : newList) {
            if (campaign.RecordTypeid != (ID) system.label.RecordType_CampaignService)          
                continue;
            campaign.StartDate__c = campaign.CeremonyDate__c == null  ?
                                    campaign.Lesson1Date__c : campaign.CeremonyDate__c; 
        }
        System.debug('1>>>>');
        // OBPM市场活动联动 add 手动绑定决裁信息时，从决裁上获取信息并写入 start
        GetInfoFromAdjudication();
        // OBPM市场活动联动 add 手动绑定决裁信息时，从决裁上获取信息并写入 end
        System.debug('2>>>>');
    }
    protected override void beforeUpdate() {
        System.debug('3>>>>');
        beforeDateModifyDecide();
        System.debug('4>>>>');
        CampaignToContact();
        System.debug('5>>>>');
        // OBPM市场活动联动 add 手动绑定决裁信息时，从决裁上获取信息并写入 start
        GetInfoFromAdjudication();
        // OBPM市场活动联动 add 手动绑定决裁信息时，从决裁上获取信息并写入 end
        System.debug('6>>>>');
    }
    protected override void afterInsert() {
        
    }
    protected override void afterUpdate(){
        sendToComPlat();
    }
    //通过触发器更新操作发送活动信息到智慧医疗 精琢技术 thh 2021-09-23 start
    public void sendToComPlat() {
        List<String> qisIdList = new List<String>();
        for(Campaign campaign : newList){
            Campaign old = oldMap.get(campaign.Id);
            if(campaign.IF_Submit__c && campaign.IF_Submit__c != old.IF_Submit__c){ 
                qisIdList.add(campaign.Id);
            }
        }
        if(qisIdList.size() > 0){
            BatchIF_Log__c iflog = new BatchIF_Log__c();
            iflog.Type__c = 'NFM622';
            iflog.Log__c  = 'callout start\n';
            insert iflog;
            iflog = [Select Id, Name from BatchIF_Log__c where Id = :iflog.Id];                
            NFM622Controller.callout(iflog.Id, qisIdList);
        }
    }
    //通过触发器更新操作发送活动信息到智慧医疗 精琢技术 thh 2021-09-23 end
    private void CampaignToContact() {
        list<Campaign> unApprovedCampaignList = new list<Campaign>();
        //list<Campaign> approvedCampaignList = new list<Campaign>();
        for (Campaign campaign : NewList) {
            if (campaign.RecordTypeid != (ID) system.label.RecordType_CampaignService)
                continue;
            Campaign old = oldMap.get(campaign.Id);
            //if (old.Status.equals('申请中') && campaign.Status.equals('公开中')) {
            //  approvedCampaignList.add(campaign);

            //} else campaign.Status.equals('草案中') ||
            if (campaign.Status.equals('取消')) {
                unApprovedCampaignList.add(campaign);
            }
        }

        list<Contact> updateContact = new list<Contact>();
        //if (approvedCampaignList.size() > 0) {
        //  list<CampaignMember__c> CampaignMemberlist =
        //      [select id, Contact_ID__c, Campaign__c
        //       from CampaignMember__c
        //       where Campaign__c in: approvedCampaignList];
        //  for (CampaignMember__c CampaignMember : CampaignMemberlist) {
        //      Contact temContact = new Contact(id = CampaignMember.Contact_ID__c, Campaign__c = CampaignMember.Campaign__c );
        //      updateContact.add(temContact);
        //  }
        //}

        if (unApprovedCampaignList.size() > 0) {
            list<CampaignMember__c> CampaignMemberlist =
                [select id, Contact_ID__c, Campaign__c
                 from CampaignMember__c
                 where Campaign__c in: unApprovedCampaignList];
            for (CampaignMember__c CampaignMember : CampaignMemberlist) {
                Contact temContact = new Contact(id = CampaignMember.Contact_ID__c, Campaign__c = null);
                updateContact.add(temContact);
            }

        }
        if (updateContact.size() > 0) {
            ControllerUtil.updateSObjectContact(updateContact);
        }


    }

    private void beforeDateModifyDecide() {
        for (Campaign campaign : newList) {
            if (campaign.RecordTypeid != (ID) system.label.RecordType_CampaignService)
                continue;
            Campaign old = oldMap.get(campaign.Id);
            //服务技师  2018/12/18   Start
            System.debug(campaign.Status + '____________' + campaign.is_true_or_false__c + '___________' + campaign.is_pass_or_fail__c);
            if (campaign.Status == '时间修改申请中' && campaign.is_true_or_false__c == true && campaign.is_pass_or_fail__c == 1) {

                campaign.CeremonyDate_copy__c = null;
                campaign.CeremonyDate_original__c = null;
                campaign.Lesson1Date_copy__c = null;
                campaign.Lesson1Date_original__c = null;
                campaign.Lesson2Date_copy__c = null;
                campaign.Lesson2Date_original__c = null;
                campaign.Lesson3Date_copy__c = null;
                campaign.Lesson3Date_original__c = null;
                campaign.Lesson4Date_copy__c = null;
                campaign.Lesson4Date_original__c = null;
                campaign.Lesson5Date_copy__c = null;
                campaign.Lesson5Date_original__c = null;
                campaign.Lesson6Date_copy__c = null;
                campaign.Lesson6Date_original__c = null;
                campaign.Lesson7Date_copy__c = null;
                campaign.Lesson7Date_original__c = null;
                campaign.Lesson8Date_copy__c = null;
                campaign.Lesson8Date_original__c = null;

                campaign.FinalExamDate_copy__c = null;
                campaign.FinalExamDate_original__c = null;
                campaign.Status = '公开中';
                campaign.is_true_or_false__c = false;
                //campaign.is_pass_or_fail__c = 0;
                //system.debug(campaign.Status);
            }
            if (campaign.Status == '时间修改申请中' && campaign.is_true_or_false__c == true && campaign.is_pass_or_fail__c == 2) {

                campaign.CeremonyDate__c = campaign.CeremonyDate_original__c;
                campaign.Lesson1Date__c = campaign.Lesson1Date_original__c;
                campaign.Lesson2Date__c = campaign.Lesson2Date_original__c;
                campaign.Lesson3Date__c = campaign.Lesson3Date_original__c;
                campaign.Lesson4Date__c = campaign.Lesson4Date_original__c;
                campaign.Lesson5Date__c = campaign.Lesson5Date_original__c;
                campaign.Lesson6Date__c = campaign.Lesson6Date_original__c;
                campaign.Lesson7Date__c = campaign.Lesson7Date_original__c;
                campaign.Lesson8Date__c = campaign.Lesson8Date_original__c;
                campaign.FinalExamDate__c = campaign.FinalExamDate_original__c;


                campaign.CeremonyDate_copy__c = null;
                campaign.CeremonyDate_original__c = null;

                campaign.Lesson1Date_copy__c = null;
                campaign.Lesson1Date_original__c = null;
                campaign.Lesson2Date_copy__c = null;
                campaign.Lesson2Date_original__c = null;
                campaign.Lesson3Date_copy__c = null;
                campaign.Lesson3Date_original__c = null;
                campaign.Lesson4Date_copy__c = null;
                campaign.Lesson4Date_original__c = null;
                campaign.Lesson5Date_copy__c = null;
                campaign.Lesson5Date_original__c = null;
                campaign.Lesson6Date_copy__c = null;
                campaign.Lesson6Date_original__c = null;
                campaign.Lesson7Date_copy__c = null;
                campaign.Lesson7Date_original__c = null;
                campaign.Lesson8Date_copy__c = null;
                campaign.Lesson8Date_original__c = null;

                campaign.FinalExamDate_copy__c = null;
                campaign.FinalExamDate_original__c = null;
                campaign.Status = '公开中';
                campaign.is_true_or_false__c = false;
                //campaign.is_pass_or_fail__c = 0;

            }
            campaign.StartDate__c = campaign.CeremonyDate__c == null  ?
                                    campaign.Lesson1Date__c : campaign.CeremonyDate__c;
            //system.debug(campaign.Status + '_______1_____' + campaign.is_true_or_false__c + '_______1____' + campaign.is_pass_or_fail__c);

            // 服务技师  2018/12/18 END
        }

    }

    // OBPM市场活动联动 add 手动绑定决裁信息时，从决裁上获取信息并写入 start
    public void GetInfoFromAdjudication() {
        List<String> app_ids = new List<String>();
        List<String> cam_ids = new List<String>();
        for (Campaign cam : this.newList) {
            Campaign old_cam = this.oldMap != null ? this.oldMap.get(cam.Id) : new Campaign();
            // 会议决裁不为空时
            if (String.isNotBlank(cam.Meeting_Approved_No__c) && cam.Meeting_Approved_No__c != old_cam.Meeting_Approved_No__c) {
                app_ids.add(cam.Meeting_Approved_No__c);
                if (String.isNotBlank(cam.Id)) {
                    cam_ids.add(cam.Id);
                }
            }
        }
        System.debug('app_ids size>>>' + app_ids.size());  
        System.debug('cam_ids size>>>' + cam_ids.size());  
        for(String num : app_ids) {
          System.debug('app_ids>>>' + num);  
        }
        for(String num2 : cam_ids) {
            System.debug('cam_ids>>>' + num2);  
          }
        
        if (app_ids.size() > 0) {
            List<OPDPlan__c> plans = cam_ids.size() > 0 ? [select Id, Campaign__c, IF_Approved__c, Approved_Status__c, Approved_No__c from OPDPlan__c where Campaign__c in :cam_ids] : null;
            List<Application_for_Conference_Adjudication__c> apps = [select Id, 
                                                                        MeetingApprovedNo__c, 
                                                                        Num__c,
                                                                        ProcessState__c,  
                                                                        StartDate__c, 
                                                                        EndDate__c, 
                                                                        Implementation_Purpose1__c, 
                                                                        OfficeCategory__c, 
                                                                        MeetingType__c, 
                                                                        ActivityTypeName__c, 
                                                                        WorkshopPlace__c, 
                                                                        HostName__c, 
                                                                        CooperatorCompany__c, 
                                                                        ExpectedOlympusAttendance__c, 
                                                                        ExpectedHcpAttendance__c, 
                                                                        StateMaster__c, 
                                                                        CityMaster__c, 
                                                                        IsPaidToSponsor__c, 
                                                                        SponsorshipCategory__c, 
                                                                        ConveningParticipantsNum__c,
                                                                        Type_of_adjudication_meeting__c  
                                                                    from 
                                                                        Application_for_Conference_Adjudication__c 
                                                                    where 
                                                                        Id in :app_ids];
            System.debug('apps size>>>>>' + apps.size());
            if (apps != null && apps.size() > 0) {
                List<Application_for_Conference_Adjudication__c> set_cam_num_apps = new List<Application_for_Conference_Adjudication__c>();
                for (Application_for_Conference_Adjudication__c app : apps) {
                    for (Campaign cam : this.newList) {
                        if (cam.Meeting_Approved_No__c == app.Id && cam_ids.contains(cam.Id)) {
                            cam.MeetingApprovedNo__c             = app.MeetingApprovedNo__c;
                            // 非赞助会的情况下更新以下内容
                            if (app.MeetingType__c != '赞助会') {
                                //20221206 lt DB202212060256 start 
                                // System.debug('lt123-------Cam------记录类型----'+cam.RecordTypeId);
                                // System.debug('lt123-------Cam------会议类型----'+cam.Meeting_Type__c);
                                if(cam.RecordTypeId == System.label.camp_1 || cam.RecordTypeId == System.label.camp_2){ //niwu  -012C600000003I2-012C600000003Hy
                                    // System.debug('lt123-------Cam------1----');
                                    if (cam.Meeting_Type__c != '主办会'){
                                        // System.debug('lt123-------Cam------2----');
                                        cam.StartDate                        = app.StartDate__c;
                                        cam.EndDate                          = app.EndDate__c;
                                    }
                                }
                                if(cam.RecordTypeId != System.label.camp_1 && cam.RecordTypeId != System.label.camp_2){
                                    // System.debug('lt123-------Cam------3----');
                                    cam.StartDate                        = app.StartDate__c;
                                    cam.EndDate                          = app.EndDate__c;
                                }
                                // cam.StartDate                        = app.StartDate__c;
                                // cam.EndDate                          = app.EndDate__c;
                                //20221206 lt DB202212060256 end
                                cam.Implementation_Purpose__c                   = app.Implementation_Purpose1__c;
                                cam.OfficeCategory__c                = app.OfficeCategory__c;
                                cam.Meeting_Type__c                  = app.MeetingType__c;

                                cam.WorkshopPlace__c                 = String.isNotBlank(app.WorkshopPlace__c) ? app.WorkshopPlace__c : cam.WorkshopPlace__c;
                                cam.HostName__c                      = app.HostName__c;
                                cam.cooperatorCompany__c             = app.CooperatorCompany__c;
                                cam.Convening_Participants_Num__c    = app.ConveningParticipantsNum__c;

                                // OBPM对接 新增字段
                                cam.Expected_Olympus_Attendance__c   = app.ExpectedOlympusAttendance__c;
                                cam.Expected_Hcp_Attendance__c       = app.ExpectedHcpAttendance__c;
                                cam.SponsorshipCategory__c           = app.SponsorshipCategory__c;
                                cam.IsPaidToSponsor__c               = app.IsPaidToSponsor__c;
                                // cam.Activity_Type_Name__c            = app.ActivityTypeName__c;
                                cam.Activity_Type_Name__c            = app.Type_of_adjudication_meeting__c;
                                // 这里需要额外加操作
                                cam.State_Master__c                  = String.isNotBlank(app.StateMaster__c) ? StateCityUtil.get_state(app.StateMaster__c) : cam.State_Master__c;                 
                                cam.City_Master__c                   = String.isNotBlank(app.CityMaster__c)  ? StateCityUtil.get_city(app.CityMaster__c) : cam.City_Master__c;
                                
                                if (cam.Num__c != app.Num__c) {
                                    app.Num__c = cam.Num__c;
                                    set_cam_num_apps.add(app);
                                }
                            }
                            // opd计划上的内容更新
                            if (plans != null && plans.size() > 0) {
                                for (OPDPlan__c plan : plans) {
                                    if (plan.Campaign__c == cam.Id) {
                                        plan.IF_Approved__c = true;
                                        plan.Approved_Status__c = cam.Approved_Status__c;
                                        plan.Approved_No__c = cam.Meeting_Approved_No__c;
                                        break;
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
                if (set_cam_num_apps.size() > 0) {
                    update set_cam_num_apps;
                }
                if (plans != null && plans.size() > 0) {
                    update plans;
                }
            }
        }
    }
    // OBPM市场活动联动 add 手动绑定决裁信息时，从决裁上获取信息并写入 end
}