@isTest
private class ControllerUtilEventTest {
    @TestSetup
    static void setup(){
        Oly_TriggerHandler.bypass('ContactTriggerHandler');
        Oly_TriggerHandler.bypass('PowerBIBaseHandler');
        Oly_TriggerHandler.bypass('AgencyOppUpdHandler');
        Oly_TriggerHandler.bypass('UpdateContractAimAmountHandler');
        Oly_TriggerHandler.bypass('NFM701ControllerHandler');
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        ControllerUtil.EscapeNFM001Trigger = true;
        Account account1 = new Account();
        Account account2 = new Account();
        Contact contact1 = new Contact();
        Contact contact2 = new Contact();
        List<RecordType> HPrectCo = [select Id, Name, DeveloperName from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        List<RecordType> GIrectCo = [select Id, Name, DeveloperName from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_Class_GI'];
        List<RecordType> DoctorrectCo = [select Id, Name, DeveloperName from RecordType where IsActive = true and SobjectType = 'Contact' and DeveloperName = 'Doctor'];
        if (HPrectCo.size() == 0) {
            throw new ControllerUtil.myException('not found HP recordtype');
        }
        if (GIrectCo.size() == 0) {
            throw new ControllerUtil.myException('not found Department_Class_GI recordtype');
        }
        if (DoctorrectCo.size() == 0) {
            throw new ControllerUtil.myException('not found Doctor recordtype');
        }
		// 取引先
        account1.Name = 'test1医院';
        account1.RecordTypeId = HPrectCo[0].Id;
        insert account1;
        // 取引先責任者
        contact1.AccountId = account1.Id;
        contact1.FirstName = '責任者';
        contact1.LastName = 'test1医院';
        Account dept = [select Id from Account where RecordTypeId =: GIrectCo[0].Id];
        contact1.Strategic_dept_Class__c = dept.Id;
        contact1.MobilePhone = '13409507069';
        contact1.RecordTypeId = DoctorrectCo[0].Id;
        contact1.UnifiedI_Contact_ID__c = '100000';
        contact1.AWS_Data_Id__c = '128940237854731';
        insert contact1;

        contact2.FirstName = 'test';
        contact2.LastName = 'Data';
        contact2.RecordTypeId = DoctorrectCo[0].Id;
        contact2.AccountId = account1.Id;
        contact2.Agency_User__c = true;
        contact2.MobilePhone = '18999999999';
        contact2.AWS_Data_Id__c = '128940237854732';
        insert contact2;
    } 
    
    @isTest
    static void Test1(){
        List<Contact> contacts = [select Id from Contact];
        String field = 'Contact.Name';
        List<String> fields = new List<String>();
        fields.add(field);
        Test.startTest();
        ControllerUtilEvent.getRecordForContact(contacts[0].Id, fields);
        ControllerUtilEvent.getRecordForLwc(contacts[0].Id, fields);
    }

    @isTest
    static void Test2() {
        Oly_TriggerHandler.bypass('PowerBIBaseHandler');
        Oly_TriggerHandler.bypass('AgencyOppUpdHandler');
        Oly_TriggerHandler.bypass('UpdateContractAimAmountHandler');
        Oly_TriggerHandler.bypass('NFM701ControllerHandler');
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        ControllerUtil.EscapeNFM001Trigger = true;
       
        Profile pf = [select Id from Profile where Id =:system.label.ProfileId_SystemAdmin];
        List<String> nameList = new List<String>();
        nameList.add('HP');
        nameList.add('Department_Class_ENT');
        nameList.add('Department_ENT');
        nameList.add('Doctor');
        List<RecordType> rtList = [select id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName in :nameList];
        RecordType rtHP;
        RecordType rtDepClass;
        RecordType rtDep;
        for (RecordType rt : rtList) 
        {
            if (rt.DeveloperName == 'HP') {
                rtHP = rt;
            } else if (rt.DeveloperName == 'Department_Class_ENT') {
                rtDepClass = rt;
            } else if (rt.DeveloperName == 'Department_ENT') {
                rtDep = rt;
            }
        }
        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '東京';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;
        // 病院を作る
        Account hospital = new Account();

        String rid = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        hospital.recordtypeId = rid;
        hospital.Is_Active__c = '有効';
        insert hospital;

        Account accHP = new Account();
        accHP.Name = '病院1';
        accHP.Grade__c = '一般';
        accHP.OCM_Category__c = '一般';
        accHP.Attribute_Type__c = '保険省';
        accHP.Speciality_Type__c = '総合病院';
        accHP.Is_Active__c = '有効';
        accHP.State_Master__c = al.id;
        accHP.City_Master__c = al2.id;
        accHP.RecordTypeId = rid;
        accHP.Valid_To__c = Date.today() + 2;
        insert accHP;

        // 戦略科室を得る
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
        // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;
        Account accDepClass = new Account();
        accDepClass.Name = '戦略科室分類1';
        accDepClass.Department_Class_Label__c = '耳鼻喉科';
        accDepClass.Hospital_Department_Class__c = accHP.id;
        accDepClass.ParentId = accHP.id;
        accDepClass.RecordTypeId = rtDepClass.id;
        insert accDepClass;
        Account accDep = new Account();
        accDep.Name = '診療科1';
        accDep.Department_Class_Label__c = '診療科1';
        accDep.Hospital__c = accHP.id;
        accDep.ParentId = accDepClass.id;
        accDep.Department_Class__c = accDepClass.id;
        accDep.Department_Name__c = '診療科1';
        accDep.CurrencyIsoCode = 'CNY';
        accDep.RecordTypeId = rtDep.id;
        insert accDep;

        Test.startTest();

        RecordType Type = [select id from RecordType where DeveloperName='SI_Oppor'];
        String TypeID = Type.Id;
        Opportunity opp1 = new Opportunity();
        opp1.Name = '引合1';
        opp1.AccountId = accDep.Id;
        opp1.Opportunity_Category__c = 'ENT';
        opp1.Trade__c = '外貿';
        opp1.StageName = '引合';
        opp1.CurrencyIsoCode = 'CNY';
        opp1.Close_Forecasted_Date__c = date.today().addMonths(1);
        opp1.CloseDate = date.today().addMonths(1);
        opp1.Competitor__c = 'B';
        opp1.Purchase_Type__c ='SI（手術室案件）';
        opp1.Sales_Root__c = 'OCM直接販売';
        opp1.Hospital__c = accHP.id;
        //opp1.Department_Class__c = accDepClass.id;
        opp1.StageName = '引合';
        opp1.RecordTypeid = TypeID;
        insert opp1;

        SI_Attachment__c newSac = new SI_Attachment__c();
        newSac.Opportunity_ID__c = opp1.id;
        newSac.Type__c = '项目方案书';
        newSac.Name__c = '*';
        insert newSac;

        ISO_DemandOperAndDemonsController iso = new ISO_DemandOperAndDemonsController();
        iso.init();
        iso.OpporIdStr = opp1.id;
        IS_Opportunity_Demand__c ISOhead = new IS_Opportunity_Demand__c();
        ISOhead.name='*';
        ISOhead.Opportunity_ID__c = opp1.id;
        ISOhead.Public_Hospital_TF__c = true;
        ISOhead.Preparation_Stage_TF__c = true;
        ISOhead.Data_Check_TF__c = true;
        ISOhead.Operating_Room_Plane_Graph_TF__c = true;
        ISOhead.Demonstration_Area_Plane_Graph_TF__c = true;
        insert ISOhead;

        Oly_TriggerHandler.clearAllBypasses();
        ControllerUtilEvent.selectDepartByHospitalState('', '東京', 10);
        ControllerUtilEvent.getOppSearchSQOLforLwc(accDep.Id, Date.today(), Date.today().addDays(1));
        ControllerUtilEvent.getOpportunitys('select Id from Opportunity');
    }
}