@isTest
private class OCMManagementProvinceBatchTest {

    @isTest static void test_method_one() {
        ControllerUtil.EscapeNFM001Trigger = true;

        Profile p = [select id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        String loginId = UserInfo.getUserId();
        User sys = [select id from User where Id = :loginId];

        User u1 = new User(Test_staff__c = true);
        u1.LastName = '_サンブリッジ';
        u1.Batch_User__c = true;
        u1.Alias = 'あ';
        u1.Email = 'olympusTest01@sunbridge.com';
        u1.Username = 'olympusTest10@sunbridge.com';
        u1.CommunityNickname = 'あ1';
        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 = '東京';

        User u2 = new User(Test_staff__c = true);
        u2.LastName = '_サンブリッジ';
        u2.Batch_User__c = true;
        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 = '東京';

        User u3 = new User(Test_staff__c = true);
        u3.LastName = '_サンブリッジ';
        u3.Batch_User__c = true;
        u3.Alias = 'う';
        u3.Email = 'olympusTest03@sunbridge.com';
        u3.Username = 'olympusTest03@sunbridge.com';
        u3.CommunityNickname = 'う';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = p.id;
        u3.Job_Category__c = '支援';
        u3.Province__c = '東京';
        u3.Dept__c = '能量事业本部';

        insert new User[] {u1, u2, u3};

        System.runAs(sys) {
            OCM_Management_Province__c mp1 = new OCM_Management_Province__c();
            mp1.Name = '北京';
            mp1.GI_assistant__c = u1.Id;
            mp1.SP_assistant__c = u1.Id;
            mp1.Energy_assistant__c = u1.Id;

            OCM_Management_Province__c mp2 = new OCM_Management_Province__c();
            mp2.Name = '上海';
            mp2.GI_assistant__c = u2.Id;
            mp2.SP_assistant__c = u2.Id;
            mp2.Energy_assistant__c = u2.Id;

            OCM_Management_Province__c mp3 = new OCM_Management_Province__c();
            mp3.Name = '深圳';
            mp3.GI_assistant__c = u3.Id;
            mp3.SP_assistant__c = u3.Id;
            mp3.Energy_assistant__c = u3.Id;

            OCM_Management_Province__c mp4 = new OCM_Management_Province__c();
            mp4.Name = '特约经销商管理支援本部';
            mp4.GI_assistant__c = loginId;
            mp4.SP_assistant__c = loginId;
            mp4.Energy_assistant__c = loginId;

            insert new OCM_Management_Province__c[] {mp1, mp2, mp3, mp4};

            Address_Level__c al1 = new Address_Level__c();
            al1.Name = '北京市';
            al1.Level1_Code__c = 'CN-01';
            al1.Level1_Sys_No__c = '353341';

            Address_Level__c al2 = new Address_Level__c();
            al2.Name = '上海市';
            al2.Level1_Code__c = 'CN-16';
            al2.Level1_Sys_No__c = '353334';

            Address_Level__c al3 = new Address_Level__c();
            al3.Name = '广东省';
            al3.Level1_Code__c = 'CN-22';
            al3.Level1_Sys_No__c = '353343';

            insert new Address_Level__c[] {al1, al2, al3};

            Address_Level2__c al21 = new Address_Level2__c();
            al21.Level1_Code__c = 'CN-01';
            al21.Level1_Sys_No__c = '353341';
            al21.Level1_Name__c = '北京市';
            al21.Name = '海淀区';
            al21.Level2_Code__c = 'CN-0123';
            al21.Level2_Sys_No__c = '353664';
            al21.Address_Level__c = al1.id;

            Address_Level2__c al22 = new Address_Level2__c();
            al22.Level1_Code__c = 'CN-16';
            al22.Level1_Sys_No__c = '353334';
            al22.Level1_Name__c = '上海市';
            al22.Name = '徐汇区';
            al22.Level2_Code__c = 'CN-1631';
            al22.Level2_Sys_No__c = '353572';
            al22.Address_Level__c = al2.id;

            Address_Level2__c al23 = new Address_Level2__c();
            al23.Level1_Code__c = 'CN-22';
            al23.Level1_Sys_No__c = '353343';
            al23.Level1_Name__c = '广东省';
            al23.Name = '深圳市';
            al23.Level2_Code__c = 'CN-2259';
            al23.Level2_Sys_No__c = '353700';
            al23.Address_Level__c = al3.id;

            insert new Address_Level2__c[] {al21, al22, al23};

            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_GS') order by DeveloperName];

            Account acc1 = new Account();
            acc1.RecordTypeId = rectCo.Id;
            acc1.Name = 'HP test1';
            //acc1.OCM_Management_Province__c = mp1.Id;
            acc1.State_Master__c = al1.Id;
            acc1.City_Master__c = al21.Id;

            Account acc2 = new Account();
            acc2.RecordTypeId = rectCo.Id;
            acc2.Name = 'HP test2';
            //acc2.OCM_Management_Province__c = mp2.Id;
            acc2.State_Master__c = al2.Id;
            acc2.City_Master__c = al22.Id;

            Account acc3 = new Account();
            acc3.RecordTypeId = rectCo.Id;
            acc3.Name = 'HP test3';
            //acc3.OCM_Management_Province__c = mp3.Id;
            acc3.State_Master__c = al3.Id;
            acc3.City_Master__c = al23.Id;

            insert new Account[] {acc1, acc2, acc3};

            List<Account> dept1 = [select Id, Name from Account where ParentId = :acc1.Id and Department_Class_Label__c = '消化科'];
            List<Account> dept2 = [select Id, Name from Account where ParentId = :acc2.Id and Department_Class_Label__c = '普外科'];
            List<Account> dept3 = [select Id, Name from Account where ParentId = :acc3.Id and Department_Class_Label__c = '消化科'];

            dept3[0].OwnerId = u3.Id;
            update dept3[0];

            Account depart1 = new Account();
            depart1.RecordTypeId = rectDpt[0].Id;
            depart1.Name         = '*';
            depart1.Department_Name__c  = '診療科1';
            depart1.ParentId            = dept1[0].Id;
            depart1.Department_Class__c = dept1[0].Id;
            depart1.Hospital__c         = acc1.Id;

            Account depart2 = new Account();
            depart2.RecordTypeId = rectDpt[1].Id;
            depart2.Name         = '*';
            depart2.Department_Name__c  = '診療科2';
            depart2.ParentId            = dept2[0].Id;
            depart2.Department_Class__c = dept2[0].Id;
            depart2.Hospital__c         = acc2.Id;

            Account depart3 = new Account();
            depart3.RecordTypeId = rectDpt[0].Id;
            depart3.Name         = '*';
            depart3.Department_Name__c  = '診療科3';
            depart3.ParentId            = dept3[0].Id;
            depart3.Department_Class__c = dept3[0].Id;
            depart3.Hospital__c         = acc3.Id;

            insert new Account[] {depart1, depart2, depart3};

            RecordType oppVND = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity'];

            System.Test.startTest();
            Opportunity opp1 = new Opportunity(
                Name = 'test opp1',
                StageName = '引合',
                CurrencyIsoCode = 'USD',
                CloseDate = Date.today().addDays(5),
                AccountId = depart1.Id,
                RecordTypeId = oppVND.Id,
                Closing_Bid_Date__c = Date.today(),
                Hospital__c = acc1.Id,
                Competitor__c = 'A',
                SAP_Send_OK__c = false,
                Close_Forecasted_Date__c = date.today().addDays(1),
                Opportunity_Category__c = 'GI'
            );
            Opportunity opp2 = new Opportunity(
                Name = 'test opp2',
                StageName = '引合',
                CurrencyIsoCode = 'USD',
                CloseDate = Date.today().addDays(5),
                AccountId = depart2.Id,
                RecordTypeId = oppVND.Id,
                Closing_Bid_Date__c = Date.today(),
                Hospital__c = acc2.Id,
                Competitor__c = 'B',
                SAP_Send_OK__c = false,
                Close_Forecasted_Date__c = date.today().addDays(1),
                Opportunity_Category__c = 'SP'
            );
            Opportunity opp3 = new Opportunity(
                Name = 'test opp3',
                StageName = '引合',
                CurrencyIsoCode = 'USD',
                CloseDate = Date.today().addDays(5),
                AccountId = depart3.Id,
                RecordTypeId = oppVND.Id,
                Closing_Bid_Date__c = Date.today(),
                Hospital__c = acc3.Id,
                Competitor__c = 'B',
                Opportunity_Category__c = 'GI',
                OwnerId = u3.Id,
                SAP_Send_OK__c = false,
                Close_Forecasted_Date__c = date.today().addDays(2),
                owner_not_automatically_update__c = true
            );
            Opportunity opp4 = new Opportunity(
                Name = 'test opp4',
                StageName = '引合',
                CurrencyIsoCode = 'USD',
                CloseDate = Date.today().addDays(5),
                AccountId = depart3.Id,
                RecordTypeId = oppVND.Id,
                Closing_Bid_Date__c = Date.today(),
                Hospital__c = acc3.Id,
                Competitor__c = 'B',
                Opportunity_Category__c = 'GI',
                SAP_Send_OK__c = false,
                Close_Forecasted_Date__c = date.today().addDays(1),
                Distributor_InCharge_opp__c = true
            );
            insert new Opportunity[] {opp1, opp2, opp3, opp4};

            List<Opportunity> oppList = [select id,Sales_assistant_name_text__c from Opportunity order by Name];
            // System.assertEquals(u1.Id, oppList[0].Sales_assistant_name_text__c);
            // System.assertEquals(u2.Id, oppList[1].Sales_assistant_name_text__c);
            // System.assertEquals(u3.Id, oppList[2].Sales_assistant_name_text__c);
            // System.assertEquals(loginId, oppList[3].Sales_assistant_name_text__c);

            opp1.Sales_assistant_name_text__c = UserInfo.getUserId();
            opp2.Sales_assistant_name_text__c = null;
            opp3.Sales_assistant_name_text__c = null;
            opp4.Sales_assistant_name_text__c = null;
            update new Opportunity[] {opp1, opp2, opp3, opp4};

           

            OCMManagementProvinceWebService.updateOpportunity();

            System.Test.stopTest();
        }
    }

    @isTest static void test_method_two() {
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;

        Product2 product = new Product2( Name='テスト商品');
        product.SFDA_Status__c = '有効';
        product.Intra_Trade_List_RMB_1__c = 100;
        product.Intra_Trade_List_RMB_Date1__c = date.today();
        product.Intra_Trade_Cost_RMB_1__c = 200;
        product.Intra_Trade_Cost_RMB_Date1__c = date.today();
        product.Manual_Entry__c = false;
        product.Asset_Model_No__c = '11111';
        product.ProductCode = 'OTV-SP1H-NA-12E';
        insert product;

        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        insert entry;

        Opportunity opp = new Opportunity();
        opp.Name='aiueo';
        opp.StageName='contact';
        opp.Trade__c = '内貿';
        opp.CloseDate=Date.today();
        opp.CurrencyIsoCode = 'CNY';
        opp.Estimation_List_Price__c = 100;
        opp.Wholesale_Price__c = 101;
        opp.Dealer_Final_Price__c = 102;
        opp.OCM_Agent1_Price__c = 103;
        opp.Stocking_Price__c = 104;
        opp.Estimation_No__c = '105';
        opp.Estimation_Name__c = '106';
        opp.Estimation_Id__c = '107';
        insert opp;

        OpportunityLineItem oppli = new OpportunityLineItem();
        oppli.OpportunityId = opp.Id;
        oppli.Id__c = '110';
        oppli.SFDA_Status__c = '有効';
        oppli.Name__c = '111';
        oppli.ListPrice__c = 112;
        oppli.Quantity = 113;
        oppli.UnitPrice = 114;
        oppli.UnitPrice__c = 115;
        oppli.Qty_Unit__c = '116';
        oppli.Cost__c = 117;
        oppli.BSS_Category__c = 'G&R';
        oppli.Item_Order__c = 1;
        oppli.PricebookEntryId = entry.Id;
        insert oppli;

        Quote quo = new Quote();
        quo.Name = '206';
        quo.OpportunityId = opp.Id;
        quo.Quote_No__c = '205';
        quo.Estimation_List_Price__c = 200;
        quo.Dealer_Final_Price__c = 202;
        quo.Stocking_Price__c = 204;
        quo.OCM_Sales_Forecast__c = 201;
        quo.OCM_Agent1_Price__c = 203;
        quo.Pricebook2Id = pricebookId;
        insert quo;

        QuoteLineItem qli = new QuoteLineItem();
        qli.QuoteId = quo.Id;
        qli.Id__c = '210';
        qli.SFDA_Status__c = '有効';
        qli.Name__c = '211';
        qli.ListPrice__c = 212;
        qli.Quantity = 213;
        qli.UnitPrice__c = 215;
        qli.UnitPrice = 0;
        qli.Qty_Unit__c = '216';
        qli.Cost__c = 217;
        qli.BSS_Category__c = 'ET';
        qli.Subtotal__c = 218;
        qli.PricebookEntryId = entry.Id;
        insert qli;

        opp.Estimation_Id__c = quo.Id;
        opp.Estimation_Decision__c = true;
        update opp;

        opp.Estimation_Decision__c = false;
        update opp;
    }
    @isTest static void test_method_three() {
         //医院
        //  List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Agency'];
        //  if (rectHp.size() == 0) {
        //      return;
        //  } 
         String rectHpid = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
         Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
         User hpOwner = new User(Test_staff__c = true, LastName = 'hp',  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);
         
         User thisUser = [select Id,Alias from User where Id = :UserInfo.getUserId() ];
         System.runAs ( thisUser ){
             insert hpOwner;
             // 省
             Address_Level__c al = new Address_Level__c();
             al.Name = '广东省';
             al.Level1_Code__c = 'CN-99';
             al.Level1_Sys_No__c = '999999';
             insert al;
             // 市
             Address_Level2__c al2 = new Address_Level2__c();
             al2.Level1_Code__c = 'CN-99';
             al2.Level1_Sys_No__c = '999999';
             al2.Level1_Name__c = '广东省';
             al2.Name = '深圳市';
             al2.Level2_Code__c = 'CN-9999';
             al2.Level2_Sys_No__c = '9999999';
             al2.Address_Level__c = al.id;
             insert al2;
            //  医院
            ControllerUtil.EscapeNFM001Trigger = true;
StaticParameter.EscapeNFM001Trigger = true;
StaticParameter.EscapeNFM001AgencyContractTrigger = true;
StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
             Account hp = new Account(RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId(), Name = 'hp', OwnerId = thisUser.Id);
             hp.Name = 'test hospital';
             hp.FSE_GI_Main_Leader__c = thisUser.Id;
             hp.FSE_SP_Main_Leader__c = thisUser.Id;
             hp.State_Master__c = al.id;
             hp.City_Master__c = al2.id;
             hp.ET_owner__c = thisUser.Id;
             hp.OCM_man_province_txt__c = '深圳';
             insert hp;

            // 战略科室
             Account section = new Account();
            section.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_Class_BF').getRecordTypeId();
            section.Name         = '*';
            section.Department_Class_Label__c = '消化科';
            section.ParentId                  = hp.Id;
            section.Hospital_Department_Class__c = hp.Id;
            upsert section;
            // 科室
            Account depart = new Account();
            depart.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_GI').getRecordTypeId();
            depart.Name         = '*';
            depart.Department_Name__c  = 'NFM007TestDepart';
            depart.ParentId            = section.Id;
            depart.Department_Class__c = section.Id;
            depart.Hospital__c         = hp.Id;
            upsert depart;

            // //  RecordType recByoin = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name='病院' limit 1];
            //  Account hospital = new Account(Name = 'テスト病院');
            //  hospital.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
            //  hospital.Name = 'test hospital1';
            //  hospital.ET_owner__c = UserInfo.getUserId();
            //  hospital.State_Master__c = al.id;
            //  hospital.City_Master__c = al2.id;
            //  hospital.OCM_man_province_txt__c = '深圳';
            //  insert hospital;
            //  医院
            String agencyid = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            Account agency = new Account(RecordTypeId =agencyid , Name = 'hp', OwnerId = thisUser.Id);
            agency.Name = 'test hospital';
            agency.FSE_GI_Main_Leader__c = thisUser.Id;
            agency.FSE_SP_Main_Leader__c = thisUser.Id;
            agency.State_Master__c = al.id;
            agency.City_Master__c = al2.id;
            agency.ET_owner__c = thisUser.Id;
            agency.OCM_man_province_txt__c = '深圳';
            insert agency;
             OCM_Management_Province__c mp1 = new OCM_Management_Province__c();
             mp1.Name = '深圳';
             mp1.Province__c = '广东省';
             mp1.Window1__c = hpOwner.Id;
             mp1.ET_DataWindow__c = hpOwner.Id;
             mp1.ET_BusinessWindow__c = hpOwner.Id;
             mp1.ET_Assistant1__c = hpOwner.Id;
             mp1.ET_Assistant2__c = hpOwner.Id;
             mp1.ENG_DataWindow__c = hpOwner.Id;
             mp1.ENG_BusinessWindow__c = hpOwner.Id;
             mp1.ETShareOpp__c = hpOwner.Id;
             mp1.ENGShareOpp__c = hpOwner.Id;
             mp1.ENG_Assistant1__c = hpOwner.Id;
             mp1.ENG_Assistant2__c = hpOwner.Id;
             insert mp1;
             System.Test.startTest();
             Opportunity opp = new Opportunity(
                Name = 'test opp1',
                StageName = '完了',
                CurrencyIsoCode = 'USD',
                CloseDate = Date.today().addDays(5),
                AccountId = depart.Id,
                Department_Class__c = section.Id,
                Hospital__c         = hp.Id,
                Closing_Bid_Date__c = Date.today(),
                //Hospital__c = hp.Id,
                Competitor__c = 'A',
                SAP_Send_OK__c = false,
                Close_Forecasted_Date__c = date.today().addDays(1),
                Opportunity_Category__c = 'GI',
                Opp_order_Type__c ='ET',
                SAP_Province__c = '广东省'
            );
            // insert opp;
            // Opportunity opp = new Opportunity();
            // opp.Name='aiueo';
            // opp.StageName='完了';
            // opp.Trade__c = '内貿';
            // opp.CloseDate=Date.today();
            // opp.CurrencyIsoCode = 'CNY';
            // opp.Estimation_List_Price__c = 100;
            // opp.Wholesale_Price__c = 101;
            // opp.Dealer_Final_Price__c = 102;
            // opp.OCM_Agent1_Price__c = 103;
            // opp.Stocking_Price__c = 104;
            // opp.Estimation_No__c = '105';
            // opp.Estimation_Name__c = '106';
            // opp.Estimation_Id__c = '107';
            // opp.Opp_order_Type__c ='ET';
            // opp.SAP_Province__c = '广东省';
            // insert opp;
            // Opportunity opp1 = new Opportunity();
            // opp1.Name='aiueo';
            // opp1.StageName='完了';
            // opp1.Trade__c = '内貿';
            // opp1.CloseDate=Date.today();
            // opp1.CurrencyIsoCode = 'CNY';
            // opp1.Estimation_List_Price__c = 100;
            // opp1.Wholesale_Price__c = 101;
            // opp1.Dealer_Final_Price__c = 102;
            // opp1.OCM_Agent1_Price__c = 103;
            // opp1.Stocking_Price__c = 104;
            // opp1.Estimation_No__c = '105';
            // opp1.Estimation_Name__c = '106';
            // opp1.Estimation_Id__c = '107';
            // opp1.Opp_order_Type__c ='ENG';
            // opp1.SAP_Province__c = '广东省';
            // opp1.Department_Class__c = section.Id;
            // opp1.Hospital__c         = hp.Id;
            // opp1.AccountId = depart.Id;

            // insert opp1;
            insert new Opportunity[] {opp};
            // 共享计划创建
            // opp = [select id,OCM_man_province_new__c from  Opportunity where id = :opp.id];
            // System.assertEquals('深圳', opp.OCM_man_province_new__c);
            OpportunityShare oppshare = new OpportunityShare();
            oppshare.RowCause= 'Manual';//手动
            oppshare.OpportunityId = opp.id;
            oppshare.UserOrGroupId = hpOwner.Id;
            oppshare.OpportunityAccessLevel = 'Read';//编辑
            // insert oppshare;
            // OpportunityShare oppshare1 = new OpportunityShare();
            // oppshare1.RowCause= 'Manual';//手动
            // oppshare1.OpportunityId = opp1.id;
            // oppshare1.UserOrGroupId = hpOwner.Id;
            // oppshare1.OpportunityAccessLevel = 'Read';//编辑
            // insert oppshare1;
            insert new OpportunityShare[] {oppshare};
            //TMS数据
            Dealer_ConsumableManage__c tms =new Dealer_ConsumableManage__c();
            tms.Dealer_Name__c =agency.id;
            tms.ET_ENG__c ='ET';
            tms.TMS_Key__c='1';
            // insert tms;
            
            // Dealer_ConsumableManage__c tms1 =new Dealer_ConsumableManage__c();
            // tms1.Dealer_Name__c =hp.id;
            // tms1.ET_ENG__c ='ENG';
            // tms1.TMS_Key__c='2';
            // insert tms1;
            insert new Dealer_ConsumableManage__c[] {tms};
            // 共享计划创建
            // tms = [select id,TMS_Province__c  from Dealer_ConsumableManage__c where id = :tms.id];
            // System.assertEquals('深圳', tms.TMS_Province__c);
            Dealer_ConsumableManage__Share tmsshare = new Dealer_ConsumableManage__Share();
            tmsshare.RowCause= 'OCSMToTMSUser__c';//手动
            tmsshare.ParentId = tms.id;
            tmsshare.UserOrGroupId = hpOwner.Id;
            tmsshare.AccessLevel = 'Read';
            insert tmsshare;
            //IMS数据
            Oly_TriggerHandler.bypass('ConsumableTargetManageHandler');
            ConsumableTargetManage__c ims =new ConsumableTargetManage__c();
            ims.Hospital__c =hp.id;
            ims.productType__c ='ET耗材';
            ims.ConsumableTargetManageKey__c ='001';
            // insert ims;
            // ConsumableTargetManage__c ims1 =new ConsumableTargetManage__c();
            // ims1.Hospital__c =agency.id;
            // ims1.productType__c ='外科耗材';
            // ims1.ConsumableTargetManageKey__c ='002';
            // insert ims1;
            insert new ConsumableTargetManage__c[] {ims};

        
             // 共享计划创建
            ConsumableTargetManage__Share imsshare = new ConsumableTargetManage__Share();
            imsshare.RowCause= 'OCSMToIMSUser__c';//手动
            imsshare.ParentId = ims.id;
            imsshare.UserOrGroupId = hpOwner.Id;
            imsshare.AccessLevel = 'Read';
            insert imsshare;
            

            OCMManagementProvinceWebService.updateOpportunity();
            System.Test.stopTest();
         }
    }
    @isTest static void test_method_4() {
        //医院
       //  List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Agency'];
       //  if (rectHp.size() == 0) {
       //      return;
       //  } 
        String rectHpid = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp',  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);
        
        User thisUser = [select Id,Alias from User where Id = :UserInfo.getUserId() ];
        System.runAs ( thisUser ){
            insert hpOwner;
            // 省
            Address_Level__c al = new Address_Level__c();
            al.Name = '广东省';
            al.Level1_Code__c = 'CN-99';
            al.Level1_Sys_No__c = '999999';
            insert al;
            // 市
            Address_Level2__c al2 = new Address_Level2__c();
            al2.Level1_Code__c = 'CN-99';
            al2.Level1_Sys_No__c = '999999';
            al2.Level1_Name__c = '广东省';
            al2.Name = '深圳市';
            al2.Level2_Code__c = 'CN-9999';
            al2.Level2_Sys_No__c = '9999999';
            al2.Address_Level__c = al.id;
            insert al2;
           //  医院
           ControllerUtil.EscapeNFM001Trigger = true;
StaticParameter.EscapeNFM001Trigger = true;
StaticParameter.EscapeNFM001AgencyContractTrigger = true;
StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
            Account hp = new Account(RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId(), Name = 'hp', OwnerId = thisUser.Id);
            hp.Name = 'test hospital';
            hp.FSE_GI_Main_Leader__c = thisUser.Id;
            hp.FSE_SP_Main_Leader__c = thisUser.Id;
            hp.State_Master__c = al.id;
            hp.City_Master__c = al2.id;
            hp.ET_owner__c = thisUser.Id;
            hp.OCM_man_province_txt__c = '深圳';
            insert hp;

           // 战略科室
            Account section = new Account();
           section.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_Class_BF').getRecordTypeId();
           section.Name         = '*';
           section.Department_Class_Label__c = '消化科';
           section.ParentId                  = hp.Id;
           section.Hospital_Department_Class__c = hp.Id;
           upsert section;
           // 科室
           Account depart = new Account();
           depart.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_GI').getRecordTypeId();
           depart.Name         = '*';
           depart.Department_Name__c  = 'NFM007TestDepart';
           depart.ParentId            = section.Id;
           depart.Department_Class__c = section.Id;
           depart.Hospital__c         = hp.Id;
           upsert depart;

           // //  RecordType recByoin = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name='病院' limit 1];
           //  Account hospital = new Account(Name = 'テスト病院');
           //  hospital.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
           //  hospital.Name = 'test hospital1';
           //  hospital.ET_owner__c = UserInfo.getUserId();
           //  hospital.State_Master__c = al.id;
           //  hospital.City_Master__c = al2.id;
           //  hospital.OCM_man_province_txt__c = '深圳';
           //  insert hospital;
           //  医院
           String agencyid = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
           Account agency = new Account(RecordTypeId =agencyid , Name = 'hp', OwnerId = thisUser.Id);
           agency.Name = 'test hospital';
           agency.FSE_GI_Main_Leader__c = thisUser.Id;
           agency.FSE_SP_Main_Leader__c = thisUser.Id;
           agency.State_Master__c = al.id;
           agency.City_Master__c = al2.id;
           agency.ET_owner__c = thisUser.Id;
           agency.OCM_man_province_txt__c = '深圳';
           insert agency;
            OCM_Management_Province__c mp1 = new OCM_Management_Province__c();
            mp1.Name = '深圳';
            mp1.Province__c = '广东省';
            mp1.Window1__c = hpOwner.Id;
            mp1.ET_DataWindow__c = hpOwner.Id;
            mp1.ET_BusinessWindow__c = hpOwner.Id;
            mp1.ET_Assistant1__c = hpOwner.Id;
            mp1.ET_Assistant2__c = hpOwner.Id;
            mp1.ENG_DataWindow__c = hpOwner.Id;
            mp1.ENG_BusinessWindow__c = hpOwner.Id;
            mp1.ETShareOpp__c = hpOwner.Id;
            mp1.ENGShareOpp__c = hpOwner.Id;
            mp1.ENG_Assistant1__c = hpOwner.Id;
            mp1.ENG_Assistant2__c = hpOwner.Id;
            insert mp1;
            System.Test.startTest();
        //     Opportunity opp = new Opportunity(
        //        Name = 'test opp1',
        //        StageName = '完了',
        //        CurrencyIsoCode = 'USD',
        //        CloseDate = Date.today().addDays(5),
        //        AccountId = depart.Id,
        //        Department_Class__c = section.Id,
        //        Hospital__c         = hp.Id,
        //        Closing_Bid_Date__c = Date.today(),
        //        //Hospital__c = hp.Id,
        //        Competitor__c = 'A',
        //        SAP_Send_OK__c = false,
        //        Close_Forecasted_Date__c = date.today().addDays(1),
        //        Opportunity_Category__c = 'GI',
        //        Opp_order_Type__c ='ET',
        //        SAP_Province__c = '广东省'
        //    );
           // insert opp;
           // Opportunity opp = new Opportunity();
           // opp.Name='aiueo';
           // opp.StageName='完了';
           // opp.Trade__c = '内貿';
           // opp.CloseDate=Date.today();
           // opp.CurrencyIsoCode = 'CNY';
           // opp.Estimation_List_Price__c = 100;
           // opp.Wholesale_Price__c = 101;
           // opp.Dealer_Final_Price__c = 102;
           // opp.OCM_Agent1_Price__c = 103;
           // opp.Stocking_Price__c = 104;
           // opp.Estimation_No__c = '105';
           // opp.Estimation_Name__c = '106';
           // opp.Estimation_Id__c = '107';
           // opp.Opp_order_Type__c ='ET';
           // opp.SAP_Province__c = '广东省';
           // insert opp;
           Opportunity opp1 = new Opportunity();
           opp1.Name='aiueo';
           opp1.StageName='完了';
           opp1.Trade__c = '内貿';
           opp1.CloseDate=Date.today();
           opp1.CurrencyIsoCode = 'CNY';
           opp1.Estimation_List_Price__c = 100;
           opp1.Wholesale_Price__c = 101;
           opp1.Dealer_Final_Price__c = 102;
           opp1.OCM_Agent1_Price__c = 103;
           opp1.Stocking_Price__c = 104;
           opp1.Estimation_No__c = '105';
           opp1.Estimation_Name__c = '106';
           opp1.Estimation_Id__c = '107';
           opp1.Opp_order_Type__c ='ENG';
           opp1.SAP_Province__c = '广东省';
           opp1.Department_Class__c = section.Id;
           opp1.Hospital__c         = hp.Id;
           opp1.AccountId = depart.Id;

           // insert opp1;
           insert new Opportunity[] {opp1};
           // 共享计划创建
           // opp = [select id,OCM_man_province_new__c from  Opportunity where id = :opp.id];
           // System.assertEquals('深圳', opp.OCM_man_province_new__c);
        //    OpportunityShare oppshare = new OpportunityShare();
        //    oppshare.RowCause= 'Manual';//手动
        //    oppshare.OpportunityId = opp.id;
        //    oppshare.UserOrGroupId = hpOwner.Id;
        //    oppshare.OpportunityAccessLevel = 'Read';//编辑
           // insert oppshare;
           OpportunityShare oppshare1 = new OpportunityShare();
           oppshare1.RowCause= 'Manual';//手动
           oppshare1.OpportunityId = opp1.id;
           oppshare1.UserOrGroupId = hpOwner.Id;
           oppshare1.OpportunityAccessLevel = 'Read';//编辑
           // insert oppshare1;
           insert new OpportunityShare[] {oppshare1};
           //TMS数据
        //    Dealer_ConsumableManage__c tms =new Dealer_ConsumableManage__c();
        //    tms.Dealer_Name__c =hp.id;
        //    tms.ET_ENG__c ='ET';
        //    tms.TMS_Key__c='1';
           // insert tms;
       /**   
           Dealer_ConsumableManage__c tms1 =new Dealer_ConsumableManage__c();
           tms1.Dealer_Name__c =agency.id;
           tms1.ET_ENG__c ='ENG';
           tms1.TMS_Key__c='2';
           // insert tms1;
           insert new Dealer_ConsumableManage__c[] {tms1};
           // 共享计划创建
           // tms = [select id,TMS_Province__c  from Dealer_ConsumableManage__c where id = :tms.id];
           // System.assertEquals('深圳', tms.TMS_Province__c);
           Dealer_ConsumableManage__Share tmsshare = new Dealer_ConsumableManage__Share();
           tmsshare.RowCause= 'OCSMToTMSUser__c';//手动
           tmsshare.ParentId = tms1.id;
           tmsshare.UserOrGroupId = hpOwner.Id;
           tmsshare.AccessLevel = 'Read';
           insert tmsshare;
           //IMS数据
           Oly_TriggerHandler.bypass('ConsumableTargetManageHandler');
        //    ConsumableTargetManage__c ims =new ConsumableTargetManage__c();
        //    ims.Hospital__c =agency.id;
        //    ims.productType__c ='ET耗材';
        //    ims.ConsumableTargetManageKey__c ='001';
           // insert ims;
           ConsumableTargetManage__c ims1 =new ConsumableTargetManage__c();
           ims1.Hospital__c =hp.id;
           ims1.productType__c ='外科耗材';
           ims1.ConsumableTargetManageKey__c ='002';
           // insert ims1;
           insert new ConsumableTargetManage__c[] {ims1};

       
            // 共享计划创建
        //    ConsumableTargetManage__Share imsshare = new ConsumableTargetManage__Share();
        //    imsshare.RowCause= 'OCSMToIMSUser__c';//手动
        //    imsshare.ParentId = ims2.id;
        //    imsshare.UserOrGroupId = hpOwner.Id;
        //    imsshare.AccessLevel = 'Read';
        //    insert imsshare;
           
           */ 
           OCMManagementProvinceWebService.updateOpportunity();

           Dealer_ConsumableManage__c tms1 =new Dealer_ConsumableManage__c();
           tms1.Dealer_Name__c =agency.id;
           tms1.ET_ENG__c ='ENG';
           tms1.TMS_Key__c='1';
           Dealer_ConsumableManage__c tms2 =new Dealer_ConsumableManage__c();
           tms2.Dealer_Name__c =agency.id;
           tms2.ET_ENG__c ='ET';
           tms2.TMS_Key__c='2';
           // insert tms1;
           insert new Dealer_ConsumableManage__c[] {tms1,tms2};

           Dealer_ConsumableManage__Share tmsshare = new Dealer_ConsumableManage__Share();
           tmsshare.RowCause= 'OCSMToTMSUser__c';//手动
           tmsshare.ParentId = tms1.id;
           tmsshare.UserOrGroupId = hpOwner.Id;
           tmsshare.AccessLevel = 'Read';
           insert tmsshare;

           Database.executeBatch(new OCMTOTMSShareUserBatch(),200);
           Oly_TriggerHandler.bypass('ConsumableTargetManageHandler');
           ConsumableTargetManage__c ims =new ConsumableTargetManage__c();
           ims.Hospital__c =hp.id;
           ims.productType__c ='ET耗材';
           ims.ConsumableTargetManageKey__c ='001';

           ConsumableTargetManage__c ims1 =new ConsumableTargetManage__c();
           ims1.Hospital__c =hp.id;
           ims1.productType__c ='外科耗材';
           ims1.ConsumableTargetManageKey__c ='002';

           insert new ConsumableTargetManage__c[] {ims,ims1};
           Database.executeBatch(new OCMTOIMSShareUserBatch(),200);
           System.Test.stopTest();
        }
   }
    
}