@isTest
private class ChangedDepartmentOwnerTest {
    static testMethod void test01() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User subOwner = new User(Test_staff__c = true, LastName = 'oppSub', Province__c = '新疆自治区', Job_Category__c = '销售推广',
                FirstName = 'owner', Alias = 'hp', CommunityNickname = 'subOwner', Email = 'olympus_subowner@sb.com', Username = 'olympus_siubowner@sb.com',
                IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id
        );
        User subOwner2 = new User(Test_staff__c = true, LastName = 'oppSub2', Province__c = '新疆自治区', Job_Category__c = '销售推广',
                FirstName = 'owner', Alias = 'hp', CommunityNickname = 'subOwner2', Email = 'olympus_subowner2@sb.com', Username = 'olympus_siubowner2@sb.com',
                IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id
        );
        insert new User[] {subOwner, subOwner2};
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科'];
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'SoakupTestHp', OwnerId = UserInfo.getUserId(),
                OCM_Category__c = 'LLL'
        );
        insert hp;
        List<Account> dcs = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp.Id and Department_Class_Label__c = '呼吸科'];
        
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = '診療科';
        depart.ParentId            = dcs[0].Id;
        depart.Department_Class__c = dcs[0].Id;
        depart.Hospital__c         = hp.Id;
        depart.OwnerId             = subOwner.Id;
        insert depart;
        
        RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity'];
        
        Opportunity opp = new Opportunity(
            Name = 'test opp',
            OwnerId = subOwner.Id,
            SAP_Province__c = '青海省',
            Opportunity_Category__c = 'GIGI',
            Distributor_InCharge_opp__c = true,
            StageName = '引合',
            CurrencyIsoCode = 'CNY',
            CloseDate = Date.today(),
            AccountId = depart.Id,
            RecordTypeId = rectOpp.Id,
            Closing_Bid_Date__c = Date.today().addDays(-5),
            Hospital__c = hp.Id,
            Competitor__c = 'A',
            owner_not_automatically_update__c = false
        );
        insert opp;
        List<Opportunity> opList = [Select Id, OwnerId from Opportunity];
        //System.assertEquals(opList[0].OwnerId, subOwner.Id);
        
        Test.startTest();
        depart.OwnerId = subOwner2.Id;
        hp.FSE_Main__c = subOwner2.Id;
        hp.FSE_Extend_viceLeaderStr__c = subOwner2.Id;
        hp.GI_Main__c = subOwner2.Id;
        hp.GI_Product_Leader__c = subOwner2.Id;
        hp.SP_Main__c = subOwner2.Id;
        hp.GS_Product_Leader__c = subOwner2.Id;
        hp.BF_owner__c = subOwner2.Id;
        hp.BF_Product_Leader__c = subOwner2.Id;
        hp.GYN_owner__c = subOwner2.Id;
        hp.GYN_Product_Leader__c = subOwner2.Id;
        hp.ET_owner__c = subOwner2.Id;
        hp.ET_Product_Leader__c = subOwner2.Id;
        hp.ENT_owner_ID__c = subOwner2.Id;
        hp.ENT_Product_Leader__c = subOwner2.Id;
        hp.URO_owner_ID__c = subOwner2.Id;
        hp.URO_Produc_Leader__c = subOwner2.Id;
        hp.FSE_GI_Main_Leader__c = subOwner2.Id;
        hp.FSE_GI_Vice_Leader__c = subOwner2.Id;
        hp.FSE_SP_Main_Leader__c = subOwner2.Id;
        hp.FSE_SP_Vice_Leader__c = subOwner2.Id;
        hp.FSE_ENG_Main_Leader__c = subOwner2.Id;
        hp.FSE_ENG_Vice_Leader__c = subOwner2.Id;
        hp.Extend_Leader_Str__c = subOwner2.Id;
        hp.Energy_LeaderStr__c = subOwner2.Id;
        update new Account[] {depart, hp};
        Test.stopTest();

        opList = [Select Id, OwnerId from Opportunity];
        //System.assertEquals(opList[0].OwnerId, subOwner2.Id);
    }
}