@isTest
private class DigCasesNumberControllerTest {

    static testMethod void myUnitTest() {
        
        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', 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;
        // 创建客户
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectDp = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科' order by Name desc];
        if (rectDp.size() == 0) {
            return;
        }

        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科' order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
        Account hp1 = new Account(RecordTypeId = rectHp[0].Id,Is_Active__c = '有効' ,Name = 'testHp1', OCM_Category__c = 'H0', OwnerId = hpOwner.Id);
        insert hp1;

        Account dp1 = new Account(RecordTypeId = rectDp[0].Id, Is_Active__c = '有効' , Department_Class_Label__c = '消化科',
            ParentId  = hp1.id, Name = 'testdp1', OCM_Category__c = 'H0', OwnerId = hpOwner.Id);
        insert dp1;

        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp1.Id and Department_Class_Label__c  = '消化科' order by Department_Class_Label__c];
        
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = hp1.Id;
        depart1.Is_Active__c        = '有効';
        
        insert depart1;

        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[0].Id;
        depart2.Name         = '123';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc1s[0].Id;
        depart2.Department_Class__c = dc1s[0].Id;
        depart2.Hospital__c         = hp1.Id;
        depart2.Is_Active__c        = '有効';
        insert depart2;

        List<Account> queryList = new List<Account>();
        queryList.add(depart1);
        queryList.add(depart2);

        Account_Number_of_case__c ano = new Account_Number_of_case__c();
        ano.Account__c = dc1s[0].Id;
        ano.Account_HP__c = hp1.Id;
        ano.OCM_Year__c = '2020年度';
        ano.ESD_UpNumber__c = 1;
        ano.ESD_DownNumber__c = 1;
        ano.EUS_UpNumber__c = 1;
        ano.EUS_DownNumber__c = 1;
        ano.ERCP_UpNumber__c = 1;
        ano.ERCP_DownNumber__c = 1;
        insert ano;
        
        PageReference page = new PageReference('/apex/DigCasesNumber');
        System.Test.setCurrentPage(page);
        DigCasesNumberController controller = new DigCasesNumberController();
        // 初始化测试
        controller.init();
        
        controller.makeOppRecordsForView(queryList);
        //System.assertEquals(1, controller.oppRecords.size());
        
        // 排序测试
        controller.sortKey = '1';
        controller.sortTable();
        controller.sortTable();
        
        // 检索测试
        controller.text = 'S:Name';
        controller.condition = 'equals';
        controller.value = 'testHp1';
        controller.searchOpp();
        controller.condition = 'contains';
        controller.value = 'test 1';
        controller.searchOpp();
        controller.setUrlStr();
        
        //System.assertEquals(1, controller.oppRecords.size());
        
        // 保存测试
        //controller.oppRecords[0].changeFlg = '1';
        controller.save();
        
        // 返回测试
        controller.cancel();
    }
    
    static testMethod void myUnitTest2() {
        
        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', 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;
                // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectDp = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科' order by Name desc];
        if (rectDp.size() == 0) {
            return;
        }

        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科' order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
        Account hp1 = new Account(RecordTypeId = rectHp[0].Id,Is_Active__c = '有効' ,Name = 'testHp1', OCM_Category__c = 'H0', OwnerId = hpOwner.Id);
        insert hp1;

        Account dp1 = new Account(RecordTypeId = rectDp[0].Id, Is_Active__c = '有効' , Department_Class_Label__c = '消化科',
            ParentId  = hp1.id, Name = 'testdp1', OCM_Category__c = 'H0', OwnerId = hpOwner.Id);
        insert dp1;

        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp1.Id and Department_Class_Label__c  = '消化科' order by Department_Class_Label__c];
        
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = hp1.Id;
        depart1.Is_Active__c        = '有効';
        depart1.Ban_On_Use_Date__c  = Date.today().addDays(1);
        insert depart1;

       
                
        PageReference page = new PageReference('/apex/DigCasesNumber?uid=' + UserInfo.getUserId() + '&hpid=' + hp1.Id);
        System.Test.setCurrentPage(page);
        DigCasesNumberController controller = new DigCasesNumberController();
        // 初始化测试
        controller.init();
        //System.assertEquals(1, controller.oppRecords.size());
        
        // 排序测试
        controller.sortKey = '10';
        controller.sortTable();
        controller.sortTable();

        // 保存测试
        //controller.oppRecords[0].changeFlg = '1';
        controller.saveType = '1';
        controller.save();
        
        //controller.oppRecords[0].changeFlg = '1';
        
        controller.saveType = '2';

        controller.save();
        
        // 检索测试
        controller.text = 'S:Name';
        controller.condition = 'notequals';
        controller.value = '';
        controller.searchOpp();
        
        //controller.dateField = 'Ban_On_Use_Date__c';
        //controller.con1.Birthdate = Date.today();
        //controller.con2.Birthdate = Date.today().addDays(2);
        controller.text = 'S:Name';
        controller.condition = 'contains';
        controller.value = 'test 1';
        controller.searchOpp();
        controller.accTypeForSort = '01210000000QezZ';
        controller.sortOrder = null;
        controller.setLayoutRWInfo();
        
        //System.assertEquals(1, controller.oppRecords.size());
    }
}