@isTest
private class RentalApplyOtherTriggerTest {
    private static final RecordType campaignRC = [SELECT Id, DeveloperName FROM RecordType WHERE DeveloperName = 'Internal_training' AND SObjectType = 'Campaign'];

    static testMethod void myUnitTest() {
        String loginUserId = UserInfo.getUserId();
        // システム管理者
        User user = new User(Test_staff__c = true);
        user.LastName = '_サンブリッジ';
        user.FirstName = 'う';
        user.Alias = 'う';
        user.Email = 'olympusTest03@sunbridge.com';
        user.Username = 'olympusTest03@sunbridge.com';
        user.CommunityNickname = 'う';
        user.IsActive = true;
        user.EmailEncodingKey = 'ISO-2022-JP';
        user.TimeZoneSidKey = 'Asia/Tokyo';
        user.LocaleSidKey = 'ja_JP';
        user.LanguageLocaleKey = 'ja';
        user.ProfileId = System.Label.ProfileId_SystemAdmin;
        user.Job_Category__c = '销售推广';
        user.Province__c = '上海市';
        user.Use_Start_Date__c = Date.today().addMonths(-6);
        user.SalesManager__c = UserInfo.getUserId();
        user.BuchangApprovalManagerSales__c = UserInfo.getUserId();
        user.JingliApprovalManager__c = UserInfo.getUserId();
        user.BuchangApprovalManager__c = UserInfo.getUserId();
        user.ZongjianApprovalManager__c = UserInfo.getUserId();
        user.Dept__c = '医疗西北营业本部';

        // システム管理者
        User user2 = new User(Test_staff__c = true);
        user2.LastName = '_サンブリッジ';
        user2.FirstName = 'う';
        user2.Alias = 'う';
        user2.Email = 'olympusTest04@sunbridge.com';
        user2.Username = 'olympusTest04@sunbridge.com';
        user2.CommunityNickname = 'う1';
        user2.IsActive = true;
        user2.EmailEncodingKey = 'ISO-2022-JP';
        user2.TimeZoneSidKey = 'Asia/Tokyo';
        user2.LocaleSidKey = 'ja_JP';
        user2.LanguageLocaleKey = 'ja';
        user2.ProfileId = System.Label.ProfileId_SystemAdmin;
        user2.Job_Category__c = '销售推广';
        user2.Province__c = '上海市';
        user2.Use_Start_Date__c = Date.today().addMonths(-6);
        user2.SalesManager__c = UserInfo.getUserId();
        user2.BuchangApprovalManagerSales__c = UserInfo.getUserId();
        user2.JingliApprovalManager__c = UserInfo.getUserId();
        user2.BuchangApprovalManager__c = UserInfo.getUserId();
        user2.ZongjianApprovalManager__c = UserInfo.getUserId();
        user2.Dept__c = '医疗西北营业本部';

        insert new User[] {user, user2};
        System.runAs(user) {
            System.Test.startTest();
            Campaign target = new Campaign();
            target.Name = 'test campaign';
            target.RecordTypeId = campaignRC.Id;    // 内部トレーニング
            insert target;

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

            // insert
            Account hp1 = new Account();
            hp1.RecordTypeId = recthp[0].Id;
            hp1.Name = '病院テスト1';

            Account hp2 = new Account();
            hp2.RecordTypeId = recthp[0].Id;
            hp2.Name = '病院テスト2';

            insert new Account[] {hp1, hp2};

            List<Account> dc1s = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp1.Id order by Department_Class_Label__c];
            List<Account> dc2s = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp2.Id order by Department_Class_Label__c];

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

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

            insert new Account[] {depart1, depart2};
            System.Test.stopTest();


            Rental_Apply__c raObj = new Rental_Apply__c(Account__c = depart1.Id
                    , Hospital__c = hp1.Id
                                    , Strategic_dept__c = dc1s[0].Id);
            raObj.Name = 'test';
            raObj.Status__c = FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Cao_An_Zhong.ordinal());
            raObj.Request_shipping_day__c = Date.today().addDays(3);
            raObj.Request_return_day__c = Date.today().addDays(30);
            raObj.Person_In_Charge__c = user.Id;
            raObj.demo_purpose1__c = '其他';
            raObj.demo_purpose2__c = '其他';
            raObj.Campaign__c = target.Id;
            raObj.direct_shippment_address__c = 'test';
            raObj.applyUser__c = UserInfo.getUserId();
            insert raObj;
            raObj.direct_shippment_address__c = 'test2';
            upsert raObj;
        }
    }

}