@isTest
private class EquipmentRepairBatchTest {
    static testMethod void testMethod1() {
          database.executeBatch(new EquipmentRepairBatch(),200);
    }
    static testMethod void testMethod2() {
      Account acc =   [select id from account where name = 'hospitalabc'];
      List<String> l = new List<String>();
      l.add(acc.id);
          database.executeBatch(new EquipmentCoverageTargetBatch(l),200);
    }
    @testSetup
    private static void setUpDate(){
        // 病院を作る
        Date toDayTime = Date.today();
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'hospitalabc';
        hospital.Is_Active__c = '有効';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.IF_Coverage_Target_HP__c = '1';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.Town__c = '东京';
        Oly_TriggerHandler.bypass('NFM701ControllerHandler');
        Oly_TriggerHandler.bypass('UpdateContractAimAmountHandler');
        insert hospital;

        // 戦略科室を得る
        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 = System.Label.Account_Asset_FJZ;
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        Oly_TriggerHandler.bypass('NFM701ControllerHandler');
        Oly_TriggerHandler.bypass('UpdateContractAimAmountHandler');
        insert dep;

        

        // 产品
        Product2 pro1 = new Product2(Name='name01',IsActive=true,Family='GI',
                Fixture_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n01',
                Maintenance_Price_Year__c = 999,
                Extend_Gurantee_Start_MD__c = Date.newInstance(toDayTime.year(),3,2),
                Extend_Gurantee_End_MD__c = Date.newInstance(3999,12,31),
                Extend_new_product_gurantee_MD__c = true,
                PartSupplyFinishDate__c  = Date.newInstance(toDayTime.year()+2,3,2),
                ProductCode_Ext__c='pc01',Manual_Entry__c=false,Brand_Name__c='奥林巴斯',
                Category2__c='本体',
                Category3__c='电子镜');
        Product2 pro2 = new Product2(Name='name02',IsActive=true,Family='GI',
                Fixture_Model_No__c='n02',Serial_Lot_No__c='Lot tracing',
                ProductCode_Ext__c='pc02',Manual_Entry__c=false,Brand_Name__c='奥林巴斯',
                Category2__c='本体',
                Extend_Gurantee_Start_MD__c = Date.newInstance(toDayTime.year(),3,2),
                Extend_Gurantee_End_MD__c = Date.newInstance(3999,12,31),
                Extend_new_product_gurantee_MD__c = true,
                PartSupplyFinishDate__c  = Date.newInstance(toDayTime.year()+2,3,2),
                Maintenance_Price_Year__c = 999,
                Category3__c='纤维镜');
        Product2 pro3 = new Product2(Name='name03',IsActive=true,Family='GI',
                Fixture_Model_No__c='n03',Serial_Lot_No__c='Lot tracing',
                PartSupplyFinishDate__c  = Date.newInstance(toDayTime.year()+2,3,2),
                ProductCode_Ext__c='pc03',Manual_Entry__c=false,Brand_Name__c='奥林巴斯',
                Category2__c='本体',
                Extend_Gurantee_Start_MD__c = Date.newInstance(toDayTime.year(),3,2),
                Extend_Gurantee_End_MD__c = Date.newInstance(3999,12,31),
                Extend_new_product_gurantee_MD__c = true,
                Maintenance_Price_Year__c = 999,
                Category3__c='电子镜');
        Product2 pro4 = new Product2(Name='name04',IsActive=true,Family='GI',
                Fixture_Model_No__c='n04',Serial_Lot_No__c='Lot tracing',
                ProductCode_Ext__c='pc04',Manual_Entry__c=false,Brand_Name__c='奥林巴斯',
                Category4__c='SD EndoEYE',
                Extend_Gurantee_Start_MD__c = Date.newInstance(toDayTime.year(),3,2),
                Extend_Gurantee_End_MD__c = Date.newInstance(3999,12,31),
                Extend_new_product_gurantee_MD__c = true,
                PartSupplyFinishDate__c  = Date.newInstance(toDayTime.year()+2,3,2),
                Maintenance_Price_Year__c = 999);
                
        Product2 pro5 = new Product2(Name='name05',IsActive=true,Family='GI',
                Fixture_Model_No__c='n05',Serial_Lot_No__c='Lot tracing',Brand_Name__c='奥林巴斯',
                // Category2__c='本体',
                Extend_Gurantee_Start_MD__c = Date.newInstance(toDayTime.year(),3,2),
                Extend_Gurantee_End_MD__c = Date.newInstance(3999,12,31),
                Extend_new_product_gurantee_MD__c = true,
                Maintenance_Price_Year__c = 999,
                PartSupplyFinishDate__c  = Date.newInstance(toDayTime.year()+2,3,2),
                ProductCode_Ext__c='pc05',Manual_Entry__c=false,
                Category3__c = 'EUS主机');
        insert new Product2[] {pro1, pro2, pro3,pro4,pro5};


        
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.RecordType.Name = '服务合同';
        contract.Name = 'tect contract';
        contract.status__c ='契約';
        contract.Maintenance_Contract_No__c = 'Kami_Contract_No';
        contract.Contract_Conclusion_Date__c = Date.today();
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep[0].Id;
        contract.Department__c = dep.Id;
        contract.Contract_Start_Date__c=Date.newInstance(toDayTime.year(),3,2);  // 10日前
        contract.Contract_End_Date__c = Date.newInstance(toDayTime.year()+1,3,20);       // 5日後
        contract.SalesOfficeCode_selection__c = '北京RC';
        contract.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('VM_Contract').getRecordTypeId();
        insert contract;

        List<Maintenance_Contract__c> MaintenanceContract=[select id from Maintenance_Contract__c];

        //---------------------------主体 两个附属品(个体管理 数量管理)
        // 保有设备A (主体)
        Asset assetA1 = new Asset(Asset_Owner__c = 'Olympus');
        assetA1.RecordTypeId = System.Label.Asset_RecordType;
        assetA1.SerialNumber = 'ass01';
        assetA1.Name = 'ass01';
        assetA1.AccountId = dep.Id;
        assetA1.Guarantee_period_for_products__c = toDayTime;
        assetA1.Department_Class__c = strategicDep[0].Id;
        assetA1.Hospital__c = hospital.Id;
        assetA1.Product2Id = pro1.Id;
        assetA1.Quantity = 1;
        assetA1.IF_Coverage_Target_Asset__c  = '1';
        
        assetA1.Status = '使用中';
        assetA1.Manage_type__c = '个体管理';
        assetA1.Loaner_accsessary__c = false;
        assetA1.Product_category__c = 'GI';
        assetA1.Delete_Flag__c = false;
        assetA1.Freeze_sign__c = false;
        assetA1.Out_of_wh__c = 0;
        assetA1.Salesdepartment__c = '1.华北营业本部';
        assetA1.Internal_asset_location__c = '北京 备品中心';
        assetA1.Product_category__c = 'GI';
        assetA1.Equipment_Type__c = '产品试用';
        assetA1.SalesProvince__c = '北京';
        assetA1.WH_location__c = '货架号1';
        assetA1.Asset_loaner_category__c = '固定资产';
        assetA1.CompanyOfEquipment__c = '123';
        assetA1.Internal_Asset_number__c = '123';
        assetA1.CurrentContract__c=MaintenanceContract[0].Id;
        // assetA1.AssetManageConfirm__c = true;
        // 保有设备A (附属品 数量管理)
        Asset assetA2 = new Asset(Asset_Owner__c = 'Olympus');
        assetA2.RecordTypeId = System.Label.Asset_RecordType;
        assetA2.SerialNumber = 'ass02';
        assetA2.Name = 'ass02';
        assetA2.AccountId = dep.Id;
        assetA2.Department_Class__c = strategicDep[0].Id;
        assetA2.Hospital__c = hospital.Id;
        assetA2.Product2Id = pro2.Id;
        assetA2.Quantity = 10;
        assetA2.Status = '使用中';
        assetA2.Manage_type__c = '数量管理';
        assetA2.Loaner_accsessary__c = true;
        assetA2.Product_category__c = 'GI';
        assetA2.Delete_Flag__c = false;
        assetA2.Guarantee_period_for_products__c = toDayTime;
        assetA2.Freeze_sign__c = false;
        assetA2.Out_of_wh__c = 3;
        
        assetA2.Frozen_Quantity__c = 2;
        assetA2.Salesdepartment__c = '1.华北营业本部';
        assetA2.Internal_asset_location__c = '北京 备品中心';
        assetA2.Product_category__c = 'GI';
        assetA2.Equipment_Type__c = '产品试用';
        assetA2.SalesProvince__c = '北京';
        assetA2.WH_location__c = '货架号2';
        assetA2.Asset_loaner_category__c = '固定资产';
        assetA2.CompanyOfEquipment__c = '123';
        assetA2.Internal_Asset_number__c = '123';
        assetA2.CurrentContract__c=MaintenanceContract[0].Id;
        assetA2.IF_Coverage_Target_Asset__c  = '1';
        // assetA2.AssetManageConfirm__c = true;
        // 保有设备A (附属品 个体管理)
        Asset assetA3 = new Asset(Asset_Owner__c = 'Olympus');
        assetA3.RecordTypeId = System.Label.Asset_RecordType;
        assetA3.SerialNumber = 'ass03';
        assetA3.Name = 'ass03';
        assetA3.AccountId = dep.Id;
        assetA3.Department_Class__c = strategicDep[0].Id;
        assetA3.Hospital__c = hospital.Id;
        assetA3.Product2Id = pro3.Id;
        assetA3.Quantity = 10;
        assetA3.Status = '使用中';
        assetA3.Manage_type__c = '数量管理';
        assetA3.Loaner_accsessary__c = true;
        assetA3.Product_category__c = 'GI';
        assetA3.Delete_Flag__c = false;
        assetA3.Guarantee_period_for_products__c = toDayTime;
        
        assetA3.Freeze_sign__c = false;
        assetA3.Out_of_wh__c = 3;
        assetA3.Salesdepartment__c = '1.华北营业本部';
        assetA3.Internal_asset_location__c = '北京 备品中心';
        assetA3.Product_category__c = 'GI';
        assetA3.Equipment_Type__c = '产品试用';
        assetA3.SalesProvince__c = '北京';
        assetA3.WH_location__c = '货架号3';
        assetA3.Abandoned_Inventory__c = 1;
        assetA3.Asset_loaner_category__c = '固定资产';
        assetA3.CompanyOfEquipment__c = '123';
        assetA3.Internal_Asset_number__c = '123';
        assetA3.CurrentContract__c=MaintenanceContract[0].Id;
        assetA3.IF_Coverage_Target_Asset__c  = '1';

    // 保有设备A (附属品 数量管理)
        Asset assetA4 = new Asset(Asset_Owner__c = 'Olympus');
        assetA4.RecordTypeId = System.Label.Asset_RecordType;
        assetA4.SerialNumber = 'ass04';
        assetA4.Name = 'ass04';
        assetA4.AccountId = dep.Id;
        assetA4.Department_Class__c = strategicDep[0].Id;
        assetA4.Hospital__c = hospital.Id;
        assetA4.Product2Id = pro4.Id;
        assetA4.Quantity = 10;
        assetA4.Guarantee_period_for_products__c = toDayTime;
        assetA4.Status = '使用中';
        
        assetA4.Manage_type__c = '数量管理';
        assetA4.Loaner_accsessary__c = true;
        assetA4.Product_category__c = 'GI';
        assetA4.Delete_Flag__c = false;
        assetA4.Freeze_sign__c = false;
        assetA4.Out_of_wh__c = 3;
        assetA4.Frozen_Quantity__c = 2;
        assetA4.Salesdepartment__c = '1.华北营业本部';
        assetA4.Internal_asset_location__c = '北京 备品中心';
        assetA4.Product_category__c = 'GI';
        assetA4.Equipment_Type__c = '产品试用';
        assetA4.SalesProvince__c = '北京';
        assetA4.WH_location__c = '货架号2';
        assetA4.Asset_loaner_category__c = '固定资产';
        assetA4.CompanyOfEquipment__c = '123';
        assetA4.Internal_Asset_number__c = '123';
        assetA4.CurrentContract__c=MaintenanceContract[0].Id;
        assetA4.IF_Coverage_Target_Asset__c  = '1';

        // assetA2.AssetManageConfirm__c = true;

        // 保有设备A (附属品 数量管理)
        Asset assetA5 = new Asset(Asset_Owner__c = 'Olympus');
        assetA5.RecordTypeId = System.Label.Asset_RecordType;
        assetA5.SerialNumber = 'ass05';
        assetA5.Name = 'ass05';
        assetA5.AccountId = dep.Id;
        assetA5.Department_Class__c = strategicDep[0].Id;
        assetA5.Hospital__c = hospital.Id;
        assetA5.Product2Id = pro5.Id;
        assetA5.Quantity = 10;
        assetA5.Status = '使用中';
        assetA5.Manage_type__c = '数量管理';
        assetA5.Loaner_accsessary__c = true;
        assetA5.Product_category__c = 'GI';
        assetA5.Delete_Flag__c = false;
        assetA5.Guarantee_period_for_products__c = toDayTime;

        assetA5.Freeze_sign__c = false;
        assetA5.Out_of_wh__c = 3;
        assetA5.Frozen_Quantity__c = 2;
        assetA5.Salesdepartment__c = '1.华北营业本部';
        assetA5.Internal_asset_location__c = '北京 备品中心';
        assetA5.Product_category__c = 'GI';
        assetA5.Equipment_Type__c = '产品试用';
        assetA5.SalesProvince__c = '北京';
        assetA5.WH_location__c = '货架号2';
        assetA5.Asset_loaner_category__c = '固定资产';
        assetA5.CompanyOfEquipment__c = '123';
        assetA5.Internal_Asset_number__c = '123';
        assetA5.CurrentContract__c=MaintenanceContract[0].Id;
        assetA5.IF_Coverage_Target_Asset__c  = '1';

        // assetA2.AssetManageConfirm__c = true;
        Oly_TriggerHandler.bypass('AssetHandlerCheck');
        insert new Asset[] {assetA1, assetA2, assetA3,assetA4,assetA5};
        List<Asset> Assetss = new Asset[] {assetA1, assetA2, assetA3,assetA4,assetA5};
        Account_Service_Of_Target__c asot = new Account_Service_Of_Target__c();
        asot.OCSM_Period_half__c='1H';
        asot.OCSM_Period__c = 'FY2023';
        asot.Account_HP__c=hospital.Id;
    
        insert asot;

        //维修合同/保有设备表
        Maintenance_Contract_Asset__c midd1 = new Maintenance_Contract_Asset__c();
        midd1.Asset__c = assetA1.Id;
        midd1.Maintenance_Contract__c = contract.id;
        midd1.CurrencyIsoCode= 'CNY';
        midd1.startDateGurantee_Text__c =Date.today().addDays( 5);
        midd1.endDateGurantee_Text__c =Date.today().addDays( 30);
        insert midd1;

        //维修表创建（包含在维修表的保有设备）
        Repair__c repairObj1 = new Repair__c();
        repairObj1.Delivered_Product__c = Assetss[0].Id;
        repairObj1.Hospital__c = hospital.Id;
        repairObj1.Department_Class__c = strategicDep[0].Id;
        repairObj1.Account__c = hospital.Id;
        repairObj1.SalesOfficeCode_selection__c = '北京石景山';
        repairObj1.On_site_repair__c = 'RC修理';
        repairObj1.Failure_Occurrence_Date__c = Date.today();
        repairObj1.Agreed_Date__c = Date.today().addDays(-1);

        // insert repairObj1;
		
		Repair__c repairObj2 = new Repair__c();
        repairObj2.Delivered_Product__c = Assetss[1].Id;
        repairObj2.Hospital__c = hospital.Id;
        repairObj2.Department_Class__c = strategicDep[0].Id;
        repairObj2.Account__c = hospital.Id;
        repairObj2.SalesOfficeCode_selection__c = '北京石景山';
        repairObj2.On_site_repair__c = 'RC修理';
        repairObj2.Failure_Occurrence_Date__c = Date.today();
        repairObj2.Agreed_Date__c = Date.today().addDays(-1);

        Repair__c repairObj3 = new Repair__c();
        repairObj3.Delivered_Product__c = Assetss[2].Id;
        repairObj3.Hospital__c = hospital.Id;
        repairObj3.Department_Class__c = strategicDep[0].Id;
        repairObj3.Account__c = hospital.Id;
        repairObj3.SalesOfficeCode_selection__c = '北京石景山';
        repairObj3.On_site_repair__c = 'RC修理';
        repairObj3.Failure_Occurrence_Date__c = Date.today();
        repairObj3.Agreed_Date__c = Date.today().addDays(-1);

        insert new Repair__c[] {repairObj1, repairObj2, repairObj3};
    }
}