@isTest
private class AssetWhereaboutsTest {
    @testSetup
    private static void init(){
        Oly_TriggerHandler.bypass(Product2Handler.Class.getName());
        Oly_TriggerHandler.bypass(AssetHandler.Class.getName());
        Oly_TriggerHandler.bypass(FixtureSetHandler.Class.getName());
        Oly_TriggerHandler.bypass(RentalApplyTriggerHandler.Class.getName());
        Oly_TriggerHandler.bypass(RentalApplyEquipmentSetHandler.Class.getName());
        Oly_TriggerHandler.bypass(RentalApplyEquipmentSetDetailHandler.Class.getName());
        Oly_TriggerHandler.bypass(ContactTriggerHandler.Class.getName());
        Oly_TriggerHandler.bypass('AssetTrigger');
        Oly_TriggerHandler.bypass('MaintenanceContractTrigger');
        Oly_TriggerHandler.bypass('MaintenanceContractAfterUpdate');
        Oly_TriggerHandler.bypass('MaintenanceContractBeforeDelete');
        Oly_TriggerHandler.bypass('MaintenanceContractHpDeptUpd');
        Oly_TriggerHandler.bypass('NFM106Controller');
        Oly_TriggerHandler.bypass('NFM101Controller');
       Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        User MacOwner = new User(Test_staff__c = true, LastName = 'hp1', FirstName = 'owner1', Alias = 'hp', CommunityNickname = 'hpOwner1', Email = 'olympus_hpowner@sunbridge.com',Job_Category__c = '销售服务', Username = 'olympus_hpowner@sunbridge.com1', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert MacOwner;
        Account hospital = new Account();
        hospital.recordtypeId = 
            Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
            
        hospital.Name = 'test hospita/l';
        insert hospital;
        
        // 戦略科室を得る
        List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
        
        // // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_GI').getRecordTypeId();
        dep.Name = 'test de/p';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;
        
        // 製品を作る
        Product2 productA = new Product2( Name='テスト商品', Maintenance_Price_Year__c = 12000, Manual_Entry__c = false,IsActive=true,Family='GI',
                Fixture_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n01',
                ProductCode_Ext__c='pc01');
        insert productA;
        
       // 第一期合同
         Maintenance_Contract__c contract1 = new Maintenance_Contract__c();
        contract1.Name = 'tect contract1';
        contract1.Hospital__c = hospital.Id;
        contract1.Department_Class__c = strategicDep[0].Id;
        contract1.Department__c = dep.Id;
        contract1.Service_Contract_Staff__c = MacOwner.Id;
        contract1.Payment_Plan_Sum_First__c = 1;
        contract1.Status__c = '契約';
        contract1.Maintenance_Contract_No__c = '11123';
         contract1.recordtypeId = 
             Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
        contract1.Not_Upper_limit_reason__c = ' 1';
        contract1.Contract_Start_Date__c = Date.today().addDays( -10);   
        contract1.Contract_End_Date__c = Date.today().addDays( 5);       
        contract1.SalesOfficeCode_selection__c = '北京RC';
        insert contract1;

        // 第二期合同
        Maintenance_Contract__c contract12 = new Maintenance_Contract__c();
        contract12.Name = 'tect contract12';
        contract12.Not_Upper_limit_reason__c = 'tect contract12';
        contract12.RecordtypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('VM_Contract').getRecordTypeId();
        contract12.Hospital__c = hospital.Id;
        contract1.Switch_TimeBase_WF__c = true;
        contract12.Department_Class__c = strategicDep[0].Id;
        contract12.Department__c = dep.Id;
        contract12.Service_Contract_Staff__c = UserInfo.getUserId();
        contract12.Maintenance_Contract_No__c = 'tect contract12';
        contract12.Status__c = '契約満了';
        contract12.Contract_Start_Date__c = Date.today().addMonths(-13);
        contract12.Contract_End_Date__c = Date.today().addMonths(-1);
        insert contract12;
        // insert new Maintenance_Contract__c[] {contract1};
        
        System.debug('contract1 =='+ contract1 );
         List<Maintenance_Contract__c> listmc = [select id ,Status__c,RecordType_Name__c from Maintenance_Contract__c ];
        // for (Maintenance_Contract__c mc:listmc) {
        //     mc1.id = mc.id;
        //     mc1.Status__c = '契約';
        //     update mc1;
        //     System.debug('更新成功'+mc1);

        // }
        // List<Maintenance_Contract__c> listmc1 = [select id ,Status__c,RecordType_Name__c from Maintenance_Contract__c ];
        System.debug('listmc'+listmc);
        // System.debug('contract12.Status__c =='+ contract12.Status__c );
        Asset asset = new Asset();
        // Asset assetA1 = new Asset(Asset_Owner__c = 'Olympus');
        asset.RecordTypeId = System.Label.Asset_RecordType;
        asset.SerialNumber = 'ass01';
        asset.Name = 'ass01';
        asset.AccountId = dep.Id;
        asset.Department_Class__c = strategicDep[0].Id;
        asset.Hospital__c = hospital.Id;
        asset.Product2Id = productA.Id;
        asset.Quantity = 1;
        asset.Status = '有库存';
        asset.Manage_type__c = '个体管理';
        asset.Loaner_accsessary__c = false;
        asset.Out_of_wh__c = 0;
        asset.Salesdepartment__c = '1.华北营业本部';
        asset.Internal_asset_location__c = '北京 备品中心';
        asset.Product_category__c = 'GI';
        asset.Equipment_Type__c = '产品试用';
        asset.SalesProvince__c = '北京';
        asset.CurrentContract__c = contract1.Id;
        asset.CurrentContract_Asset_Price__c = 0;
        insert asset;
        Asset asset2 = new Asset();
        // Asset assetA1 = new Asset(Asset_Owner__c = 'Olympus');
        asset2.RecordTypeId = System.Label.Asset_RecordType;
        asset2.SerialNumber = 'ass02';
        asset2.Name = 'ass02';
        asset2.AccountId = dep.Id;
        asset2.Department_Class__c = strategicDep[0].Id;
        asset2.Hospital__c = hospital.Id;
        asset2.Product2Id = productA.Id;
        asset2.Quantity = 1;
        asset2.Status = '有库存';
        asset2.Manage_type__c = '个体管理';
        asset2.Loaner_accsessary__c = false;
        asset2.Out_of_wh__c = 0;
        asset2.Salesdepartment__c = '1.华北营业本部';
        asset2.Internal_asset_location__c = '北京 备品中心';
        asset2.Product_category__c = 'GI';
        asset2.Equipment_Type__c = '产品试用';
        asset2.SalesProvince__c = '北京';
        asset2.CurrentContract__c = contract12.Id;
        asset2.CurrentContract_Asset_Price__c = 0;
        insert asset2;
        //System.Test.stopTest();

        Maintenance_Contract_Asset__c contract1asset1 = new Maintenance_Contract_Asset__c();
        contract1asset1.Asset__c = asset.Id;
        contract1asset1.endDateGurantee_Text__c = Date.today().addYears(-1);
        contract1asset1.Maintenance_Contract__c = contract1.Id;
        contract1asset1.Estimate_List_Price_All_Manual__c = 1000;
        insert contract1asset1;
        // contract1asset.Maintenance_Contract_Asset_Estimate__c = mcae1.id;
         Maintenance_Contract_Asset__c contract1asset2 = new Maintenance_Contract_Asset__c();
         contract1asset2.Asset__c = asset2.Id;
         contract1asset2.endDateGurantee_Text__c = Date.today().addYears(-1);
        contract1asset2.Maintenance_Contract__c = contract12.Id;
        insert contract1asset2;
        // insert new list<Maintenance_Contract_Asset__c> {contract1asset1,contract1asset2 };
        Maintenance_Contract_Asset__c contract1asset3 = new Maintenance_Contract_Asset__c();
         contract1asset3.Asset__c = asset.Id;
         contract1asset3.endDateGurantee_Text__c = Date.today().addYears(-1);
        contract1asset3.Maintenance_Contract__c = contract12.Id;
        insert contract1asset3;
        
        // repair01.Account__c = dep.Id;
        
        // // repair01.Repair_Start_Date__c = Date.newInstance(2022,7,20);
        
        // repair01.Department_Class__c = strategicDep[0].Id;
        // repair01.Hospital__c = hospital.Id;
        // repair01.Dealer__c = dep.Id;
        // // repair01.Status1__c = '3.维修阶段';
        // repair01.Delivered_Product__c = asset.Id;
       
        //  insert repair01;
        Repair__c repair1 = new Repair__c();
        repair1.Service_Repair_No__c = 'repair1';
        repair1.Hospital__c            = hospital.Id;
        repair1.Account__c             = dep.Id;
        repair1.Department_Class__c = strategicDep[0].id;
        repair1.Delivered_Product__c = asset.Id;
        repair1.Repair_List_Price__c = 100;
        repair1.Billing_Amount__c = 10;
        repair1.Paid_Amount__c = 1;
        repair1.DateReceiptQuestions__c = Date.newInstance(2022,7,20);
        repair1.Failure_Occurrence_Date__c = Date.today().addDays(-1);
        repair1.Repair_Returned_To_HP_Date__c = Date.today().addDays(3);
        repair1.Repair_Shipped_Date__c = Date.today().addDays(1);
          System.debug('repair1.Maintenance_Contract__c =='+ repair1.Maintenance_Contract__c );
          System.debug('repair01.Status1__c =='+ repair1.Status1__c );
          system.debug('day=='+Date.today().addYears(-1));
        System.debug('repair01.DateReceiptQuestions__c =='+ repair1.DateReceiptQuestions__c );
            insert repair1;
    }
     @isTest
    private static void testMethod1() {
        
         System.Test.startTest();
        Maintenance_Contract__c  mc1 = new Maintenance_Contract__c();

        List<Maintenance_Contract__c> listmc = [select id ,Status__c,RecordType_Name__c from Maintenance_Contract__c ];
        for (Maintenance_Contract__c mc:listmc) {
            mc1.id = mc.id;
            mc1.Status__c = '契約';
            update mc1;
            System.debug('更新成功'+mc1);

        }
        List<Maintenance_Contract__c> listmc1 = [select id ,Status__c,RecordType_Name__c from Maintenance_Contract__c ];
        System.debug('listmc'+listmc1);

        Database.executeBatch(new AssetWhereabouts(),200);
         System.Test.stopTest();
    }
    // private static void testMethod2() {
    //      System.Test.startTest();
    //      select id,
    //     Database.executeBatch(new AssetWhereabouts(),200);
    //      System.Test.stopTest();
    // }
}