@isTest
private class TargetCustomerAssignmentBatchTest {
    //SWAG-BHP42V    精琢技术  20191209 START 
    static testMethod void UnitTest_01() {
        //医院 记录类型
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        if (rectHp.size() == 0) {
            return;
        }
        //科室 记录类型
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'];
        if (rectDpt.size() == 0) {
            return;
        }

        //简档
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];

        //用户
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', Work_Location__c = '北京', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        User hpOwner2 = new User(Test_staff__c = true, LastName = 'hp2', FirstName = 'owner', Alias = 'hp2', Work_Location__c = '重庆', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner2;


        //医院
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.Id);
        hp.FSE_GI_Main_Leader__c = hpOwner.Id;
        hp.FSE_SP_Main_Leader__c = hpOwner2.Id;
        insert hp;

        //战略科室
        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];
        //科室
        Account dpt = new Account(RecordTypeId = rectDpt[0].Id);
        dpt.Name         = '*';
        dpt.Department_Name__c  = 'TestDepart';
        dpt.ParentId            = dc.Id;
        dpt.Department_Class__c = dc.Id;
        dpt.Hospital__c         = hp.Id;
        insert dpt;

        //OPD计划 
        OPDPlan__c tempOPDPlan = new OPDPlan__c();
        tempOPDPlan.ThisMonthPlan__c = 1;
        tempOPDPlan.Status__c = '计划中';
        tempOPDPlan.Account_Laboratory__c =dpt.Id;
        tempOPDPlan.OPD_Customers_Target__c = null;
        //opd计划实施日期为今天
        tempOPDPlan.OPDPlan_ImplementDate__c = Date.today();
        tempOPDPlan.NoOpp_Reason__c = 'HCP对应';
        insert tempOPDPlan;

        //根据opd计划实施日期 算出本财年
        Date dateToday = Date.today();
        Integer year = dateToday.year();
        Integer month = dateToday.month();
        if (month < 4) {
            year -= 1;
        }
        String selectedTab = String.valueOf(year - 1867 - 1 + 'P'); 

        //客户-目标客户 默认让opd计划和客户-目标客户的财年相同
        Account_Number_of_target__c anot = new Account_Number_of_target__c();
        anot.Account__c = dc.Id;
        anot.Account_HP__c = hp.Id;
        anot.OCM_Period__c = selectedTab;
        anot.Target_amount_170_190__c =36597;
        insert anot;
        
        //测试
        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new TargetCustomerAssignmentBatch(), 10);
        System.Test.StopTest();
    }

    //测试 传入一个id 指定更新某一条 batch
    static testMethod void UnitTest_02() {
        //医院 记录类型
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        if (rectHp.size() == 0) {
            return;
        }
        // 科室 记录类型
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'];
        if (rectDpt.size() == 0) {
            return;
        }

        //简档
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];

        //用户
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', Work_Location__c = '北京', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        User hpOwner2 = new User(Test_staff__c = true, LastName = 'hp2', FirstName = 'owner', Alias = 'hp2', Work_Location__c = '重庆', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner2;


        //医院
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.Id);
        hp.FSE_GI_Main_Leader__c = hpOwner.Id;
        hp.FSE_SP_Main_Leader__c = hpOwner2.Id;
        insert hp;

        //战略科室
        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];

        //科室
        Account dpt = new Account(RecordTypeId = rectDpt[0].Id);
        dpt.Name         = '*';
        dpt.Department_Name__c  = 'TestDepart';
        dpt.ParentId            = dc.Id;
        dpt.Department_Class__c = dc.Id;
        dpt.Hospital__c         = hp.Id;
        insert dpt;

        //opd计划
        OPDPlan__c tempOPDPlan = new OPDPlan__c();
        tempOPDPlan.ThisMonthPlan__c = 1;
        tempOPDPlan.Status__c = '计划中';
        tempOPDPlan.Account_Laboratory__c =dpt.Id;
        tempOPDPlan.OPD_Customers_Target__c = null;
        tempOPDPlan.OPDPlan_ImplementDate__c = Date.today();
        tempOPDPlan.NoOpp_Reason__c = 'HCP对应';
        insert tempOPDPlan;

        //opd计划实施日期为今天
        Date dateToday = Date.today();
        Integer year = dateToday.year();
        Integer month = dateToday.month();
        if (month < 4) {
            year -= 1;
        }
        String selectedTab = String.valueOf(year - 1867 - 1 + 'P'); 

        //客户-目标客户
        Account_Number_of_target__c anot = new Account_Number_of_target__c();
        anot.Account__c = dc.Id;
        anot.Account_HP__c = hp.Id;
        anot.OCM_Period__c = selectedTab;
        anot.Target_amount_170_190__c =36597;
        insert anot;
        
        //测试
        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new TargetCustomerAssignmentBatch(tempOPDPlan.Id), 10);
        System.Test.StopTest();
    }
    //测试传入一批Id 批量更新batch
    static testMethod void UnitTest_03() {
        //医院 记录类型
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        if (rectHp.size() == 0) {
            return;
        }
        //科室 记录类型
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'];
        if (rectDpt.size() == 0) {
            return;
        }

        //简档
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];

        //用户
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', Work_Location__c = '北京', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        User hpOwner2 = new User(Test_staff__c = true, LastName = 'hp2', FirstName = 'owner', Alias = 'hp2', Work_Location__c = '重庆', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner2;


        //医院
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.Id);
        hp.FSE_GI_Main_Leader__c = hpOwner.Id;
        hp.FSE_SP_Main_Leader__c = hpOwner2.Id;
        insert hp;

        //战略科室
        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];

        //科室1
        Account dpt = new Account(RecordTypeId = rectDpt[0].Id);
        dpt.Name         = '*';
        dpt.Department_Name__c  = 'TestDepart';
        dpt.ParentId            = dc.Id;
        dpt.Department_Class__c = dc.Id;
        dpt.Hospital__c         = hp.Id;
        insert dpt;

        //科室2
        Account dpt1 = new Account(RecordTypeId = rectDpt[0].Id);
        dpt1.Name         = '*';
        dpt1.Department_Name__c  = 'TestDepart1';
        dpt1.ParentId            = dc.Id;
        dpt1.Department_Class__c = dc.Id;
        dpt1.Hospital__c         = hp.Id;
        insert dpt1;

        //opd计划1
        OPDPlan__c tempOPDPlan1 = new OPDPlan__c();
        tempOPDPlan1.ThisMonthPlan__c = 1;
        tempOPDPlan1.Status__c = '计划中';
        tempOPDPlan1.Account_Laboratory__c =dpt.Id;
        tempOPDPlan1.OPD_Customers_Target__c = null;
        tempOPDPlan1.OPDPlan_ImplementDate__c = Date.today();
        tempOPDPlan1.NoOpp_Reason__c = 'HCP对应';
        insert tempOPDPlan1;

        //opd计划2
        OPDPlan__c tempOPDPlan2 = new OPDPlan__c();
        tempOPDPlan2.ThisMonthPlan__c = 1;
        tempOPDPlan2.Status__c = '计划中';
        tempOPDPlan2.Account_Laboratory__c =dpt1.Id;
        tempOPDPlan2.OPD_Customers_Target__c = null;
        tempOPDPlan2.OPDPlan_ImplementDate__c = Date.today();
        tempOPDPlan2.NoOpp_Reason__c = 'HCP对应';
        insert tempOPDPlan2;

        //opd计划实施日期为今天
        Date dateToday = Date.today();
        Integer year = dateToday.year();
        Integer month = dateToday.month();
        if (month < 4) {
            year -= 1;
        }
        String selectedTab = String.valueOf(year - 1867 - 1 + 'P'); 

        //客户-目标客户1
        Account_Number_of_target__c anot = new Account_Number_of_target__c();
        anot.Account__c = dc.Id;
        anot.Account_HP__c = hp.Id;
        anot.OCM_Period__c = selectedTab;
        anot.Target_amount_170_190__c =36597;
        insert anot;

        //客户-目标客户2
        Account_Number_of_target__c anot1 = new Account_Number_of_target__c();
        anot1.Account__c = dc.Id;
        anot1.Account_HP__c = hp.Id;
        anot1.OCM_Period__c = selectedTab;
        anot1.Target_amount_170_190__c =36500;
        insert anot1;

        //批量传入id
        List<OPDPlan__c> opdList = [select Id from OPDPlan__c where OPD_Customers_Target__c = null and OCM_category_ID__c  != null];
        List<String> opdIdList = new List<String>();
        for( OPDPlan__c op : opdList){
            opdIdList.add(op.Id);
        }

        //测试
        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new TargetCustomerAssignmentBatch(opdIdList), 10);
        System.Test.StopTest();
    }
    //SWAG-BHP42V    精琢技术  20191209 END
}