@isTest
private class NFM624RestTest {
    static testMethod void testMethod1(){
        BatchIF_Log__c testLog = new BatchIF_Log__c();
        testLog.CurrencyIsoCode = 'CNY';
        testLog.Type__c = 'NFM624';
        testLog.MessageGroupNumber__c = '123456789987';
        // testLog.Name = '20211207';
        testLog.ErrorLog__c = '';
        testLog.MessageGroupNumber__c = '20211207';
        testLog.RowDataFlg__c = true;
        testLog.Log__c = '[{"Type":"医生","State":"北京市","ServiceUserId":"","RelatedHospital":"354044","RelatedDepartment":"760129","RegSource":"2","PersonManagementCode":"C000132345","Name":"刘璐","Mobile":"13683268803","HospitalManagementCode2":"415","ForbiddenStatus":true,"DepartmentName":"北京大学第三医院 呼吸科 呼吸科","DepartmentManagementCode2":"416","DepartmentClass":"呼吸科","ContactId":"2552","ContactAddress":"","City":"海淀区","ApproverID":"","AgentFlag":false,"AccountName":"北京大学第三医院"}]';
        //[Select Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c,NFM624_Secondary_processing__c  from BatchIF_Log__c where RowDataFlg__c = true and Id =: rowData_Id]
        insert testLog;
        NFM624Rest.main(testLog.Id);
    }
    static testMethod void testMethod2(){
        
        //Test.startTest();
        //用户
        User hpowner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', Work_Location__c = '北京', 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 = '00e10000000xnp2');
        insert hpowner;

        User hpowner2 = new User(Test_staff__c = true, LastName = 'hp1', FirstName = 'owner1', Alias = 'hp1', Work_Location__c = '北京', CommunityNickname = 'hpOwner1', Email = 'olympus_hpowner1@sunbridge.com', Username = 'olympus_hpowner1@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = '00e10000000xnp2');
        insert hpowner2;
        
        //医院
        Account hp = new Account(RecordTypeId = '01210000000QemG', Name = 'hp', OwnerId = hpOwner.Id);
        hp.FSE_GI_Main_Leader__c = hpOwner.Id;
        hp.Advance_Payment_Identification_Approval__c = Date.today().addyears(-1);
        insert hp;
        Account hptemp = [select Id,PlatformCode__c,AgentCode_Ext__c from Account where Id = :hp.Id];
        //战略科室
        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_BF'];
        
        //科室
        // Account dpt = new Account(RecordTypeId = '01210000000QfmR');
        // dpt.Name         = '*';
        // dpt.Department_Name__c  = 'TestDepart';
        // dpt.ParentId            = dc.Id;
        // dpt.Department_Class__c = dc.Id;
        // dpt.Hospital__c         = hp.Id;
        // insert dpt;
        
        //科室1
        Account dpt1 = new Account();
        dpt1.RecordTypeId = '01210000000QfmR';
        dpt1.Name         = '123';
        dpt1.Department_Name__c  = 'TestDepart1';
        dpt1.ParentId            = dc.Id;
        dpt1.Department_Class__c = dc.Id;
        dpt1.Hospital__c         = hp.Id;
        insert dpt1;

        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '{"Monitoring":{"TransmissionDateTime":"202107131529","Text":null,"Tag":null,"Sender":"OBPM","Receiver":"SFDC","NumberOfRecord":"2","MessageType":"NFM624","MessageGroupNumber":null},"GeData":[{"TypeEncrypted":"53173e61ac22874aab5b8d1f802515db","Type":"*****","State":"北京市","ServiceUserId":"958717798199066624","RelatedHospital":"8020739","RelatedDepartment":"","RegSource":"1","PersonManagementCode":"","NameEncrypted":"d5cd51938e71eebbc0641f4e976146e7","Name":"***","MobileEncrypted":"c34725fe79b3965ea9abfd7c1435cf9a","Mobile":"***********","HospitalManagementCode2":"1377","ForbiddenStatus":false,"DepartmentName":"普外科","DepartmentManagementCode2":"1378","DepartmentClass":"普外科","DataId":"958717797737693184","ContactId":"958717798199066624","ContactAddressEncrypted":"","ContactAddress":"","City":"昌平区","ApproverID":"om003669","AgentFlag":false,"AccountName":"北京京都儿童医院"}]}';
        req.requestURI = 'services/apexrest/NFM624/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM624Rest.execute();

        RestRequest req2 = new RestRequest();
        RestResponse res2 = new RestResponse();

        String JsonMsg2 = '{"Monitoring":{"TransmissionDateTime":"202107131529","Text":null,"Tag":null,"Sender":"OBPM","Receiver":"SFDC","NumberOfRecord":"2","MessageType":"NFM624","MessageGroupNumber":null},"GeData":[{"Type":"医生","State":"北京市","ServiceUserId":"","RelatedHospital":"'+hptemp.PlatformCode__c+'","RelatedDepartment":" ","RegSource":"2","PersonManagementCode":"C000132345","Name":"刘璐","Mobile":"13683268803","HospitalManagementCode2":"415","ForbiddenStatus":true,"DepartmentName":"北京大学第三医院 呼吸科 呼吸科","DepartmentManagementCode2":"416","DepartmentClass":"呼吸科","ContactId":"2552","ContactAddress":"","City":"海淀区","ApproverID":"","AgentFlag":false,"AccountName":"北京大学第三医院"}]}';
        req2.requestURI = 'services/apexrest/NFM624/execute';
        req2.httpMethod = 'POST';
        req2.requestBody = Blob.valueof(JsonMsg2);
        RestContext.request = req2;
        RestContext.response= res2;

        NFM624Rest.execute();

        //Test.stopTest();
    }
    static testMethod void testMethod3(){
        BatchIF_Log__c testLog = new BatchIF_Log__c();
        testLog.CurrencyIsoCode = 'CNY';
        testLog.Type__c = 'NFM624';
        testLog.MessageGroupNumber__c = '123456789987';
        // testLog.Name = '20211207';
        testLog.ErrorLog__c = '';
        testLog.MessageGroupNumber__c = '20211207';
        testLog.RowDataFlg__c = true;
        testLog.Log__c = '[{"TypeEncrypted":"53173e61ac22874aab5b8d1f802515db","Type":"*****","State":"北京市","ServiceUserId":"","RelatedHospital":"","RelatedDepartment":"","RegSource":"1","PersonManagementCode":"","NameEncrypted":"24616254c7c7b65d985567f475b667d7","Name":"***","MobileEncrypted":"c34725fe79b3965ea9abfd7c1435cf9a","Mobile":"***********","HospitalManagementCode2":"","ForbiddenStatus":false,"DepartmentName":"北京德胜门中医院 消化科 胃镜室","DepartmentManagementCode2":"","DepartmentClass":"消化科","DataId":"958432058273693696","ContactId":"958432058911227904","ContactAddressEncrypted":"121a09fd9e0e9b090c4aa9c95da52810","ContactAddress":"**********","City":"西城区","ApproverID":"om003669","AgentFlag":false,"AccountName":"北京德胜门中医院"}]';
        //[Select Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c,NFM624_Secondary_processing__c  from BatchIF_Log__c where RowDataFlg__c = true and Id =: rowData_Id]
        insert testLog;
        NFM624Rest.main(testLog.Id);

        // BatchIF_Log__c testLog2 = new BatchIF_Log__c();
        // testLog2.CurrencyIsoCode = 'CNY';
        // testLog2.Type__c = 'NFM624';
        // testLog2.MessageGroupNumber__c = '123456789987';
        // // testLog.Name = '20211207';
        // testLog2.ErrorLog__c = '';
        // testLog2.MessageGroupNumber__c = '20211207';
        // testLog2.RowDataFlg__c = true;
        // testLog2.Log__c = '[{"Type":"医生","State":"北京市","ServiceUserId":"","RelatedHospital":"8087702","RelatedDepartment":"8087702","RegSource":"1","PersonManagementCode":"","Name":"张三","Mobile":"17734281527","HospitalManagementCode2":"1118000","ForbiddenStatus":false,"DepartmentName":"消化神经科","DepartmentManagementCode2":"1118001","DepartmentClass":"消化科","ContactId":"1118002","ContactAddress":"北京市朝阳区亮马桥","City":"朝阳区","AgentFlag":false,"AccountName":"恩惠医院"}]';
        // //[Select Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c,NFM624_Secondary_processing__c  from BatchIF_Log__c where RowDataFlg__c = true and Id =: rowData_Id]
        // insert testLog2;
        // NFM624Rest.main(testLog2.Id);

        // BatchIF_Log__c testLog3 = new BatchIF_Log__c();
        // testLog3.CurrencyIsoCode = 'CNY';
        // testLog3.Type__c = 'NFM624';
        // testLog3.MessageGroupNumber__c = '123456789987';
        // // testLog.Name = '20211207';
        // testLog3.ErrorLog__c = '';
        // testLog3.MessageGroupNumber__c = '20211207';
        // testLog3.RowDataFlg__c = true;
        // testLog3.Log__c = '[{"Type":"医生","State":"北京市","ServiceUserId":"","RelatedHospital":"","RelatedDepartment":"","RegSource":"1","PersonManagementCode":"","Name":"张三","Mobile":"17734281527","HospitalManagementCode2":"1118000","ForbiddenStatus":false,"DepartmentName":"消化神经科","DepartmentManagementCode2":"1118001","DepartmentClass":"消化科","ContactId":"1118002","ContactAddress":"北京市朝阳区亮马桥","City":"朝阳区","AgentFlag":false,"AccountName":"恩惠医院"}]';
        // //[Select Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c,NFM624_Secondary_processing__c  from BatchIF_Log__c where RowDataFlg__c = true and Id =: rowData_Id]
        // insert testLog3;
        // NFM624Rest.main(testLog3.Id);

        // BatchIF_Log__c testLog4 = new BatchIF_Log__c();
        // testLog4.CurrencyIsoCode = 'CNY';
        // testLog4.Type__c = 'NFM624';
        // testLog4.MessageGroupNumber__c = '123456789987';
        // // testLog.Name = '20211207';
        // testLog4.ErrorLog__c = '';
        // testLog4.MessageGroupNumber__c = '20211207';
        // testLog4.RowDataFlg__c = true;
        // testLog4.Log__c = '[{"Type":"医生","State":"北京市","ServiceUserId":"","RelatedHospital":"","RelatedDepartment":"","RegSource":"1","PersonManagementCode":"","Name":"胖达","Mobile":"17734281527","HospitalManagementCode2":"1118000","ForbiddenStatus":true,"DepartmentName":"消化神经混合科","DepartmentManagementCode2":"1118004","DepartmentClass":"消化科","ContactId":"1118002","ContactAddress":"北京市朝阳区亮马桥","City":"朝阳区","AgentFlag":false,"AccountName":"恩惠医院"}]';
        // //[Select Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c,NFM624_Secondary_processing__c  from BatchIF_Log__c where RowDataFlg__c = true and Id =: rowData_Id]
        // insert testLog4;
        // NFM624Rest.main(testLog4.Id);

        BatchIF_Log__c testLog5 = new BatchIF_Log__c();
        testLog5.CurrencyIsoCode = 'CNY';
        testLog5.Type__c = 'NFM624';
        testLog5.MessageGroupNumber__c = '123456789987';
        // testLog.Name = '20211207';
        testLog5.ErrorLog__c = '';
        testLog5.MessageGroupNumber__c = '20211207';
        testLog5.RowDataFlg__c = true;
        testLog5.Log__c = '[{"TypeEncrypted":"53173e61ac22874aab5b8d1f802515db","Type":"*****","State":"北京市","ServiceUserId":"958432058911227904","RelatedHospital":"3728239","RelatedDepartment":"5311053","RegSource":"1","PersonManagementCode":"","NameEncrypted":"24616254c7c7b65d985567f475b667d7","Name":"***","MobileEncrypted":"c34725fe79b3965ea9abfd7c1435cf9a","Mobile":"***********","HospitalManagementCode2":"1375","ForbiddenStatus":false,"DepartmentName":"北京德胜门中医院 消化科 胃镜室","DepartmentManagementCode2":"1376","DepartmentClass":"消化科","DataId":"958432058273693696","ContactId":"958432058911227904","ContactAddressEncrypted":"121a09fd9e0e9b090c4aa9c95da52810","ContactAddress":"**********","City":"西城区","ApproverID":"om003669","AgentFlag":false,"AccountName":"北京德胜门中医院"}]';
        //[Select Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c,NFM624_Secondary_processing__c  from BatchIF_Log__c where RowDataFlg__c = true and Id =: rowData_Id]
        upsert testLog5;
        NFM624Rest.main(testLog5.Id);
    }
    static testMethod void testMethod4(){
        //用户
        User hpowner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', Work_Location__c = '北京', 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 = '00e10000000xnp2');
        insert hpowner;

        User hpowner2 = new User(Test_staff__c = true, LastName = 'hp1', FirstName = 'owner1', Alias = 'hp1', Work_Location__c = '北京', CommunityNickname = 'hpOwner1', Email = 'olympus_hpowner1@sunbridge.com', Username = 'olympus_hpowner1@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = '00e10000000xnp2');
        insert hpowner2;
        
        //医院
        Account hp = new Account(RecordTypeId = '01210000000QemG', Name = '北京德胜门中医院', OwnerId = hpOwner.Id);
        hp.FSE_GI_Main_Leader__c = hpOwner.Id;
        hp.Advance_Payment_Identification_Approval__c = Date.today().addyears(-1);
        hp.AgentCode_Ext__c = '1375';
        hp.PlatformCode__c = '112358';
        hp.Is_Active__c = '有効';
        insert hp;
      

        Account hptemp = [select Id,PlatformCode__c,AgentCode_Ext__c from Account where Id = :hp.Id];
        //战略科室
        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_BF'];
        
        NFM624Rest.GeData GeData1 = new NFM624Rest.GeData();
        NFM624Rest.GeDatas GaDatas = new  NFM624Rest.GeDatas();

        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GaDatas.Monitoring = new NFMUtil.Monitoring();
        GaDatas.Monitoring.MessageGroupNumber = nowStr + '01';

        GaDatas.GeData = new NFM624Rest.Gedata[] { GeData1 };
        GeData1.ContactId = '958432058911227904';
        GeData1.ServiceUserId = '958432058911227904';
        GeData1.PersonManagementCode = '';
        GeData1.HospitalManagementCode2 = '1375';
        GeData1.DepartmentManagementCode2 = '1376';
        GeData1.Name = '***';
        GeData1.NameEncrypted = '24616254c7c7b65d985567f475b667d7';
        GeData1.Mobile = '***********';
        GeData1.MobileEncrypted = 'c34725fe79b3965ea9abfd7c1435cf9a';
        GeData1.State = '北京市';
        GeData1.City = '西城区';
        GeData1.AccountName = '北京德胜门中医院';
        GeData1.RelatedHospital = '112358';
        GeData1.DepartmentClass = '消化科';
        GeData1.DepartmentName = '北京德胜门中医院 消化科 胃镜室';
        GeData1.RelatedDepartment = '5311053';
        GeData1.Type = '*****';
        GeData1.TypeEncrypted = '53173e61ac22874aab5b8d1f802515db';
        GeData1.ContactAddress = '**********';
        GeData1.ContactAddressEncrypted = '121a09fd9e0e9b090c4aa9c95da52810';
        GeData1.ForbiddenStatus = false;
        GeData1.RegSource = '1';
        GeData1.AgentFlag = false;
        GeData1.ApproverID = hpowner.Id;
        GeData1.DataId = '958432058273693696';

        NFM624Rest.GeData GeData2 = new NFM624Rest.GeData();
        GaDatas.GeData = new NFM624Rest.Gedata[] { GeData2 };
        GeData2.ContactId = '';
        GeData2.ServiceUserId = '';
        GeData2.PersonManagementCode = '';
        GeData2.HospitalManagementCode2 = hptemp.AgentCode_Ext__c;
        GeData2.DepartmentManagementCode2 = '';
        GeData2.Name = '';
        GeData2.NameEncrypted = '';
        GeData2.Mobile = '';
        GeData2.MobileEncrypted = '';
        GeData2.State = '';
        GeData2.City = '';
        GeData2.AccountName = '';
        GeData2.RelatedHospital = hptemp.PlatformCode__c;
        GeData2.DepartmentClass = '';
        GeData2.DepartmentName = '';
        GeData2.RelatedDepartment = '';
        GeData2.Type = '';
        GeData2.TypeEncrypted = '';
        GeData2.ContactAddress = '';
        GeData2.ContactAddressEncrypted = '';
        GeData2.ForbiddenStatus = false;
        GeData2.RegSource = '';
        GeData2.AgentFlag = false;
        GeData2.ApproverID = hpowner.Id;
        GeData2.DataId = '';

        System.Test.startTest();
        BatchIF_Log__c rowData = NFMUtil.saveRowData(GaDatas.Monitoring, 'NFM624', GaDatas.GeData);
        if (String.isBlank(rowData.Log__c) == false)
            NFM624Rest.executefuture(rowData.Id);
        //NFM110WebService.NFM110(ProductsDelivery);
        System.Test.stopTest();
    }
}