using IRaCIS.Core.Domain.Models;
using Microsoft.AspNetCore.Mvc;
using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Infra.EFCore.Common;
using MassTransit;
using IRaCIS.Core.Infrastructure;
using IRaCIS.Core.Application.Service.Reading.Dto;
using IRaCIS.Core.Domain.Share;
namespace IRaCIS.Core.Application.Service
{
///
/// 阅片医学审核
///
[ ApiExplorerSettings(GroupName = "Reading")]
public class ReadingMedicalReviewService : BaseService
{
private readonly IRepository _readingMedicineTrialQuestionRepository;
private readonly IRepository _trialRepository;
private readonly IRepository _visitTaskRepository;
private readonly IRepository _taskMedicalReviewRepository;
private readonly IRepository _readingMedicineQuestionAnswerRepository;
private readonly IRepository _readingMedicineSystemQuestionRepository;
public ReadingMedicalReviewService(
IRepository readingMedicineTrialQuestionRepository,
IRepository trialRepository,
IRepository visitTaskRepository,
IRepository taskMedicalReviewRepository,
IRepository readingMedicineQuestionAnswerRepository,
IRepository readingMedicineSystemQuestionRepository
)
{
this._readingMedicineTrialQuestionRepository = readingMedicineTrialQuestionRepository;
this._trialRepository = trialRepository;
this._visitTaskRepository = visitTaskRepository;
this._taskMedicalReviewRepository = taskMedicalReviewRepository;
this._readingMedicineQuestionAnswerRepository = readingMedicineQuestionAnswerRepository;
this._readingMedicineSystemQuestionRepository = readingMedicineSystemQuestionRepository;
}
///
/// 获取医学审核任务信息
///
///
///
[HttpPost]
public async Task GetMedicalReviewReadingTask(GetMedicalReviewReadingTaskInDto inDto)
{
var medicalReviewInfo = await _trialRepository.Where(x => x.Id == inDto.TrialId).Select(x => new GetMedicalReviewReadingTaskOutDto()
{
ReadingType=x.ReadingType,
IsReadingTaskViewInOrder=x.IsReadingTaskViewInOrder
}).FirstNotNullAsync();
medicalReviewInfo.MedicalReviewInfo = await _taskMedicalReviewRepository.Where(x => x.Id == inDto.TaskMedicalReviewId)
.ProjectTo(_mapper.ConfigurationProvider).FirstNotNullAsync();
var taskInfo = await _visitTaskRepository.Where(x => x.Id == medicalReviewInfo.MedicalReviewInfo.VisitTaskId).FirstNotNullAsync();
medicalReviewInfo.VisitTaskId = taskInfo.Id;
#region 取任务
if (medicalReviewInfo.IsReadingTaskViewInOrder)
{
medicalReviewInfo.TaskList = await _visitTaskRepository
.WhereIf(taskInfo.ArmEnum == Arm.JudgeArm, x => x.ArmEnum == Arm.JudgeArm)
.WhereIf(taskInfo.ArmEnum != Arm.JudgeArm, x => x.ArmEnum != Arm.JudgeArm)
.Where(x => x.SubjectId == taskInfo.SubjectId
&& x.DoctorUserId == taskInfo.DoctorUserId && x.ReadingTaskState == ReadingTaskState.HaveSigned && x.ReReadingApplyState != ReReadingApplyState.Agree
).OrderBy(x => x.VisitTaskNum).Select(x => new TaskInfo()
{
TaskId = x.Id,
IsCurrentTask = x.Id == taskInfo.Id,
ReadingCategory = x.ReadingCategory,
TaskBlindName = x.TaskBlindName,
ArmEnum=x.ArmEnum,
TaskName = x.TaskName,
SouceReadModuleId=x.SouceReadModuleId,
SourceSubjectVisitId=x.SourceSubjectVisitId,
}).ToListAsync();
List< TaskInfo > otherTask = await _visitTaskRepository.Where(x => x.ArmEnum != Arm.JudgeArm && x.SubjectId == taskInfo.SubjectId
&& x.DoctorUserId != taskInfo.DoctorUserId && x.ReadingTaskState == ReadingTaskState.HaveSigned && x.ReReadingApplyState != ReReadingApplyState.Agree
).OrderBy(x => x.VisitTaskNum).Select(x => new TaskInfo()
{
TaskId = x.Id,
IsCurrentTask = x.Id == taskInfo.Id,
ReadingCategory = x.ReadingCategory,
TaskBlindName = x.TaskBlindName,
ArmEnum = x.ArmEnum,
TaskName = x.TaskName,
SouceReadModuleId = x.SouceReadModuleId,
SourceSubjectVisitId = x.SourceSubjectVisitId,
}).ToListAsync();
medicalReviewInfo.TaskList.ForEach(x =>
{
x.OtherTaskId = otherTask.Where(y => y.SouceReadModuleId == x.SouceReadModuleId && y.SourceSubjectVisitId == x.SourceSubjectVisitId).Select(y => y.TaskId).FirstOrDefault();
});
}
else
{
medicalReviewInfo.TaskList = await _visitTaskRepository.Where(x => x.Id == taskInfo.Id).Select(x => new TaskInfo()
{
TaskId = x.Id,
IsCurrentTask = x.Id == taskInfo.Id,
ReadingCategory = x.ReadingCategory,
TaskBlindName = x.TaskBlindName,
ArmEnum = x.ArmEnum,
TaskName = x.TaskName,
SouceReadModuleId = x.SouceReadModuleId,
SourceSubjectVisitId = x.SourceSubjectVisitId,
}).ToListAsync();
List otherTask = await _visitTaskRepository.Where(x => x.ArmEnum != Arm.JudgeArm && x.SouceReadModuleId == taskInfo.SouceReadModuleId && x.SourceSubjectVisitId == taskInfo.SourceSubjectVisitId
&& x.DoctorUserId != taskInfo.DoctorUserId && x.ReadingTaskState == ReadingTaskState.HaveSigned && x.ReReadingApplyState != ReReadingApplyState.Agree
).OrderBy(x => x.VisitTaskNum).Select(x => new TaskInfo()
{
TaskId = x.Id,
IsCurrentTask = x.Id == taskInfo.Id,
ReadingCategory = x.ReadingCategory,
TaskBlindName = x.TaskBlindName,
ArmEnum = x.ArmEnum,
TaskName = x.TaskName,
SouceReadModuleId = x.SouceReadModuleId,
SourceSubjectVisitId = x.SourceSubjectVisitId,
}).ToListAsync();
medicalReviewInfo.TaskList.ForEach(x =>
{
x.OtherTaskId = otherTask.Where(y => y.SouceReadModuleId == x.SouceReadModuleId && y.SourceSubjectVisitId == x.SourceSubjectVisitId).Select(y => y.TaskId).FirstOrDefault();
});
}
#endregion
medicalReviewInfo.QuestionAnswerList = await this.GetMedicalQuestionAnswer(inDto);
return medicalReviewInfo;
}
private async Task> GetMedicalQuestionAnswer(GetMedicalReviewReadingTaskInDto inDto)
{
var questionQuery = from data in _readingMedicineTrialQuestionRepository.Where(x => x.TrialId == inDto.TrialId&&x.IsConfirm&&x.IsEnable)
join questionAnswer in _readingMedicineQuestionAnswerRepository.Where(x => x.TaskMedicalReviewId == inDto.TaskMedicalReviewId) on data.Id equals questionAnswer.ReadingMedicineQuestionId into questionAnswerTemp
from leftquestionAnswer in questionAnswerTemp.DefaultIfEmpty()
select new ReadingMedicineQuestion()
{
MedicineQuestionId = data.Id,
Type = data.Type,
ParentTriggerValue = data.ParentTriggerValue,
IsEnable=data.IsEnable,
IsConfirm=data.IsConfirm,
QuestionName = data.QuestionName,
IsRequired = data.IsRequired,
ShowOrder = data.ShowOrder,
ParentId = data.ParentId,
TypeValue = data.TypeValue,
Answer = leftquestionAnswer.Answer
};
var questionList = await questionQuery.ToListAsync();
List < ReadingMedicineQuestion > medicineQuestionList = questionList.Where(x => x.ParentId == null).ToList();
medicineQuestionList.ForEach(x =>
{
FindChildQuestion(x, questionList);
});
return medicineQuestionList;
}
private void FindChildQuestion(ReadingMedicineQuestion medicineQuestionList, List questionlists)
{
medicineQuestionList.Childrens = questionlists.Where(x => x.ParentId == medicineQuestionList.MedicineQuestionId).ToList();
if (medicineQuestionList.Childrens != null && medicineQuestionList.Childrens.Count != 0)
{
medicineQuestionList.Childrens.ForEach(x =>
{
this.FindChildQuestion(x, questionlists);
});
}
}
/////
///// 保存医学审核问题
/////
/////
//[HttpPost]
//public async Task SaveMedicineQuestion(SaveMedicineQuestionInDto inDto)
//{
// await _readingMedicineQuestionAnswerRepository.BatchDeleteNoTrackingAsync(x => x.TaskMedicalReviewId == inDto.TaskMedicalReviewId);
//}
}
}