From d74e3a9f60ffceea35cf28fb617c413609524f9f Mon Sep 17 00:00:00 2001 From: he <109787524@qq.com> Date: Fri, 24 Apr 2026 11:14:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=85=B3=E8=81=94=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E7=AD=94=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Reading/Segment/SegmentationService.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/IRaCIS.Core.Application/Service/Reading/Segment/SegmentationService.cs b/IRaCIS.Core.Application/Service/Reading/Segment/SegmentationService.cs index c43b52d20..56caa18f2 100644 --- a/IRaCIS.Core.Application/Service/Reading/Segment/SegmentationService.cs +++ b/IRaCIS.Core.Application/Service/Reading/Segment/SegmentationService.cs @@ -31,6 +31,8 @@ public class SegmentationService(IRepository _segmentationReposito IRepository _readingTableAnswerRowInfoRepository, IRepository _readingTableQuestionAnswerRepository, IRepository _segmentRepository, + IRepository _readingQuestionTrialRepository, + IRepository _readingTableQuestionTrialRepository, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer): BaseService, ISegmentationService { @@ -347,14 +349,20 @@ public class SegmentationService(IRepository _segmentationReposito foreach (var binding in bindings) { + var visited = new HashSet(); + // 处理问题 if (binding.RowId != null && binding.TableQuestionId != null) { await _readingTableQuestionAnswerRepository.DeleteFromQueryAsync(x => x.VisitTaskId == binding.VisitTaskId && x.RowId == binding.RowId && x.TableQuestionId == binding.TableQuestionId); + + await DeleteDependentAnswersAsync(binding.TableQuestionId.Value, binding.VisitTaskId, binding.RowId, visited); } else if (binding.QuestionId != null) { await _readingTaskQuestionAnswerRepository.DeleteFromQueryAsync(x => x.VisitTaskId == binding.VisitTaskId && x.ReadingQuestionTrialId == binding.QuestionId); + + await DeleteDependentAnswersAsync(binding.QuestionId.Value, binding.VisitTaskId, null, visited); } } @@ -369,6 +377,42 @@ public class SegmentationService(IRepository _segmentationReposito } } + /// + /// 递归清除依赖此问题的计算问题答案 + /// + private async Task DeleteDependentAnswersAsync(Guid sourceQuestionId, Guid visitTaskId, Guid? rowId, HashSet visited) + { + if (visited.Contains(sourceQuestionId)) return; + visited.Add(sourceQuestionId); + + var sourceQuestionIdStr = sourceQuestionId.ToString(); + + var dependentTableQuestions = await _readingTableQuestionTrialRepository + .Where(x => x.DataSource == DataSources.Automatic && x.Type == "number" && x.CalculateQuestions.Contains(sourceQuestionIdStr)) + .Select(x => x.Id) + .ToListAsync(); + + foreach (var tableQuestionId in dependentTableQuestions) + { + if (rowId != null) + { + await _readingTableQuestionAnswerRepository.DeleteFromQueryAsync(x => x.VisitTaskId == visitTaskId && x.RowId == rowId && x.TableQuestionId == tableQuestionId); + } + await DeleteDependentAnswersAsync(tableQuestionId, visitTaskId, rowId, visited); + } + + var dependentTaskQuestions = await _readingQuestionTrialRepository + .Where(x => x.DataSource == DataSources.Automatic && x.Type == "number" && x.CalculateQuestions.Contains(sourceQuestionIdStr)) + .Select(x => x.Id) + .ToListAsync(); + + foreach (var taskQuestionId in dependentTaskQuestions) + { + await _readingTaskQuestionAnswerRepository.DeleteFromQueryAsync(x => x.VisitTaskId == visitTaskId && x.ReadingQuestionTrialId == taskQuestionId); + await DeleteDependentAnswersAsync(taskQuestionId, visitTaskId, rowId, visited); + } + } + }