@isTest
public class ApplicationForCAHandlerTest {

    private static User setupUser(){
        User u = new User(alias = 'manager', email='manager@acme.com',
                emailencodingkey='UTF-8', lastname='manager',
                languagelocalekey='en_US',
                localesidkey='ja_JP', profileid = System.Label.ProfileId_SystemAdmin,
                timezonesidkey='Asia/Shanghai',
                username='manager@acme.com' + Datetime.now().getTime(),
                ZongjianApprovalManager__c =  UserInfo.getUserId()
            );
        insert u;
        return u;
    }

    static testMethod void test_update_1() {
        Application_for_Conference_Adjudication__c aca = new Application_for_Conference_Adjudication__c();
        aca.MeetingApprovedNo__c            = 'ME154P03-0012';
        aca.Name                            = 'ME154P03-0012'; // 用决裁编号替代名称
        aca.CampaignTheme__c                = 'obpm发起赞助决裁';
        aca.Num__c                          = 'test1';
        aca.StartDate__c                    = NFMUtil.parseDateTimeStr2Date('2022-04-19');
        aca.EndDate__c                      = NFMUtil.parseDateTimeStr2Date('2022-04-22');
        aca.BudgetType__c                   = '其他';
        aca.OfficeCategory__c               = '1.消化科';
        aca.MeetingType__c                  = '主办会';
        aca.StateMaster__c                  = '北京市';
        aca.CityMaster__c                   = '北京市';
        aca.WorkshopPlace__c                = 'CTEC';
        aca.HostName__c                     = '医学会';
        aca.CooperatorCompany__c            = '中华医学会';
        aca.ConveningParticipantsNum__c     = 0;
        aca.IsPaidToSponsor__c              = '否';
        aca.SponsorshipCategory__c          = '资金';
        aca.ExpectedOlympusAttendance__c    = 0;
        aca.ActivityTypeName__c             = '自主主办会';
        aca.ExpectedHcpAttendance__c        = 0;
        aca.ProcessState__c                 = '草稿';
        aca.Type_of_adjudication_meeting__c = '线下';
        aca.Activities_of_a_share__c        = '否';
        aca.IsSupplemental__c = true;

        insert aca;

        Campaign cam = new Campaign();
        Id RtId = Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Society').getRecordTypeId();
        // Id RtId2 = Schema.SObjectType.CampaignMember.getRecordTypeInfosByDeveloperName().get('WorkShop').getRecordTypeId();
        cam.RecordTypeId                     = RtId;
        // cam.CampaignMemberRecordTypeId       = RtId2;

        cam.Name                             = aca.MeetingApprovedNo__c;
        cam.Name2__c                         = aca.MeetingApprovedNo__c;
        cam.MeetingApprovedNo__c             = aca.MeetingApprovedNo__c;
        cam.Meeting_Approved_No__c           = aca.Id;
        cam.StartDate                        = aca.StartDate__c;
        cam.EndDate                          = aca.EndDate__c;
        cam.Budget_Type__c                   = aca.BudgetType__c;
        cam.OfficeCategory__c                = aca.OfficeCategory__c;
        cam.Meeting_Type__c                  = aca.MeetingType__c;
        cam.Activity_Type_Name__c            = aca.ActivityTypeName__c;
        
        cam.WorkshopPlace__c                 = aca.WorkshopPlace__c;
        cam.HostName__c                      = aca.HostName__c;
        cam.cooperatorCompany__c             = aca.CooperatorCompany__c;
        cam.Convening_Participants_Num__c    = aca.ConveningParticipantsNum__c;
        cam.IF_Approved__c                   = true;

        // OBPM对接 新增字段
        cam.Expected_Olympus_Attendance__c   = aca.ExpectedOlympusAttendance__c;
        cam.Expected_Hcp_Attendance__c       = aca.ExpectedHcpAttendance__c;
        cam.SponsorshipCategory__c           = aca.SponsorshipCategory__c;
        cam.IsPaidToSponsor__c               = aca.IsPaidToSponsor__c;
        cam.Activity_Type_Name__c            = aca.ActivityTypeName__c;
        cam.Is_LendProduct__c                = '是';
        cam.PlanBackData__c                  = NFMUtil.parseDateTimeStr2Date('2022-04-30');
        cam.LoadNum__c                       = 'CV-290*1; GI-290镜子*1;';
        insert cam;

        User u = setupUser();
        System.runAs(u){
            Test.startTest();
            OPDPlan__c opd = new OPDPlan__c();
            opd.OPDPlan_ImplementDate_temp__c  =  cam.StartDate ;
            opd.Campaign__c = cam.Id ;
            opd.Status__c = '计划中';
            opd.OwnerId = cam.OwnerId;
            opd.PlanProdDetail__c = cam.LoadNum__c;
            opd.OPDType__c = '学会';
            // opd.Meeting_Cooperate__c = cam.Meeting_Cooperate__c;
            // opd.Meeting_Cooperate2__c = cam.Meeting_Cooperate2__c;
            // opd.Meeting_Cooperate3__c = cam.Meeting_Cooperate3__c;
            // opd.Meeting_Cooperate4__c = cam.Meeting_Cooperate4__c;
            // opd.Open_scope__c = cam.Open_scope__c;
            opd.IF_Approved__c = cam.IF_Approved__c;//是否申请决裁
            opd.Approved_Status__c = cam.Approved_Status__c;//决裁状态
            opd.Approved_No__c = cam.Meeting_Approved_No__c;
            opd.OwnerId = u.Id;
            insert opd;


            aca.ProcessState__c = '审批成功';
            aca.Num__c = cam.Num__c;
            update aca;
            
            Test.stopTest();
        }
    }

