using Dapper; using Npgsql; using ServiceReferenceAiDiKangLisResult; using ServiceReferenceDianLisReport; using ServiceReferenceDianLisResult; using Shentun.Peis.ImportLisResults; using Shentun.Peis.PlugIns.Extensions.ImportLisResults.Dian; using Shentun.Peis.PlugIns.ImportLisResults; using Shentun.Peis.PrintReports; using Shentun.Peis.RegisterCheckPictures; using System; using System.Collections.Generic; using System.Data.Common; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace Shentun.Peis.PlugIns.Extensions.ImportLisResults.AiDiKang { public class ImportLisResultPlugInsAiDiKang : ImportLisResultPlugInsBase { private readonly string _userName; private readonly string _passWord; public ImportLisResultPlugInsAiDiKang(Guid thirdInterfaceId) : base(thirdInterfaceId) { _userName = InterfaceConfig.GetSection("Interface").GetSection("UserName").Value; _passWord = InterfaceConfig.GetSection("Interface").GetSection("PassWord").Value; } public ImportLisResultPlugInsAiDiKang(string parmValue) : base(parmValue) { _userName = InterfaceConfig.GetSection("Interface").GetSection("UserName").Value; _passWord = InterfaceConfig.GetSection("Interface").GetSection("PassWord").Value; } public override Task ImportResultByPatientRegisterIdAsync(Guid patientRegisterId) { return base.ImportResultByPatientRegisterIdAsync(patientRegisterId); } public override async Task DoWork() { var queryDaysStr = InterfaceConfig.GetSection("Interface").GetSection("Scheduler").GetSection("QueryDays").Value; if (string.IsNullOrWhiteSpace(queryDaysStr)) { queryDaysStr = "1"; } if (!int.TryParse(queryDaysStr, out int days)) { days = 1; } var patientRegisters = GetRequestPatientRegistersDianAsync(days).Result; foreach (var patientRegister in patientRegisters) { await ImportOueSendResultByPatientRegisterIdAsync(patientRegister.PatientRegisterId); } } public async Task ImportOueSendResultByPatientRegisterIdAsync(Guid patientRegisterId) { var lisResultAddress = InterfaceConfig.GetSection("Interface").GetSection("LisResultAddress").Value; using (var client = CreateClient(lisResultAddress)) { // 设置超时时间 client.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(2); var aiDiKangKeyValue = await client.LoginAsync(_userName, _passWord); if (aiDiKangKeyValue == "失败") { return new ImportLisResultPlugInsOut(); } var execOrganizationUnitIdStr = InterfaceConfig.GetSection("Interface").GetSection("ExecOrganizationUnitId").Value; var defaultCheckDoctorName = InterfaceConfig.GetSection("Interface").GetSection("DefaultCheckDoctorName").Value; var barcodeMode = InterfaceConfig.GetSection("Interface").GetSection("BarcodeMode").Value; //0-人员条码 1-检验条码 #region 导入数据 List itemCommonTableOuts = new List(); //List asbitemCommonTableOuts = new List(); string sql_item = $"select data_code as dian_code,display_name as item_id from common_table " + $" where common_table_type_id='{InterfaceConfig.GetSection("Interface").GetSection("ItemCommonTableTypeId").Value}' "; //string sql_asbitem = $"select data_code as dian_code,display_name as item_id from common_table " + // $" where common_table_type_id='{InterfaceConfig.GetSection("Interface").GetSection("AsbitemCommonTableTypeId").Value}' "; var result = new ImportLisResultPlugInsOut(); using (DbConnection conn = new NpgsqlConnection(AppConnctionStr)) { itemCommonTableOuts = (await conn.QueryAsync(sql_item)).ToList(); //asbitemCommonTableOuts = (await conn.QueryAsync(sql_asbitem)).ToList(); try { var createImportLisResultDtos = new List(); //设置结果,生成小结 string register_item_sql = $"select d.lis_request_no,f.item_id::TEXT as item_id,g.display_name as item_name,patient_register_no" + " from patient_register as a " + " left join register_check as b on a.id=b.patient_register_id " + " left join register_check_asbitem as c on b.id=c.register_check_id " + " left join lis_request as d on c.lis_request_id=d.id " + " left join asbitem as e on c.asbitem_id=e.id " + " left join register_check_item as f on b.id=f.register_check_id " + " left join item as g on f.item_id=g.id " + $" where a.id='{patientRegisterId}' and e.is_outsend='Y' and b.is_audit='N' "; var lisResultFromInterfaces = (await conn.QueryAsync(register_item_sql)).ToList(); string barCode = lisResultFromInterfaces.FirstOrDefault().PatientRegisterNo; var isCheck = await client.ExistsByYYtmAsync(barCode, aiDiKangKeyValue); if (isCheck == "True") { //检查完 var result_cg = await client.GetReportItemListByCustomerBarocdeAsync(aiDiKangKeyValue, barCode, "常规报告"); var resultObj_cg = XmlHelper.DeserializeXml(result_cg); foreach (var li in lisResultFromInterfaces) { var dian_code = itemCommonTableOuts.FirstOrDefault(f => f.ItemId == li.ItemId); if (dian_code != null) { if (resultObj_cg.Items != null && resultObj_cg.Items.Any()) { var dianResult = resultObj_cg.Items.FirstOrDefault(f => f.ItemCode == dian_code.DianCode); if (dianResult != null) { DateTime collectionDate = DateTime.Now; if (DateTime.TryParse(dianResult.CollectionDate, out var itemCollectionDate)) { // 使用 collectionDate collectionDate = itemCollectionDate; } var createImportLisResultDto = new CreateImportLisResultDto() { LisRequestNo = li.LisRequestNo, ItemId = Guid.Parse(li.ItemId), ItemName = li.ItemName, Result = dianResult.Result, // Unit = lisResult.Unit, // ReferenceRangeValue = lisResult.ReferenceRangeValue, // CriticalRangeValue = lisResult.CriticalRangeValue, // ResultStatusId = lisResult.ResultStatusId, // ReportPrompt = lisResult.ReportPrompt, CheckDoctorName = defaultCheckDoctorName, //CheckDate = dianResult.ApproveDate, CheckDate = collectionDate, ExecOrganizationUnitId = Guid.Parse(execOrganizationUnitIdStr) }; createImportLisResultDtos.Add(createImportLisResultDto); } } } } if (createImportLisResultDtos.Any()) { var callResult = await CallAppServiceAsync, object>("api/app/ImportLisResult/ImportResult", createImportLisResultDtos); } } } catch (Exception ex) { } } #endregion } return new ImportLisResultPlugInsOut(); } private AdiconWebServiceSoapClient CreateClient(string url) { // 创建自定义绑定,设置更大的消息大小限制 var binding = new BasicHttpBinding(); // 设置最大接收消息大小(根据需要调整,建议先设为 10MB) binding.MaxReceivedMessageSize = 10485760; // 10MB = 10 * 1024 * 1024 binding.MaxBufferSize = 10485760; // 必须与 MaxReceivedMessageSize 保持一致 // 设置 ReaderQuotas 以支持大字符串和大数组 binding.ReaderQuotas.MaxDepth = 2147483647; binding.ReaderQuotas.MaxStringContentLength = 10485760; binding.ReaderQuotas.MaxArrayLength = 10485760; binding.ReaderQuotas.MaxBytesPerRead = 10485760; binding.ReaderQuotas.MaxNameTableCharCount = 10485760; // 可选:增加超时时间(因为大数据传输需要更长时间) binding.SendTimeout = TimeSpan.FromMinutes(5); binding.ReceiveTimeout = TimeSpan.FromMinutes(5); binding.OpenTimeout = TimeSpan.FromMinutes(5); binding.CloseTimeout = TimeSpan.FromMinutes(1); return new AdiconWebServiceSoapClient(binding, new EndpointAddress(url)); } //private string ToBase64(string filename) //{ // Stream stream; // string fileBase64; // if (filename.StartsWith("http")) // { // using (var httpClient = new HttpClient()) // { // var response = httpClient.GetAsync(filename).Result; // if (response.IsSuccessStatusCode) // { // var contentStream = response.Content.ReadAsStreamAsync().Result; // using var memoryStream = new MemoryStream(); // contentStream.CopyTo(memoryStream); // var fileBytes = memoryStream.ToArray(); // return Convert.ToBase64String(fileBytes); // } // else // { // throw new Exception("转换base64时,返回错误:" + response.StatusCode); // } // } // } // else // { // using (stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) // { // byte[] byteData = new byte[stream.Length]; // stream.Read(byteData, 0, byteData.Length); // fileBase64 = Convert.ToBase64String(byteData); // stream.Close(); // } // return fileBase64; // } //} } }