From b313a89dd6f94e2e48b9c946c4e3070d8d83b200 Mon Sep 17 00:00:00 2001
From: he <10978375@qq.com>
Date: Wed, 21 Sep 2022 13:24:30 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../Reading/Dto/ReadingImageTaskViewModel.cs  | 13 +++
 .../Service/Reading/OrganInfoService.cs       | 28 +++----
 .../Reading/ReadingImageTaskService.cs        | 79 ++++++++++++++++++-
 IRaCIS.Core.Domain/Reading/OrganTrialInfo.cs  |  8 +-
 .../Reading/ReadingTableAnswerRowInfo.cs      | 11 +++
 5 files changed, 114 insertions(+), 25 deletions(-)

diff --git a/IRaCIS.Core.Application/Service/Reading/Dto/ReadingImageTaskViewModel.cs b/IRaCIS.Core.Application/Service/Reading/Dto/ReadingImageTaskViewModel.cs
index 21537325..4addce65 100644
--- a/IRaCIS.Core.Application/Service/Reading/Dto/ReadingImageTaskViewModel.cs
+++ b/IRaCIS.Core.Application/Service/Reading/Dto/ReadingImageTaskViewModel.cs
@@ -191,6 +191,19 @@ namespace IRaCIS.Core.Application.Service.Reading.Dto
         public bool IsCurrentTask { get; set; }
     }
 
