From e80dc6f763270fc92f56002231bfec98463504bf Mon Sep 17 00:00:00 2001 From: wxd <123@qq.com> Date: Sun, 16 Jun 2024 18:56:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BF=83=E7=94=B5=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...mportElectrocardiogramResultPlugInsMdks.cs | 140 ++++++++++++ .../ImportPacsResultAppService.cs | 209 ++++++++++++++++++ src/Shentun.Utilities/FileHelper.cs | 41 ++++ 3 files changed, 390 insertions(+) create mode 100644 ThirdPlugIns/Shentun.Peis.PlugIns.Gem/ImportElectrocardiogramResults/Mdks/ImportElectrocardiogramResultPlugInsMdks.cs diff --git a/ThirdPlugIns/Shentun.Peis.PlugIns.Gem/ImportElectrocardiogramResults/Mdks/ImportElectrocardiogramResultPlugInsMdks.cs b/ThirdPlugIns/Shentun.Peis.PlugIns.Gem/ImportElectrocardiogramResults/Mdks/ImportElectrocardiogramResultPlugInsMdks.cs new file mode 100644 index 0000000..7bd7c67 --- /dev/null +++ b/ThirdPlugIns/Shentun.Peis.PlugIns.Gem/ImportElectrocardiogramResults/Mdks/ImportElectrocardiogramResultPlugInsMdks.cs @@ -0,0 +1,140 @@ +using Newtonsoft.Json.Converters; +using Newtonsoft.Json; +using Shentun.Peis.ImportPacsResults; +using Shentun.Peis.PlugIns.Extensions.ImportPacsResults.Hzcy; +using Shentun.Peis.PlugIns.ImportPacsResults; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http.Headers; +using System.Text; + +namespace Shentun.Peis.PlugIns.Extensions.ImportElectrocardiogramResults.Mdks +{ + + public class ImportElectrocardiogramResultPlugInsMdks : ImportPacsResultPlugInsBase + { + public ImportElectrocardiogramResultPlugInsMdks(Guid thirdInterfaceId) : base(thirdInterfaceId) + { + } + public ImportElectrocardiogramResultPlugInsMdks(string parmValue) : base(parmValue) + { + } + + public override async Task ImportResultByPatientRegisterIdAsync(Guid patientRegisterId) + { + //if (DepartmentColumnReferenceId == null || DepartmentColumnReferenceId == Guid.Empty) + //{ + // throw new Exception("没有设置科室编码对照"); + //} + var pacsRequests = await GetPacsRequestForImportResultPlugInssAsync(patientRegisterId); + + var beginTime = DateTime.Now.Date.AddDays(-30); + foreach (var pacsRequest in pacsRequests) + { + if (string.IsNullOrWhiteSpace(pacsRequest.CheckRequestNo)) + { + continue; + } + var interfaceInpu = new ImportPacsResultInterfaceInput() + { + tjNum = pacsRequest.CheckRequestNo, + organizeCode = "1", + patientType = "3", + ////requestId = pacsRequest.CheckRequestNo, + //beginTime = beginTime.ToString("yyyy-MM-dd HH:mm:ss"), + //endTime=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), + pageNo = 1, + pageSize = 20 + }; + //if(interfaceInpu.tjNum == "2405112185") + //{ + // ; + //} + var apiResult = await CallInterfaceServiceAsync(interfaceInpu); + if (apiResult == null || !apiResult.data.Any()) + { + continue; + } + var firstData = apiResult.data[0]; + if (!DateTime.TryParse(firstData.reportDateTime, out var checkDate)) + { + throw new Exception("报告时间格式不正确"); + } + var createImportPacsResultDto = new CreateImportPacsResultDto() + { + CheckRequestNo = pacsRequest.CheckRequestNo, + PatientName = pacsRequest.PatientName, + Result = firstData.examDescript, + Summary = firstData.examDiagnosis, + Suggestion = firstData.suggestion, + CheckDate = checkDate, + CheckDoctorName = firstData.reportDoctorName, + Files = new List() + { + new CreateImportPacsResultPictureDto() + { + IsPrint = 'Y', + FileTransMode = "1",//0-json,1-url + FileName = firstData.reportUrl, + FileFormat = "1",//0-图片,1-pdf + FileUrl = firstData.reportUrl, + //FileBase64 = Shentun.Utilities.FileHelper.ToBase64(firstData.reportUrl) + }, + + } + }; + + + var callResult = await CallAppServiceAsync("api/app/Electrocardiogram/ImportElectrocardiogramResult", createImportPacsResultDto); + + } + var result = new ImportPacsResultPlugInsOut(); + return result; + } + + + public async Task CallInterfaceServiceAsync(TInput data) + { + string baseAddress = InterfaceWebApiUrl; + using (var httpClientHandler = new HttpClientHandler()) + { + using (var httpClient = new HttpClient(httpClientHandler)) + { + httpClient.BaseAddress = new Uri(baseAddress); + + httpClient.DefaultRequestHeaders.Accept.Add( + new MediaTypeWithQualityHeaderValue("application/json"));//设置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, Formatting.Indented, timeFormat); + using (HttpContent httpContent = new StringContent(sendData)) + { + httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + HttpResponseMessage response = await httpClient.PostAsync("", httpContent); + string result; + if (!response.IsSuccessStatusCode) + { + result = response.Content.ReadAsStringAsync().Result; + throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result); + } + result = await response.Content.ReadAsStringAsync(); + + dynamic resultDto = JsonConvert.DeserializeObject(result); + if (resultDto != null) + { + if (resultDto.code != "200") + { + throw new Exception($"调用WebApi失败,返回错误,消息:" + resultDto.code + resultDto.msg); + } + } + return resultDto; + } + + } + } + } + } +} diff --git a/src/Shentun.Peis.Application/ImportPacsResults/ImportPacsResultAppService.cs b/src/Shentun.Peis.Application/ImportPacsResults/ImportPacsResultAppService.cs index ba3896b..87d281c 100644 --- a/src/Shentun.Peis.Application/ImportPacsResults/ImportPacsResultAppService.cs +++ b/src/Shentun.Peis.Application/ImportPacsResults/ImportPacsResultAppService.cs @@ -345,6 +345,215 @@ namespace Shentun.Peis.ImportPacsResults } } + + + + /// + /// 导入心电图结果 + /// + /// + /// + /// + [HttpPost("api/app/Electrocardiogram/ImportElectrocardiogramResult")] + public async Task ImportElectrocardiogramResult(CreateImportPacsResultDto input) + { + if (input == null) + { + throw new UserFriendlyException("input参数不能为空"); + + } + if (string.IsNullOrWhiteSpace(input.CheckRequestNo)) + { + throw new UserFriendlyException("CheckRequestNo不能为空"); + } + //设置结果 + + var list = (from patientRegister in await _patientRegisterRepository.GetQueryableAsync() + join registerCheck in await _registerCheckRepository.GetQueryableAsync() + on patientRegister.Id equals registerCheck.PatientRegisterId + join registerCheckItem in await _registerCheckItemRepository.GetQueryableAsync() + on registerCheck.Id equals registerCheckItem.RegisterCheckId + join item in await _itemRepository.GetQueryableAsync() + on registerCheckItem.ItemId equals item.Id + where registerCheck.CheckRequestNo == input.CheckRequestNo + select new + { + patientRegister, + registerCheck, + registerCheckItem, + item, + }).ToList(); + if (!list.Any()) + { + return; + } + var firstEntity = list.First(); + + if (string.IsNullOrWhiteSpace(input.Result)) + { + return; + } + if (string.IsNullOrWhiteSpace(input.CheckDoctorName)) + { + throw new UserFriendlyException("检查医生不能为空"); + } + if (string.IsNullOrWhiteSpace(input.PatientName)) + { + throw new UserFriendlyException("姓名不能为空"); + } + if (input.PatientName != firstEntity.patientRegister.PatientName) + { + throw new UserFriendlyException($"Pacs姓名{input.PatientName}和体检系统姓名{firstEntity.patientRegister.PatientName}不一致"); + } + var updateCheckResultDto = new UpdateCheckResultDto() + { + RegisterCheckId = firstEntity.registerCheck.Id, + CheckDoctorId = input.CheckDoctorName, + CheckDate = input.CheckDate, + ExecOrganizationUnitId = input.ExecOrganizationUnitId, + RegisterCheckItems = list + .Select(o => new UpdateRegisterCheckItemDetail() + { + ItemId = o.registerCheckItem.ItemId, + Result = input.Result, + + }).ToList() + + }; + + + //生成小结 + updateCheckResultDto.Summarys.Add(new UpdateRegisterCheckSummaryDetail() + { + Summary = input.Summary, + SummaryFlag = '0' + }); + //保存明细结果和小结 + await _registerCheckAppService.UpdateCheckResult(updateCheckResultDto); + //保存图片 + string absolutePath = _configuration.GetValue("VirtualPath:RealPath"); + //删除以前的图片 + var registerCheckPictures = await _registerCheckPictureRepository.GetListAsync(o => o.RegisterCheckId == firstEntity.registerCheck.Id); + await _registerCheckPictureRepository.DeleteManyAsync(registerCheckPictures); + //插入新图片 + string pictureUrl; + foreach (var file in input.Files) + { + if (string.IsNullOrWhiteSpace(file.FileTransMode)) + { + throw new UserFriendlyException("文件传输模式不能为空"); + } + if (string.IsNullOrWhiteSpace(file.FileFormat)) + { + throw new UserFriendlyException("文件格式不能为空"); + } + if (file.FileTransMode != "0" && file.FileTransMode != "1") + { + throw new UserFriendlyException("文件传输模式不正确"); + } + if (file.FileFormat != "0" && file.FileFormat != "1") + { + throw new UserFriendlyException("文件格式不正确"); + } + string fileName; + if (string.IsNullOrWhiteSpace(file.FileName)) + { + fileName = Guid.NewGuid().ToString(); + } + else + { + fileName = Path.GetFileNameWithoutExtension(file.FileName); + } + if (file.FileTransMode == "1") + { + if (string.IsNullOrWhiteSpace(file.FileUrl)) + { + throw new UserFriendlyException("文件URL不能为空"); + } + if (!string.IsNullOrWhiteSpace(file.FileBase64)) + { + throw new UserFriendlyException("文件Base64不能传值"); + } + file.FileBase64 = Shentun.Utilities.FileHelper.ToBase64str(file.FileUrl); + } + else + { + if (string.IsNullOrWhiteSpace(file.FileBase64)) + { + throw new UserFriendlyException("文件Base64不能为空"); + } + } + //0-图片,1-PDF + if (file.FileFormat == "0") + { + pictureUrl = ImageHelper.Base64ToImageUseAbsolutePath(absolutePath, fileName, file.FileBase64, firstEntity.patientRegister.Id.ToString(), + firstEntity.registerCheck.Id.ToString()); + + } + else if (file.FileFormat == "1") + { + + byte[] pdfBytes = Convert.FromBase64String(file.FileBase64); + var tempFilePath = DirectoryHelper.GetAppDirectory() + "\\temp-files"; + if (!Directory.Exists(tempFilePath)) + { + Directory.CreateDirectory(tempFilePath); + } + + string tempPdf = tempFilePath + "\\temp.pdf"; + using (System.IO.FileStream stream = new System.IO.FileStream(tempPdf, System.IO.FileMode.Create)) + { + //通过流的方式写入 + using (System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream)) + { + writer.Write(pdfBytes, 0, pdfBytes.Length); + writer.Close(); + } + + } + + string hostName = file.FileUrl.Replace("http://", "").Replace("https://", ""); + if (hostName.IndexOf(":") > 0) + { + hostName = hostName.Substring(0, hostName.IndexOf(":")); + } + else + { + hostName = hostName.Substring(0, hostName.IndexOf("/")); + } + + string tempImageName = "tempImage" + hostName; + PDFHelper.ConvertPdfToImage(tempPdf, tempFilePath, tempImageName); + + pictureUrl = ImageHelper.SavePacsFile(absolutePath, tempFilePath + "\\" + tempImageName + "_0.jpg", firstEntity.patientRegister.Id.ToString(), + firstEntity.registerCheck.Id.ToString(), fileName); + } + else + { + throw new UserFriendlyException("不支持的文件类型"); + } + + + + if (string.IsNullOrWhiteSpace(pictureUrl)) + { + throw new UserFriendlyException("pictureUrl不能是空值"); + } + var registerCheckPicture = new RegisterCheckPicture + { + DisplayOrder = input.Files.IndexOf(file) + 1, + IsPrint = file.IsPrint, + PictureFilename = pictureUrl, + RegisterCheckId = firstEntity.registerCheck.Id, + }; + + await _registerCheckPictureRepository.InsertAsync(registerCheckPicture); + } + + + + } + /// /// 导入心电图 /// diff --git a/src/Shentun.Utilities/FileHelper.cs b/src/Shentun.Utilities/FileHelper.cs index 50ca9a3..7151e14 100644 --- a/src/Shentun.Utilities/FileHelper.cs +++ b/src/Shentun.Utilities/FileHelper.cs @@ -48,5 +48,46 @@ namespace Shentun.Utilities } } + + public static string ToBase64str(string filename) + { + Stream stream; + string fileBase64; + if (filename.StartsWith("http")) + { + using (var httpClientHandler = new HttpClientHandler()) + { + using (var httpClient = new HttpClient(httpClientHandler)) + { + 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; + } + + } } }