/**
 * @author 张玉山
 * @DateTime 2019-03-11T14:20:19+0800
 * 时间修改申请日期处理
 * 开班日期初始化
 */
public with 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; 
        }
    }
    protected override void beforeUpdate() {
        beforeDateModifyDecide();
        CampaignToContact();
        // OBPM市场活动联动 add 手动绑定决裁信息时，从决裁上获取信息并写入 start
        GetInfoFromAdjudication();
        // OBPM市场活动联动 add 手动绑定决裁信息时，从决裁上获取信息并写入 end
    }
    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.get(cam.Id);
            // 会议决裁发生变化且不为空时
            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);
                cam_ids.add(cam.Id);
            }
        }
        if (app_ids.size() > 0) {
            List<Application_for_Conference_Adjudication__c> apps = [select Id, 
                                                                        MeetingApprovedNo__c, 
                                                                        ActivityTypeName__c, 
                                                                        ExpectedOlympusAttendance__c, 
                                                                        ExpectedHcpAttendance__c, 
                                                                        StateMaster__c, 
                                                                        CityMaster__c, 
                                                                        IsPaidToSponsor__c, 
                                                                        SponsorshipCategory__c, 
                                                                        ConveningParticipantsNum__c 
                                                                    from 
                                                                        Application_for_Conference_Adjudication__c 
                                                                    where 
                                                                        Id in :app_ids];
            if (apps != null && apps.size() > 0) {
                init_state_city();
                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;
                            cam.Meeting_Approved_No__c           = app.Id;

                            cam.Activity_Type_Name__c            = app.ActivityTypeName__c;
                            cam.Expected_Olympus_Attendance__c   = app.ExpectedOlympusAttendance__c;
                            cam.Expected_Hcp_Attendance__c       = app.ExpectedHcpAttendance__c;
                            // 这里需要额外加操作
                            cam.State_Master__c                  = get_state(app.StateMaster__c);                 
                            cam.City_Master__c                   = get_city(app.CityMaster__c);
                            cam.IsPaidToSponsor__c               = app.IsPaidToSponsor__c;
                            cam.SponsorshipCategory__c           = app.SponsorshipCategory__c;
                            cam.Convening_Participants_Num__c    = app.ConveningParticipantsNum__c;
                            break;
                        }
                    }
                }
            }
        }
    }

    // 初始化省市信息
    public static void init_state_city() {
        if (states == null || states.size() <= 0) {
            states = new Map<String, String>();
            List<Address_Level__c> lst_state = [select Id, Name from Address_Level__c];
            for (Address_Level__c level1 : lst_state) {
                states.put(level1.Name, level1.Id);
            }
        }
        if (cities == null || cities.size() <= 0) {
            cities = new Map<String, String>();
            List<Address_Level2__c> lst_city = [select Id, Name from Address_Level2__c];
            for (Address_Level2__c level2 : lst_city) {
                cities.put(level2.Name, level2.Id);
            }
        }
    }

    // 设置会议上的省
    public static String get_state(String state_name) {
        if (String.isBlank(state_name)) {
            return null;
        }
        if (states == null || states.size() <= 0) {
            return null;
        }
        // 处理空格 或许后续还有别的处理
        state_name = state_name.trim().replace(' ', '');
        for(String state : states.keySet()) {
            if (state_name.contains(state) || state.contains(state_name)) {
                return states.get(state);
            }
        }
        return null;
    }

    // 设置会议上的市
    public static String get_city(String city_name) {
        if (String.isBlank(city_name)) {
            return null;
        }
        if (cities == null || cities.size() <= 0) {
            return null;
        }
        // 处理空格 或许后续还有别的处理
        city_name = city_name.trim().replace(' ', '');
        for(String city : cities.keySet()) {
            if (city_name.contains(city) || city.contains(city_name)) {
                return cities.get(city);
            }
        }
        return null;
    }
    // OBPM市场活动联动 add 手动绑定决裁信息时，从决裁上获取信息并写入 end
}