using Dapper; using Newtonsoft.Json.Converters; using Newtonsoft.Json; using Npgsql; using NPOI.SS.Formula.Functions; using System; using System.Collections.Generic; using System.Data.Common; using System.Linq; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace Shentun.Peis.PlugIns.Gem { public class ImportPatientRegisterPlugInsGem : ImportPatientRegisterPlugInsBase { private Guid _importCustomerOrgId; private string _hospitalId; private string _aesKEY; private string _year; public ImportPatientRegisterPlugInsGem(string parmValue) : base(parmValue) { } public override async Task ImportAsync() { await InitAsync(); var qztlPatientRegisterFromInterface = await CallInterfaceServiceAsync(); if (qztlPatientRegisterFromInterface == null || !qztlPatientRegisterFromInterface.plans.Any()) { return; } foreach(var patient in qztlPatientRegisterFromInterface.plans) { } } public async Task InitAsync() { var customerOrgIdStr = InterfaceConfig.GetSection("Interface").GetSection("单位编号").Value; _hospitalId = InterfaceConfig.GetSection("Interface").GetSection("HospitalId").Value; _aesKEY = InterfaceConfig.GetSection("Interface").GetSection("aesKEY").Value; if (string.IsNullOrWhiteSpace(customerOrgIdStr)) { return; } if (!Guid.TryParse(customerOrgIdStr, out _importCustomerOrgId)) { throw new Exception("单位编号不正确"); } if (string.IsNullOrWhiteSpace(_hospitalId)) { throw new Exception("HospitalId参数不能为空"); } if (string.IsNullOrWhiteSpace(_aesKEY)) { throw new Exception("_aesKEY参数不能为空"); } using (DbConnection conn = new NpgsqlConnection(AppConnctionStr)) { string sql; sql = @" SELECT id as customer_org_id, display_name as customer_org_name, parent_id , path_code from customer_org where id = @CustomerOrgId "; var customerOrgForPlugInss = (await conn.QueryAsync(sql, new { CustomerOrgId = _importCustomerOrgId })).FirstOrDefault(); if (customerOrgForPlugInss == null) { throw new Exception("单位编号不正确,找不到单位"); } var year = customerOrgForPlugInss.CustomerOrgName.Substring( customerOrgForPlugInss.CustomerOrgName.Length - 5 , 4); if (!int.TryParse(year, out var yearNumber)) { throw new Exception("单位名称必须是以2000年这样的格式结尾"); } if (yearNumber < 2024 || yearNumber > 2050) { throw new Exception("单位名称年限范围必须在2024-2050之间"); } _year = year; } } public async Task CallInterfaceServiceAsync() { string baseAddress = InterfaceWebApiUrl; string ary = "HospitalId=" + _hospitalId + (Char)38 + "year=" + _year + (Char)38 + "AesKey=" + _aesKEY; baseAddress = baseAddress + ary; using (var httpClientHandler = new HttpClientHandler()) { using (var httpClient = new HttpClient(httpClientHandler)) { httpClient.BaseAddress = new Uri(baseAddress); httpClient.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));//设置accept标头,告诉JSON是可接受的响应类型 //httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accesToken); IsoDateTimeConverter timeFormat = new IsoDateTimeConverter(); timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; //var sendData = JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented, timeFormat); using (HttpContent httpContent = new StringContent("")) { httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); HttpResponseMessage response = await httpClient.GetAsync(""); string result; if (!response.IsSuccessStatusCode) { result = response.Content.ReadAsStringAsync().Result; throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result); } result = await response.Content.ReadAsStringAsync(); result = result.Replace(":\"[{", ":[{").Replace("}]\"", "}]").Replace("\\", ""); QztlPatientRegisterFromInterface? resultDto = JsonConvert.DeserializeObject(result); if (resultDto != null) { if (resultDto.status != 0) { throw new Exception($"调用WebApi失败,返回错误,消息:" + resultDto.status + resultDto.errorMsg); } //插入数据库中备份 using (DbConnection conn = new NpgsqlConnection(AppConnctionStr)) { string sql; var qztlImportDataEntity = new QztlImportDataEntity() { Id = Guid.NewGuid(), Data = result, DataType = '0', IsComplete = 'Y', CreationTime = DateTime.Now, }; sql = @"insert into qztl_import_data(id, data, data_type, is_complete,creation_time) values(@Id, @Data, @DataType, @IsComplete,@CreationTime)"; conn.Execute(sql, qztlImportDataEntity); } return (QztlPatientRegisterFromInterface)resultDto; } return null; } } } } } }