@isTest
private class Email888AlarmBatchScheduleTest {
    @TestSetup
    public static void setup(){
        // 省
        Address_Level__c al1 = new Address_Level__c();
        al1.Name = '西南';
        al1.Level1_Code__c = 'CN-25';
        al1.Level1_Sys_No__c = '999999';

        Address_Level__c al02;
        al02 = al1.clone();
        al02.Name = '华东';
        al02.Level1_Code__c = 'CN-16';
        Address_Level__c al3 = al1.clone(false);
        al3.Name = '华南';
        al3.Level1_Code__c = 'CN-22';
        Address_Level__c al4= al1.clone(false);
        al4.Name = '华北';
        al4.Level1_Code__c = 'CN-01';
        Address_Level__c al5 = al1.clone(false);
        al5.Name = '东北';
        al5.Level1_Code__c = 'CN-13';
        Address_Level__c al6= al1.clone(false);
        al6.Name = '西北';
        al6.Level1_Code__c = 'CN-03';
        insert new Address_Level__c[] {al1,al02,al3,al4,al5,al6};
        System.assertEquals(al4.Level1_Code__c, 'CN-01');
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-14';
        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 = al1.id;
        insert al2;
        // 診療科を作る
        
        

        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);
        insert new Product2[] {pro1};
        

    }
    @isTest static void testBatchOnly () {
        

        // call the batch
        System.Test.startTest();
        Address_Level__c al1 = [SELECT Id FROM Address_Level__c WHERE Name='西南'];
        Address_Level__c al2 = [SELECT Id FROM Address_Level__c WHERE Name='渋谷区'];
        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 = al1.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;

        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 dep;
        Product2 pro1 = [SELECT Id FROm Product2 limit 1];
        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);
        insert asset1;

        Date nowDate = Date.today();

        String numberStr = '1';
        Alarm_888_Month__c alarm8881 = new Alarm_888_Month__c(
                            Number_Of_Operations__c = numberStr,
                            Nth_Operating_Day__c = nowDate.addMonths(1),
                            Alarm_Date__c = nowDate.addMonths(-2),
                            Asset_ID__c = asset1.Id
            );
        insert alarm8881;
        Database.executeBatch(new Email888AlarmBatch(),50);
        Integer expectedNumOfAlarm888 = [SELECT COUNT() FROM Alarm_888_Month__c];
        System.assertEquals(expectedNumOfAlarm888 > 0,true);
        System.Test.StopTest();

        
    }

     @isTest static void testBatchOnly1 () {
        

        System.Test.startTest();
        Address_Level__c al1 = [SELECT Id FROM Address_Level__c WHERE Name='华东'];
        Address_Level__c al2 = [SELECT Id FROM Address_Level__c WHERE Name='渋谷区'];
        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 = al1.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;

        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 dep;
        Product2 pro1 = [SELECT Id FROm Product2 limit 1];
        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);
        insert asset1;
        Date nowDate = Date.today();

        String numberStr = '1';
        Alarm_888_Month__c alarm8881 = new Alarm_888_Month__c(
                            Number_Of_Operations__c = numberStr,
                            Nth_Operating_Day__c = nowDate.addMonths(1),
                            Alarm_Date__c = nowDate.addMonths(-2),
                            Asset_ID__c = asset1.Id
            );
        insert alarm8881;
        Database.executeBatch(new Email888AlarmBatch(),50);
        Integer expectedNumOfAlarm888 = [SELECT COUNT() FROM Alarm_888_Month__c];
        System.assertEquals(expectedNumOfAlarm888 > 0,true);
        System.Test.StopTest();
    }
    
}