@isTest
private class OpdAmountBatchTest {

    static testMethod void myUnitTest1() {
         ControllerUtil.EscapeNFM001Trigger = true;
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM007TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM007TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;

        List<Account2__c> acc2List = [select id from Account2__c where Account_org__c = :section.Id];
        Account2__c acc2 = acc2List[0];
        acc2.OPD_290Miro_BF__c = 0;
        acc2.OPD_290Miro_GI__c = 0;
        acc2.OPD_3D__c = 1;
        acc2.OPD_CV_170__c = 2;
        acc2.OPD_CV_190__c = 3;
        acc2.OPD_CV_290__c = 4;
        acc2.OPD_EU_ME2__c = 5;
        acc2.OPD_OTV_S190__c = 6;
        acc2.OPD_STMS__c = 7;
        acc2.OPD_GW__c = 8;
        update acc2;

        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new OpdAmountBatch(), 10);
        System.Test.StopTest();

        List<Account_Number_of_target__c> atList = [select id,
                                                           OPD_amount_290Miro__c,
                                                           OPD_amount_3D__c,
                                                           OPD_amount_CV170__c,
                                                           OPD_amount_CV190__c,
                                                           OPD_amount_CV290__c,
                                                           OPD_amount_EUME2__c,
                                                           OPD_amount_OTVS190__c,
                                                           OPD_amount_USG400__c,
                                                           OPD_amount_GW__c
                                                      from Account_Number_of_target__c
                                                     where Account__c = :section.Id];
        //System.assertEquals(0, atList[0].OPD_amount_290Miro__c);
        //System.assertEquals(1, atList[0].OPD_amount_3D__c);
        //System.assertEquals(2, atList[0].OPD_amount_CV170__c);
        //System.assertEquals(3, atList[0].OPD_amount_CV190__c);
        //System.assertEquals(4, atList[0].OPD_amount_CV290__c);
        //System.assertEquals(5, atList[0].OPD_amount_EUME2__c);
        //System.assertEquals(6, atList[0].OPD_amount_OTVS190__c);
        //System.assertEquals(7, atList[0].OPD_amount_USG400__c);
        //System.assertEquals(8, atList[0].OPD_amount_GW__c);

