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.

262 lines
12 KiB

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.ImportLisResults;
using Shentun.Peis.Items;
using Shentun.Peis.Models;
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.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<RegisterCheckItem> _registerCheckItemRepository;
private readonly IRepository<RegisterCheckPicture> _registerCheckPictureRepository;
private readonly IRepository<PatientRegister, Guid> _patientRegisterRepository;
private readonly IRepository<ReferenceRange, Guid> _referenceRangeRepository;
private readonly IRepository<RegisterCheck, Guid> _registerCheckRepository;
private readonly IRepository<RegisterCheckAsbitem, Guid> _registerCheckAsbitemRepository;
private readonly IRepository<LisRequest, Guid> _lisRequestRepository;
private readonly IRepository<Item, Guid> _itemRepository;
private readonly IRepository<RegisterCheckSummary, Guid> _registerCheckSummaryRepository;
private readonly IRepository<RegisterCheckSuggestion, Guid> _registerCheckSuggestionRepository;
private readonly IRepository<IdentityUser, Guid> _userRepository;
private readonly IRepository<Unit, Guid> _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<UserItemType> _userItemTypeRepository;
private readonly IRepository<Asbitem, Guid> _asbitemRepository;
private readonly DiagnosisFunctionAppService _diagnosisFunctionAppService;
private readonly ReferenceRangeManager _referenceRangeManager;
private readonly ItemManager _itemManager;
private readonly UnitManager _unitManager;
private readonly RegisterCheckAppService _registerCheckAppService;
private List<Unit> _units;
private List<ReferenceRange> _referenceRanges;
private static bool _isRunning = false;
private static readonly object lockObject = new object();
public ImportPacsResultAppService(IRepository<RegisterCheck, Guid> registerCheckRepository,
IRepository<RegisterCheckItem> registerCheckItemRepository,
IRepository<PatientRegister, Guid> patientRegisterRepository,
IRepository<ReferenceRange, Guid> referenceRangeRepository,
IRepository<RegisterCheckSummary, Guid> registerCheckSummaryRepository,
IRepository<RegisterCheckSuggestion, Guid> registerCheckSuggestionRepository,
IRepository<IdentityUser, Guid> userRepository,
RegisterCheckManager registerCheckManager,
RegisterCheckItemManager registerCheckItemManager,
CacheService cacheService,
IGuidGenerator guidGenerator,
IConfiguration configuration,
ICurrentUser currentUser,
IRepository<UserItemType> userItemTypeRepository,
IRepository<Asbitem, Guid> asbitemRepository,
IRepository<RegisterCheckAsbitem, Guid> registerCheckAsbitemRepository,
IRepository<LisRequest, Guid> lisRequestRepository,
IRepository<Unit, Guid> unitRepository,
DiagnosisFunctionAppService diagnosisFunctionAppService,
ReferenceRangeManager referenceRangeManager,
IRepository<Item, Guid> itemRepository,
ItemManager itemManager,
UnitManager unitManager,
RegisterCheckAppService registerCheckAppService,
IRepository<RegisterCheckPicture> registerCheckPictureRepository)
{
_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;
}
[HttpPost("api/app/ImportPacsResult/ImportResult")]
public async Task ImportResultAsync(CreateImportPacsResultDto input)
{
//设置结果
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<string>("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)
{
string fileName;
if (string.IsNullOrWhiteSpace(file.FileName))
{
fileName = Guid.NewGuid().ToString();
}
else
{
fileName = Path.GetFileNameWithoutExtension(file.FileName);
}
//0-图片,1-PDF
if (file.FileFormat == "0")
{
pictureUrl = ImageHelper.Base64StrToImageInAbsolutePath(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.IsNullOrEmpty(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);
}
}
}
}