@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.FirstName = 'あ';
        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.FirstName = 'い';
        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.FirstName = 'う';
        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',
                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',
                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,
                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',
                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;
    }

}