@isTest
private class CreateSLAReportTaskBatchTest {
    static testMethod void testMethod1() {
        ControllerUtil.EscapeNFM001Trigger = true;
        ControllerUtil.EscapeMaintenanceContractAfterUpdateTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
        StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;

        // Implement test code
        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;
        // 病院作成
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.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 dpt1 = new Account(RecordTypeId = rectDpt[0].Id);
        dpt1.Name         = '*';
        dpt1.Department_Name__c  = 'TestDepart1';
        dpt1.ParentId            = dc.Id;
        dpt1.Department_Class__c = dc.Id;
        dpt1.Hospital__c         = hp.Id;
        insert dpt1;


     
       	Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Status__c ='契約';
        contract.Maintenance_Contract_No__c = 'Kami_Contract_No';
        contract.Contract_Conclusion_Date__c = Date.today().addDays(-1);
        contract.Hospital__c = hp.Id;
        contract.Department_Class__c = dc.Id;
        contract.Department__c = dpt1.Id;
        contract.Contract_Start_Date__c = Date.today().addMonths(-1);
        contract.Contract_End_Date__c = Date.today().addMonths(11);
        contract.SalesOfficeCode_selection__c = '北京RC';
        contract.Service_Contract_Staff__c = UserInfo.getUserId();
        contract.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId(); 
        insert contract;

        //生成服务合同报告书
        NewMaintenanceReport_Task__c tempNMCT = new NewMaintenanceReport_Task__c();
        tempNMCT.Name = 'FWreport-01';
        tempNMCT.Distribution_Start_Date__c = Date.today().addDays(-30);
        tempNMCT.Distribution_End_Date__c = Date.today().addMonths(2).addDays(-1);
        tempNMCT.NewMaintenance_Contract__c = contract.Id;
        tempNMCT.ActualDistributionTimes_Quarter__c = 0;
        insert tempNMCT;
 		Date today = Date.today();

        List<NewMaintenanceReport_Task__c> NMCTList = [SELECT Id FROM NewMaintenanceReport_Task__c where Distribution_Start_Date__c <= :today AND  Distribution_End_Date__c >= :today AND Task__c = NULL];
        //System.assertEquals(0,NMCTList.size());

        System.Test.StartTest();
        Database.executeBatch(new CreateSLAReportTaskBatch(tempNMCT.Id),1);
        System.Test.StopTest();

    }

        static testMethod void testMethod2() {
        ControllerUtil.EscapeNFM001Trigger = true;
        ControllerUtil.EscapeMaintenanceContractAfterUpdateTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
        StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;

        // Implement test code
        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;
        // 病院作成
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.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 dpt1 = new Account(RecordTypeId = rectDpt[0].Id);
        dpt1.Name         = '*';
        dpt1.Department_Name__c  = 'TestDepart1';
        dpt1.ParentId            = dc.Id;
        dpt1.Department_Class__c = dc.Id;
        dpt1.Hospital__c         = hp.Id;
        insert dpt1;


     
       	Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Status__c ='契約';
        contract.Maintenance_Contract_No__c = 'Kami_Contract_No';
        contract.Contract_Conclusion_Date__c = Date.today().addDays(-1);
        contract.Hospital__c = hp.Id;
        contract.Department_Class__c = dc.Id;
        contract.Department__c = dpt1.Id;
        contract.Contract_Start_Date__c = Date.today().addMonths(-1);
        contract.Contract_End_Date__c = Date.today().addMonths(11);
        contract.SalesOfficeCode_selection__c = '北京RC';
        contract.Service_Contract_Staff__c = UserInfo.getUserId();
        contract.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId(); 
        insert contract;

        task__c t = new task__c();
        t.taskDifferent__c = '主动任务';
        t.RecordTypeId  = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('SLAReport_Task').getRecordTypeId();
        t.Activity_Date__c = Date.today().addDays(15);
        t.Maintenance_Contract__c = contract.Id;
        insert t;

        //生成服务合同报告书
        NewMaintenanceReport_Task__c tempNMCT = new NewMaintenanceReport_Task__c();
        tempNMCT.Name = 'FWreport-01';
        tempNMCT.Distribution_Start_Date__c = Date.today().addDays(-30);
        tempNMCT.Distribution_End_Date__c = Date.today().addMonths(2).addDays(-1);
        tempNMCT.NewMaintenance_Contract__c = contract.Id;
        tempNMCT.ActualDistributionTimes_Quarter__c = 0;
        insert tempNMCT;

        Date today = Date.today();
        List<NewMaintenanceReport_Task__c> NMCTList = [SELECT Id FROM NewMaintenanceReport_Task__c where Distribution_Start_Date__c <= :today AND  Distribution_End_Date__c >= :today AND Task__c = NULL];
        //System.assertEquals(0,NMCTList.size());

        System.Test.StartTest();
        Database.executeBatch(new CreateSLAReportTaskBatch(),1);
        System.Test.StopTest();

    }
}