From 751121e8783b6a61fdcc65b748eb23a6e6ae54eb Mon Sep 17 00:00:00 2001 From: Hewt <109787524@qq.com> Date: Fri, 20 Mar 2026 16:29:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=88=86=E5=89=B2=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AF=B9=E5=BA=94=E7=9A=84=E7=BB=91=E5=AE=9A=E5=92=8C?= =?UTF-8?q?=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 | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/IRaCIS.Core.Application/Service/Reading/Segment/SegmentationService.cs b/IRaCIS.Core.Application/Service/Reading/Segment/SegmentationService.cs index adc7e8102..07508b76f 100644 --- a/IRaCIS.Core.Application/Service/Reading/Segment/SegmentationService.cs +++ b/IRaCIS.Core.Application/Service/Reading/Segment/SegmentationService.cs @@ -12,6 +12,8 @@ using IRaCIS.Core.Infrastructure.Extention; using System.Threading.Tasks; using IRaCIS.Core.Infra.EFCore; using Microsoft.Extensions.Logging; +using Microsoft.EntityFrameworkCore; + namespace IRaCIS.Core.Application.Service; /// @@ -85,6 +87,7 @@ public class SegmentationService(IRepository _segmentationReposito [HttpDelete("{segmentationId:guid}")] public async Task DeleteSegmentation(Guid segmentationId) { + await DeleteBindingsAndAnswersAsync(segmentationId, null); var success = await _segmentationRepository.DeleteFromQueryAsync(t => t.Id == segmentationId,true); return ResponseOutput.Ok(); } @@ -138,6 +141,7 @@ public class SegmentationService(IRepository _segmentationReposito [HttpDelete("{segmentId:guid}")] public async Task DeleteSegment(Guid segmentId) { + await DeleteBindingsAndAnswersAsync(null, segmentId); var success = await _segmentRepository.DeleteFromQueryAsync(t => t.Id == segmentId, true); return ResponseOutput.Ok(); } @@ -309,6 +313,44 @@ public class SegmentationService(IRepository _segmentationReposito return ResponseOutput.Ok(); } + /// + /// 删除分割组和分割时的关联数据删除逻辑 + /// + /// + /// + /// + private async Task DeleteBindingsAndAnswersAsync(Guid? segmentationId, Guid? segmentId) + { + var bindingsQuery = _segmentBindingRepository + .WhereIf(segmentationId != null, x => x.SegmentationId == segmentationId) + .WhereIf(segmentId != null, x => x.SegmentId == segmentId); + + var bindings = await bindingsQuery.ToListAsync(); + + foreach (var binding in bindings) + { + // 处理问题 + if (binding.RowId != null && binding.TableQuestionId != null) + { + await _readingTableQuestionAnswerRepository.DeleteFromQueryAsync(x => x.VisitTaskId == binding.VisitTaskId && x.RowId == binding.RowId && x.TableQuestionId == binding.TableQuestionId); + } + else if (binding.QuestionId != null) + { + await _readingTaskQuestionAnswerRepository.DeleteFromQueryAsync(x => x.VisitTaskId == binding.VisitTaskId && x.ReadingQuestionTrialId == binding.QuestionId); + } + } + + // 处理绑定关系 + if (segmentationId != null) + { + await _segmentBindingRepository.DeleteFromQueryAsync(x => x.SegmentationId == segmentationId); + } + else if (segmentId != null) + { + await _segmentBindingRepository.DeleteFromQueryAsync(x => x.SegmentId == segmentId); + } + } + }