@isTest
private class Create888AlarmBatchTest {
	@TestSetup
	public static void setup(){
        // 省
        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '東京';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;

        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        hospital.Is_Active__c = '有効';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.State_Master__c = al.id;
        hospital.City_Master__c = al2.id;
        hospital.Town__c = '东京';
        insert hospital;
        // 戦略科室を得る
        Account strategicDep = new Account();
        strategicDep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_Class_OTH'].id;
        strategicDep.Name = '其它';
        strategicDep.Department_Class_Label__c = '其他';
        strategicDep.Hospital__c = hospital.Id;
        strategicDep.ParentId = hospital.Id;
        insert strategicDep;
        // 診療科を作る
        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.Id;
        dep.Department_Class__c = strategicDep.Id;
        dep.Hospital__c = hospital.Id;
        insert new Account[] {dep};

        Product2 pro1 = new Product2(Name='name01',IsActive=true,Family='GI',
                Fixture_Model_No__c='MH-241',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'MH-241', Asset_Model_No__c = 'Pro1',
                ProductCode_Ext__c='pc01',Manual_Entry__c=false);
        Product2 pro2 = new Product2(Name='name02',IsActive=true,Family='GI',
                Fixture_Model_No__c='MB-466',Serial_Lot_No__c='Lot tracing',
                Fixture_Model_No_T__c = 'MB-466', Asset_Model_No__c = 'Pro2',
                ProductCode_Ext__c='pc02',Manual_Entry__c=false,Extend_new_product_gurantee__c = true,
                Extend_Gurantee_Start__c = Date.newInstance(2022, 3, 3),Extend_Gurantee_End__c = Date.newInstance(2023, 10,12));
        insert new Product2[] {pro1, pro2};

        Asset asset1 = new Asset(Asset_Owner__c = 'Olympus');
        asset1.InstallDate  = Date.newInstance(2022, 12,12);
        asset1.Is_Add_888_Alarm__c = 'False';
        asset1.Product2Id = pro1.Id;
        // asset1.IS_Extend_Gurantee__c = false;
        asset1.warrantyType__c = '市场多年保修';
        // asset1.SerMarGuranteeType__c = '市场多年保';
        asset1.New_logic_data__c = true;
        asset1.IS_Extend_Gurantee_Txt__c = false;
        asset1.ProvistonPeriod__c = 3;
        // asset1.Extend_Gurantee_DateTo__c = Date.today().addYears(1);
        asset1.AssetMark__c = '主机';
        // asset1.Posting_Date__c =  Date.today().addDays(-30);
        asset1.ChangeWarrantyStartDate__c = Date.newInstance(2022, 7,12);
        asset1.WarrantyChanges__c = true;

        // asset1.InstallDate__c  = Date.newInstance(2022, 12,12);

        asset1.SerialNumber = 'sn01';
        asset1.Name = 'MH-2411';
        asset1.AccountId = dep.Id;
        asset1.Department_Class__c = strategicDep.Id;
        asset1.Hospital__c = hospital.Id;
        // asset1.Product2Id = pro1.Id;
        asset1.Quantity = 100;
        asset1.Manage_type__c = '数量管理';
        asset1.Loaner_accsessary__c = true;
        asset1.Out_of_wh__c = 0;
        asset1.Salesdepartment__c = '0.备品中心';
        asset1.Product_category__c = 'GI';

        asset1.Asset_loaner_category__c = '耗材';
        asset1.Delete_Flag__c = false;
        asset1.AssetManageConfirm__c = true;
        asset1.Equipment_Type__c = '产品试用';
        asset1.Fixture_OneToOne_Link__c = null;
        asset1.Internal_asset_location__c = '上海 备品中心';
        asset1.Consumable_Guaranteen_end__c = System.today().addYears(1);
        // asset1.RecordTypeId = '01210000000kOPM';
        // asset1.InstallDate__c =  Date.newInstance(2022, 12,12);
        /*asset1.RecordTypeId = System.Label.Asset_RecordType;
        asset1.SerialNumber = 'sn01';
        asset1.Name = 'MH-241';
        asset1.AccountId = dep.Id;
        asset1.Department_Class__c = strategicDep.Id;
        asset1.Hospital__c = hospital.Id;
        asset1.Product2Id = pro1.Id;
        asset1.Quantity = 100;
        asset1.Manage_type__c = '数量管理';
        asset1.Loaner_accsessary__c = true;
        asset1.Out_of_wh__c = 0;
        asset1.Salesdepartment__c = '0.备品中心';
        asset1.Product_category__c = 'GI';

        asset1.Asset_loaner_category__c = '耗材';
        asset1.Delete_Flag__c = false;
        asset1.AssetManageConfirm__c = true;
        asset1.Equipment_Type__c = '产品试用';
        asset1.Fixture_OneToOne_Link__c = null;
        asset1.Internal_asset_location__c = '上海 备品中心';
        asset1.RecordTypeId = '01210000000kOPR';
        asset1.Consumable_Guaranteen_end__c = System.today().addYears(1);*/

        Asset asset2 = new Asset(Asset_Owner__c = 'Olympus');
        asset2.InstallDate = Date.newInstance(2023, 1,12);
        asset2.Is_Add_888_Alarm__c = 'False';
        asset2.Product2Id = pro2.Id;
        asset2.RecordTypeId = '01210000000kOPM';
        // asset2.Asset_OwnerNew__c = '奥林巴斯';
        asset2.FirstApproveDate_old__c  = Date.newInstance(2023, 2,12);
        // asset2.IS_Extend_Gurantee__c = true;
        // asset2.SerMarGuranteeType__c = '服务多年保修';
        asset2.New_logic_data__c = true;
        asset2.IS_Extend_Gurantee_Txt__c = true;
        asset2.Guarantee_period_year__c  = 4;
        // asset2.Extend_Gurantee_DateTo__c = Date.today().addYears(1);
        asset2.AssetMark__c = '主机';
        // asset1.Posting_Date__c =  Date.today().addDays(-30);
        asset2.ChangeWarrantyStartDate__c = Date.newInstance(2022, 12,12);
        asset2.WarrantyChanges__c = true;
        // asset2.InstallDate__c =  Date.newInstance(2023, 2,12);

        asset2.SerialNumber = 'sn02';
        asset2.Name = 'MB-4662';
        asset2.AccountId = dep.Id;
        asset2.Department_Class__c = strategicDep.Id;
        asset2.Hospital__c = hospital.Id;
        // asset2.Product2Id = pro2.Id;
        asset2.Quantity = 100;
        asset2.Manage_type__c = '数量管理';
        asset2.Loaner_accsessary__c = true;
        asset2.Out_of_wh__c = 0;
        asset2.Salesdepartment__c = '0.备品中心';
        asset2.Product_category__c = 'GI';

        asset2.Asset_loaner_category__c = '耗材';
        asset2.Delete_Flag__c = false;
        asset2.AssetManageConfirm__c = true;
        asset2.Equipment_Type__c = '产品试用';
        asset2.Fixture_OneToOne_Link__c = null;
        asset2.Internal_asset_location__c = '上海 备品中心';
        // asset2.RecordTypeId = '01210000000kOPR';
        asset2.Consumable_Guaranteen_end__c = System.today().addYears(1);

        Asset asset3 = new Asset(Asset_Owner__c = 'Olympus');
        asset3.InstallDate = Date.newInstance(2023, 1,12);
        asset3.Is_Add_888_Alarm__c = 'False';
        asset3.Product2Id = pro2.Id;
        // asset3.RecordTypeId = '01210000000kOPR';
        // asset2.Asset_OwnerNew__c = '奥林巴斯';
        // asset3.FirstApproveDate_old__c  = Date.newInstance(2023, 2,12);
        // asset2.IS_Extend_Gurantee__c = true;
        // asset2.SerMarGuranteeType__c = '服务多年保修';
        // asset3.New_logic_data__c = true;
        // asset3.IS_Extend_Gurantee_Txt__c = true;
        // asset3.Guarantee_period_year__c  = 4;
        // asset2.Extend_Gurantee_DateTo__c = Date.today().addYears(1);
        // asset3.AssetMark__c = '主机';
        // asset1.Posting_Date__c =  Date.today().addDays(-30);
        /*asset3.ChangeWarrantyStartDate__c = Date.newInstance(2022, 11,12);
        asset3.WarrantyChanges__c = true;*/
        // asset2.InstallDate__c =  Date.newInstance(2023, 2,12);

        asset3.CurrentContract_End_Date__c = Date.newInstance(2023, 11,12);
        asset3.CurrentContract_Start_Date__c = Date.newInstance(2023, 1,12);

        asset3.SerialNumber = 'sn03';
        asset3.Name = 'MB-4663';
        asset3.AccountId = dep.Id;
        asset3.Department_Class__c = strategicDep.Id;
        asset3.Hospital__c = hospital.Id;
        // asset2.Product2Id = pro2.Id;
        asset3.Quantity = 100;
        asset3.Manage_type__c = '数量管理';
        asset3.Loaner_accsessary__c = true;
        asset3.Out_of_wh__c = 0;
        asset3.Salesdepartment__c = '0.备品中心';
        asset3.Product_category__c = 'GI';

        asset3.Asset_loaner_category__c = '耗材';
        asset3.Delete_Flag__c = false;
        asset3.AssetManageConfirm__c = true;
        asset3.Equipment_Type__c = '产品试用';
        asset3.Fixture_OneToOne_Link__c = null;
        asset3.Internal_asset_location__c = '上海 备品中心';
        // asset2.RecordTypeId = '01210000000kOPR';
        asset3.Consumable_Guaranteen_end__c = System.today().addYears(1);

        Oly_TriggerHandler.bypass('AssetHandlerCheck');
        Oly_TriggerHandler.bypass('AssetHandlerMEBG');
        Oly_TriggerHandler.bypass('AssetHandler');
        Oly_TriggerHandler.bypass('PCLLostProduct2AssetHandler');
        StaticParameter.EscapeVMCTrigger = true;
		insert new Asset[] {asset1, asset2,asset3};
        StaticParameter.EscapeVMCTrigger = false;

	}

	@isTest
    public static void tesExecute() {
    	Test.StartTest();
        /*List<Asset> testList = [Select IS_Extend_Gurantee__c,AssetMark__c,Asset_OwnerNew__c,FirstApproveDate__c ,Id,InstallDate,SerMarGuranteeType__c,InstallDate__c,Extend_Gurantee_DateTo__c,CurrentContract__c,CurrentContract_Start_Date__c,CurrentContract_End_Date__c,Is_Add_888_Alarm__c FROM Asset 
                                ];
        System.debug(LoggingLevel.INFO, '*** testList: ' + testList);*/
        Id execBTId = Database.executeBatch( new Create888AlarmBatch(),50);
        System.assertEquals(execBTId != null, true,'执行失败');
        Test.stopTest();

    }
}