删除分割删除对应的绑定和答案
continuous-integration/drone/push Build is passing Details

Test_IRC_Net8
Hewt 2026-03-20 16:29:05 +08:00
parent 61c4d79fa1
commit 751121e878
1 changed files with 42 additions and 0 deletions

View File

@ -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;
/// <summary>
@ -85,6 +87,7 @@ public class SegmentationService(IRepository<Segmentation> _segmentationReposito
[HttpDelete("{segmentationId:guid}")]
public async Task<IResponseOutput> 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<Segmentation> _segmentationReposito
[HttpDelete("{segmentId:guid}")]
public async Task<IResponseOutput> 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<Segmentation> _segmentationReposito
return ResponseOutput.Ok();
}
/// <summary>
/// 删除分割组和分割时的关联数据删除逻辑
/// </summary>
/// <param name="segmentationId"></param>
/// <param name="segmentId"></param>
/// <returns></returns>
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);
}
}
}