@Istest
public with sharing class CBF2batchTest {
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    
    static Asset createAsset( Account hospital, Account strategicDep, Account dep) {
        // 製品を作る
        Product2 productA = new Product2( Name = 'テスト商品',Asset_Model_No__c = 'BF-1T260');
        insert productA;

        // 価格表エントリを作成する
        PricebookEntry entry = new PricebookEntry( Pricebook2Id = pricebookId, Product2Id = productA.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        entry.Product2Id = productA.Id;
        insert entry;

        // 納入機器を作成する
        Asset asset = new Asset();
        asset.Name = 'テスト機器';
        asset.AccountId = dep.Id;
        asset.Department_Class__c = strategicDep.Id;
        asset.Hospital__c = hospital.Id;
        asset.SerialNumber = 'testserial';
        asset.Quantity = 3;
        asset.Extend_Gurantee_DateTo_Text__c =Date.today().addDays(30);
        asset.IS_Extend_Gurantee_Txt__c =true;
        asset.Order_No__c = 'BJ_2020';
        insert asset;

        return asset;
    }
    @testSetup
    private static void setupTestData(){
        //  ControllerUtil.EscapeNFM001Trigger = true;

        // 病院を作る
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdminGPI];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', 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);
        insert hpOwner;
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectDp = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科' order by Name desc];
        if (rectDp.size() == 0) {
            return;
        }

        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科' order by Name desc];
        if (rectDpt.size() == 0) {
            return; 
        }
        Account hp1 = new Account(RecordTypeId = rectHp[0].Id,Is_Active__c = '有効' ,Name = 'testHp1', OCM_Category__c = 'H0', OwnerId = hpOwner.Id);
        insert hp1;

        Account dp1 = new Account(RecordTypeId = rectDp[0].Id, Is_Active__c = '有効' , Department_Class_Label__c = '呼吸科',
            ParentId  = hp1.id, Name = 'testdp1', OCM_Category__c = 'H0', OwnerId = hpOwner.Id);
        insert dp1;

        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp1.Id and Department_Class_Label__c  = '呼吸科' order by Department_Class_Label__c];
        
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = 'hospitalName';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = hp1.Id;
        depart1.Is_Active__c        = '有効';
        insert depart1;
        // Account dep = createDep( depart1, dc1s[0]);
         // 納入機器を作る
         Asset asset = createAsset( hp1, dc1s[0], depart1);
        String BUid = System.Label.interfaceUserID;
        User thisUser = [select Id from User where Id = '00510000000gmxH'];
        System.runAs ( thisUser ){
        //  NFM603Controller.isRunning = false;
        // 修理
         Repair__c re = new Repair__c();
         re.SAPRepairNo__c = '000010168255';
         re.Account__c = depart1.Id;
         re.Department_Class__c = dc1s[0].Id;
         re.Hospital__c = hp1.Id;
         re.Cumulative_Uses__c = 1811;
         re.Cumulative_Times__c = 38011;
         re.AwaitToSendAWS__c = true;
         re.DeliveryLogisticsMode__c = '快递';
         re.engineerSendDate__c = Date.today(); //修理品寄送日
         re.Delivered_Product__c = asset.Id;
         re.Failure_Occurrence_Date__c  = Date.today(); //1.故障发生日
         re.AWS_Data_Id__c = 'sssssswqsss';
         re.Repair_Inspection_Date__c = Date.today();
         insert re;

         Repair__c re2 = new Repair__c();
         re2.SAPRepairNo__c = '000010168254';
         re2.Account__c = depart1.Id;
         re2.Department_Class__c = dc1s[0].Id;
         re2.Hospital__c = hp1.Id;
         re2.Cumulative_Uses__c = 0;
         re2.Cumulative_Times__c = 38011;
         re2.AwaitToSendAWS__c = true;
         re2.AWS_Data_Id__c = 'sssssswsss';
         re2.DeliveryLogisticsMode__c = '快递';
         re2.engineerSendDate__c = Date.today(); //修理品寄送日
         re2.Delivered_Product__c = asset.Id;
         re2.Failure_Occurrence_Date__c  = Date.today(); //1.故障发生日
         re2.Repair_Inspection_Date__c = Date.today();
         insert re2;
        }
    }
    @isTest
    private static void testMethod1() {
        User thisUser = [select Id from User where Id = '00510000000gmxH'];
        System.runAs ( thisUser ){
            Account mc = [select id from Account where Is_Active__c = '有効' limit 1];
            // System.Test.startTest();
            Database.executeBatch(new CBF2batch(mc.Id,null),1);
            Database.executeBatch(new CBF2batch(),1);
            Database.executeBatch(new CBF2batch(mc.Id,'xxss'),1);
            CBF2batch.calculateDaysBetween(Date.today(),Date.today().addDays(10));
            // System.Test.stopTest();
        }
    }
}