/**
 * wangweipeng   20210705
 * OPDStrategyPersonShareBatch  
 * OPD计划模块增加共享给战略科室所有人的batch测试程序
 */
@isTest
private class OPDStrategyPersonShareBatchTest {

    @isTest
    static void testMethod1() {

        user UserTeamMember1 = New User(
            Alias = '张三',
            Alias__c = '张三',
            Email='TeamMember1@testorg.com',
            EmailEncodingKey='UTF-8',
            LastName='张',
            Firstname ='三',
            LanguageLocaleKey='zh_CN',
            LocaleSidKey='zh_CN',
            ProfileId = System.Label.ProfileId_SystemAdmin,
            TimeZoneSidKey='Asia/Shanghai',
            UserName='zhangsan@testorg.com');
        insert UserTeamMember1;

        user UserTeamMember2 = New User(
            Alias = '李四',
            Alias__c = '李四',
            Email='lisi@testorg.com',
            EmailEncodingKey='UTF-8',
            LastName='李',
            Firstname ='四',
            LanguageLocaleKey='zh_CN',
            LocaleSidKey='zh_CN',
            ProfileId = System.Label.ProfileId_SystemAdmin,
            TimeZoneSidKey='Asia/Shanghai',
            UserName='lisi@testorg.com');
        insert UserTeamMember2;


        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }

        
        Account company = new Account(
                                name = 'Test Hospital',
                                RecordTypeId = rectCo[0].Id,
                                OCM_Category__c = 'OCM_Test',
                                Is_Active__c ='有効',
                                GI_Product_Leader__c = '张三,李四',
                                GS_Product_Leader__c = '张三,李四',
                                GYN_Product_Leader__c = '张三,李四',
                                ET_Product_Leader__c = '张三,李四',
                                URO_Produc_Leader__c = '张三,李四',
                                BF_Product_Leader__c = '张三,李四',
                                ENT_Product_Leader__c = '张三,李四',
                                FSE_GI_Vice_Leader__c = '张三,李四',
                                FSE_SP_Vice_Leader__c = '张三,李四',
                                Energy_LeaderStr__c = '张三,李四',
                                FSE_Extend_viceLeaderStr__c = '张三,李四'
                                );

        insert company;

        //战略科室级别
        Account section = [Select id,Name, Department_Class_Label__c, OwnerId, ParentId, Hospital_Department_Class__c from Account where ParentId =: company.Id and RecordTypeId =: rectSct[0].Id];
        System.assertEquals('消化科', section.Department_Class_Label__c);
        //科室级别
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM001TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        depart.Department_Class_Label__c  = rectSct[0].Id;
        insert depart;

        // チーム追加
        Profile p = [select Id from Profile where id =: System.Label.ProfileId_SystemAdmin];
        User u1 = new User(Test_staff__c = true);
        u1.LastName = '_サンブリッジ';
        u1.FirstName = 'あ';
        u1.Alias = 'あ';
        u1.Email = 'olympusTest01@sunbridge.com';
        u1.Username = 'olympusTest01@sunbridge.com';
        u1.CommunityNickname = 'あ';
        u1.IsActive = true;
        u1.EmailEncodingKey = 'ISO-2022-JP';
        u1.TimeZoneSidKey = 'Asia/Tokyo';
        u1.LocaleSidKey = 'ja_JP';
        u1.LanguageLocaleKey = 'ja';
        u1.ProfileId = p.Id;
        u1.Job_Category__c = '销售服务';
        u1.Province__c = '東京';
        u1.Use_Start_Date__c = Date.today().addMonths(-6);
        insert u1;