    static testMethod void test_update_2() {
        Application_for_Conference_Adjudication__c aca = new Application_for_Conference_Adjudication__c();
        aca.MeetingApprovedNo__c            = 'ME154P03-0012';
        aca.Name                            = 'ME154P03-0012'; // 用决裁编号替代名称
        aca.CampaignTheme__c                = 'obpm发起赞助决裁';
        aca.Num__c                          = 'test1';
        aca.StartDate__c                    = NFMUtil.parseDateTimeStr2Date('2022-04-19');
        aca.EndDate__c                      = NFMUtil.parseDateTimeStr2Date('2022-04-22');
        aca.BudgetType__c                   = '其他';
        aca.OfficeCategory__c               = '1.消化科';
        aca.MeetingType__c                  = '主办会';
        aca.StateMaster__c                  = '北京市';
        aca.CityMaster__c                   = '北京市';
        aca.WorkshopPlace__c                = 'CTEC';
        aca.HostName__c                     = '医学会';
        aca.CooperatorCompany__c            = '中华医学会';
        aca.ConveningParticipantsNum__c     = 0;
        aca.IsPaidToSponsor__c              = '否';
        aca.SponsorshipCategory__c          = '资金';
        aca.ExpectedOlympusAttendance__c    = 0;
        aca.ActivityTypeName__c             = '自主主办会';
        aca.ExpectedHcpAttendance__c        = 0;
        aca.ProcessState__c                 = '草稿';
        aca.Type_of_adjudication_meeting__c = '线下';
        aca.Activities_of_a_share__c        = '否';
        aca.IsSupplemental__c = false;

        insert aca;

        Campaign cam = new Campaign();
        Id RtId = Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Society').getRecordTypeId();
        // Id RtId2 = Schema.SObjectType.CampaignMember.getRecordTypeInfosByDeveloperName().get('WorkShop').getRecordTypeId();
        cam.RecordTypeId                     = RtId;
        // cam.CampaignMemberRecordTypeId       = RtId2;

        cam.Name                             = aca.MeetingApprovedNo__c;
        cam.Name2__c                         = aca.MeetingApprovedNo__c;
        cam.MeetingApprovedNo__c             = aca.MeetingApprovedNo__c;
        cam.Meeting_Approved_No__c           = aca.Id;
        cam.StartDate                        = aca.StartDate__c;
        cam.EndDate                          = aca.EndDate__c;
        cam.Budget_Type__c                   = aca.BudgetType__c;
        cam.OfficeCategory__c                = aca.OfficeCategory__c;
        cam.Meeting_Type__c                  = aca.MeetingType__c;
        cam.Activity_Type_Name__c            = aca.ActivityTypeName__c;
        
        cam.WorkshopPlace__c                 = aca.WorkshopPlace__c;
        cam.HostName__c                      = aca.HostName__c;
        cam.cooperatorCompany__c             = aca.CooperatorCompany__c;
        cam.Convening_Participants_Num__c    = aca.ConveningParticipantsNum__c;
        cam.IF_Approved__c                   = true;

        // OBPM对接 新增字段
        cam.Expected_Olympus_Attendance__c   = aca.ExpectedOlympusAttendance__c;
        cam.Expected_Hcp_Attendance__c       = aca.ExpectedHcpAttendance__c;
        cam.SponsorshipCategory__c           = aca.SponsorshipCategory__c;
        cam.IsPaidToSponsor__c               = aca.IsPaidToSponsor__c;
        cam.Activity_Type_Name__c            = aca.ActivityTypeName__c;
        cam.Is_LendProduct__c                = '是';
        cam.PlanBackData__c                  = NFMUtil.parseDateTimeStr2Date('2022-04-30');
        cam.LoadNum__c                       = 'CV-290*1; GI-290镜子*1;';
        insert cam;

        
        User u = setupUser();
        
        OPDPlan__c opd = new OPDPlan__c();
        opd.OPDPlan_ImplementDate_temp__c  =  cam.StartDate ;
        opd.Campaign__c = cam.Id ;
        opd.Status__c = '计划中';
        opd.OwnerId = cam.OwnerId;
        opd.PlanProdDetail__c = cam.LoadNum__c;
        opd.OPDType__c = '学会';
        // opd.Meeting_Cooperate__c = cam.Meeting_Cooperate__c;
        // opd.Meeting_Cooperate2__c = cam.Meeting_Cooperate2__c;
        // opd.Meeting_Cooperate3__c = cam.Meeting_Cooperate3__c;
        // opd.Meeting_Cooperate4__c = cam.Meeting_Cooperate4__c;
        // opd.Open_scope__c = cam.Open_scope__c;
        opd.IF_Approved__c = cam.IF_Approved__c;//是否申请决裁
        opd.Approved_Status__c = cam.Approved_Status__c;//决裁状态
        opd.Approved_No__c = aca.Id;
        opd.OwnerId = u.Id;
        insert opd;


        aca.ProcessState__c = '驳回';
        aca.Num__c = cam.Num__c;
        update aca;

    }

