@isTest
private class NFM601ControllerTest {
    
    @testSetup
    static void makeTestRepair() {
        

        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');
        }
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Is_Active__c = '有効';
        company.Name = 'NFM105TestCompany';
        company.AwaitToSendAWS__c = true;
        upsert company;

        Account section = [Select Management_Code__c, Management_Code_Auto__c, Name, Id 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;
        upsert depart;

        company.Site = '测试变更别名1';

        upsert company; 

        List < RecordType > rectDpt2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account'
            and DeveloperName = 'Agency'
        ];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }
        ControllerUtil.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
        Account company2 = new Account();
        company2.RecordTypeId = rectDpt2[0].Id;
        company2.Name = 'NFM105TestCompany';
        upsert company2;

        License_Information__c lic = new License_Information__c();
        lic.name = 'Test20181204';
        lic.LicenseType__c = '医疗器械经营许可证';
        lic.BusinessLicense__c = '20180522';
        lic.ValidFrom__c = date.newinstance(2018, 05, 22);
        lic.ValidTo__c = date.newinstance(2018, 05, 22);
        lic.Scope3__c = '6815;6822;6823;6825';
        lic.IsInquire__c = false;
        lic.LicenseAndAccount__c = company2.Id;
        insert lic;
        License_Information__c lic1 = new License_Information__c();
        lic1.name = 'Test20190111';
        lic1.LicenseType__c = '第二类医疗器械经营备案凭证';
        lic1.BusinessLicense__c = '20190522';
        lic1.ValidFrom__c = date.newinstance(2018, 05, 21);
        lic1.ValidTo__c = date.newinstance(2018, 05, 21);
        lic.IsInquire__c = false;
        lic1.Scope__c = '6815;6822;6823;6825';
        lic1.LicenseAndAccount__c = company2.Id;
        insert lic1;

        List < RecordType > rectDptAgencyContract = [select Id from RecordType where IsActive = true and SobjectType = 'Account'
            and DeveloperName = 'AgencyContract'
        ];
        if (rectDptAgencyContract.size() == 0) {
            return;
        }
    
        Account AagencyContractAccount = new Account();
        AagencyContractAccount.RecordTypeId = rectDptAgencyContract[0].Id;
        AagencyContractAccount.Contract_Decide_Start_Date__c = System.today();
        AagencyContractAccount.Contract_Decide_End_Date__c = System.today();
        AagencyContractAccount.Contract_End_Date__c = Date.today();
        AagencyContractAccount.Name = '*';
        AagencyContractAccount.Department_Name__c = 'NFM601TestDepart';
        AagencyContractAccount.ParentId = company2.Id;
        AagencyContractAccount.Agent_Ref__c = company2.Id;
        AagencyContractAccount.ET_SP_Dealer__c = true;
        upsert AagencyContractAccount;


        List < BatchIF_Transfer__c > transfers = new List < BatchIF_Transfer__c > ();
        BatchIF_Transfer__c transfer = new BatchIF_Transfer__c();
        transfer.Table__c = 'Account';
        transfer.Column__c = 'RecordTypeId';
        transfer.External_Value__c = rectDpt[0].Id;
        transfer.Internal_Value__c = '消化科';
        insert transfer;
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Log__c = '{"GeData":[{"HospitalInfoData":[{"StrategicDeptCode":"","Province":"CN-05","ParentCode":"360155呼吸科","HospitalRank":"L","HospitalName":"忻州市二八二医院 呼吸科 呼吸一科","HospitalCode":"","DeptType":"BF","DeptCode":"8087357","City":"CN-0567","Area":"3.西北","Accounttype":"3"}],"DealerInfoData":[]}]}';
        iflog.Type__c = '601test';
        insert iflog;
    }

    @isTest
    static void testCallOut1() {
        List < Account > userList = [select id from Account];

        Test.startTest();

        List < Id > idList = new List < Id > ();
        for (Account user: userList) {
            idList.add(user.Id);
        }
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Log__c = 'test start \n';
        insert iflog;

        NFM601Controller.callout(iflog.Id, idList);

        Test.stopTest();
    }

    // @isTest
    // static void testCallOut2() {
    //     List < Account > userList = [select id from Account];

    //     Test.startTest();

    //     List < Id > idList = new List < Id > ();
    //     for (Account user: userList) {
    //         idList.add(user.Id);
    //     }
    //     NFM601Controller.executefuture(null, idList);

    //     Test.stopTest();
    // }

    @isTest
    static void testcallout3() {
        BatchIF_Log__c iflog = [select id from BatchIF_Log__c where Type__c = '601test'];
        NFM601Controller.ManualExecute(iflog.Id);
    }

    @isTest
    static void testcallout4() {
        // BatchIF_Log__c iflog = [select id from BatchIF_Log__c where Type__c = '601test'];
        // NFM601Controller.ManualExecute(iflog.Id);
        Database.executeBatch(new Sfdc2PoAccountBatch(),100);
    }

    @isTest
    static void testcallout5() {
         List < RecordType > rectCo00 = [select Id from RecordType where IsActive = true and SobjectType = 'Account'
            and Name = '病院'
        ];
        Account hospital = new Account();
        hospital.RecordTypeId = rectCo00[0].Id;
        hospital.Is_Active__c = '有効';
        hospital.Name = 'hospital';
        hospital.AwaitToSendAWS__c = true;
        insert hospital;
        List < String > accountIdList = new List < String >();
        accountIdList.add(hospital.Id);
        Database.executeBatch(new Sfdc2PoAccountBatch(accountIdList),100);
    }

}