        User u2 = new User(Test_staff__c = true);
        u2.LastName = '_サンブリッジ';
        u2.FirstName = 'い';
        u2.Alias = 'い';
        u2.Email = 'olympusTest02@sunbridge.com';
        u2.Username = 'olympusTest02@sunbridge.com';
        u2.CommunityNickname = 'い';
        u2.IsActive = true;
        u2.EmailEncodingKey = 'ISO-2022-JP';
        u2.TimeZoneSidKey = 'Asia/Tokyo';
        u2.LocaleSidKey = 'ja_JP';
        u2.LanguageLocaleKey = 'ja';
        u2.ProfileId = p.Id;
        u2.Job_Category__c = '销售推广';
        u2.Province__c = '東京';
        u1.Use_Start_Date__c = Date.today().addMonths(-6);
        insert u2;

        //opd计划 数据创建
        OPDPlan__c oc = new OPDPlan__c();
        oc.Status__c = '完毕未报告';
        oc.OPDPlan_ImplementDate__c = Date.today().addDays(1);
        oc.NoOpp_Reason__c = 'HCP对应';
        oc.OwnerId = u2.id;
        oc.Account_Laboratory__c = depart.id;//科室
        //oc.Department_owner__c = u1.id;
        insert oc;

        //opd 共享计划创建
        OPDPlan__Share ras = new OPDPlan__Share();
        ras.RowCause= 'AccountOwnerShare__c';//手动
        ras.ParentId = oc.id;
        ras.UserOrGroupId = u1.id;
        ras.AccessLevel = 'Edit';//编辑
        insert ras;

        List<Id> listId = new List<ID>();
        listId.add(oc.id);
        Id execBTId = Database.executeBatch(new OPDStrategyPersonShareBatch(listId), 200);

        //opd计划 数据创建
        OPDPlan__c occ = new OPDPlan__c();
        occ.Status__c = '完毕未报告';
        occ.OPDPlan_ImplementDate__c = Date.today().addDays(1);
        occ.NoOpp_Reason__c = 'HCP对应';
        occ.Share_one_id__c = oc.id;
        occ.Share_Produc_Leader__c = UserTeamMember2.id;
        occ.OwnerId = u2.id;
        occ.Account_Laboratory__c = depart.id;//科室
        //oc.Department_owner__c = u1.id;
        insert occ;

         //opd 共享计划创建
        OPDPlan__Share rass = new OPDPlan__Share();
        rass.RowCause= 'AccountOwnerShare__c';//手动
        rass.ParentId = occ.id;
        rass.UserOrGroupId = u1.id;
        rass.AccessLevel = 'Edit';//编辑
        insert rass;

        Opportunity opp1 = new Opportunity(
            Name='testOpp1',
            StageName='引合',
            CloseDate=Date.today(),
            AccountId=depart.Id,
            Competitor__c ='A',
            Click_Close_Date__c = null,
            RecordType = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity' ]
            );
        insert opp1;

        Rental_Apply__c raObj1 = new Rental_Apply__c();
        raObj1.Rental_Start_Date__c = Date.today().addDays(1);
        raObj1.Request_return_day__c=Date.today().addDays(5);
        raObj1.DataMigration_Flag__c = true;
        raObj1.demo_purpose2__c = 'ceshi';
        raObj1.Follow_UP_Opp__c = opp1.Id;
        raObj1.Account__c = depart.Id;
        raObj1.Status__c = '已批准';
        raObj1.OPDPlan__c = occ.id;
        insert raObj1;

        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Rental_Start_Date__c = Date.today().addDays(1);
        raObj.Request_return_day__c=Date.today().addDays(5);
        raObj.DataMigration_Flag__c = true;
        raObj.demo_purpose2__c = '试用（有询价）';
        raObj.Old_Rental_Apply__c = raObj1.id;
        raObj.Account__c = depart.Id;
        raObj.Follow_UP_Opp__c = opp1.Id;
        //raObj.OPD_Share_one_id__c = occ.id;
        raObj.Status__c = '已批准';
        raObj.OPDPlan__c = occ.id;
        insert raObj;

        Database.executeBatch(new OPDStrategyPersonShareBatch(), 200);
    }
}