@isTest
private class RepairProductGuaranteUpdateBatchTest {

    public static Repair__c repair { get; set;}
    public static Asset asset { get; set;}

    private static void init() {
        Test.StartTest();
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'];
        if (rectDpt.size() == 0) {
            return;
        }

        Profile p = [select Id from Profile where id = :System.Label.ProfileId_SystemAdmin];

        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', Work_Location__c = '北京', 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;
        User hpOwner2 = new User(Test_staff__c = true, LastName = 'hp2', FirstName = 'owner', Alias = 'hp2', Work_Location__c = '重庆', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner2;
        // 病院作成
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.Id);
        hp.FSE_GI_Main_Leader__c = hpOwner.Id;
        hp.FSE_SP_Main_Leader__c = hpOwner2.Id;
        insert hp;

        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];

        // 診療科を作る
        Account dpt = new Account(RecordTypeId = rectDpt[0].Id);
        dpt.Name         = '*';
        dpt.Department_Name__c  = 'TestDepart';
        dpt.ParentId            = dc.Id;
        dpt.Department_Class__c = dc.Id;
        dpt.Hospital__c         = hp.Id;
        insert dpt;
        Test.StopTest();

        RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity' ];
        Opportunity opp = new Opportunity(
            Name = 'testOpp1',
            StageName = '引合',
            CloseDate = Date.today(),
            AccountId = dpt.Id,
            Sales_Root__c = 'OCM直接販売',
            Competitor__c = 'A',
            Click_Close_Date__c = null,
            RecordType = rectOpp
        );
        insert opp;


        Statu_Achievements__c Sac = new Statu_Achievements__c(
            name = 'zhucan_one',
            Opportunity__c = opp.id,
            ContractAmount__c = 0,
            UnshippedAmount__c = 0,
            DeliveryDate__c = Date.today(),
            FirstApproveDate__c = Date.valueOf('2016-08-10')
        );
        insert Sac;

        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);
        pro1.Extend_new_product_gurantee__c = true;
        pro1.Entend_gurantee_period__c = '两年';
        pro1.Extend_Gurantee_Start__c = Date.valueOf('2015-01-01');
        pro1.Extend_Gurantee_End__c = Date.valueOf('2099-01-04');
        insert pro1;
        // 納入機器を作成する
        asset = new Asset();
        asset.Name = 'テスト機器';
        asset.AccountId = dpt.Id;
        asset.Department_Class__c = dc.Id;
        asset.Hospital__c = hp.Id;
        asset.SerialNumber = 'testserial';
        asset.Product2Id = pro1.Id;
        asset.Backorder__c = Sac.Id;
        asset.InstallDate = Date.today().addDays(-10);
        asset.Guarantee_period_for_products__c =  Date.today().addDays(345);
        insert asset;

        // 修理を作成する01
        repair = new Repair__c();
        repair.Account__c = dpt.Id;
        repair.Department_Class__c = dc.Id;
        repair.Hospital__c = hp.Id;
        repair.Delivered_Product__c = asset.Id;
        repair.SERVICE_CONTRACT_JUDEGE_DAY__C = Date.today().addDays(0);  // 维修合同判断日がサービス契約の中間辺りの日付
        repair.FSE_Work_Location__c = '上海';
        repair.NewProductGuarante_Txt__c = '2: 多年保修';
        repair.Failure_Occurrence_Date__c =  Date.today().addDays(-1);
        repair.SAP_Transfer_time__c =  Datetime.now().addDays(1);
        insert repair;
        // Repair__c a = [select id , SAP_Transfer_day__c, CreatedDate,Failure_Occurrence_Date__c
        //                 ,NewProductGuarante_Formula__c
        //                from Repair__c where id = : repair.id
        //               ];
        // Asset ast =
        //     [select id, Guarantee_period_formula__c,
        //     guarantee_period__c, IS_Extend_Gurantee_Txt__c,
        //         Extend_Gurantee_DateTo_Text__c,warrantyType__c
        //      from asset where id =: asset.Id ];
        // throw new ControllerUtil.myException('a:' + a +'\nast'+ast);
        // system.assertEquals('1', a.NewProductGuarante_Formula__c);
        //system.assertEquals('1', a.NewProductGuarante_Txt__c);
    }

    @isTest
    static void test_method_one() {
        // Implement test code
        init();

        //Test.StartTest();
        //insert repair;
        //system.assertEquals('1', repair.NewProductGuarante_Formula__c);
        //system.assertEquals('1', repair.NewProductGuarante_Txt__c);
        asset.Guarantee_period_for_products__c = Date.valueOf('2018-06-10');
        update asset;

        Id execBTId = Database.executeBatch(new RepairProductGuaranteUpdateBatch(), 5);
        //Test.StopTest();
    }

}