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.

259 lines
12 KiB

  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Extensions.Configuration;
  3. using NPOI.SS.Formula.Functions;
  4. using NUglify.Helpers;
  5. using Shentun.Peis.DiagnosisFunctions;
  6. using Shentun.Peis.ImportLisResults;
  7. using Shentun.Peis.Items;
  8. using Shentun.Peis.Models;
  9. using Shentun.Peis.ReferenceRanges;
  10. using Shentun.Peis.RegisterCheckItems;
  11. using Shentun.Peis.RegisterChecks;
  12. using Shentun.Peis.Units;
  13. using Shentun.Utilities;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using Volo.Abp;
  21. using Volo.Abp.Application.Services;
  22. using Volo.Abp.Domain.Repositories;
  23. using Volo.Abp.Guids;
  24. using Volo.Abp.Identity;
  25. using Volo.Abp.Users;
  26. namespace Shentun.Peis.ImportPacsResults
  27. {
  28. public class ImportPacsResultAppService : ApplicationService
  29. {
  30. private readonly IRepository<RegisterCheckItem> _registerCheckItemRepository;
  31. private readonly IRepository<RegisterCheckPicture> _registerCheckPictureRepository;
  32. private readonly IRepository<PatientRegister, Guid> _patientRegisterRepository;
  33. private readonly IRepository<ReferenceRange, Guid> _referenceRangeRepository;
  34. private readonly IRepository<RegisterCheck, Guid> _registerCheckRepository;
  35. private readonly IRepository<RegisterCheckAsbitem, Guid> _registerCheckAsbitemRepository;
  36. private readonly IRepository<LisRequest, Guid> _lisRequestRepository;
  37. private readonly IRepository<Item, Guid> _itemRepository;
  38. private readonly IRepository<RegisterCheckSummary, Guid> _registerCheckSummaryRepository;
  39. private readonly IRepository<RegisterCheckSuggestion, Guid> _registerCheckSuggestionRepository;
  40. private readonly IRepository<IdentityUser, Guid> _userRepository;
  41. private readonly IRepository<Unit, Guid> _unitRepository;
  42. private readonly RegisterCheckManager _registerCheckManager;
  43. private readonly RegisterCheckItemManager _registerCheckItemManager;
  44. private readonly CacheService _cacheService;
  45. private readonly IGuidGenerator _guidGenerator;
  46. private readonly IConfiguration _configuration;
  47. private readonly ICurrentUser _currentUser;
  48. private readonly IRepository<UserItemType> _userItemTypeRepository;
  49. private readonly IRepository<Asbitem, Guid> _asbitemRepository;
  50. private readonly DiagnosisFunctionAppService _diagnosisFunctionAppService;
  51. private readonly ReferenceRangeManager _referenceRangeManager;
  52. private readonly ItemManager _itemManager;
  53. private readonly UnitManager _unitManager;
  54. private readonly RegisterCheckAppService _registerCheckAppService;
  55. private List<Unit> _units;
  56. private List<ReferenceRange> _referenceRanges;
  57. private static bool _isRunning = false;
  58. private static readonly object lockObject = new object();
  59. public ImportPacsResultAppService(IRepository<RegisterCheck, Guid> registerCheckRepository,
  60. IRepository<RegisterCheckItem> registerCheckItemRepository,
  61. IRepository<PatientRegister, Guid> patientRegisterRepository,
  62. IRepository<ReferenceRange, Guid> referenceRangeRepository,
  63. IRepository<RegisterCheckSummary, Guid> registerCheckSummaryRepository,
  64. IRepository<RegisterCheckSuggestion, Guid> registerCheckSuggestionRepository,
  65. IRepository<IdentityUser, Guid> userRepository,
  66. RegisterCheckManager registerCheckManager,
  67. RegisterCheckItemManager registerCheckItemManager,
  68. CacheService cacheService,
  69. IGuidGenerator guidGenerator,
  70. IConfiguration configuration,
  71. ICurrentUser currentUser,
  72. IRepository<UserItemType> userItemTypeRepository,
  73. IRepository<Asbitem, Guid> asbitemRepository,
  74. IRepository<RegisterCheckAsbitem, Guid> registerCheckAsbitemRepository,
  75. IRepository<LisRequest, Guid> lisRequestRepository,
  76. IRepository<Unit, Guid> unitRepository,
  77. DiagnosisFunctionAppService diagnosisFunctionAppService,
  78. ReferenceRangeManager referenceRangeManager,
  79. IRepository<Item, Guid> itemRepository,
  80. ItemManager itemManager,
  81. UnitManager unitManager,
  82. RegisterCheckAppService registerCheckAppService,
  83. IRepository<RegisterCheckPicture> registerCheckPictureRepository)
  84. {
  85. _registerCheckRepository = registerCheckRepository;
  86. _userRepository = userRepository;
  87. _registerCheckManager = registerCheckManager;
  88. _patientRegisterRepository = patientRegisterRepository;
  89. _referenceRangeRepository = referenceRangeRepository;
  90. _registerCheckItemRepository = registerCheckItemRepository;
  91. _registerCheckSummaryRepository = registerCheckSummaryRepository;
  92. _registerCheckSuggestionRepository = registerCheckSuggestionRepository;
  93. _registerCheckItemManager = registerCheckItemManager;
  94. _cacheService = cacheService;
  95. _guidGenerator = guidGenerator;
  96. _configuration = configuration;
  97. _currentUser = currentUser;
  98. _userItemTypeRepository = userItemTypeRepository;
  99. _asbitemRepository = asbitemRepository;
  100. _registerCheckAsbitemRepository = registerCheckAsbitemRepository;
  101. _lisRequestRepository = lisRequestRepository;
  102. _diagnosisFunctionAppService = diagnosisFunctionAppService;
  103. _referenceRangeManager = referenceRangeManager;
  104. _itemRepository = itemRepository;
  105. _itemManager = itemManager;
  106. _unitRepository = unitRepository;
  107. _units = _unitRepository.GetListAsync().Result;
  108. _unitManager = unitManager;
  109. _registerCheckAppService = registerCheckAppService;
  110. _registerCheckPictureRepository = registerCheckPictureRepository;
  111. }
  112. [HttpPost("api/app/ImportPacsResult/ImportResult")]
  113. public async Task ImportResultAsync(CreateImportPacsResultDto input)
  114. {
  115. //设置结果
  116. var list = (from patientRegister in await _patientRegisterRepository.GetQueryableAsync()
  117. join registerCheck in await _registerCheckRepository.GetQueryableAsync()
  118. on patientRegister.Id equals registerCheck.PatientRegisterId
  119. join registerCheckItem in await _registerCheckItemRepository.GetQueryableAsync()
  120. on registerCheck.Id equals registerCheckItem.RegisterCheckId
  121. join item in await _itemRepository.GetQueryableAsync()
  122. on registerCheckItem.ItemId equals item.Id
  123. where registerCheck.CheckRequestNo == input.CheckRequestNo
  124. select new
  125. {
  126. patientRegister,
  127. registerCheck,
  128. registerCheckItem,
  129. item,
  130. }).ToList();
  131. if (!list.Any())
  132. {
  133. return;
  134. }
  135. var firstEntity = list.First();
  136. if (string.IsNullOrWhiteSpace(input.Result))
  137. {
  138. return;
  139. }
  140. if (string.IsNullOrWhiteSpace(input.CheckDoctorName))
  141. {
  142. throw new UserFriendlyException("检查医生不能为空");
  143. }
  144. if (string.IsNullOrWhiteSpace(input.PatientName))
  145. {
  146. throw new UserFriendlyException("姓名不能为空");
  147. }
  148. if (input.PatientName != firstEntity.patientRegister.PatientName)
  149. {
  150. throw new UserFriendlyException($"Pacs姓名{input.PatientName}和体检系统姓名{firstEntity.patientRegister.PatientName}不一致");
  151. }
  152. var updateCheckResultDto = new UpdateCheckResultDto()
  153. {
  154. RegisterCheckId = firstEntity.registerCheck.Id,
  155. CheckDoctorId = input.CheckDoctorName,
  156. CheckDate = input.CheckDate,
  157. ExecOrganizationUnitId = input.ExecOrganizationUnitId,
  158. RegisterCheckItems = list
  159. .Select(o => new UpdateRegisterCheckItemDetail()
  160. {
  161. ItemId = o.registerCheckItem.ItemId,
  162. Result = input.Result,
  163. }).ToList()
  164. };
  165. //生成小结
  166. updateCheckResultDto.Summarys.Add(new UpdateRegisterCheckSummaryDetail()
  167. {
  168. Summary = input.Summary,
  169. SummaryFlag = '0'
  170. });
  171. //保存明细结果和小结
  172. await _registerCheckAppService.UpdateCheckResult(updateCheckResultDto);
  173. //保存图片
  174. string absolutePath = _configuration.GetValue<string>("VirtualPath:RealPath");
  175. //删除以前的图片
  176. var registerCheckPictures = await _registerCheckPictureRepository.GetListAsync(o => o.RegisterCheckId == firstEntity.registerCheck.Id);
  177. await _registerCheckPictureRepository.DeleteManyAsync(registerCheckPictures);
  178. //插入新图片
  179. string pictureUrl;
  180. foreach (var file in input.Files)
  181. {
  182. string fileName;
  183. if (string.IsNullOrWhiteSpace(file.FileName))
  184. {
  185. fileName = Guid.NewGuid().ToString();
  186. }
  187. else
  188. {
  189. fileName = Path.GetFileNameWithoutExtension(file.FileName);
  190. }
  191. //0-图片,1-PDF
  192. if (file.FileFormat == "0")
  193. {
  194. pictureUrl = ImageHelper.Base64StrToImageInAbsolutePath(absolutePath, fileName, file.FileBase64, firstEntity.patientRegister.Id.ToString(),
  195. firstEntity.registerCheck.Id.ToString());
  196. }
  197. else if (file.FileFormat == "1")
  198. {
  199. byte[] pdfBytes = Convert.FromBase64String(file.FileBase64);
  200. var tempFilePath = DirectoryHelper.GetAppDirectory() + "\\temp-files";
  201. if (!Directory.Exists(tempFilePath))
  202. {
  203. Directory.CreateDirectory(tempFilePath);
  204. }
  205. string tempPdf = tempFilePath + "\\temp.pdf";
  206. using (System.IO.FileStream stream = new System.IO.FileStream(tempPdf, System.IO.FileMode.Create))
  207. {
  208. //通过流的方式写入
  209. using(System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream) )
  210. {
  211. writer.Write(pdfBytes, 0, pdfBytes.Length);
  212. writer.Close();
  213. }
  214. }
  215. PDFHelper.ConvertPdfToImage(tempPdf, tempFilePath ,"tempImage");
  216. pictureUrl = ImageHelper.SavePacsFile(absolutePath, tempFilePath + "\\tempImage_0.jpg", firstEntity.patientRegister.Id.ToString(),
  217. firstEntity.registerCheck.Id.ToString(), fileName);
  218. }
  219. else
  220. {
  221. throw new UserFriendlyException("不支持的文件类型");
  222. }
  223. if (string.IsNullOrEmpty(pictureUrl))
  224. {
  225. throw new UserFriendlyException("pictureUrl是空值");
  226. }
  227. var registerCheckPicture = new RegisterCheckPicture
  228. {
  229. DisplayOrder = input.Files.IndexOf(file) + 1,
  230. IsPrint = file.IsPrint,
  231. PictureFilename = pictureUrl,
  232. RegisterCheckId = firstEntity.registerCheck.Id,
  233. };
  234. await _registerCheckPictureRepository.InsertAsync(registerCheckPicture);
  235. }
  236. }
  237. }
  238. }