/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class OppMonthlyRatingBatchTest {

    static testMethod void myUnitTest() {
        // recode type を取得
        //病院
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'] ;
        if (rectCo.size() == 0) {
            return;
        }
        
        //戦略科室分類
        RecordType rectSct1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 普外科'];
        RecordType rectSct2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        
        //科室分類
        RecordType rectDpt1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 普外科'];
        RecordType rectDpt2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科'];
        
        //商談のレコードタイプ
        RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity' ];
        
         Address_Level__c al1 = new Address_Level__c(
            Name = '四川省'
        );
        insert al1;
        Address_Level2__c al2 = new Address_Level2__c(
            Name = '成都市',
            Address_Level__c = al1.Id
        );
        insert al2;
        

        // insert
        Account company1 = new Account();
        Account company2 = new Account();
        
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = 'HPテスト1';
        company1.State_Master__c = al1.Id;
        company1.City_Master__c = al2.Id;
        
        company2.RecordTypeId = rectCo[0].Id;
        company2.Name = 'HPテスト2';
        company2.State_Master__c = al1.Id;
        company2.City_Master__c = al2.Id;
        List<Account> hps = new Account[] {company1, company2};
        insert hps;
  
        Account dc1= [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id and RecordTypeId =: rectSct1.Id] ;
        Account dc2= [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id and RecordTypeId =: rectSct2.Id] ;
        
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt1.Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1.Id;
        depart1.Department_Class__c = dc1.Id;
        depart1.Hospital__c         = company1.Id;
        
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt2.Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc2.Id;
        depart2.Department_Class__c = dc2.Id;
        depart2.Hospital__c         = company2.Id;

        insert new Account[] {depart1, depart2};
        
        Date prevDate = Date.today().addMonths(-1);
        Date prevDate2 = Date.today().addMonths(-2);
        
        Opportunity opp1 = new Opportunity(
            Name='testOpp1',
            StageName='引合',
            CloseDate=Date.today(),
            AccountId=depart1.Id,
            Competitor__c ='A',
            Click_Close_Date__c = null,
            RecordType = rectOpp
        );
        Opportunity opp2 = new Opportunity(
            Name='testOpp2',
            StageName='引合',
            CloseDate=Date.today(),
            AccountId=depart2.Id,
            Competitor__c ='B',
            Click_Close_Date__c = null,
            RecordType = rectOpp
        );
        Opportunity opp3 = new Opportunity(
            Name='testOpp3',
            StageName='引合',
            CloseDate=Date.today(),
            AccountId=depart1.Id,
            Competitor__c ='C',
            Click_Close_Date__c = Date.today().addMonths(-5),
            RecordType = rectOpp
        );
        Opportunity opp4 = new Opportunity(
            Name='testOpp4',
            StageName='引合',
            CloseDate=Date.today(),
            AccountId=depart1.Id,
            Competitor__c ='C',
            RecordType = rectOpp
        );
        
        
        insert new Opportunity[] {opp1, opp2, opp3, opp4};
        
        opp4.StageName = '完了';
        update opp4;
        
        
        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new OppMonthlyRatingBatch(), 4);
        System.Test.StopTest();
        
        
        Date prevYMD = Date.newInstance(prevDate.year(), prevDate.month(), 1);
        List<Opportunity> oppList = [select Id from Opportunity where RecordType.DeveloperName in :ControllerUtil.oppRecordTypeDevNames
                                         and (Click_Close_Date__c = null or Click_Close_Date__c >= :prevYMD)
                                         and (id = :opp1.Id) ];
                                         
        system.assertEquals(1, oppList.size());
        Integer thisMonth = Date.today().month();
        opp1 = [select Rating__c, Rating01__c,Rating02__c,Rating03__c,Rating04__c,Rating05__c,Rating06__c,Rating07__c,Rating08__c,Rating09__c,Rating10__c,Rating11__c,Rating12__c
                from Opportunity where id = :opp1.Id];
        if (thisMonth == 1) {
            system.assertEquals(opp1.Rating__c, opp1.Rating12__c);
            system.assertEquals(null, opp1.Rating01__c);
        } else if (thisMonth == 2) {
            system.assertEquals(opp1.Rating__c, opp1.Rating01__c);
            system.assertEquals(null, opp1.Rating02__c);
        } else if (thisMonth == 3) {
            system.assertEquals(opp1.Rating__c, opp1.Rating02__c);
            system.assertEquals(null, opp1.Rating03__c);
        } else if (thisMonth == 4) {
            system.assertEquals(opp1.Rating__c, opp1.Rating03__c);
            system.assertEquals(null, opp1.Rating04__c);
        } else if (thisMonth == 5) {
            system.assertEquals(opp1.Rating__c, opp1.Rating04__c);
            system.assertEquals(null, opp1.Rating05__c);
        } else if (thisMonth == 6) {
            system.assertEquals(opp1.Rating__c, opp1.Rating05__c);
            system.assertEquals(null, opp1.Rating06__c);
        } else if (thisMonth == 7) {
            system.assertEquals(opp1.Rating__c, opp1.Rating06__c);
            system.assertEquals(null, opp1.Rating07__c);
        } else if (thisMonth == 8) {
            system.assertEquals(opp1.Rating__c, opp1.Rating07__c);
            system.assertEquals(null, opp1.Rating08__c);
        } else if (thisMonth == 9) {
            system.assertEquals(opp1.Rating__c, opp1.Rating08__c);
            system.assertEquals(null, opp1.Rating09__c);
        } else if (thisMonth == 10) {
            system.assertEquals(opp1.Rating__c, opp1.Rating09__c);
            system.assertEquals(null, opp1.Rating10__c);
        } else if (thisMonth == 11) {
            system.assertEquals(opp1.Rating__c, opp1.Rating10__c);
            system.assertEquals(null, opp1.Rating11__c);
        } else if (thisMonth == 12) {
            system.assertEquals(opp1.Rating__c, opp1.Rating11__c);
            system.assertEquals(null, opp1.Rating12__c);
        } else {}
        
        opp2 = [select Rating__c, Rating01__c,Rating02__c,Rating03__c,Rating04__c,Rating05__c,Rating06__c,Rating07__c,Rating08__c,Rating09__c,Rating10__c,Rating11__c,Rating12__c
                from Opportunity where id = :opp2.Id];
        if (thisMonth == 1) {
            system.assertEquals(opp2.Rating__c, opp2.Rating12__c);
            system.assertEquals(null, opp2.Rating01__c);
        } else if (thisMonth == 2) {
            system.assertEquals(opp2.Rating__c, opp2.Rating01__c);
            system.assertEquals(null, opp2.Rating02__c);
        } else if (thisMonth == 3) {
            system.assertEquals(opp2.Rating__c, opp2.Rating02__c);
            system.assertEquals(null, opp2.Rating03__c);
        } else if (thisMonth == 4) {
            system.assertEquals(opp2.Rating__c, opp2.Rating03__c);
            system.assertEquals(null, opp2.Rating04__c);
        } else if (thisMonth == 5) {
            system.assertEquals(opp2.Rating__c, opp2.Rating04__c);
            system.assertEquals(null, opp2.Rating05__c);
        } else if (thisMonth == 6) {
            system.assertEquals(opp2.Rating__c, opp2.Rating05__c);
            system.assertEquals(null, opp2.Rating06__c);
        } else if (thisMonth == 7) {
            system.assertEquals(opp2.Rating__c, opp2.Rating06__c);
            system.assertEquals(null, opp2.Rating07__c);
        } else if (thisMonth == 8) {
            system.assertEquals(opp2.Rating__c, opp2.Rating07__c);
            system.assertEquals(null, opp2.Rating08__c);
        } else if (thisMonth == 9) {
            system.assertEquals(opp2.Rating__c, opp2.Rating08__c);
            system.assertEquals(null, opp2.Rating09__c);
        } else if (thisMonth == 10) {
            system.assertEquals(opp2.Rating__c, opp2.Rating09__c);
            system.assertEquals(null, opp2.Rating10__c);
        } else if (thisMonth == 11) {
            system.assertEquals(opp2.Rating__c, opp2.Rating10__c);
            system.assertEquals(null, opp2.Rating11__c);
        } else if (thisMonth == 12) {
            system.assertEquals(opp2.Rating__c, opp2.Rating11__c);
            system.assertEquals(null, opp2.Rating12__c);
        } else {}
        
        opp3 = [select Rating__c, Rating01__c,Rating02__c,Rating03__c,Rating04__c,Rating05__c,Rating06__c,Rating07__c,Rating08__c,Rating09__c,Rating10__c,Rating11__c,Rating12__c
                from Opportunity where id = :opp3.Id];
        system.assertEquals(null, opp3.Rating01__c);
        system.assertEquals(null, opp3.Rating02__c);
        system.assertEquals(null, opp3.Rating03__c);
        system.assertEquals(null, opp3.Rating04__c);
        system.assertEquals(null, opp3.Rating05__c);
        system.assertEquals(null, opp3.Rating06__c);
        system.assertEquals(null, opp3.Rating07__c);
        system.assertEquals(null, opp3.Rating08__c);
        system.assertEquals(null, opp3.Rating09__c);
        system.assertEquals(null, opp3.Rating10__c);
        system.assertEquals(null, opp3.Rating11__c);
        system.assertEquals(null, opp3.Rating12__c);
        
        opp4 = [select Rating__c, Rating01__c,Rating02__c,Rating03__c,Rating04__c,Rating05__c,Rating06__c,Rating07__c,Rating08__c,Rating09__c,Rating10__c,Rating11__c,Rating12__c,StageName,Click_Close_Date__c
                from Opportunity where id = :opp4.Id];
                
        system.assertEquals('完了', opp4.StageName);        
        system.assertEquals(Date.today(), opp4.Click_Close_Date__c);
        if (thisMonth == 1) {
            system.assertEquals(opp4.Rating__c, opp4.Rating12__c);
            system.assertEquals(null, opp4.Rating01__c);
        } else if (thisMonth == 2) {
            system.assertEquals(opp4.Rating__c, opp4.Rating01__c);
            system.assertEquals(null, opp4.Rating02__c);
        } else if (thisMonth == 3) {
            system.assertEquals(opp4.Rating__c, opp4.Rating02__c);
            system.assertEquals(null, opp4.Rating03__c);
        } else if (thisMonth == 4) {
            system.assertEquals(opp4.Rating__c, opp4.Rating03__c);
            system.assertEquals(null, opp4.Rating04__c);
        } else if (thisMonth == 5) {
            system.assertEquals(opp4.Rating__c, opp4.Rating04__c);
            system.assertEquals(null, opp4.Rating05__c);
        } else if (thisMonth == 6) {
            system.assertEquals(opp4.Rating__c, opp4.Rating05__c);
            system.assertEquals(null, opp4.Rating06__c);
        } else if (thisMonth == 7) {
            system.assertEquals(opp4.Rating__c, opp4.Rating06__c);
            system.assertEquals(null, opp4.Rating07__c);
        } else if (thisMonth == 8) {
            system.assertEquals(opp4.Rating__c, opp4.Rating07__c);
            system.assertEquals(null, opp4.Rating08__c);
        } else if (thisMonth == 9) {
            system.assertEquals(opp4.Rating__c, opp4.Rating08__c);
            system.assertEquals(null, opp4.Rating09__c);
        } else if (thisMonth == 10) {
            system.assertEquals(opp4.Rating__c, opp4.Rating09__c);
            system.assertEquals(null, opp4.Rating10__c);
        } else if (thisMonth == 11) {
            system.assertEquals(opp4.Rating__c, opp4.Rating10__c);
            system.assertEquals(null, opp4.Rating11__c);
        } else if (thisMonth == 12) {
            system.assertEquals(opp4.Rating__c, opp4.Rating11__c);
            system.assertEquals(null, opp4.Rating12__c);
        } else {}
    }

}