    static testMethod void test_update_3() {
        Application_for_Conference_Adjudication__c aca = new Application_for_Conference_Adjudication__c();
        aca.MeetingApprovedNo__c            = 'ME154P03-0012';
        aca.Name                            = 'ME154P03-0012'; // 用决裁编号替代名称
        aca.CampaignTheme__c                = 'obpm发起赞助决裁';
        aca.Num__c                          = 'test1';
        aca.StartDate__c                    = NFMUtil.parseDateTimeStr2Date('2022-04-19');
        aca.EndDate__c                      = NFMUtil.parseDateTimeStr2Date('2022-04-22');
        aca.BudgetType__c                   = '其他';
        aca.OfficeCategory__c               = '1.消化科';
        aca.MeetingType__c                  = '主办会';
        aca.StateMaster__c                  = '北京市';
        aca.CityMaster__c                   = '北京市';
        aca.WorkshopPlace__c                = 'CTEC';
        aca.HostName__c                     = '医学会';
        aca.CooperatorCompany__c            = '中华医学会';
        aca.ConveningParticipantsNum__c     = 0;
        aca.IsPaidToSponsor__c              = '否';
        aca.SponsorshipCategory__c          = '资金';
        aca.ExpectedOlympusAttendance__c    = 0;
        aca.ActivityTypeName__c             = '自主主办会';
        aca.ExpectedHcpAttendance__c        = 0;
        aca.ProcessState__c                 = '草稿';
        aca.Type_of_adjudication_meeting__c = '线下';
        aca.Activities_of_a_share__c        = '否';
        aca.IsSupplemental__c = false;

        insert aca;

        Campaign cam = new Campaign();
        Id RtId = Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Society').getRecordTypeId();
        // Id RtId2 = Schema.SObjectType.CampaignMember.getRecordTypeInfosByDeveloperName().get('WorkShop').getRecordTypeId();
        cam.RecordTypeId                     = RtId;
        // cam.CampaignMemberRecordTypeId       = RtId2;

        cam.Name                             = aca.MeetingApprovedNo__c;
        cam.Name2__c                         = aca.MeetingApprovedNo__c;
        cam.MeetingApprovedNo__c             = aca.MeetingApprovedNo__c;
        cam.Meeting_Approved_No__c           = aca.Id;
        cam.StartDate                        = aca.StartDate__c;
        cam.EndDate                          = aca.EndDate__c;
        cam.Budget_Type__c                   = aca.BudgetType__c;
        cam.OfficeCategory__c                = aca.OfficeCategory__c;
        cam.Meeting_Type__c                  = aca.MeetingType__c;
        cam.Activity_Type_Name__c            = aca.ActivityTypeName__c;
        
        cam.WorkshopPlace__c                 = aca.WorkshopPlace__c;
        cam.HostName__c                      = aca.HostName__c;
        cam.cooperatorCompany__c             = aca.CooperatorCompany__c;
        cam.Convening_Participants_Num__c    = aca.ConveningParticipantsNum__c;
        cam.IF_Approved__c                   = true;

        // OBPM对接 新增字段
        cam.Expected_Olympus_Attendance__c   = aca.ExpectedOlympusAttendance__c;
        cam.Expected_Hcp_Attendance__c       = aca.ExpectedHcpAttendance__c;
        cam.SponsorshipCategory__c           = aca.SponsorshipCategory__c;
        cam.IsPaidToSponsor__c               = aca.IsPaidToSponsor__c;
        cam.Activity_Type_Name__c            = aca.ActivityTypeName__c;
        cam.Is_LendProduct__c                = '是';
        cam.PlanBackData__c                  = NFMUtil.parseDateTimeStr2Date('2022-04-30');
        cam.LoadNum__c                       = 'CV-290*1; GI-290镜子*1;';
        cam.Num__c                           = 'test1';
        insert cam;

        Campaign cam_n = new Campaign();
        cam_n.RecordTypeId                     = RtId;
        // cam_n.CampaignMemberRecordTypeId       = RtId2;

        cam_n.Name                             = aca.MeetingApprovedNo__c;
        cam_n.Name2__c                         = aca.MeetingApprovedNo__c;
        cam_n.MeetingApprovedNo__c             = aca.MeetingApprovedNo__c;
        cam_n.Meeting_Approved_No__c           = aca.Id;
        cam_n.StartDate                        = aca.StartDate__c;
        cam_n.EndDate                          = aca.EndDate__c;
        cam_n.Budget_Type__c                   = aca.BudgetType__c;
        cam_n.OfficeCategory__c                = aca.OfficeCategory__c;
        cam_n.Meeting_Type__c                  = aca.MeetingType__c;
        cam_n.Activity_Type_Name__c            = aca.ActivityTypeName__c;
        
        cam_n.WorkshopPlace__c                 = aca.WorkshopPlace__c;
        cam_n.HostName__c                      = aca.HostName__c;
        cam_n.cooperatorCompany__c             = aca.CooperatorCompany__c;
        cam_n.Convening_Participants_Num__c    = aca.ConveningParticipantsNum__c;
        cam_n.IF_Approved__c                   = true;

        // OBPM对接 新增字段
        cam_n.Expected_Olympus_Attendance__c   = aca.ExpectedOlympusAttendance__c;
        cam_n.Expected_Hcp_Attendance__c       = aca.ExpectedHcpAttendance__c;
        cam_n.SponsorshipCategory__c           = aca.SponsorshipCategory__c;
        cam_n.IsPaidToSponsor__c               = aca.IsPaidToSponsor__c;
        cam_n.Activity_Type_Name__c            = aca.ActivityTypeName__c;
        cam_n.Is_LendProduct__c                = '是';
        cam_n.PlanBackData__c                  = NFMUtil.parseDateTimeStr2Date('2022-04-30');
        cam_n.LoadNum__c                       = 'CV-290*1; GI-290镜子*1;';
        cam_n.Num__c                           = 'test2';
        insert cam_n;

        aca.Num__c = cam.Num__c;
        update aca;

        aca.Num__c = cam_n.Num__c;
        update aca;

    }
}