@isTest
public with sharing class taskObjectTriggerHandlerTest {

    @testSetup
    static void makeTestRepair() {
        
    }

    //消化科
    static testMethod void testMethod1() {
        test.startTest();

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 消化科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }
        User u = [SELECT id,Alias FROM User WHERE Id = :UserInfo.getUserId()];
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM105TestCompany';
        company.OwnerId      = UserInfo.getUserId();
        company.GI_Main__c = UserInfo.getUserId();
        company.GI_Product_Leader__c = u.Alias +','+u.Alias;
        upsert company;
        Account section = [Select Management_Code__c, Management_Code_Auto__c, Name, Id,Department_Class_Label__c from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];


        Account depart = new Account();
        depart.RecordTypeId        = rectDpt[0].Id;
        depart.Name                = '*';
        depart.Department_Name__c  = 'NFM105TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        depart.OwnerId      = UserInfo.getUserId();
        upsert depart;

        Opportunity opp1 = new Opportunity(Name='aiueo1', StageName='引合', CurrencyIsoCode='CNY', CloseDate=Date.today());
        insert opp1;
        // ------------ 
        
        task__c tsk = new task__c();
        tsk.name = '*';
        tsk.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('OPD').getRecordTypeId();
        tsk.distributionCount__c = 1;
        tsk.taskDifferent__c = '被动任务';
        tsk.taskStatus__c = '01 分配';
        tsk.account__c = depart.id;
        tsk.assignee__c =  UserInfo.getUserId();
        tsk.OpportunityId__c = opp1.Id;
        insert tsk;

        tsk.taskStatus__c = '02 接受';
        update tsk;

        task__c tsk2 = new task__c();
        tsk2.name = '*';
        tsk2.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('OPD').getRecordTypeId();
        tsk2.distributionCount__c = 1;
        tsk2.taskDifferent__c = '上级分配任务';
        tsk2.taskStatus__c = '01 分配';
        tsk2.account__c = depart.id;
        tsk2.assignee__c =  UserInfo.getUserId();
        insert tsk2;

        task__c tsk3 = new task__c();
        tsk3.name = '123';
        tsk3.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('Other').getRecordTypeId();
        tsk3.distributionCount__c = 1;
        tsk3.taskDifferent__c = '上级分配任务';
        tsk3.taskStatus__c = '01 分配';
        tsk3.assignee__c =  UserInfo.getUserId();
        tsk3.OpportunityId__c = opp1.Id;
        insert tsk3;
        test.stopTest();
    }

    //呼吸科
    static testMethod void testMethod2() {
        test.startTest();

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 呼吸科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 呼吸科 recodetype');
        }
        User u = [SELECT id,Alias FROM User WHERE Id = :UserInfo.getUserId()];
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM105TestCompany';
        company.OwnerId      = UserInfo.getUserId();
        company.BF_owner__c = UserInfo.getUserId();
        company.BF_Product_Leader__c = u.Alias +','+u.Alias;
        upsert company;
        Account section = [Select Management_Code__c, Management_Code_Auto__c, Name, Id,Department_Class_Label__c from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];


        Account depart = new Account();
        depart.RecordTypeId        = rectDpt[0].Id;
        depart.Name                = '*';
        depart.Department_Name__c  = 'NFM105TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        depart.OwnerId      = UserInfo.getUserId();
        upsert depart;

        // ------------ 
        
        task__c tsk = new task__c();
        tsk.name = '*';
        tsk.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('OPD').getRecordTypeId();
        tsk.distributionCount__c = 1;
        tsk.taskDifferent__c = '被动任务';
        tsk.taskStatus__c = '01 分配';
        tsk.account__c = depart.id;
        tsk.assignee__c =  UserInfo.getUserId();
        insert tsk;

        tsk.taskStatus__c = '02 接受';
        update tsk;

        task__c tsk2 = new task__c();
        tsk2.name = '*';
        tsk2.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('OPD').getRecordTypeId();
        tsk2.distributionCount__c = 1;
        tsk2.taskDifferent__c = '上级分配任务';
        tsk2.taskStatus__c = '01 分配';
        tsk2.account__c = depart.id;
        tsk2.assignee__c =  UserInfo.getUserId();
        insert tsk2;
        test.stopTest();
    }

    //普外科
    static testMethod void testMethod3() {
        test.startTest();

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 普外科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 普外科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 普外科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 普外科 recodetype');
        }
        User u = [SELECT id,Alias FROM User WHERE Id = :UserInfo.getUserId()];
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM105TestCompany';
        company.OwnerId      = UserInfo.getUserId();
        company.SP_Main__c = UserInfo.getUserId();
        company.GS_Product_Leader__c = u.Alias +','+u.Alias;
        upsert company;
        Account section = [Select Management_Code__c, Management_Code_Auto__c, Name, Id,Department_Class_Label__c from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];


        Account depart = new Account();
        depart.RecordTypeId        = rectDpt[0].Id;
        depart.Name                = '*';
        depart.Department_Name__c  = 'NFM105TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        depart.OwnerId      = UserInfo.getUserId();
        upsert depart;

        // ------------ 
        
        task__c tsk = new task__c();
        tsk.name = '*';
        tsk.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('OPD').getRecordTypeId();
        tsk.distributionCount__c = 1;
        tsk.taskDifferent__c = '被动任务';
        tsk.taskStatus__c = '01 分配';
        tsk.account__c = depart.id;
        tsk.assignee__c =  UserInfo.getUserId();
        insert tsk;

        tsk.taskStatus__c = '02 接受';
        update tsk;

        task__c tsk2 = new task__c();
        tsk2.name = '*';
        tsk2.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('OPD').getRecordTypeId();
        tsk2.distributionCount__c = 1;
        tsk2.taskDifferent__c = '上级分配任务';
        tsk2.taskStatus__c = '01 分配';
        tsk2.account__c = depart.id;
        tsk2.assignee__c =  UserInfo.getUserId();
        insert tsk2;
        test.stopTest();
    }

     //泌尿科
     static testMethod void testMethod4() {
        test.startTest();

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 泌尿科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 泌尿科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 泌尿科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 泌尿科 recodetype');
        }
        User u = [SELECT id,Alias FROM User WHERE Id = :UserInfo.getUserId()];
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM105TestCompany';
        company.OwnerId      = UserInfo.getUserId();
        company.URO_owner_ID__c = UserInfo.getUserId();
        company.URO_Produc_Leader__c = u.Alias +','+u.Alias;
        upsert company;
        Account section = [Select Management_Code__c, Management_Code_Auto__c, Name, Id,Department_Class_Label__c from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];


        Account depart = new Account();
        depart.RecordTypeId        = rectDpt[0].Id;
        depart.Name                = '*';
        depart.Department_Name__c  = 'NFM105TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        depart.OwnerId      = UserInfo.getUserId();
        upsert depart;

        // ------------ 
        
        task__c tsk = new task__c();
        tsk.name = '*';
        tsk.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('OPD').getRecordTypeId();
        tsk.distributionCount__c = 1;
        tsk.taskDifferent__c = '被动任务';
        tsk.taskStatus__c = '01 分配';
        tsk.account__c = depart.id;
        tsk.assignee__c =  UserInfo.getUserId();
        insert tsk;

        tsk.taskStatus__c = '02 接受';
        update tsk;

        task__c tsk2 = new task__c();
        tsk2.name = '*';
        tsk2.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('OPD').getRecordTypeId();
        tsk2.distributionCount__c = 1;
        tsk2.taskDifferent__c = '上级分配任务';
        tsk2.taskStatus__c = '01 分配';
        tsk2.account__c = depart.id;
        tsk2.assignee__c =  UserInfo.getUserId();
        insert tsk2;
        test.stopTest();
    }

    //婦人科
    static testMethod void testMethod5() {
        test.startTest();

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 婦人科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 婦人科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 婦人科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 婦人科 recodetype');
        }
        User u = [SELECT id,Alias FROM User WHERE Id = :UserInfo.getUserId()];
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM105TestCompany';
        company.OwnerId      = UserInfo.getUserId();
        company.GYN_owner__c = UserInfo.getUserId();
        company.GYN_Product_Leader__c = u.Alias +','+u.Alias;
        upsert company;
        Account section = [Select Management_Code__c, Management_Code_Auto__c, Name, Id,Department_Class_Label__c from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];


        Account depart = new Account();
        depart.RecordTypeId        = rectDpt[0].Id;
        depart.Name                = '*';
        depart.Department_Name__c  = 'NFM105TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        depart.OwnerId      = UserInfo.getUserId();
        upsert depart;

        // ------------ 
        
        task__c tsk = new task__c();
        tsk.name = '*';
        tsk.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('OPD').getRecordTypeId();
        tsk.distributionCount__c = 1;
        tsk.taskDifferent__c = '被动任务';
        tsk.taskStatus__c = '01 分配';
        tsk.account__c = depart.id;
        tsk.assignee__c =  UserInfo.getUserId();
        insert tsk;

        tsk.taskStatus__c = '02 接受';
        update tsk;

        task__c tsk2 = new task__c();
        tsk2.name = '*';
        tsk2.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('OPD').getRecordTypeId();
        tsk2.distributionCount__c = 1;
        tsk2.taskDifferent__c = '上级分配任务';
        tsk2.taskStatus__c = '01 分配';
        tsk2.account__c = depart.id;
        tsk2.assignee__c =  UserInfo.getUserId();
        insert tsk2;
        test.stopTest();
    }

     //耳鼻喉科
     static testMethod void testMethod6() {
        test.startTest();

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 耳鼻喉科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 耳鼻喉科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 耳鼻喉科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 耳鼻喉科 recodetype');
        }
        User u = [SELECT id,Alias FROM User WHERE Id = :UserInfo.getUserId()];
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM105TestCompany';
        company.OwnerId      = UserInfo.getUserId();
        company.ENT_owner_ID__c = UserInfo.getUserId();
        company.ENT_Product_Leader__c = u.Alias +','+u.Alias;
        upsert company;
        Account section = [Select Management_Code__c, Management_Code_Auto__c, Name, Id,Department_Class_Label__c from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];


        Account depart = new Account();
        depart.RecordTypeId        = rectDpt[0].Id;
        depart.Name                = '*';
        depart.Department_Name__c  = 'NFM105TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        depart.OwnerId      = UserInfo.getUserId();
        upsert depart;

        // ------------ 
        
        task__c tsk = new task__c();
        tsk.name = '*';
        tsk.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('OPD').getRecordTypeId();
        tsk.distributionCount__c = 1;
        tsk.taskDifferent__c = '被动任务';
        tsk.taskStatus__c = '01 分配';
        tsk.account__c = depart.id;
        tsk.assignee__c =  UserInfo.getUserId();
        insert tsk;

        tsk.taskStatus__c = '02 接受';
        update tsk;

        task__c tsk2 = new task__c();
        tsk2.name = '*';
        tsk2.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('OPD').getRecordTypeId();
        tsk2.distributionCount__c = 1;
        tsk2.taskDifferent__c = '上级分配任务';
        tsk2.taskStatus__c = '01 分配';
        tsk2.account__c = depart.id;
        tsk2.assignee__c =  UserInfo.getUserId();
        insert tsk2;
        test.stopTest();
    }    
}