using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using NPOI.SS.Formula.Functions; using NUglify.Helpers; using Shentun.Peis.DiagnosisFunctions; using Shentun.Peis.Enums; using Shentun.Peis.ImportLisResults; using Shentun.Peis.Items; using Shentun.Peis.Models; using Shentun.Peis.PatientRegisters; using Shentun.Peis.PlugIns.ImportPacsResults; using Shentun.Peis.ReferenceRanges; using Shentun.Peis.RegisterCheckItems; using Shentun.Peis.RegisterChecks; using Shentun.Peis.Units; using Shentun.Utilities; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography.Xml; using System.Text; using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; using Volo.Abp.Guids; using Volo.Abp.Identity; using Volo.Abp.Users; namespace Shentun.Peis.ImportPacsResults { [ApiExplorerSettings(GroupName = "Work")] [Authorize] public class ImportPacsResultAppService : ApplicationService { private readonly IRepository _thirdInterfaceRepository; private readonly IRepository _registerCheckItemRepository; private readonly IRepository _registerCheckPictureRepository; private readonly IRepository _patientRegisterRepository; private readonly IRepository _referenceRangeRepository; private readonly IRepository _registerCheckRepository; private readonly IRepository _registerCheckAsbitemRepository; private readonly IRepository _lisRequestRepository; private readonly IRepository _itemRepository; private readonly IRepository _registerCheckSummaryRepository; private readonly IRepository _registerCheckSuggestionRepository; private readonly IRepository _userRepository; private readonly IRepository _unitRepository; private readonly RegisterCheckManager _registerCheckManager; private readonly RegisterCheckItemManager _registerCheckItemManager; private readonly CacheService _cacheService; private readonly IGuidGenerator _guidGenerator; private readonly IConfiguration _configuration; private readonly ICurrentUser _currentUser; private readonly IRepository _userItemTypeRepository; private readonly IRepository _asbitemRepository; private readonly DiagnosisFunctionAppService _diagnosisFunctionAppService; private readonly ReferenceRangeManager _referenceRangeManager; private readonly ItemManager _itemManager; private readonly UnitManager _unitManager; private readonly RegisterCheckAppService _registerCheckAppService; private List _units; private List _referenceRanges; private static bool _isRunning = false; private static readonly object lockObject = new object(); public ImportPacsResultAppService(IRepository registerCheckRepository, IRepository registerCheckItemRepository, IRepository patientRegisterRepository, IRepository referenceRangeRepository, IRepository registerCheckSummaryRepository, IRepository registerCheckSuggestionRepository, IRepository userRepository, RegisterCheckManager registerCheckManager, RegisterCheckItemManager registerCheckItemManager, CacheService cacheService, IGuidGenerator guidGenerator, IConfiguration configuration, ICurrentUser currentUser, IRepository userItemTypeRepository, IRepository asbitemRepository, IRepository registerCheckAsbitemRepository, IRepository lisRequestRepository, IRepository unitRepository, DiagnosisFunctionAppService diagnosisFunctionAppService, ReferenceRangeManager referenceRangeManager, IRepository itemRepository, ItemManager itemManager, UnitManager unitManager, RegisterCheckAppService registerCheckAppService, IRepository registerCheckPictureRepository, IRepository thirdInterfaceRepository) { _registerCheckRepository = registerCheckRepository; _userRepository = userRepository; _registerCheckManager = registerCheckManager; _patientRegisterRepository = patientRegisterRepository; _referenceRangeRepository = referenceRangeRepository; _registerCheckItemRepository = registerCheckItemRepository; _registerCheckSummaryRepository = registerCheckSummaryRepository; _registerCheckSuggestionRepository = registerCheckSuggestionRepository; _registerCheckItemManager = registerCheckItemManager; _cacheService = cacheService; _guidGenerator = guidGenerator; _configuration = configuration; _currentUser = currentUser; _userItemTypeRepository = userItemTypeRepository; _asbitemRepository = asbitemRepository; _registerCheckAsbitemRepository = registerCheckAsbitemRepository; _lisRequestRepository = lisRequestRepository; _diagnosisFunctionAppService = diagnosisFunctionAppService; _referenceRangeManager = referenceRangeManager; _itemRepository = itemRepository; _itemManager = itemManager; _unitRepository = unitRepository; _units = _unitRepository.GetListAsync().Result; _unitManager = unitManager; _registerCheckAppService = registerCheckAppService; _registerCheckPictureRepository = registerCheckPictureRepository; _thirdInterfaceRepository = thirdInterfaceRepository; } [HttpPost("api/app/ImportPacsResult/ImportResult")] public async Task ImportResultAsync(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.ToBase64(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(); } } PDFHelper.ConvertPdfToImage(tempPdf, tempFilePath ,"tempImage"); pictureUrl = ImageHelper.SavePacsFile(absolutePath, tempFilePath + "\\tempImage_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); } } [HttpPost("api/app/ImportPacsResult/ImportResultByPatientRegisterId")] public async Task ImportResultByPatientRegisterIdAsync(PatientRegisterIdInputDto input) { var patientRegister = await _patientRegisterRepository.GetAsync(input.PatientRegisterId); var thirdInterfaces = await _thirdInterfaceRepository.GetListAsync(o => o.ThirdInterfaceType == ThirdInterfaceTypeFlag.ImportPacsResult && o.MedicalCenterId == patientRegister.MedicalCenterId && o.IsActive == 'Y'); foreach (var thirdInterface in thirdInterfaces) { var parmValue = thirdInterface.ParmValue; var configurationBuilder = new ConfigurationBuilder() .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue))); var config = configurationBuilder.Build(); var assemblyName = config.GetSection("Interface").GetSection("AssemblyName").Value; var className = config.GetSection("Interface").GetSection("ClassName").Value; object[] objects = new object[] { input.PatientRegisterId }; var pluginsOut = await Utilities.ReflectionHelper.InvokeAsync(assemblyName, className, [thirdInterface.Id], "ImportResultByPatientRegisterIdAsync", objects); } } /// /// 导入心电图 /// /// /// [HttpPost("api/app/ImportElectrocardiogramResult/ImportElectrocardiogramResultByPatientRegisterId")] public async Task ImportElectrocardiogramResultByPatientRegisterIdAsync(PatientRegisterIdInputDto input) { var patientRegister = await _patientRegisterRepository.GetAsync(input.PatientRegisterId); var thirdInterfaces = await _thirdInterfaceRepository.GetListAsync(o => o.ThirdInterfaceType == ThirdInterfaceTypeFlag.Electrocardiogram && o.MedicalCenterId == patientRegister.MedicalCenterId && o.IsActive == 'Y'); foreach (var thirdInterface in thirdInterfaces) { var parmValue = thirdInterface.ParmValue; var configurationBuilder = new ConfigurationBuilder() .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue))); var config = configurationBuilder.Build(); var assemblyName = config.GetSection("Interface").GetSection("AssemblyName").Value; var className = config.GetSection("Interface").GetSection("ClassName").Value; object[] objects = new object[] { input.PatientRegisterId }; var pluginsOut = await Utilities.ReflectionHelper.InvokeAsync(assemblyName, className, [thirdInterface.Id], "ImportResultByPatientRegisterIdAsync", objects); } } } }