diff --git a/IRaCIS.Core.Application/Service/Reading/Dto/SegmentBindingViewModel.cs b/IRaCIS.Core.Application/Service/Reading/Dto/SegmentBindingViewModel.cs index 8658aa0ae..39207ca81 100644 --- a/IRaCIS.Core.Application/Service/Reading/Dto/SegmentBindingViewModel.cs +++ b/IRaCIS.Core.Application/Service/Reading/Dto/SegmentBindingViewModel.cs @@ -18,6 +18,16 @@ public class SegmentBindingView : SegmentBindingAddOrEdit } +public class SaveSegmentBindingAndAnswerInDto +{ + public Guid VisitTaskId { get; set; } + public List BindingList { get; set; } +} + +public class SaveSegmentBindingDto : SegmentBindingAddOrEdit +{ + public string Answer { get; set; } +} public class SegmentBindingAddOrEdit { diff --git a/IRaCIS.Core.Application/Service/Reading/Segment/SegmentationService.cs b/IRaCIS.Core.Application/Service/Reading/Segment/SegmentationService.cs index 33a0c0bb2..af253f806 100644 --- a/IRaCIS.Core.Application/Service/Reading/Segment/SegmentationService.cs +++ b/IRaCIS.Core.Application/Service/Reading/Segment/SegmentationService.cs @@ -23,6 +23,10 @@ namespace IRaCIS.Core.Application.Service; [ ApiExplorerSettings(GroupName = "Reading")] public class SegmentationService(IRepository _segmentationRepository, IRepository _segmentBindingRepository, + IRepository _readingTaskQuestionAnswerRepository, + IRepository _visitTaskRepository, + IRepository _readingTableAnswerRowInfoRepository, + IRepository _readingTableQuestionAnswerRepository, IRepository _segmentRepository, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer): BaseService, ISegmentationService { @@ -146,15 +150,13 @@ public class SegmentationService(IRepository _segmentationReposito { var segmentBindingQueryable = _segmentBindingRepository - .WhereIf(inQuery.QuestionId != null, x => x.QuestionId == inQuery.QuestionId) - .WhereIf(inQuery.RowId != null, x => x.RowId == inQuery.RowId) - .WhereIf(inQuery.SegmentId != null, x => x.SegmentId == inQuery.SegmentId) - .WhereIf(inQuery.SegmentationId != null, x => x.SegmentationId == inQuery.SegmentationId) - .WhereIf(inQuery.TableQuestionId != null, x => x.TableQuestionId == inQuery.TableQuestionId) - - .WhereIf(inQuery.VisitTaskId != null, x => x.VisitTaskId == inQuery.VisitTaskId) - - .ProjectTo(_mapper.ConfigurationProvider); + .WhereIf(inQuery.QuestionId != null, x => x.QuestionId == inQuery.QuestionId) + .WhereIf(inQuery.RowId != null, x => x.RowId == inQuery.RowId) + .WhereIf(inQuery.SegmentId != null, x => x.SegmentId == inQuery.SegmentId) + .WhereIf(inQuery.SegmentationId != null, x => x.SegmentationId == inQuery.SegmentationId) + .WhereIf(inQuery.TableQuestionId != null, x => x.TableQuestionId == inQuery.TableQuestionId) + .WhereIf(inQuery.VisitTaskId != null, x => x.VisitTaskId == inQuery.VisitTaskId) + .ProjectTo(_mapper.ConfigurationProvider); var pageList = await segmentBindingQueryable.ToPagedListAsync(inQuery); @@ -190,6 +192,107 @@ public class SegmentationService(IRepository _segmentationReposito return ResponseOutput.Ok(); } + /// + /// 保存分割绑定和答案 + /// + /// + /// + public async Task SaveSegmentBindingAndAnswer(SaveSegmentBindingAndAnswerInDto inDto) + { + + var taskinfo = await _visitTaskRepository.Where(x => x.Id == inDto.VisitTaskId) + .FirstNotNullAsync(); + foreach (var item in inDto.BindingList) + { + // 处理绑定关系 + var binding = await _segmentBindingRepository.Where(x => x.VisitTaskId == inDto.VisitTaskId && x.QuestionId == item.QuestionId && x.RowId == item.RowId && x.TableQuestionId == item.TableQuestionId) + .FirstOrDefaultAsync(); + if (binding == null) + { + await _segmentBindingRepository.InsertFromDTOAsync(new SegmentBinding + { + VisitTaskId = inDto.VisitTaskId, + QuestionId = item.QuestionId, + RowId = item.RowId, + TableQuestionId = item.TableQuestionId, + SegmentId = item.SegmentId, + SegmentationId = item.SegmentationId, + }); + } + else + { + await _segmentBindingRepository.UpdatePartialFromQueryAsync(x => x.Id == binding.Id, t => new SegmentBinding + { + SegmentId = item.SegmentId, + SegmentationId = item.SegmentationId, + }); + } + + + + // 处理问题 + if (item.RowId != null && item.TableQuestionId != null) + { + var answer = await _readingTableQuestionAnswerRepository.Where(x => x.VisitTaskId == inDto.VisitTaskId && x.RowId == item.VisitTaskId && x.TableQuestionId == item.TableQuestionId) + .FirstOrDefaultAsync(); + var rowinfo= await _readingTableAnswerRowInfoRepository.Where(x => x.Id==item.RowId) + .Include(x=>x.VisitTask) + .FirstNotNullAsync(); + if (answer != null) + { + await _readingTableQuestionAnswerRepository.UpdatePartialFromQueryAsync(x => x.Id == answer.Id, t => new ReadingTableQuestionAnswer + { + Answer = item.Answer + }); + } + else + { + await _readingTableQuestionAnswerRepository.InsertFromDTOAsync(new ReadingTableQuestionAnswer + { + VisitTaskId = inDto.VisitTaskId, + RowId = item.RowId.Value, + TableQuestionId = item.TableQuestionId.Value, + Answer = item.Answer, + TrialId= rowinfo.VisitTask.TrialId, + RowIndex= rowinfo.RowIndex, + }); + } + } + else + { + + var answer = await _readingTaskQuestionAnswerRepository.Where(x => x.VisitTaskId == inDto.VisitTaskId &&x.ReadingQuestionTrialId==item.QuestionId) + .FirstOrDefaultAsync(); + + if (answer != null) + { + await _readingTaskQuestionAnswerRepository.UpdatePartialFromQueryAsync(x => x.Id == answer.Id, t => new ReadingTaskQuestionAnswer + { + Answer = item.Answer + }); + } + else + { + await _readingTaskQuestionAnswerRepository.InsertFromDTOAsync(new ReadingTaskQuestionAnswer + { + ReadingQuestionTrialId= item.QuestionId.Value, + ReadingQuestionCriterionTrialId=taskinfo.TrialReadingCriterionId, + TrialId= taskinfo.TrialId, + SubjectId= taskinfo.SubjectId, + VisitTaskId = inDto.VisitTaskId, + Answer=item.Answer, + }); + } + } + + + + } + + await _segmentationRepository.SaveChangesAsync(); + return ResponseOutput.Ok(); + } + }