// TODO EquipmentSetDailyBatchSchedule.test
@isTest
private class RentalApplyDailyBatchTest {

    @isTest static void test_method_Three() {
        // システム管理者
        User user_admin = new User(Test_staff__c = true);
        user_admin.LastName = '_サンブリッジ';
        user_admin.FirstName = 'う';
        user_admin.Alias = 'う';
        user_admin.Email = 'olympusTest03@sunbridge.com';
        user_admin.Username = 'olympusTest03@sunbridge.com';
        user_admin.CommunityNickname = 'う';
        user_admin.IsActive = true;
        user_admin.EmailEncodingKey = 'ISO-2022-JP';
        user_admin.TimeZoneSidKey = 'Asia/Tokyo';
        user_admin.LocaleSidKey = 'ja_JP';
        user_admin.LanguageLocaleKey = 'ja';
        user_admin.ProfileId = System.Label.ProfileId_SystemAdmin;
        user_admin.Province__c = '北京';
        user_admin.Dept__c = '医疗华北营业本部';
        user_admin.Use_Start_Date__c = Date.today().addMonths(-6);
        insert user_admin;

        System.runAs(user_admin) {
            // 病院を作る
            Account hospital = new Account();
            hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
            hospital.Name = 'test hospital';
            insert hospital;
            // 产品
            Product2 pro1 = new Product2(Name='name01',IsActive=true,Family='GI',Asset_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',ProductCode_Ext__c='pc01',Manual_Entry__c=false);
            Product2 pro2 = new Product2(Name='name02',IsActive=true,Family='GI',Asset_Model_No__c='n02',Serial_Lot_No__c='Lot tracing',ProductCode_Ext__c='pc02',Manual_Entry__c=false);
            Product2 pro3 = new Product2(Name='name03',IsActive=true,Family='GI',Asset_Model_No__c='n03',Serial_Lot_No__c='S/N tracing',ProductCode_Ext__c='pc03',Manual_Entry__c=false);
            insert new Product2[] {pro1, pro2, pro3};
            // 戦略科室を得る
            Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
            // 診療科を作る
            Account dep = new Account();
            dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
            dep.Name = 'test dep';
            dep.AgentCode_Ext__c = '9999998';
            dep.ParentId = strategicDep[0].Id;
            dep.Department_Class__c = strategicDep[0].Id;
            dep.Hospital__c = hospital.Id;
            insert dep;

            //新建备品借出申请-普通
            Rental_Apply__c raObj = new Rental_Apply__c();
            raObj.Name='*';
            raObj.Product_category__c = 'GI';
            raObj.Person_In_Charge__c = UserInfo.getUserId();
            raObj.applyUser__c =  UserInfo.getUserId();
            raObj.Hospital__c = hospital.Id;//医院
            raObj.Strategic_dept__c = strategicDep[0].id;//战略科室
            raObj.Demo_purpose1__c = '其他';
            raObj.demo_purpose2__c = '其他';
            raObj.Demo_purpose_text__c ='测试用的备品借出申请';
            raObj.Request_shipping_day__c = Date.today() + 5;
            raObj.Hope_Lonaer_date_Num__c = 16;
            raObj.Loaner_received_staff__c = '测试用户';
            raObj.Loaner_received_staff_phone__c = '13700008';
            raObj.direct_send__c = '上门自提';
            raObj.Account__c = dep.id;//科室
            raObj.pickup_time__c = Datetime.now()+8;
            raObj.direct_shippment_address__c = '233333的地址';
            raObj.Phone_number__c = '1234567890';
            insert raObj;
            raObj = [Select Salesdept_text_Update_F__c , Owner_Info_text_Update_F__c FROM Rental_Apply__c];
            System.assertEquals(false, raObj.Salesdept_text_Update_F__c);
            System.assertEquals(false, raObj.Owner_Info_text_Update_F__c);

            // 修改申请人信息
            user_admin.Dept__c = '医疗华东营业本部';
            user_admin.Province__c = '上海';
            update user_admin;
            //batch前，公式和text不一致
            raObj = [Select Salesdept_text_Update_F__c
                        , Salesdept_text__c
                        , Salesdept__c
                        , Owner_province_text__c
                        , Owner_province__c
                        , Owner_Info_text_Update_F__c
                    FROM Rental_Apply__c];
            System.assertNotEquals(raObj.Salesdept_text__c, raObj.Salesdept__c);
            System.assertEquals(true, raObj.Salesdept_text_Update_F__c);
            System.assertNotEquals(raObj.Owner_province_text__c, raObj.Owner_province__c);
            System.assertEquals(true, raObj.Owner_Info_text_Update_F__c);

            Test.startTest();
            RentalApplyDailyBatch bat = new RentalApplyDailyBatch();
            bat.errorList.add('test');
            Database.executeBatch(bat, 20);

            bat = new RentalApplyDailyBatch(1);
            bat.errorList.add('test');
            Database.executeBatch(bat, 20);
            Test.stopTest();

            // batch后，更新成一致
            raObj = [Select Salesdept_text_Update_F__c
                        , Salesdept_text__c
                        , Salesdept__c
                        , Owner_province_text__c
                        , Owner_province__c
                        , Owner_Info_text_Update_F__c
                    FROM Rental_Apply__c];
            System.assertEquals(raObj.Salesdept_text__c, raObj.Salesdept__c);
            System.assertEquals(false, raObj.Salesdept_text_Update_F__c);
            System.assertEquals(raObj.Owner_province_text__c, raObj.Owner_province__c);
            System.assertEquals(false, raObj.Owner_Info_text_Update_F__c);

        }
    }

}