324 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			324 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C#
		
	
	
| using IRaCIS.Core.Application.Helper;
 | |
| using IRaCIS.Core.Application.Service.Reading.Dto;
 | |
| using IRaCIS.Core.Application.ViewModel;
 | |
| using IRaCIS.Core.Domain.Models;
 | |
| using IRaCIS.Core.Domain.Share;
 | |
| using IRaCIS.Core.Infrastructure;
 | |
| using Microsoft.AspNetCore.Hosting;
 | |
| using Microsoft.AspNetCore.Http;
 | |
| using Microsoft.AspNetCore.Mvc;
 | |
| using MiniExcelLibs;
 | |
| using System.Data;
 | |
| using System.IO;
 | |
| 
 | |
| namespace IRaCIS.Core.Application.Service.ReadingCalculate
 | |
| {
 | |
|     [ApiExplorerSettings(GroupName = "Image")]
 | |
|     public class ReadingCalculateService(IEnumerable<ICriterionCalculateService> _criterionServices,
 | |
|        IRepository<VisitTask> _visitTaskRepository,
 | |
|          IRepository<ReadingImportFile> _readingImportFileRepository,
 | |
|           IHttpContextAccessor httpContext,
 | |
|           IOSSService oSSService,
 | |
|        IRepository<ReadingQuestionCriterionTrial> _readingQuestionCriterionTrialRepository,
 | |
|        IStringLocalizer _localizer, IUserInfo _userInfo
 | |
| 
 | |
|        ) : BaseService, IReadingCalculateService
 | |
|     {
 | |
|         private ICriterionCalculateService _useCriterion;
 | |
|         /// <summary>
 | |
|         /// 标准和服务对应
 | |
|         /// </summary>
 | |
|         Dictionary<CriterionType, Type> CalculateServiceDic = new Dictionary<CriterionType, Type>()
 | |
|         {
 | |
|             {CriterionType.RECIST1Point1,typeof(RECIST1Point1CalculateService) }, //RECIST1.1
 | |
|             {CriterionType.PCWG3,typeof(PCWG3CalculateService) },
 | |
|             {CriterionType.SelfDefine,typeof(SelfDefineCalculateService) },
 | |
|             {CriterionType.RECIST1Pointt1_MB,typeof(RECIST1Point1_BMCalculateService) },
 | |
|             {CriterionType.IRECIST1Point1,typeof(IRECIST1Point1CalculateService) },
 | |
|             {CriterionType.Lugano2014,typeof(LuganoCalculateService) },
 | |
|              {CriterionType.Lugano2014WithoutPET,typeof(LuganoWithoutPETCalculateService) },
 | |
|             {CriterionType.IVUS,typeof(IVUSCalculateService) },
 | |
|             {CriterionType.OCT,typeof(OCTCalculateService) },
 | |
|             {CriterionType.MRIPDFF,typeof(MRIPDFFCalculateService) },
 | |
|             {CriterionType.mRECISTHCC,typeof(MRECISTHCCCalculateService) },
 | |
|         };
 | |
| 
 | |
| 
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 阅片导入
 | |
|         /// </summary>
 | |
|         /// <returns></returns>
 | |
|         [HttpPost]
 | |
|         public async Task ReadingImport()
 | |
|         {
 | |
|             var request = httpContext.HttpContext!.Request;
 | |
|             var file = request.Form.Files[0];
 | |
|             Guid visitTaskId = Guid.Parse(request.Form["VisitTaskId"]);
 | |
| 
 | |
|             string tableName = request.Form["TableName"].ToString();
 | |
| 
 | |
|             var service = await this.GetService(visitTaskId);
 | |
|             if (service != null)
 | |
|             {
 | |
|                 var visitTaskInfo = await _visitTaskRepository.Where(x => x.Id == visitTaskId).FirstNotNullAsync();
 | |
| 
 | |
|                 var streamCopy = new MemoryStream();
 | |
|                 // 将上传的文件内容复制到 MemoryStream
 | |
|                 await file.CopyToAsync(streamCopy);
 | |
|                 // 重置流的位置,以便后续读取
 | |
|                 streamCopy.Position = 0;
 | |
|                 var ossRelativePath = await oSSService.UploadToOSSAsync(streamCopy, $"{visitTaskInfo.TrialId.ToString()}/InspectionUpload/ReadingImport", file.FileName);
 | |
| 
 | |
| 
 | |
|                 await _readingImportFileRepository.AddAsync(new ReadingImportFile()
 | |
|                 {
 | |
|                  
 | |
|                     FilePath = ossRelativePath,
 | |
|                     VisitTaskId = visitTaskId,
 | |
|                     TrialId = visitTaskInfo.TrialId,
 | |
|                     SubjectId = visitTaskInfo.SubjectId,
 | |
|                     SubjectVisitId = visitTaskInfo.SourceSubjectVisitId,
 | |
|                     TrialReadingCriterionId = visitTaskInfo.TrialReadingCriterionId,
 | |
|                     TableName= tableName,
 | |
| 
 | |
|                 });
 | |
| 
 | |
| 
 | |
|                 await service.ReadingImport();
 | |
|             }
 | |
|         }
 | |
| 
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 获取Service
 | |
|         /// </summary>
 | |
|         /// <param name="visitTaskId"></param>
 | |
|         /// <returns></returns>
 | |
|         public async Task<ICriterionCalculateService> GetService(Guid visitTaskId)
 | |
|         {
 | |
|             if (_useCriterion == null)
 | |
|             {
 | |
|                 var criterionId = await _visitTaskRepository.Where(x => x.Id == visitTaskId).Select(x => x.TrialReadingCriterionId).FirstNotNullAsync();
 | |
| 
 | |
|                 var criterionType = await _readingQuestionCriterionTrialRepository.Where(x => x.Id == criterionId).Select(x => x.CriterionType).FirstOrDefaultAsync();
 | |
| 
 | |
|                 if (criterionType == null)
 | |
|                 {
 | |
|                     throw new BusinessValidationFailedException(_localizer["ReadingCalculate_NoDeveloped"]);
 | |
|                 }
 | |
| 
 | |
|                 try
 | |
|                 {
 | |
|                     CriterionType thisCriterionType = criterionType;
 | |
|                     Type thisServiceType = CalculateServiceDic[thisCriterionType];
 | |
|                     _useCriterion = _criterionServices.FirstOrDefault(x => x.GetType().Name == thisServiceType.Name);
 | |
| 
 | |
| 
 | |
| 
 | |
|                 }
 | |
|                 catch (Exception)
 | |
|                 {
 | |
| 
 | |
|                     _useCriterion = null;
 | |
|                 }
 | |
| 
 | |
|                 return _useCriterion;
 | |
| 
 | |
| 
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 return _useCriterion;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 自动计算 并修改值
 | |
|         /// </summary>
 | |
|         /// <param name="inDto"></param>
 | |
|         /// <returns></returns>
 | |
|         public async Task CalculateTask(CalculateTaskInDto inDto)
 | |
|         {
 | |
| 
 | |
|             _userInfo.IsNotNeedInspection = true;
 | |
|             var service = await this.GetService(inDto.VisitTaskId);
 | |
|             if (service != null)
 | |
|             {
 | |
|                 await service.CalculateTask(inDto);
 | |
|             }
 | |
| 
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 验证访视提交
 | |
|         /// </summary>
 | |
|         /// <param name="inDto"></param>
 | |
|         /// <returns></returns>
 | |
|         public async Task VerifyVisitTaskQuestions(VerifyVisitTaskQuestionsInDto inDto)
 | |
|         {
 | |
|             var service = await this.GetService(inDto.VisitTaskId);
 | |
|             if (service != null)
 | |
|             {
 | |
|                 await service.VerifyVisitTaskQuestions(inDto);
 | |
|             }
 | |
| 
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 获取阅片的计算数据
 | |
|         /// </summary>
 | |
|         /// <param name="inDto"></param>
 | |
|         /// <returns></returns>
 | |
|         [HttpPost]
 | |
|         public async Task<object> GetReadingCalculationData(GetReadingCalculationDataInDto inDto)
 | |
|         {
 | |
|             var service = await this.GetService(inDto.VisitTaskId);
 | |
| 
 | |
|             return await service.GetReadingCalculationData(inDto);
 | |
|         }
 | |
| 
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 将上一次的访视病灶添加到这一次
 | |
|         /// </summary>
 | |
|         /// <param name="inDto"></param>
 | |
|         /// <returns></returns>
 | |
|         public async Task<AddTaskLesionAnswerFromLastTaskOutDto> AddTaskLesionAnswerFromLastTask(AddTaskLesionAnswerFromLastTaskInDto inDto)
 | |
|         {
 | |
|             var service = await this.GetService(inDto.VisitTaskId);
 | |
|             var visitTaskInfo = await _visitTaskRepository.Where(x => x.Id == inDto.VisitTaskId).FirstNotNullAsync();
 | |
|             var result = new AddTaskLesionAnswerFromLastTaskOutDto();
 | |
|             var readingTaskState = visitTaskInfo.ReadingTaskState;
 | |
|             if (service != null && visitTaskInfo.SourceSubjectVisitId != null)
 | |
|             {
 | |
|                 if (readingTaskState == ReadingTaskState.WaitReading)
 | |
|                 {
 | |
|                     if (visitTaskInfo.ReadingCategory == ReadingCategory.Visit)
 | |
|                     {
 | |
|                         result = await service.AddTaskLesionAnswerFromLastTask(inDto);
 | |
|                         await service.CalculateTask(new CalculateTaskInDto()
 | |
|                         {
 | |
|                             IsChangeOtherTask = false,
 | |
|                             VisitTaskId = inDto.VisitTaskId,
 | |
|                             ComputationTrigger = ComputationTrigger.InitialCalculation,
 | |
|                         });
 | |
|                     }
 | |
|                 }
 | |
|             }
 | |
|             if (readingTaskState == ReadingTaskState.WaitReading)
 | |
|             {
 | |
|                 await _visitTaskRepository.UpdatePartialFromQueryAsync(inDto.VisitTaskId, x => new VisitTask()
 | |
|                 {
 | |
|                     ReadingTaskState = ReadingTaskState.Reading,
 | |
|                 }, true);
 | |
|             }
 | |
| 
 | |
|             return result;
 | |
| 
 | |
| 
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 获取报告验证的信息(这里每个标准可能不一样 返回用object)
 | |
|         /// </summary>
 | |
|         /// <param name="inDto"></param>
 | |
|         /// <returns></returns>
 | |
|         public async Task<GetReportVerifyOutDto> GetReportVerify(GetReportVerifyInDto inDto)
 | |
|         {
 | |
| 
 | |
|             var service = await this.GetService(inDto.VisitTaskId);
 | |
| 
 | |
|             if (service != null)
 | |
|             {
 | |
|                 return await service.GetReportVerify(inDto);
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 return new GetReportVerifyOutDto() { };
 | |
|             }
 | |
| 
 | |
|         }
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 获取阅片报告
 | |
|         /// </summary>
 | |
|         /// <returns></returns>
 | |
|         public async Task<GetReadingReportEvaluationOutDto> GetReadingReportEvaluation(GetReadingReportEvaluationInDto inDto)
 | |
|         {
 | |
|             var service = await this.GetService(inDto.VisitTaskId);
 | |
| 
 | |
|             if (service != null)
 | |
|             {
 | |
| 
 | |
|                 var taskInfo = await _visitTaskRepository.Where(x => x.Id == inDto.VisitTaskId).FirstNotNullAsync();
 | |
|                 var result = await service.GetReadingReportEvaluation(inDto);
 | |
| 
 | |
| 
 | |
|                 // 这里统一处理字典
 | |
| 
 | |
|                 Dictionary<Guid, List<CrterionDictionaryGroup>> dictionaryGroup = result.VisitTaskList.ToDictionary(x => x.VisitTaskId, x => x.CrterionDictionaryGroup);
 | |
| 
 | |
|                 result.TaskQuestions.ForEach(y =>
 | |
|                 {
 | |
|                     SetCrterionDictionaryGroup(y, dictionaryGroup);
 | |
|                 });
 | |
| 
 | |
| 
 | |
|                 return result;
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 return new GetReadingReportEvaluationOutDto();
 | |
|             }
 | |
|         }
 | |
| 
 | |
| 
 | |
|         public void SetCrterionDictionaryGroup(ReadingReportDto item, Dictionary<Guid, List<CrterionDictionaryGroup>> dictionaryGroup)
 | |
|         {
 | |
|             item.Answer.ForEach(z =>
 | |
|             {
 | |
|                 try
 | |
|                 {
 | |
|                     z.CrterionDictionaryGroup = dictionaryGroup[z.VisitTaskId];
 | |
|                 }
 | |
|                 catch (Exception)
 | |
|                 {
 | |
| 
 | |
| 
 | |
|                 }
 | |
|             });
 | |
| 
 | |
|             item.Childrens.ForEach(x =>
 | |
|             {
 | |
| 
 | |
|                 SetCrterionDictionaryGroup(x, dictionaryGroup);
 | |
|             });
 | |
|         }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 删除病灶获取起始病灶序号
 | |
|         /// </summary>
 | |
|         /// <returns></returns>
 | |
|         public async Task<int> GetDeleteLesionStatrIndex(DeleteReadingRowAnswerInDto inDto)
 | |
|         {
 | |
|             var service = await this.GetService(inDto.VisitTaskId);
 | |
| 
 | |
|             if (service != null)
 | |
|             {
 | |
|                 return await service.GetDeleteLesionStatrIndex(inDto);
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 return 1;
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
| 
 | |
| 
 | |
| }
 |