        execBTId = Database.executeBatch(new OpdAmountBatch(), 10);
    }
    static testMethod void myUnitTest2() {
         ControllerUtil.EscapeNFM001Trigger = true;
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM007TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM007TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;

        List<Account2__c> acc2List = [select id from Account2__c where Account_org__c = :section.Id];
        Account2__c acc2 = acc2List[0];
        acc2.OPD_290Miro_BF__c = 0;
        acc2.OPD_290Miro_GI__c = 0;
        acc2.OPD_3D__c = 1;
        acc2.OPD_CV_170__c = 2;
        acc2.OPD_CV_190__c = 3;
        acc2.OPD_CV_290__c = 4;
        acc2.OPD_EU_ME2__c = 5;
        acc2.OPD_OTV_S190__c = 6;
        acc2.OPD_STMS__c = 7;
        acc2.OPD_GW__c = 8;
        update acc2;

        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new OpdAmountBatch(), 10);
        System.Test.StopTest();

        List<Account_Number_of_target__c> atList = [select id,
                                                           OPD_amount_290Miro__c,
                                                           OPD_amount_3D__c,
                                                           OPD_amount_CV170__c,
                                                           OPD_amount_CV190__c,
                                                           OPD_amount_CV290__c,
                                                           OPD_amount_EUME2__c,
                                                           OPD_amount_OTVS190__c,
                                                           OPD_amount_USG400__c,
                                                           OPD_amount_GW__c
                                                      from Account_Number_of_target__c
                                                     where Account__c = :section.Id];
        //System.assertEquals(0, atList[0].OPD_amount_290Miro__c);
        //System.assertEquals(1, atList[0].OPD_amount_3D__c);
        //System.assertEquals(2, atList[0].OPD_amount_CV170__c);
        //System.assertEquals(3, atList[0].OPD_amount_CV190__c);
        //System.assertEquals(4, atList[0].OPD_amount_CV290__c);
        //System.assertEquals(5, atList[0].OPD_amount_EUME2__c);
        //System.assertEquals(6, atList[0].OPD_amount_OTVS190__c);
        //System.assertEquals(7, atList[0].OPD_amount_USG400__c);
        //System.assertEquals(8, atList[0].OPD_amount_GW__c);

        execBTId = Database.executeBatch(new OpdAmountBatch(true), 10);
    }
    static testMethod void myUnitTest3() {
         ControllerUtil.EscapeNFM001Trigger = true;
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM007TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM007TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;

        List<Account2__c> acc2List = [select id from Account2__c where Account_org__c = :section.Id];
        Account2__c acc2 = acc2List[0];
        acc2.OPD_290Miro_BF__c = 0;
        acc2.OPD_290Miro_GI__c = 0;
        acc2.OPD_3D__c = 1;
        acc2.OPD_CV_170__c = 2;
        acc2.OPD_CV_190__c = 3;
        acc2.OPD_CV_290__c = 4;
        acc2.OPD_EU_ME2__c = 5;
        acc2.OPD_OTV_S190__c = 6;
        acc2.OPD_STMS__c = 7;
        acc2.OPD_GW__c = 8;
        update acc2;

        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new OpdAmountBatch(), 10);
        System.Test.StopTest();

        List<Account_Number_of_target__c> atList = [select id,
                                                           OPD_amount_290Miro__c,
                                                           OPD_amount_3D__c,
                                                           OPD_amount_CV170__c,
                                                           OPD_amount_CV190__c,
                                                           OPD_amount_CV290__c,
                                                           OPD_amount_EUME2__c,
                                                           OPD_amount_OTVS190__c,
                                                           OPD_amount_USG400__c,
                                                           OPD_amount_GW__c
                                                      from Account_Number_of_target__c
                                                     where Account__c = :section.Id];
        //System.assertEquals(0, atList[0].OPD_amount_290Miro__c);
        //System.assertEquals(1, atList[0].OPD_amount_3D__c);
        //System.assertEquals(2, atList[0].OPD_amount_CV170__c);
        //System.assertEquals(3, atList[0].OPD_amount_CV190__c);
        //System.assertEquals(4, atList[0].OPD_amount_CV290__c);
        //System.assertEquals(5, atList[0].OPD_amount_EUME2__c);
        //System.assertEquals(6, atList[0].OPD_amount_OTVS190__c);
        //System.assertEquals(7, atList[0].OPD_amount_USG400__c);
        //System.assertEquals(8, atList[0].OPD_amount_GW__c);

        execBTId = Database.executeBatch(new OpdAmountBatch(company.Id), 10);
    }
    static testMethod void caseTest() {
        ControllerUtil.EscapeNFM001Trigger = true;
        /*Boolean ManuallyUpdateFlag = true;
        Date today = date.today();
        Integer year = today.year();
        Date startDate = date.newInstance(year, 1, 4);
        Date endDate  = date.newInstance(year, 2, 1);
        String todayStr = NFMUtil.formatDate2Str(today);
        String startDateStr = NFMUtil.formatDate2Str(startDate);
        String endDateStr = NFMUtil.formatDate2Str(endDate);*/
        String PerformDate = '2020年度';
        List<RecordType> caseCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (caseCo.size() == 0) {
            return;
        }
        List<RecordType> caseSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (caseSct.size() == 0) {
            return;
        }
        
        Account company = new Account();
        company.RecordTypeId    = caseCo[0].Id;
        company.Name            = '医院';
        company.OCM_Category__c = 'H0';
        company.Is_Active__c    = '有效';
        upsert company;

        Account section = new Account();
        section.RecordTypeId = caseSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c    = '呼吸科';
        section.ParentId                     = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;

        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new OpdAmountBatch(company.Id,PerformDate), 20);
        System.Test.StopTest();

        List<Account_Number_of_case__c> atList = [select id,
                                                         OCM_Year__c,
                                                         OCM_Period__c,
                                                         Account_HP__c,
                                                         RecordTypeid,
                                                         Case_name__c
                                                      from Account_Number_of_case__c
                                                     where Account__c = :section.Id];
        System.debug('atList:'+atList);
        //execBTId = Database.executeBatch(new OpdAmountBatch(company.Id), 20);
    }

    static testMethod void caseTest2() {
        ControllerUtil.EscapeNFM001Trigger = true;
        /*Boolean ManuallyUpdateFlag = true;
        Date today = date.today();
        Integer year = today.year();
        Date startDate = date.newInstance(year, 1, 4);
        Date endDate  = date.newInstance(year, 2, 1);
        String todayStr = NFMUtil.formatDate2Str(today);
        String startDateStr = NFMUtil.formatDate2Str(startDate);
        String endDateStr = NFMUtil.formatDate2Str(endDate);*/
        String PerformDate = '2020年度';
        List<RecordType> caseCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (caseCo.size() == 0) {
            return;
        }
        List<RecordType> caseSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (caseSct.size() == 0) {
            return;
        }
        
        Account company = new Account();
        company.RecordTypeId    = caseCo[0].Id;
        company.Name            = '医院';
        company.OCM_Category__c = 'H0';
        company.Is_Active__c    = '有效';
        upsert company;

        Account section = new Account();
        section.RecordTypeId = caseSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c    = '呼吸科';
        section.ParentId                     = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;

        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new OpdAmountBatch(company.Id,PerformDate), 20);
        System.Test.StopTest();

        List<Account_Number_of_case__c> atList = [select id,
                                                         OCM_Year__c,
                                                         OCM_Period__c,
                                                         Account_HP__c,
                                                         RecordTypeid,
                                                         Case_name__c
                                                      from Account_Number_of_case__c
                                                     where Account__c = :section.Id];
        System.debug('atList:'+atList);
        execBTId = Database.executeBatch(new OpdAmountBatch(true,true), 20);
    }

}