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); + } + } + }