@isTest
private class CampaignMemberServiceControllerTest {

    @testSetup 
    static void makeData(){
        TestDataUtility.CreatePIPolicyConfiguration('Contact');
    }
    static void testSetupMethod() {
        RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName IN ('Department_GI', 'Department_BF') order by DeveloperName desc];

        Account acc = new Account();
        acc.RecordTypeId = rectCo.Id;
        acc.Name = 'HP test1';
        insert acc;

        List<Account> dept = [select Id, Name from Account where ParentId = :acc.Id and Department_Class_Label__c IN ('消化科', '呼吸科') order by Department_Class_Label__c];

        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = 'Gastoro Intestin Test';
        depart1.ParentId            = dept[0].Id;
        depart1.Department_Class__c = dept[0].Id;
        depart1.Hospital__c         = acc.Id;

        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[1].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dept[1].Id;
        depart2.Department_Class__c = dept[1].Id;
        depart2.Hospital__c         = acc.Id;
        insert new Account[] {depart1, depart2};

        Contact con = new Contact();
        con.LastName = 'lastname2';
        con.FirstName = 'firstname2';
        con.Email = 'olympustest033@sunbridge.com';
        con.MobilePhone = '99999999';
        con.Work_Location_manual__c = 'location2';
        con.Post_picklist__c = '部长';
        con.Job_Category_picklist__c = '销售推广';
        con.Hire_date_text__c = Date.today().addMonths(-6);
        con.Gender_text__c = '男';
        con.dept__c = '服务本部';
        con.Pregnant_Rest__c = true;

        Contact con1 = new Contact();
        con1.LastName = 'testname2';
        con1.FirstName = 'firstname2';
        con1.Email = 'olympustest0331@sunbridge.com';
        con1.MobilePhone = '999999199';
        con1.Work_Location_manual__c = 'location2';
        con1.Post_picklist__c = '部长';
        con1.Job_Category_picklist__c = '销售推广';
        con1.Hire_date_text__c = Date.today().addMonths(-6);
        con1.Gender_text__c = '男';
        con1.dept__c = '服务本部';
        con1.Pregnant_Rest__c = true;
        insert new Contact[] {con, con1};
        RecordType oppVND = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity'];

        Opportunity opp1 = new Opportunity(
            Name = 'test opp1',
            StageName = '引合',
            CurrencyIsoCode = 'USD',
            CloseDate = Date.today(),
            AccountId = depart1.Id,
            RecordTypeId = oppVND.Id,
            Closing_Bid_Date__c = Date.today().addDays(-5),
            Hospital__c = acc.Id,
            Competitor__c = 'A'
        );
        Opportunity opp2 = new Opportunity(
            Name = 'test opp2',
            StageName = '引合',
            CurrencyIsoCode = 'USD',
            CloseDate = Date.today(),
            AccountId = depart2.Id,
            RecordTypeId = oppVND.Id,
            Closing_Bid_Date__c = Date.today().addDays(-5),
            Hospital__c = acc.Id,
            Competitor__c = 'B'
        );
        insert new Opportunity[] {opp1, opp2};
        RecordType rectCA = [select Id from RecordType where IsActive = true and SobjectType = 'Campaign'
                             and DeveloperName = 'ServiceEngineerTraining'];

        Campaign cam = new Campaign();
        cam.Name = 'cam';
        cam.Name2__c = '1234';
        cam.recordtypeid = rectCA.id;
        cam.StartDate = Date.today().addDays(15);
        cam.EndDate = Date.today().addDays(18);
        cam.Lesson_Type__c = '标准';
        cam.TrainingType__c = 'BME';
        cam.Mailflg_after45__c = true;
        cam.Mailflg_cancel__c = true;
        cam.Mailflg_before15__c = true;
        cam.Mailflg_before7__c = true;
        cam.Mailflg_after3__c = true;
        insert cam;
        CampaignMember__c cm1 = new CampaignMember__c();

        list<CampaignMember__c> cmList = new list<CampaignMember__c>();
        cmList.add(cm1);

        cm1.Department__c = depart1.Name;
        cm1.Department_ID__c = depart1.Id;
        cm1.Contact__c = con.LastName;
        cm1.Contact_ID__c = con.Id;
        cm1.Campaign__c = cam.id;



        upsert cmList;


    }
    @isTest static void test_method_one() {
        testSetupMethod();
        list<Campaign> camlist = [select id from campaign];
        Campaign cam = camlist[0];
        PageReference page = new PageReference('/apex/CampaignMemberService?id=' + cam.Id);
        System.Test.setCurrentPage(page);
        CampaignMemberServiceController cmc = new CampaignMemberServiceController();
        //初始化测试
        cmc.init();

        cmc.getLineInfoListSize();

        cmc.addLine();

        cmc.checkLine();

        cmc.lineNo = 7;
        cmc.deleteLine();
        Test.startTest();
        cmc.saveLine();
        List<CampaignMember__c> cmList = [ select id, Name, Campaign__c, Department__c, Department_ID__c, Opportunity__c, Opportunity_ID__c,
                                           Contact__c, Contact_ID__c, Type__c
                                           from CampaignMember__c
                                           where Campaign__c = :cam.Id];


        System.Test.setCurrentPage(page);
        CampaignMemberServiceController cmc2 = new CampaignMemberServiceController();
        cmc2.init();
        cmc.saveLine();
        Test.stopTest();
    }

    @isTest static void test_method_two() {
        testSetupMethod();
        list<Campaign> camlist = [select id from campaign];
        Campaign cam1 = camlist[0];
        RecordType rectCA = [select Id from RecordType where IsActive = true and SobjectType = 'Campaign'
                             and DeveloperName = 'ServiceEngineerTraining'];
        Test.startTest();
        Campaign cam2 = new Campaign();
        cam2.Name = 'cam';
        cam2.Name2__c = '1234';
        cam2.recordtypeid = rectCA.id;
        cam2.Lesson_Type__c = '标准';
        cam2.TrainingType__c = 'BME';
        cam2.StartDate = Date.today().addDays(15);
        cam2.EndDate = Date.today().addDays(18);

        insert cam2;


        Contact con1 = new Contact();
        con1.LastName = 'testname2V2';
        con1.FirstName = 'firstname2V2';
        con1.Email = 'olympustest0331@sunbridge.com';
        con1.MobilePhone = '999999199V2';
        con1.Work_Location_manual__c = 'location2';
        con1.Post_picklist__c = '部长';
        con1.Job_Category_picklist__c = '销售推广';
        con1.Hire_date_text__c = Date.today().addMonths(-6);
        con1.Gender_text__c = '男';
        con1.dept__c = '服务本部';
        con1.Pregnant_Rest__c = true;
        con1.Campaign__c = cam1.id;
        insert con1;

        Test.stopTest();
        PageReference page = new PageReference('/apex/CampaignMemberService?id=' + cam2.Id);
        System.Test.setCurrentPage(page);
        CampaignMemberServiceController cmc = new CampaignMemberServiceController();
        //初始化测试
        cmc.init();
        list<Account> departList = [select id, Name from Account];
        Account depart1 = departList[0];
        cmc.lineInfoList[2].cm.Department__c = depart1.Name;
        cmc.lineInfoList[2].cm.Department_ID__c = depart1.Id;
        cmc.lineInfoList[2].cm.Contact__c = con1.LastName;
        cmc.lineInfoList[2].cm.Contact_ID__c = con1.Id;
        cmc.saveLine();
        cmc.saveflg = '1';
        cmc.checkLine();
        cmc.errorStr = '1';
        cmc.checkLine();

    }

}