You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
17 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Configuration;
  4. using NPOI.SS.Formula.Functions;
  5. using NUglify.Helpers;
  6. using Shentun.Peis.DiagnosisFunctions;
  7. using Shentun.Peis.Enums;
  8. using Shentun.Peis.ImportLisResults;
  9. using Shentun.Peis.Items;
  10. using Shentun.Peis.Models;
  11. using Shentun.Peis.PatientRegisters;
  12. using Shentun.Peis.PlugIns.ImportPacsResults;
  13. using Shentun.Peis.ReferenceRanges;
  14. using Shentun.Peis.RegisterCheckItems;
  15. using Shentun.Peis.RegisterChecks;
  16. using Shentun.Peis.Units;
  17. using Shentun.Utilities;
  18. using System;
  19. using System.Collections.Generic;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Security.Cryptography.Xml;
  23. using System.Text;
  24. using System.Threading.Tasks;
  25. using Volo.Abp;
  26. using Volo.Abp.Application.Services;
  27. using Volo.Abp.Domain.Repositories;
  28. using Volo.Abp.Guids;
  29. using Volo.Abp.Identity;
  30. using Volo.Abp.Users;
  31. namespace Shentun.Peis.ImportPacsResults
  32. {
  33. [ApiExplorerSettings(GroupName = "Work")]
  34. [Authorize]
  35. public class ImportPacsResultAppService : ApplicationService
  36. {
  37. private readonly IRepository<ThirdInterface> _thirdInterfaceRepository;
  38. private readonly IRepository<RegisterCheckItem> _registerCheckItemRepository;
  39. private readonly IRepository<RegisterCheckPicture> _registerCheckPictureRepository;
  40. private readonly IRepository<PatientRegister, Guid> _patientRegisterRepository;
  41. private readonly IRepository<ReferenceRange, Guid> _referenceRangeRepository;
  42. private readonly IRepository<RegisterCheck, Guid> _registerCheckRepository;
  43. private readonly IRepository<RegisterCheckAsbitem, Guid> _registerCheckAsbitemRepository;
  44. private readonly IRepository<LisRequest, Guid> _lisRequestRepository;
  45. private readonly IRepository<Item, Guid> _itemRepository;
  46. private readonly IRepository<RegisterCheckSummary, Guid> _registerCheckSummaryRepository;
  47. private readonly IRepository<RegisterCheckSuggestion, Guid> _registerCheckSuggestionRepository;
  48. private readonly IRepository<IdentityUser, Guid> _userRepository;
  49. private readonly IRepository<Unit, Guid> _unitRepository;
  50. private readonly RegisterCheckManager _registerCheckManager;
  51. private readonly RegisterCheckItemManager _registerCheckItemManager;
  52. private readonly CacheService _cacheService;
  53. private readonly IGuidGenerator _guidGenerator;
  54. private readonly IConfiguration _configuration;
  55. private readonly ICurrentUser _currentUser;
  56. private readonly IRepository<UserItemType> _userItemTypeRepository;
  57. private readonly IRepository<Asbitem, Guid> _asbitemRepository;
  58. private readonly DiagnosisFunctionAppService _diagnosisFunctionAppService;
  59. private readonly ReferenceRangeManager _referenceRangeManager;
  60. private readonly ItemManager _itemManager;
  61. private readonly UnitManager _unitManager;
  62. private readonly RegisterCheckAppService _registerCheckAppService;
  63. private List<Unit> _units;
  64. private List<ReferenceRange> _referenceRanges;
  65. private static bool _isRunning = false;
  66. private static readonly object lockObject = new object();
  67. public ImportPacsResultAppService(IRepository<RegisterCheck, Guid> registerCheckRepository,
  68. IRepository<RegisterCheckItem> registerCheckItemRepository,
  69. IRepository<PatientRegister, Guid> patientRegisterRepository,
  70. IRepository<ReferenceRange, Guid> referenceRangeRepository,
  71. IRepository<RegisterCheckSummary, Guid> registerCheckSummaryRepository,
  72. IRepository<RegisterCheckSuggestion, Guid> registerCheckSuggestionRepository,
  73. IRepository<IdentityUser, Guid> userRepository,
  74. RegisterCheckManager registerCheckManager,
  75. RegisterCheckItemManager registerCheckItemManager,
  76. CacheService cacheService,
  77. IGuidGenerator guidGenerator,
  78. IConfiguration configuration,
  79. ICurrentUser currentUser,
  80. IRepository<UserItemType> userItemTypeRepository,
  81. IRepository<Asbitem, Guid> asbitemRepository,
  82. IRepository<RegisterCheckAsbitem, Guid> registerCheckAsbitemRepository,
  83. IRepository<LisRequest, Guid> lisRequestRepository,
  84. IRepository<Unit, Guid> unitRepository,
  85. DiagnosisFunctionAppService diagnosisFunctionAppService,
  86. ReferenceRangeManager referenceRangeManager,
  87. IRepository<Item, Guid> itemRepository,
  88. ItemManager itemManager,
  89. UnitManager unitManager,
  90. RegisterCheckAppService registerCheckAppService,
  91. IRepository<RegisterCheckPicture> registerCheckPictureRepository,
  92. IRepository<ThirdInterface> thirdInterfaceRepository)
  93. {
  94. _registerCheckRepository = registerCheckRepository;
  95. _userRepository = userRepository;
  96. _registerCheckManager = registerCheckManager;
  97. _patientRegisterRepository = patientRegisterRepository;
  98. _referenceRangeRepository = referenceRangeRepository;
  99. _registerCheckItemRepository = registerCheckItemRepository;
  100. _registerCheckSummaryRepository = registerCheckSummaryRepository;
  101. _registerCheckSuggestionRepository = registerCheckSuggestionRepository;
  102. _registerCheckItemManager = registerCheckItemManager;
  103. _cacheService = cacheService;
  104. _guidGenerator = guidGenerator;
  105. _configuration = configuration;
  106. _currentUser = currentUser;
  107. _userItemTypeRepository = userItemTypeRepository;
  108. _asbitemRepository = asbitemRepository;
  109. _registerCheckAsbitemRepository = registerCheckAsbitemRepository;
  110. _lisRequestRepository = lisRequestRepository;
  111. _diagnosisFunctionAppService = diagnosisFunctionAppService;
  112. _referenceRangeManager = referenceRangeManager;
  113. _itemRepository = itemRepository;
  114. _itemManager = itemManager;
  115. _unitRepository = unitRepository;
  116. _units = _unitRepository.GetListAsync().Result;
  117. _unitManager = unitManager;
  118. _registerCheckAppService = registerCheckAppService;
  119. _registerCheckPictureRepository = registerCheckPictureRepository;
  120. _thirdInterfaceRepository = thirdInterfaceRepository;
  121. }
  122. [HttpPost("api/app/ImportPacsResult/ImportResult")]
  123. public async Task ImportResultAsync(CreateImportPacsResultDto input)
  124. {
  125. if (input == null)
  126. {
  127. throw new UserFriendlyException("input参数不能为空");
  128. }
  129. if(string.IsNullOrWhiteSpace(input.CheckRequestNo))
  130. {
  131. throw new UserFriendlyException("CheckRequestNo不能为空");
  132. }
  133. //设置结果
  134. var list = (from patientRegister in await _patientRegisterRepository.GetQueryableAsync()
  135. join registerCheck in await _registerCheckRepository.GetQueryableAsync()
  136. on patientRegister.Id equals registerCheck.PatientRegisterId
  137. join registerCheckItem in await _registerCheckItemRepository.GetQueryableAsync()
  138. on registerCheck.Id equals registerCheckItem.RegisterCheckId
  139. join item in await _itemRepository.GetQueryableAsync()
  140. on registerCheckItem.ItemId equals item.Id
  141. where registerCheck.CheckRequestNo == input.CheckRequestNo
  142. select new
  143. {
  144. patientRegister,
  145. registerCheck,
  146. registerCheckItem,
  147. item,
  148. }).ToList();
  149. if (!list.Any())
  150. {
  151. return;
  152. }
  153. var firstEntity = list.First();
  154. if (string.IsNullOrWhiteSpace(input.Result))
  155. {
  156. return;
  157. }
  158. if (string.IsNullOrWhiteSpace(input.CheckDoctorName))
  159. {
  160. throw new UserFriendlyException("检查医生不能为空");
  161. }
  162. if (string.IsNullOrWhiteSpace(input.PatientName))
  163. {
  164. throw new UserFriendlyException("姓名不能为空");
  165. }
  166. if (input.PatientName != firstEntity.patientRegister.PatientName)
  167. {
  168. throw new UserFriendlyException($"Pacs姓名{input.PatientName}和体检系统姓名{firstEntity.patientRegister.PatientName}不一致");
  169. }
  170. var updateCheckResultDto = new UpdateCheckResultDto()
  171. {
  172. RegisterCheckId = firstEntity.registerCheck.Id,
  173. CheckDoctorId = input.CheckDoctorName,
  174. CheckDate = input.CheckDate,
  175. ExecOrganizationUnitId = input.ExecOrganizationUnitId,
  176. RegisterCheckItems = list
  177. .Select(o => new UpdateRegisterCheckItemDetail()
  178. {
  179. ItemId = o.registerCheckItem.ItemId,
  180. Result = input.Result,
  181. }).ToList()
  182. };
  183. //生成小结
  184. updateCheckResultDto.Summarys.Add(new UpdateRegisterCheckSummaryDetail()
  185. {
  186. Summary = input.Summary,
  187. SummaryFlag = '0'
  188. });
  189. //保存明细结果和小结
  190. await _registerCheckAppService.UpdateCheckResult(updateCheckResultDto);
  191. //保存图片
  192. string absolutePath = _configuration.GetValue<string>("VirtualPath:RealPath");
  193. //删除以前的图片
  194. var registerCheckPictures = await _registerCheckPictureRepository.GetListAsync(o => o.RegisterCheckId == firstEntity.registerCheck.Id);
  195. await _registerCheckPictureRepository.DeleteManyAsync(registerCheckPictures);
  196. //插入新图片
  197. string pictureUrl;
  198. foreach (var file in input.Files)
  199. {
  200. if(string.IsNullOrWhiteSpace(file.FileTransMode))
  201. {
  202. throw new UserFriendlyException("文件传输模式不能为空");
  203. }
  204. if (string.IsNullOrWhiteSpace(file.FileFormat))
  205. {
  206. throw new UserFriendlyException("文件格式不能为空");
  207. }
  208. if(file.FileTransMode != "0" && file.FileTransMode != "1")
  209. {
  210. throw new UserFriendlyException("文件传输模式不正确");
  211. }
  212. if (file.FileFormat != "0" && file.FileFormat != "1")
  213. {
  214. throw new UserFriendlyException("文件格式不正确");
  215. }
  216. string fileName;
  217. if (string.IsNullOrWhiteSpace(file.FileName))
  218. {
  219. fileName = Guid.NewGuid().ToString();
  220. }
  221. else
  222. {
  223. fileName = Path.GetFileNameWithoutExtension(file.FileName);
  224. }
  225. if (file.FileTransMode == "1")
  226. {
  227. if(string.IsNullOrWhiteSpace(file.FileUrl))
  228. {
  229. throw new UserFriendlyException("文件URL不能为空");
  230. }
  231. if (!string.IsNullOrWhiteSpace(file.FileBase64))
  232. {
  233. throw new UserFriendlyException("文件Base64不能传值");
  234. }
  235. file.FileBase64 = Shentun.Utilities.FileHelper.ToBase64(file.FileUrl);
  236. }
  237. else
  238. {
  239. if (string.IsNullOrWhiteSpace(file.FileBase64))
  240. {
  241. throw new UserFriendlyException("文件Base64不能为空");
  242. }
  243. }
  244. //0-图片,1-PDF
  245. if (file.FileFormat == "0")
  246. {
  247. pictureUrl = ImageHelper.Base64ToImageUseAbsolutePath(absolutePath, fileName, file.FileBase64, firstEntity.patientRegister.Id.ToString(),
  248. firstEntity.registerCheck.Id.ToString());
  249. }
  250. else if (file.FileFormat == "1")
  251. {
  252. byte[] pdfBytes = Convert.FromBase64String(file.FileBase64);
  253. var tempFilePath = DirectoryHelper.GetAppDirectory() + "\\temp-files";
  254. if (!Directory.Exists(tempFilePath))
  255. {
  256. Directory.CreateDirectory(tempFilePath);
  257. }
  258. string tempPdf = tempFilePath + "\\temp.pdf";
  259. using (System.IO.FileStream stream = new System.IO.FileStream(tempPdf, System.IO.FileMode.Create))
  260. {
  261. //通过流的方式写入
  262. using(System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream) )
  263. {
  264. writer.Write(pdfBytes, 0, pdfBytes.Length);
  265. writer.Close();
  266. }
  267. }
  268. PDFHelper.ConvertPdfToImage(tempPdf, tempFilePath ,"tempImage");
  269. pictureUrl = ImageHelper.SavePacsFile(absolutePath, tempFilePath + "\\tempImage_0.jpg", firstEntity.patientRegister.Id.ToString(),
  270. firstEntity.registerCheck.Id.ToString(), fileName);
  271. }
  272. else
  273. {
  274. throw new UserFriendlyException("不支持的文件类型");
  275. }
  276. if (string.IsNullOrWhiteSpace(pictureUrl))
  277. {
  278. throw new UserFriendlyException("pictureUrl不能是空值");
  279. }
  280. var registerCheckPicture = new RegisterCheckPicture
  281. {
  282. DisplayOrder = input.Files.IndexOf(file) + 1,
  283. IsPrint = file.IsPrint,
  284. PictureFilename = pictureUrl,
  285. RegisterCheckId = firstEntity.registerCheck.Id,
  286. };
  287. await _registerCheckPictureRepository.InsertAsync(registerCheckPicture);
  288. }
  289. }
  290. [HttpPost("api/app/ImportPacsResult/ImportResultByPatientRegisterId")]
  291. public async Task ImportResultByPatientRegisterIdAsync(PatientRegisterIdInputDto input)
  292. {
  293. var patientRegister = await _patientRegisterRepository.GetAsync(input.PatientRegisterId);
  294. var thirdInterfaces = await _thirdInterfaceRepository.GetListAsync(o =>
  295. o.ThirdInterfaceType == ThirdInterfaceTypeFlag.ImportPacsResult
  296. && o.MedicalCenterId == patientRegister.MedicalCenterId
  297. && o.IsActive == 'Y');
  298. foreach (var thirdInterface in thirdInterfaces)
  299. {
  300. var parmValue = thirdInterface.ParmValue;
  301. var configurationBuilder = new ConfigurationBuilder()
  302. .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
  303. var config = configurationBuilder.Build();
  304. var assemblyName = config.GetSection("Interface").GetSection("AssemblyName").Value;
  305. var className = config.GetSection("Interface").GetSection("ClassName").Value;
  306. object[] objects = new object[] { input.PatientRegisterId };
  307. var pluginsOut = await Utilities.ReflectionHelper.InvokeAsync<ImportPacsResultPlugInsOut>(assemblyName,
  308. className, [thirdInterface.Id], "ImportResultByPatientRegisterIdAsync", objects);
  309. }
  310. }
  311. /// <summary>
  312. /// 导入心电图
  313. /// </summary>
  314. /// <param name="input"></param>
  315. /// <returns></returns>
  316. [HttpPost("api/app/ImportElectrocardiogramResult/ImportElectrocardiogramResultByPatientRegisterId")]
  317. public async Task ImportElectrocardiogramResultByPatientRegisterIdAsync(PatientRegisterIdInputDto input)
  318. {
  319. var patientRegister = await _patientRegisterRepository.GetAsync(input.PatientRegisterId);
  320. var thirdInterfaces = await _thirdInterfaceRepository.GetListAsync(o =>
  321. o.ThirdInterfaceType == ThirdInterfaceTypeFlag.Electrocardiogram
  322. && o.MedicalCenterId == patientRegister.MedicalCenterId
  323. && o.IsActive == 'Y');
  324. foreach (var thirdInterface in thirdInterfaces)
  325. {
  326. var parmValue = thirdInterface.ParmValue;
  327. var configurationBuilder = new ConfigurationBuilder()
  328. .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
  329. var config = configurationBuilder.Build();
  330. var assemblyName = config.GetSection("Interface").GetSection("AssemblyName").Value;
  331. var className = config.GetSection("Interface").GetSection("ClassName").Value;
  332. object[] objects = new object[] { input.PatientRegisterId };
  333. var pluginsOut = await Utilities.ReflectionHelper.InvokeAsync<ImportPacsResultPlugInsOut>(assemblyName,
  334. className, [thirdInterface.Id], "ImportResultByPatientRegisterIdAsync", objects);
  335. }
  336. }
  337. }
  338. }