+    public class MergeLesionInDto
+    {
+        public Guid VisitTaskId { get; set; }
+
+        public Guid QuestionId { get; set; }
+
+        public decimal MainRowIndex { get; set; }
+
+        public decimal MergeRowIndex { get; set; }
+
+
+    }
+
     public class SplitLesionInDto
     {
         public Guid VisitTaskId { get; set; }
diff --git a/IRaCIS.Core.Application/Service/Reading/OrganInfoService.cs b/IRaCIS.Core.Application/Service/Reading/OrganInfoService.cs
index 785f19c9..d631f106 100644
--- a/IRaCIS.Core.Application/Service/Reading/OrganInfoService.cs
+++ b/IRaCIS.Core.Application/Service/Reading/OrganInfoService.cs
@@ -53,12 +53,7 @@ namespace IRaCIS.Core.Application.Service
         public async Task<List<OrganDictionary>> GetCriterionLesionType(GetCriterionLesionTypeInDto inDto)
         {
             var dicNums = new List<string>();
-
-
             dicNums = await _criterionNidusRepository.Where(x => x.CriterionId == inDto.CriterionId).Select(x => ((int)x.LesionType).ToString()).Distinct().ToListAsync();
-
-
-
             var dictionaryId = await _dictionaryRepository.Where(x => x.Code == "LesionType").Select(x => x.Id).FirstOrDefaultAsync();
 
             var result = await _dictionaryRepository.Where(x => x.ParentId == dictionaryId && dicNums.Contains(x.Code)).ProjectTo<OrganDictionary>(_mapper.ConfigurationProvider).ToListAsync();
@@ -122,16 +117,12 @@ namespace IRaCIS.Core.Application.Service
         [HttpPost]
         public async Task<List<OrganInfoView>> GetOrganInfoList(OrganInfoQuery inQuery)
         {
-
-
             List<OrganType> organs = new List<OrganType>();
-
             if (inQuery.LesionType != null)
             {
                 organs =await _criterionNidusRepository.Where(x => x.CriterionId == inQuery.SystemCriterionId && x.LesionType == inQuery.LesionType)
                     .Select(x => x.OrganType).ToListAsync();
             }
-
             var organInfoQueryable = _organInfoRepository
                .Where(x=>x.SystemCriterionId==inQuery.SystemCriterionId)
                .WhereIf(inQuery.LesionType != null, x => organs.Contains(x.OrganType))
@@ -206,7 +197,6 @@ namespace IRaCIS.Core.Application.Service
             {
                 OrganInfoId=x,
                 TrialId=inDto.TrialId,
-
                 //OrganType=inDto.OrganType,
             }).ToList();
 
@@ -249,12 +239,14 @@ namespace IRaCIS.Core.Application.Service
                     .Select(x => x.OrganType).ToListAsync();
             }
 
-            var organInfoQueryable = from data in _organInfoRepository.WhereIf(inDto.OrganType != null, x => x.OrganType == inDto.OrganType)
-                                     .WhereIf(inDto.IsLymphNodes!=null,x=>x.IsLymphNodes==inDto.IsLymphNodes)
+            var organInfoQueryable = from data in _organInfoRepository
+                                                 .WhereIf(inDto.OrganType != null, x => x.OrganType == inDto.OrganType)
+                                                 .WhereIf(inDto.IsLymphNodes!=null,x=>x.IsLymphNodes==inDto.IsLymphNodes)
+                                                  .WhereIf(inDto.LesionType != null, x => organs.Contains(x.OrganType))
                                      join trialData in _organTrialInfoRepository.WhereIf(inDto.IsEnable != null, x => x.IsEnable == inDto.IsEnable)
-                                      .WhereIf(inDto.LesionType != null, x => organs.Contains(x.OrganType))
-                                     .WhereIf(inDto.IsEnable != null, x => x.IsEnable == inDto.IsEnable)
-                                     .Where(x => x.TrialId == inDto.TrialId)
+                                                 
+                                                 .WhereIf(inDto.IsEnable != null, x => x.IsEnable == inDto.IsEnable)
+                                                 .Where(x => x.TrialId == inDto.TrialId)
                                      on data.Id equals trialData.OrganInfoId
                                      select new GetTrialOrganListOutDto()
                                      {
@@ -266,7 +258,7 @@ namespace IRaCIS.Core.Application.Service
                                          IsLymphNodes = data.IsLymphNodes,
                                          
                                          IsEnable= trialData.IsEnable,
-                                         OrganType = trialData.OrganType,
+                                         OrganType = data.OrganType,
                                      };
 
 
@@ -316,7 +308,7 @@ namespace IRaCIS.Core.Application.Service
         [HttpPost]
         public async Task<IResponseOutput> SynchronizeSystemOrganToTrial(SynchronizeSystemOrganToTrialInDto inDto)
         {
-         
+            // 选中的标准进行修改
             var readingQuestionCriterionTrial = await _readingQuestionCriterionTrial.Where(x => x.TrialId == inDto.TrialId && x.IsConfirm).FirstOrDefaultAsync();
 
             if (readingQuestionCriterionTrial != null)
@@ -346,7 +338,7 @@ namespace IRaCIS.Core.Application.Service
                 var originalIds = criterionNidusList.Select(x => x.OriginalId).Distinct().ToList();
                 List<OrganTrialInfo> organTrialInfoList = await _organInfoRepository.Where(x =>x.SystemCriterionId== inDto.SystemCriterionId).Select(x => new OrganTrialInfo()
                 {
-                    OrganType=x.OrganType,
+                    //OrganType=x.OrganType,
                     Id = x.Id,
                     IsEnable = true,
                     TrialCriterionId= readingQuestionCriterionTrial.Id,
diff --git a/IRaCIS.Core.Application/Service/Reading/ReadingImageTaskService.cs b/IRaCIS.Core.Application/Service/Reading/ReadingImageTaskService.cs
index fdf6cb0c..70765e8a 100644
--- a/IRaCIS.Core.Application/Service/Reading/ReadingImageTaskService.cs
+++ b/IRaCIS.Core.Application/Service/Reading/ReadingImageTaskService.cs
@@ -109,10 +109,83 @@ namespace IRaCIS.Application.Services
             this._readingQuestionTrialRepository = readingQuestionTrialRepository;
         }
 
-
-        public async Task SplitLesion()
+        /// <summary>
+        /// 拆分病灶
+        /// </summary>
+        /// <returns></returns>
+        [HttpPost]
+        public async Task SplitLesion(SplitLesionInDto inDto)
         {
-            
+            await this.VerifyIsBaseLineTask(inDto.VisitTaskId);
+            var rowAnswer = await _readingTableAnswerRowInfoRepository.Where(x => x.VisitTaskId == inDto.VisitTaskId && x.RowIndex == inDto.RowIndex && x.QuestionId == inDto.QuestionId).FirstNotNullAsync();
+            var tableAnswers = await _readingTableQuestionAnswerRepository.Where(x => x.VisitTaskId == inDto.VisitTaskId && x.RowIndex == inDto.RowIndex && x.QuestionId == inDto.QuestionId).ToListAsync();
+            var maxRowIndex = await _readingTableAnswerRowInfoRepository.Where(x => x.VisitTaskId == inDto.VisitTaskId && x.QuestionId == inDto.QuestionId).OrderByDescending(x => x.RowIndex).Select(x => x.RowIndex).FirstOrDefaultAsync();
+            var newRowIndex = maxRowIndex + (decimal)0.01;
+          
+            rowAnswer.RowIndex = newRowIndex;
+            rowAnswer.MergeRowId = null;
+            rowAnswer.SplitRowId = rowAnswer.Id;
+            rowAnswer.Id = NewId.NextGuid();
+            tableAnswers.ForEach(x =>
+            {
+                x.Id = NewId.NextGuid();
+                x.RowIndex = newRowIndex;
+            });
+            await _readingTableAnswerRowInfoRepository.AddAsync(rowAnswer);
+            await _readingTableQuestionAnswerRepository.AddRangeAsync(tableAnswers);
+            await _readingTableAnswerRowInfoRepository.SaveChangesAsync();
+
+        }
+
+        /// <summary>
+        /// 合并病灶
+        /// </summary>
+        /// <returns></returns>
+        [HttpPost]
+        public async Task MergeLesion(MergeLesionInDto inDto)
+        {
+            await this.VerifyIsBaseLineTask(inDto.VisitTaskId);
+
+            var rowsInfo = await _readingTableAnswerRowInfoRepository.Where(x => x.VisitTaskId == inDto.VisitTaskId && x.QuestionId == inDto.QuestionId&&(x.RowIndex == inDto.MainRowIndex || x.RowIndex == inDto.MergeRowIndex)).ToListAsync();
+
+            if (rowsInfo.Count() != 2)
+            {
+                throw new BusinessValidationFailedException("合并的病灶并非同一个病灶类型");
+            }
+
+
+            var minaid = rowsInfo.Where(x => x.RowIndex == inDto.MainRowIndex).Select(x => x.Id).FirstOrDefault();
+            var mergeid = rowsInfo.Where(x => x.RowIndex == inDto.MergeRowIndex).Select(x => x.Id).FirstOrDefault();
+
+
+            await _readingTableAnswerRowInfoRepository.UpdatePartialFromQueryAsync(mergeid, x => new ReadingTableAnswerRowInfo()
+            {
+                MergeRowId = minaid,
+            });
+
+            await _readingTableAnswerRowInfoRepository.SaveChangesAsync();
+
+
+        }
+
+        /// <summary>
+        /// 验证是否为基线访视任务
+        /// </summary>
+        /// <param name="visitTaskId"></param>
+        /// <returns></returns>
+        /// <exception cref="BusinessValidationFailedException"></exception>
+        private async Task VerifyIsBaseLineTask(Guid visitTaskId)
+        {
+            var taskinfo = await _visitTaskRepository.Where(x => x.Id == visitTaskId).FirstNotNullAsync();
+            if (taskinfo.ReadingCategory!=ReadingCategory.Visit)
+            {
+                throw new BusinessValidationFailedException("当前任务不是访视任务");
+            }
+
+            if (!(await _subjectVisitRepository.AnyAsync(x => x.Id == taskinfo.SourceSubjectVisitId && x.IsBaseLine)))
+            {
+                throw new BusinessValidationFailedException("当前不是基线任务");
+            }
         }
 
         /// <summary>
diff --git a/IRaCIS.Core.Domain/Reading/OrganTrialInfo.cs b/IRaCIS.Core.Domain/Reading/OrganTrialInfo.cs
index eb2a3abf..707118d2 100644
--- a/IRaCIS.Core.Domain/Reading/OrganTrialInfo.cs
+++ b/IRaCIS.Core.Domain/Reading/OrganTrialInfo.cs
@@ -39,10 +39,10 @@ namespace IRaCIS.Core.Domain.Models
 		public Guid CreateUserId { get; set; }
 
 
-		/// <summary>
-		/// 病灶类型
-		/// </summary>
-		public OrganType OrganType { get; set; }
+		///// <summary>
+		///// 病灶类型  项目自定义标准 可能会更改
+		///// </summary>
+		//public OrganType OrganType { get; set; }
 
 		/// <summary>
 		/// 标准Id
diff --git a/IRaCIS.Core.Domain/Reading/ReadingTableAnswerRowInfo.cs b/IRaCIS.Core.Domain/Reading/ReadingTableAnswerRowInfo.cs
index 61079786..47b2357f 100644
--- a/IRaCIS.Core.Domain/Reading/ReadingTableAnswerRowInfo.cs
+++ b/IRaCIS.Core.Domain/Reading/ReadingTableAnswerRowInfo.cs
@@ -61,6 +61,17 @@ namespace IRaCIS.Core.Domain.Models
 		/// </summary>
 		public bool IsCurrentTaskAdd { get; set; } = false;
 
+
+		/// <summary>
+		/// SplitRowId
+		/// </summary>
+		public Guid? SplitRowId { get; set; }
+
+		/// <summary>
+		/// MergeRowId
+		/// </summary>
+		public Guid? MergeRowId { get; set; }
+
 		/// <summary>
 		/// CreateUserId
 		/// </summary>