irc-netcore-api/IRaCIS.Core.Application/Service/Allocation/VisitTaskService.cs

2564 lines
139 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

//--------------------------------------------------------------------
// 此代码由T4模板自动生成 byzhouhang 20210918
// 生成时间 2022-06-07 14:10:49
// 对此文件的更改可能会导致不正确的行为,并且如果重新生成代码,这些更改将会丢失。
//--------------------------------------------------------------------
using IRaCIS.Core.Domain.Models;
using Microsoft.AspNetCore.Mvc;
using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure;
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Infra.EFCore.Common;
using System.Linq.Expressions;
namespace IRaCIS.Core.Application.Service.Allocation
{
/// <summary>
/// 访视读片任务
/// </summary>
[ApiExplorerSettings(GroupName = "Trial")]
public class VisitTaskService : BaseService, IVisitTaskService
{
private readonly IRepository<VisitTask> _visitTaskRepository;
private readonly IRepository<Trial> _trialRepository;
private readonly IRepository<SubjectVisit> _subjectVisitRepository;
private readonly IRepository<TaskAllocationRule> _taskAllocationRuleRepository;
private readonly IRepository<Subject> _subjectRepository;
private readonly IRepository<SubjectUser> _subjectUserRepository;
private readonly IRepository<ReadModule> _readModuleRepository;
private readonly IRepository<VisitTaskReReading> _visitTaskReReadingRepository;
private readonly IRepository<TaskMedicalReview> _taskMedicalReviewRepository;
private readonly IRepository<ReadingTaskQuestionAnswer> _readingTaskQuestionAnswerRepository;
public VisitTaskService(IRepository<SubjectVisit> subjectVisitRepository, IRepository<VisitTask> visitTaskRepository, IRepository<Trial> trialRepository,
IRepository<Subject> subjectRepository, IRepository<SubjectUser> subjectUserRepository, IRepository<TaskAllocationRule> taskAllocationRuleRepository,
IRepository<ReadModule> readModuleRepository, IRepository<VisitTaskReReading> visitTaskReReadingRepository,
IRepository<TaskMedicalReview> taskMedicalReviewRepository,
IRepository<ReadingTaskQuestionAnswer> readingTaskQuestionAnswerRepository
)
{
_taskAllocationRuleRepository = taskAllocationRuleRepository;
_visitTaskRepository = visitTaskRepository;
_trialRepository = trialRepository;
_subjectVisitRepository = subjectVisitRepository;
_subjectRepository = subjectRepository;
_subjectUserRepository = subjectUserRepository;
_readModuleRepository = readModuleRepository;
_visitTaskReReadingRepository = visitTaskReReadingRepository;
_taskMedicalReviewRepository = taskMedicalReviewRepository;
_readingTaskQuestionAnswerRepository = readingTaskQuestionAnswerRepository;
}
/// <summary>
/// Subject 任务类型 统计 +分配情况
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<(PageOutput<SubjectAssignStat>, object)> GetSubjectAssignAndTaskStatList(SubjectAssignStatQuery querySubjectAssign)
{
var subjectQuery = _subjectRepository.Where(t => t.TrialId == querySubjectAssign.TrialId && t.SubjectVisitTaskList.Any())
.WhereIf(querySubjectAssign.SiteId != null, t => t.SiteId == querySubjectAssign.SiteId)
.WhereIf(querySubjectAssign.SubjectId != null, t => t.Id == querySubjectAssign.SubjectId)
.WhereIf(!string.IsNullOrEmpty(querySubjectAssign.SubjectCode), t => t.Code.Contains(querySubjectAssign.SubjectCode))
.ProjectTo<SubjectAssignStat>(_mapper.ConfigurationProvider);
var pageList = await subjectQuery.ToPagedListAsync(querySubjectAssign.PageIndex, querySubjectAssign.PageSize, string.IsNullOrWhiteSpace(querySubjectAssign.SortField) ? nameof(querySubjectAssign.SubjectId) : querySubjectAssign.SortField, querySubjectAssign.Asc);
var trialTaskConfig = _trialRepository.Where(t => t.Id == querySubjectAssign.TrialId).ProjectTo<TrialTaskConfigView>(_mapper.ConfigurationProvider).FirstOrDefault();
return (pageList, trialTaskConfig);
}
/// <summary>
/// 一次性分配所有医生 批量分配(添加),后端现在没限制
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
public async Task<IResponseOutput> BatchAssignDoctorToSubject(BatchAssignDoctorToSubjectCommand command)
{
foreach (var subjectId in command.SubjectIdList)
{
foreach (var doctorArm in command.DoctorArmList)
{
if (!await _subjectUserRepository.AnyAsync(t => t.SubjectId == subjectId && t.DoctorUserId == doctorArm.DoctorUserId && t.ArmEnum == doctorArm.ArmEnum && t.IsConfirmed && t.OrignalSubjectUserId == null))
{
await _subjectUserRepository.AddAsync(new SubjectUser() { TrialId = command.TrialId, ArmEnum = doctorArm.ArmEnum, DoctorUserId = doctorArm.DoctorUserId, SubjectId = subjectId, AssignTime = DateTime.Now });
await _visitTaskRepository.BatchUpdateNoTrackingAsync(t => t.SubjectId == subjectId && t.ArmEnum == doctorArm.ArmEnum && t.TrialId == command.TrialId && t.TaskAllocationState == TaskAllocationState.NotAllocate && t.TaskState == TaskState.Effect && t.IsAnalysisCreate == false,
u => new VisitTask()
{
AllocateTime = DateTime.Now,
DoctorUserId = doctorArm.DoctorUserId,
TaskAllocationState = TaskAllocationState.Allocated,
SuggesteFinishedTime = /*t.IsUrgent ? DateTime.Now.AddDays(2) :*/ DateTime.Now.AddDays(7),
});
//task.SuggesteFinishedTime = task.IsUrgent ? DateTime.Now.AddDays(2) : DateTime.Now.AddDays(7);
}
}
}
await _subjectUserRepository.SaveChangesAsync();
return ResponseOutput.Ok();
}
/// <summary>
/// 阅片人维度 Subject统计表
/// </summary>
/// <param name="trialId"></param>
/// <returns></returns>
public async Task<List<AssignDoctorStatView>> GetDoctorSubjectStat(Guid trialId)
{
var list = await _taskAllocationRuleRepository.Where(t => t.TrialId == trialId).ProjectTo<AssignDoctorStatView>(_mapper.ConfigurationProvider).ToListAsync();
return list;
}
/// <summary>
/// 获取Subject 分配医生情况
/// </summary>
/// <param name="subjectId"></param>
/// <returns></returns>
public async Task<List<SubjectUserDTO>> GetSubjectAssignedDoctorList(Guid subjectId)
{
var list = await _subjectUserRepository.Where(t => t.SubjectId == subjectId && t.OrignalSubjectUserId == null && t.IsConfirmed).ProjectTo<SubjectUserDTO>(_mapper.ConfigurationProvider).ToListAsync();
return list;
}
/// <summary>
/// 取消Subject 分配的医生
/// </summary>
/// <param name="commandList"></param>
/// <returns></returns>
/// <exception cref="BusinessValidationFailedException"></exception>
[HttpPost]
public async Task<IResponseOutput> CancelSubjectAssignedDoctor(List<CancelSubjectAssignedDoctorCommand> commandList)
{
foreach (var command in commandList.Where(t => t.IsCancelAssign))
{
if (await _visitTaskRepository.AnyAsync(t => t.SubjectId == command.SubjectId && t.DoctorUserId == command.DoctorUserId && t.ArmEnum == command.ArmEnum && t.ReadingTaskState != ReadingTaskState.WaitReading))
{
throw new BusinessValidationFailedException("当前医生已开始做该Subject的任务不允许取消分配");
}
await _subjectUserRepository.DeleteFromQueryAsync(t => t.Id == command.Id);
await _visitTaskRepository.BatchUpdateNoTrackingAsync(t => t.SubjectId == command.SubjectId && t.DoctorUserId == command.DoctorUserId && t.ArmEnum == command.ArmEnum && t.ReadingTaskState == ReadingTaskState.WaitReading && t.TaskState == TaskState.Effect && t.IsAnalysisCreate==false, u => new VisitTask()
{
AllocateTime = null,
DoctorUserId = null,
TaskAllocationState = TaskAllocationState.NotAllocate,
SuggesteFinishedTime = null
});
}
await _visitTaskRepository.SaveChangesAsync();
return ResponseOutput.Ok();
}
/// <summary>
/// 任务 手动分配 重新分配 确认 取消分配
/// </summary>分配
/// <param name="assignSubjectTaskToDoctorCommand"></param>
/// <returns></returns>
[HttpPost]
[UnitOfWork]
public async Task<IResponseOutput> AssignSubjectTaskToDoctor(AssignSubjectTaskToDoctorCommand assignSubjectTaskToDoctorCommand)
{
var visitTask = await _visitTaskRepository.FirstOrDefaultAsync(t => t.Id == assignSubjectTaskToDoctorCommand.Id);
if (assignSubjectTaskToDoctorCommand.TaskOptType == TaskOptType.Assign || assignSubjectTaskToDoctorCommand.TaskOptType == TaskOptType.ReAssign)
{
//手动分配验证规则
if (visitTask.SourceSubjectVisitId != null)
{
if (await _visitTaskRepository.AnyAsync(t => t.SourceSubjectVisitId == visitTask.SourceSubjectVisitId && t.TaskAllocationState == TaskAllocationState.Allocated && t.DoctorUserId == assignSubjectTaskToDoctorCommand.DoctorUserId && t.Id != visitTask.Id))
{
return ResponseOutput.NotOk("其中一个任务已分配给该医生,不允许分配");
}
}
else if (visitTask.SouceReadModuleId != null)
{
if (await _visitTaskRepository.AnyAsync(t => t.SouceReadModuleId == visitTask.SouceReadModuleId && t.TaskAllocationState == TaskAllocationState.Allocated && t.DoctorUserId == assignSubjectTaskToDoctorCommand.DoctorUserId && t.Id != visitTask.Id))
{
return ResponseOutput.NotOk("其中一个任务已分配给该医生,不允许分配");
}
}
else
{
throw new BusinessValidationFailedException("出现脏数据 任务来源字段没有值");
}
//PM 回退了 但是还没生成任务 当前任务编号前有访视进行了回退就不允许分配
if (await _subjectVisitRepository.AnyAsync(t => t.SubjectId == visitTask.SubjectId && t.IsPMBackOrReReading && t.VisitNum <= visitTask.VisitTaskNum))
{
return ResponseOutput.NotOk("该受试者有访视进入了退回流程,还未经过一致性核查通过,不允许分配");
}
visitTask.AllocateTime = DateTime.Now;
visitTask.DoctorUserId = assignSubjectTaskToDoctorCommand.DoctorUserId;
visitTask.TaskAllocationState = TaskAllocationState.Allocated;
visitTask.SuggesteFinishedTime = DateTime.Now.AddDays(7);
//await _subjectVisitRepository.BatchUpdateNoTrackingAsync(t => t.Id == visitTask.SourceSubjectVisitId, u => new SubjectVisit() { ReadingStatus = ReadingStatusEnum.ImageReading });
//await _readModuleRepository.BatchUpdateNoTrackingAsync(t => t.Id == visitTask.SouceReadModuleId, u => new ReadModule() { ReadingStatus = ReadingStatusEnum.ImageReading });
}
else if (assignSubjectTaskToDoctorCommand.TaskOptType == TaskOptType.ReAssign)
{
//验证 是不是两个任务都给了同一个医生
//是否删除配置规则表里的 Subject 医生绑定关系 重新添加绑定关系
//是否其该Subject 其他访视 绑定的医生 也同时变更?
}
else if (assignSubjectTaskToDoctorCommand.TaskOptType == TaskOptType.Confirm)
{
visitTask.TaskAllocationState = TaskAllocationState.Allocated;
}
else if (assignSubjectTaskToDoctorCommand.TaskOptType == TaskOptType.CancelAssign)
{
visitTask.AllocateTime = null;
visitTask.DoctorUserId = null;
visitTask.TaskAllocationState = TaskAllocationState.NotAllocate;
//await _subjectVisitRepository.BatchUpdateNoTrackingAsync(t => t.Id == visitTask.SourceSubjectVisitId, u => new SubjectVisit() { ReadingStatus = ReadingStatusEnum.TaskAllocate });
//await _readModuleRepository.BatchUpdateNoTrackingAsync(t => t.Id == visitTask.SouceReadModuleId, u => new ReadModule() { ReadingStatus = ReadingStatusEnum.TaskAllocate });
}
await _visitTaskRepository.SaveChangesAsync();
return ResponseOutput.Ok();
}
/// <summary>
/// 获取手动分配 未分配的Subject列表(IsHaveAssigned 传递false)
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<PageOutput<SubjectAssignView>> GetSubjectAssignList(SubjectAssignQuery querySubjectAssign)
{
var subjectQuery = _subjectRepository.Where(t => t.TrialId == querySubjectAssign.TrialId)
.WhereIf(querySubjectAssign.IsJudgeDoctor == false, t => t.SubjectVisitTaskList.Where(t => t.ArmEnum != Arm.JudgeArm).Any())
.WhereIf(querySubjectAssign.IsJudgeDoctor, t => t.SubjectVisitTaskList.Where(t => t.ArmEnum == Arm.JudgeArm).Any())
.WhereIf(querySubjectAssign.SiteId != null, t => t.SiteId == querySubjectAssign.SiteId)
.WhereIf(querySubjectAssign.SubjectId != null, t => t.Id == querySubjectAssign.SubjectId)
.WhereIf(querySubjectAssign.IsHaveAssigned != null && querySubjectAssign.IsHaveAssigned == true && querySubjectAssign.IsJudgeDoctor == false, t => t.SubjectDoctorList.Where(t => t.ArmEnum != Arm.JudgeArm).Any())
.WhereIf(querySubjectAssign.IsHaveAssigned != null && querySubjectAssign.IsHaveAssigned == true && querySubjectAssign.IsJudgeDoctor, t => t.SubjectDoctorList.Where(t => t.ArmEnum == Arm.JudgeArm).Any())
.WhereIf(querySubjectAssign.IsHaveAssigned != null && querySubjectAssign.IsHaveAssigned == false && querySubjectAssign.IsJudgeDoctor == false, t => !t.SubjectDoctorList.Where(t => t.ArmEnum != Arm.JudgeArm).Any())
.WhereIf(querySubjectAssign.IsHaveAssigned != null && querySubjectAssign.IsHaveAssigned == false && querySubjectAssign.IsJudgeDoctor, t => !t.SubjectDoctorList.Where(t => t.ArmEnum == Arm.JudgeArm).Any())
.WhereIf(querySubjectAssign.IsAssignConfirmed != null && querySubjectAssign.IsAssignConfirmed == true && querySubjectAssign.IsJudgeDoctor == false, t => t.SubjectDoctorList.Where(t => t.ArmEnum != Arm.JudgeArm).All(u => u.IsConfirmed))
.WhereIf(querySubjectAssign.IsAssignConfirmed != null && querySubjectAssign.IsAssignConfirmed == true && querySubjectAssign.IsJudgeDoctor, t => t.SubjectDoctorList.Where(t => t.ArmEnum == Arm.JudgeArm).All(u => u.IsConfirmed))
.WhereIf(querySubjectAssign.IsAssignConfirmed != null && querySubjectAssign.IsAssignConfirmed == false && querySubjectAssign.IsJudgeDoctor == false, t => t.SubjectDoctorList.Where(t => t.ArmEnum != Arm.JudgeArm).Any(u => u.IsConfirmed == false))
.WhereIf(querySubjectAssign.IsAssignConfirmed != null && querySubjectAssign.IsAssignConfirmed == false && querySubjectAssign.IsJudgeDoctor, t => t.SubjectDoctorList.Where(t => t.ArmEnum == Arm.JudgeArm).Any(u => u.IsConfirmed == false))
.WhereIf(querySubjectAssign.DoctorUserId != null && querySubjectAssign.IsJudgeDoctor == false, t => t.SubjectDoctorList.Where(t => t.ArmEnum != Arm.JudgeArm).Any(t => t.DoctorUserId == querySubjectAssign.DoctorUserId))
.WhereIf(querySubjectAssign.DoctorUserId != null && querySubjectAssign.IsJudgeDoctor, t => t.SubjectDoctorList.Where(t => t.ArmEnum == Arm.JudgeArm).Any(t => t.DoctorUserId == querySubjectAssign.DoctorUserId))
.ProjectTo<SubjectAssignView>(_mapper.ConfigurationProvider, new { isJudgeDoctor = querySubjectAssign.IsJudgeDoctor });
var pageList = await subjectQuery.ToPagedListAsync(querySubjectAssign.PageIndex, querySubjectAssign.PageSize, string.IsNullOrWhiteSpace(querySubjectAssign.SortField) ? nameof(querySubjectAssign.SubjectId) : querySubjectAssign.SortField, querySubjectAssign.Asc);
return pageList;
}
/// <summary>
/// 批量为 多个Subject 分配医生 手动分配 IsReAssign 为true 批量删除 重新分配
/// </summary>
/// <param name="assginSubjectDoctorCommand"></param>
/// <returns></returns>
[HttpPost]
[UnitOfWork]
public async Task<IResponseOutput> AssignSubjectDoctor(AssginSubjectDoctorCommand assginSubjectDoctorCommand)
{
var trialId = assginSubjectDoctorCommand.TrialId;
var doctorUserIdList = assginSubjectDoctorCommand.DoctorUserIdArmList.Select(t => t.DoctorUserId).ToList();
if (assginSubjectDoctorCommand.IsJudgeDoctor)
{
if (assginSubjectDoctorCommand.IsReAssign)
{
//if (await _visitTaskRepository.AnyAsync(t => assginSubjectDoctorCommand.SubjectIdList.Contains(t.SubjectId) && t.DoctorUserId != null && t.ArmEnum == Arm.JudgeArm))
//{
// throw new BusinessValidationFailedException("有Subject任务已应用不允许重新分配");
//}
await _subjectUserRepository.BatchDeleteNoTrackingAsync(t => doctorUserIdList.Contains(t.DoctorUserId) && assginSubjectDoctorCommand.SubjectIdList.Contains(t.SubjectId) && t.ArmEnum == Arm.JudgeArm);
}
}
else
{
if (assginSubjectDoctorCommand.IsReAssign)
{
//if (await _visitTaskRepository.AnyAsync(t => assginSubjectDoctorCommand.SubjectIdList.Contains(t.SubjectId) && t.DoctorUserId != null && t.ArmEnum != Arm.JudgeArm))
//{
// throw new BusinessValidationFailedException("有Subject任务已应用不允许重新分配");
//}
await _subjectUserRepository.BatchDeleteNoTrackingAsync(t => doctorUserIdList.Contains(t.DoctorUserId) && assginSubjectDoctorCommand.SubjectIdList.Contains(t.SubjectId) && t.ArmEnum != Arm.JudgeArm);
}
}
foreach (var subjectId in assginSubjectDoctorCommand.SubjectIdList)
{
foreach (var doctorUserId in doctorUserIdList)
{
var armEnum = assginSubjectDoctorCommand.DoctorUserIdArmList.Where(t => t.DoctorUserId == doctorUserId).First().ArmEnum;
if (await _subjectUserRepository.AnyAsync(t => t.TrialId == trialId && t.SubjectId == subjectId && t.DoctorUserId == doctorUserId && t.ArmEnum != armEnum && t.OrignalSubjectUserId == null))
{
throw new BusinessValidationFailedException("有Subject 在其他Arm组已有该医生不允许在新的组添加该医生");
}
if (await _subjectUserRepository.AnyAsync(t => t.TrialId == trialId && t.SubjectId == subjectId && t.DoctorUserId == doctorUserId && t.ArmEnum == armEnum && t.OrignalSubjectUserId == null))
{
throw new BusinessValidationFailedException("有Subject 已有该Arm组的医生不允许继续分配,请刷新页面,确认页面数据是否过期");
}
else
{
await _subjectUserRepository.AddAsync(new SubjectUser() { TrialId = trialId, SubjectId = subjectId, DoctorUserId = doctorUserId, ArmEnum = armEnum, AssignTime = DateTime.Now });
}
}
}
await _subjectUserRepository.SaveChangesAsync();
return ResponseOutput.Ok();
}
/// <summary>
/// 批量取消Subject 分配的医生
/// </summary>
/// <returns></returns> 数量
[HttpPost]
public async Task<IResponseOutput> CancelSubjectAssignDoctor(CancelSubjectAssignCommand cancelSubjectAssignCommand)
{
if (cancelSubjectAssignCommand.IsJudgeDoctor)
{
foreach (var subjectId in cancelSubjectAssignCommand.SubjectIdList)
{
if (await _visitTaskRepository.AnyAsync(t => t.SubjectId == subjectId && t.DoctorUserId != null && t.ArmEnum == Arm.JudgeArm))
{
throw new BusinessValidationFailedException("有Subject任务已应用不允许取消分配");
}
await _subjectUserRepository.DeleteFromQueryAsync(t => t.SubjectId == subjectId && t.ArmEnum == Arm.JudgeArm);
}
}
else
{
foreach (var subjectId in cancelSubjectAssignCommand.SubjectIdList)
{
if (await _visitTaskRepository.AnyAsync(t => t.SubjectId == subjectId && t.DoctorUserId != null && t.ArmEnum != Arm.JudgeArm))
{
throw new BusinessValidationFailedException("有Subject任务已应用不允许取消分配");
}
await _subjectUserRepository.DeleteFromQueryAsync(t => t.SubjectId == subjectId && t.ArmEnum != Arm.JudgeArm);
}
}
await _subjectUserRepository.SaveChangesAsync();
return ResponseOutput.Ok();
}
/// <summary>
/// 手动分配确认 绑定该Subject的已存在的任务给医生
/// </summary>
/// <param name="assignConfirmCommand"></param>
/// <returns></returns>
[HttpPost]
[UnitOfWork]
public async Task<IResponseOutput> ManualAssignDoctorApplyTask(AssignConfirmCommand assignConfirmCommand)
{
var trialId = assignConfirmCommand.TrialId;
//获取项目配置 判断应该分配几个医生
//var trialConfig = (await _trialRepository.Where(t => t.Id == trialId).Select(t => new { TrialId = t.Id, t.ReadingType, t.IsFollowVisitAutoAssign, t.IsFollowGlobalVisitAutoAssign, t.FollowGlobalVisitAutoAssignDefaultState, t.TaskAllocateObjEnum }).FirstOrDefaultAsync()).IfNullThrowException();
//需要确认的Subject
var subjectIdList = assignConfirmCommand.SubjectDoctorUserList.Select(t => t.SubjectId).ToList();
//将关系确认
if (assignConfirmCommand.SubjectDoctorUserList.Count == 0)
{
await _subjectUserRepository.BatchUpdateNoTrackingAsync(t => t.TrialId == trialId && t.IsConfirmed == false && t.OrignalSubjectUserId == null, u => new SubjectUser() { IsConfirmed = true });
}
else
{
await _subjectUserRepository.BatchUpdateNoTrackingAsync(t => t.TrialId == trialId && t.IsConfirmed == false && t.OrignalSubjectUserId == null
&& subjectIdList.Contains(t.SubjectId), u => new SubjectUser() { IsConfirmed = true });
}
var taskList = _visitTaskRepository.Where(t => t.TrialId == assignConfirmCommand.TrialId && t.DoctorUserId == null, true)
.WhereIf(subjectIdList.Count() > 0 && assignConfirmCommand.IsJudgeDoctor == false, t => subjectIdList.Contains(t.SubjectId) && t.Subject.SubjectDoctorList.Where(t => t.ArmEnum != Arm.JudgeArm).Any())
.WhereIf(subjectIdList.Count() > 0 && assignConfirmCommand.IsJudgeDoctor, t => subjectIdList.Contains(t.SubjectId) && t.Subject.SubjectDoctorList.Where(t => t.ArmEnum == Arm.JudgeArm).Any())
//过滤掉 那些回退的subject
.Where(t => !t.Subject.SubjectVisitList.Any(t => t.IsPMBackOrReReading))
.ToList();
foreach (var subjectTaskGroup in taskList.GroupBy(t => t.SubjectId))
{
var subjectId = subjectTaskGroup.Key;
//如果数据为空 那么就是确认所有已分配的
List<DoctorArm> subjectDoctorIdArmList = new List<DoctorArm>();
if (assignConfirmCommand.SubjectDoctorUserList.Count == 0)
{
subjectDoctorIdArmList = _subjectUserRepository.Where(t => t.SubjectId == subjectId && t.OrignalSubjectUserId == null).Select(t => new DoctorArm() { DoctorUserId = t.DoctorUserId, ArmEnum = t.ArmEnum }).ToList();
}
else
{
subjectDoctorIdArmList = assignConfirmCommand.SubjectDoctorUserList.Where(t => t.SubjectId == subjectId).First().DoctorUserIdArmList;
}
foreach (var task in subjectTaskGroup.OrderBy(t => t.ArmEnum).ToList())
{
var subjectDoctorArm = subjectDoctorIdArmList.Where(t => t.ArmEnum == task.ArmEnum).FirstOrDefault();
if (subjectDoctorArm != null)
{
task.DoctorUserId = subjectDoctorArm.DoctorUserId;
task.AllocateTime = DateTime.Now;
task.TaskAllocationState = TaskAllocationState.Allocated;
task.SuggesteFinishedTime = task.IsUrgent ? DateTime.Now.AddDays(2) : DateTime.Now.AddDays(7);
await _subjectVisitRepository.BatchUpdateNoTrackingAsync(t => t.Id == task.SourceSubjectVisitId, u => new SubjectVisit() { ReadingStatus = ReadingStatusEnum.ImageReading });
await _readModuleRepository.BatchUpdateNoTrackingAsync(t => t.Id == task.SouceReadModuleId, u => new ReadModule() { ReadingStatus = ReadingStatusEnum.ImageReading });
}
else
{
throw new BusinessValidationFailedException("在配置表中未找到配置的医生,无法应用绑定,请核对数据");
}
}
}
await _visitTaskRepository.SaveChangesAsync();
return ResponseOutput.Ok();
}
/// <summary>
/// 自动一次性分配所有未分配的 Subject 给医生
/// </summary>
/// <param name="autoSubjectAssignCommand"></param>
/// <returns></returns>
[HttpPost]
[UnitOfWork]
public async Task<IResponseOutput> AutoSubjectAssignDoctor(AutoSubjectAssignCommand autoSubjectAssignCommand)
{
//自动分配的话,需要把手动分配的给删掉
var trialId = autoSubjectAssignCommand.TrialId;
var isJudge = autoSubjectAssignCommand.IsJudgeDoctor;
//获取项目配置 判断应该分配几个医生
var trialConfig = (await _trialRepository.Where(t => t.Id == trialId).Select(t => new { TrialId = t.Id, t.ReadingType, t.IsFollowVisitAutoAssign, t.IsFollowGlobalVisitAutoAssign, t.FollowGlobalVisitAutoAssignDefaultState, t.TaskAllocateObjEnum }).FirstOrDefaultAsync()).IfNullThrowException();
//获取 已产生任务的Subject 目前分配情况
var subjectList = _subjectRepository.Where(t => t.TrialId == trialId)
.WhereIf(isJudge == false, t => t.SubjectVisitTaskList.Where(t => t.ArmEnum != Arm.JudgeArm).Any())
.WhereIf(isJudge, t => t.SubjectVisitTaskList.Where(t => t.ArmEnum == Arm.JudgeArm).Any())
////过滤掉 那些回退的subject
//.Where(t => !t.SubjectVisitList.Any(t => t.IsPMBackOrReReading))
.Select(t => new
{
SubjectId = t.Id,
//给Subject分配医生的时候 未确认绑定关系的
DoctorUserList = t.SubjectDoctorList.Where(t => isJudge ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Where(t => t.OrignalSubjectUserId == null).Select(t => new { t.DoctorUserId, t.ArmEnum }),
IsApplyed = t.SubjectDoctorList.Where(t => isJudge ? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Where(t => t.OrignalSubjectUserId == null).SelectMany(t => t.SubjectArmVisitTaskList).Any(c => c.DoctorUserId != null)
}).ToList();
//已产生任务的Subject数量裁判情况下就是产生裁判任务Subject 的数量)
var subjectCount = subjectList.Count;
//获取医生列表(裁判是裁判的医生列表)
var waitAllocationDoctorList = _taskAllocationRuleRepository.Where(t => t.TrialId == trialId && t.IsEnable)
.Where(t => t.IsJudgeDoctor == isJudge)
.Select(t => new AutoAssignResultDTO() { DoctorUserId = t.DoctorUserId, PlanReadingRatio = t.PlanReadingRatio, SubjectCount = subjectCount })
.ToList();
if (waitAllocationDoctorList.Count == 0)
{
throw new BusinessValidationFailedException("启用的医生数量为0");
}
//已分配的 医生的情况
var haveAssignedSubjectDoctorList = subjectList.Clone().SelectMany(t => t.DoctorUserList.Select(c => new { t.SubjectId, c.DoctorUserId, c.ArmEnum })).ToList();
//将目前已分配的情况 换到医生的维度
foreach (var waitAllocationDoctor in waitAllocationDoctorList)
{
waitAllocationDoctor.SubjectArmList = haveAssignedSubjectDoctorList.Where(t => t.DoctorUserId == waitAllocationDoctor.DoctorUserId)
.Select(g => new SubjectArm()
{
SubjectId = g.SubjectId,
ArmEnum = g.ArmEnum
}).ToList();
}
#region 完全按照Subject 遍历去分
//仅仅分配未应用的 而且 没有分配医生的
foreach (var subject in subjectList.Where(t => t.IsApplyed == false && !t.DoctorUserList.Any()))
{
//该Subject 已经分配的医生数量
var hasAssignDoctorCount = subject.DoctorUserList.Count();
if (isJudge)
{
if (hasAssignDoctorCount > 1)
{
throw new BusinessValidationFailedException("当前有Subject裁判绑定医生数量大于1");
}
var allocateDoctor = waitAllocationDoctorList.OrderByDescending(t => t.Weight).ThenByDescending(t => t.PlanReadingRatio).FirstOrDefault();
//将分配结果记录
waitAllocationDoctorList.FirstOrDefault(t => t.DoctorUserId == allocateDoctor.DoctorUserId).SubjectArmList.Add(new SubjectArm()
{
SubjectId = subject.SubjectId,
ArmEnum = Arm.JudgeArm
});
await _subjectUserRepository.AddAsync(new SubjectUser() { TrialId = trialId, SubjectId = subject.SubjectId, DoctorUserId = allocateDoctor.DoctorUserId, ArmEnum = Arm.JudgeArm, AssignTime = DateTime.Now });
}
else
{
//分配两个医生
if (trialConfig.ReadingType == ReadingMethod.Double)
{
if (hasAssignDoctorCount > 2)
{
throw new BusinessValidationFailedException("双重阅片当前有Subject绑定医生数量大于2");
}
var allocateDoctorList = waitAllocationDoctorList.OrderByDescending(t => t.Weight).ThenByDescending(t => t.PlanReadingRatio).Take(2).ToList();
#region 看阅片人之前在Subject哪个组做的多
var preferredDoctor1Arm = waitAllocationDoctorList.Where(t => t.DoctorUserId == allocateDoctorList[0].DoctorUserId)
.SelectMany(t => t.SubjectArmList).GroupBy(t => t.ArmEnum)
.Select(g => new { ArmEnum = g.Key, SubjectCount = g.Count() })
.OrderByDescending(t => t.SubjectCount).FirstOrDefault()?.ArmEnum;
var preferredDoctor2Arm = waitAllocationDoctorList.Where(t => t.DoctorUserId == allocateDoctorList[1].DoctorUserId)
.SelectMany(t => t.SubjectArmList).GroupBy(t => t.ArmEnum)
.Select(g => new { ArmEnum = g.Key, SubjectCount = g.Count() })
.OrderByDescending(t => t.SubjectCount).FirstOrDefault()?.ArmEnum;
//存放医生分配的Arm
var doctor1Arm = Arm.DoubleReadingArm1;
var doctor2Arm = Arm.DoubleReadingArm2;
if (preferredDoctor1Arm == null && preferredDoctor2Arm == null ||
preferredDoctor1Arm == null && preferredDoctor2Arm == Arm.DoubleReadingArm2 ||
preferredDoctor1Arm == Arm.DoubleReadingArm1 && preferredDoctor2Arm == null ||
preferredDoctor1Arm == Arm.DoubleReadingArm1 && preferredDoctor2Arm == Arm.DoubleReadingArm2
)
{
doctor1Arm = Arm.DoubleReadingArm1;
doctor2Arm = Arm.DoubleReadingArm2;
}
else if (preferredDoctor1Arm == null && preferredDoctor2Arm == Arm.DoubleReadingArm1 ||
preferredDoctor1Arm == Arm.DoubleReadingArm2 && preferredDoctor2Arm == Arm.DoubleReadingArm1 ||
preferredDoctor1Arm == Arm.DoubleReadingArm2 && preferredDoctor2Arm == null)
{
doctor1Arm = Arm.DoubleReadingArm2;
doctor2Arm = Arm.DoubleReadingArm1;
}
else if (preferredDoctor1Arm == Arm.DoubleReadingArm1 && preferredDoctor2Arm == Arm.DoubleReadingArm1)
{
doctor1Arm = Arm.DoubleReadingArm1;
doctor2Arm = Arm.DoubleReadingArm2;
}
else if (preferredDoctor1Arm == Arm.DoubleReadingArm2 && preferredDoctor2Arm == Arm.DoubleReadingArm2)
{
doctor1Arm = Arm.DoubleReadingArm2;
doctor2Arm = Arm.DoubleReadingArm1;
}
#endregion
await _subjectUserRepository.AddAsync(new SubjectUser() { TrialId = trialId, SubjectId = subject.SubjectId, DoctorUserId = allocateDoctorList[0].DoctorUserId, ArmEnum = doctor1Arm, AssignTime = DateTime.Now });
//将分配结果记录
waitAllocationDoctorList.FirstOrDefault(t => t.DoctorUserId == allocateDoctorList[0].DoctorUserId).SubjectArmList.Add(new SubjectArm()
{
SubjectId = subject.SubjectId,
ArmEnum = doctor1Arm
});
await _subjectUserRepository.AddAsync(new SubjectUser() { TrialId = trialId, SubjectId = subject.SubjectId, DoctorUserId = allocateDoctorList[1].DoctorUserId, ArmEnum = doctor2Arm, AssignTime = DateTime.Now });
//将分配结果记录
waitAllocationDoctorList.FirstOrDefault(t => t.DoctorUserId == allocateDoctorList[1].DoctorUserId).SubjectArmList.Add(new SubjectArm()
{
SubjectId = subject.SubjectId,
ArmEnum = doctor2Arm
});
}
else if (trialConfig.ReadingType == ReadingMethod.Single)
{
if (hasAssignDoctorCount > 1)
{
throw new BusinessValidationFailedException("单重阅片当前有Subject绑定医生数量大于1");
}
var allocateDoctor = waitAllocationDoctorList.OrderByDescending(t => t.Weight).ThenByDescending(t => t.PlanReadingRatio).FirstOrDefault();
//将分配结果记录
waitAllocationDoctorList.FirstOrDefault(t => t.DoctorUserId == allocateDoctor.DoctorUserId).SubjectArmList.Add(new SubjectArm()
{
SubjectId = subject.SubjectId,
ArmEnum = Arm.SingleReadingArm
});
await _subjectUserRepository.AddAsync(new SubjectUser() { TrialId = trialId, SubjectId = subject.SubjectId, DoctorUserId = allocateDoctor.DoctorUserId, ArmEnum = Arm.SingleReadingArm, AssignTime = DateTime.Now });
}
}
}
#endregion
await _subjectUserRepository.SaveChangesAsync();
return ResponseOutput.Ok();
}
/// <summary>
/// 访视任务
/// </summary>
/// <param name="queryVisitTask"></param>
/// <param name="_visitTaskCommonService"></param>
/// <returns></returns>
[HttpPost]
public async Task<(PageOutput<VisitTaskView>, object?)> GetVisitTaskList(VisitTaskQuery queryVisitTask, [FromServices] IVisitTaskHelpeService _visitTaskCommonService)
{
//以前访视未产生任务的,在查询这里要产生
var svIdList = await _subjectVisitRepository.Where(t => t.TrialId == queryVisitTask.TrialId && t.CheckState == CheckStateEnum.CVPassed && t.IsVisitTaskGenerated == false).Select(t => t.Id).ToListAsync();
await _visitTaskCommonService.GenerateVisitTaskAsync(queryVisitTask.TrialId, svIdList);
var visitTaskQueryable = _visitTaskRepository.Where(t => t.TrialId == queryVisitTask.TrialId && t.IsAnalysisCreate == false)
.WhereIf(queryVisitTask.ReadingCategory != null, t => t.ReadingCategory == queryVisitTask.ReadingCategory)
.WhereIf(queryVisitTask.ReadingCategory == null, t => t.ReadingCategory != ReadingCategory.Judge)
.WhereIf(queryVisitTask.TaskState != null, t => t.TaskState == queryVisitTask.TaskState)
.WhereIf(queryVisitTask.SiteId != null, t => t.Subject.SiteId == queryVisitTask.SiteId)
.WhereIf(queryVisitTask.SubjectId != null, t => t.SubjectId == queryVisitTask.SubjectId)
.WhereIf(queryVisitTask.IsUrgent != null, t => t.IsUrgent == queryVisitTask.IsUrgent)
.WhereIf(queryVisitTask.DoctorUserId != null, t => t.DoctorUserId == queryVisitTask.DoctorUserId)
.WhereIf(queryVisitTask.ReadingCategory != null, t => t.ReadingCategory == queryVisitTask.ReadingCategory)
.WhereIf(queryVisitTask.TaskAllocationState != null, t => t.TaskAllocationState == queryVisitTask.TaskAllocationState)
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TrialSiteCode), t => (t.BlindTrialSiteCode.Contains(queryVisitTask.TrialSiteCode) && t.IsAnalysisCreate) || (t.Subject.TrialSite.TrialSiteCode.Contains(queryVisitTask.TrialSiteCode) && t.IsAnalysisCreate == false))
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TaskName), t => t.TaskName.Contains(queryVisitTask.TaskName) || t.TaskBlindName.Contains(queryVisitTask.TaskName))
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.SubjectCode), t => t.Subject.Code.Contains(queryVisitTask.SubjectCode) || t.BlindSubjectCode.Contains(queryVisitTask.SubjectCode))
.WhereIf(queryVisitTask.BeginAllocateDate != null, t => t.AllocateTime > queryVisitTask.BeginAllocateDate)
.WhereIf(queryVisitTask.EndAllocateDate != null, t => t.AllocateTime < queryVisitTask.EndAllocateDate.Value.AddDays(1))
.ProjectTo<VisitTaskView>(_mapper.ConfigurationProvider);
var defalutSortArray = new string[] { nameof(VisitTask.IsUrgent) + " desc", nameof(VisitTask.SubjectId), nameof(VisitTask.VisitTaskNum) };
var pageList = await visitTaskQueryable.ToPagedListAsync(queryVisitTask.PageIndex, queryVisitTask.PageSize, queryVisitTask.SortField, queryVisitTask.Asc, string.IsNullOrWhiteSpace(queryVisitTask.SortField), defalutSortArray);
var trialTaskConfig = _trialRepository.Where(t => t.Id == queryVisitTask.TrialId).ProjectTo<TrialTaskConfigView>(_mapper.ConfigurationProvider).FirstOrDefault();
return (pageList, trialTaskConfig);
}
/// <summary>
/// 裁判任务
/// </summary>
/// <param name="queryVisitTask"></param>
/// <returns></returns>
[HttpPost]
public async Task<PageOutput<JudgeVisitTaskView>/*, object)*/> GetJudgeVisitTaskList(VisitTaskQuery queryVisitTask)
{
var visitTaskQueryable = _visitTaskRepository.Where(t => t.TrialId == queryVisitTask.TrialId && t.IsAnalysisCreate == false)
.Where(t => t.ReadingCategory == ReadingCategory.Judge)
.WhereIf(queryVisitTask.TaskState != null, t => t.TaskState == queryVisitTask.TaskState)
.WhereIf(queryVisitTask.ReadingCategory != null, t => t.ReadingCategory == queryVisitTask.ReadingCategory)
.WhereIf(queryVisitTask.SiteId != null, t => t.Subject.SiteId == queryVisitTask.SiteId)
.WhereIf(queryVisitTask.SubjectId != null, t => t.SubjectId == queryVisitTask.SubjectId)
.WhereIf(queryVisitTask.IsUrgent != null, t => t.IsUrgent == queryVisitTask.IsUrgent)
.WhereIf(queryVisitTask.DoctorUserId != null, t => t.DoctorUserId == queryVisitTask.DoctorUserId)
.WhereIf(queryVisitTask.TaskAllocationState != null, t => t.TaskAllocationState == queryVisitTask.TaskAllocationState)
.WhereIf(queryVisitTask.ReadingTaskState != null, t => t.ReadingTaskState == queryVisitTask.ReadingTaskState)
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TrialSiteCode), t => (t.BlindTrialSiteCode.Contains(queryVisitTask.TrialSiteCode) && t.IsAnalysisCreate) || (t.Subject.TrialSite.TrialSiteCode.Contains(queryVisitTask.TrialSiteCode) && t.IsAnalysisCreate == false))
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TaskName), t => t.TaskName.Contains(queryVisitTask.TaskName) || t.TaskBlindName.Contains(queryVisitTask.TaskName))
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.SubjectCode), t => t.Subject.Code.Contains(queryVisitTask.SubjectCode) || t.BlindSubjectCode.Contains(queryVisitTask.SubjectCode))
.WhereIf(queryVisitTask.BeginAllocateDate != null, t => t.AllocateTime > queryVisitTask.BeginAllocateDate)
.WhereIf(queryVisitTask.EndAllocateDate != null, t => t.AllocateTime < queryVisitTask.EndAllocateDate.Value.AddDays(1))
.ProjectTo<JudgeVisitTaskView>(_mapper.ConfigurationProvider);
var defalutSortArray = new string[] { nameof(VisitTask.IsUrgent) + " desc", nameof(VisitTask.SubjectId), nameof(VisitTaskView.VisitTaskNum) };
var pageList = await visitTaskQueryable.ToPagedListAsync(queryVisitTask.PageIndex, queryVisitTask.PageSize, queryVisitTask.SortField, queryVisitTask.Asc, string.IsNullOrWhiteSpace(queryVisitTask.SortField), defalutSortArray);
return pageList;
//var trialTaskConfig = _trialRepository.Where(t => t.Id == queryVisitTask.TrialId).ProjectTo<TrialTaskConfig>(_mapper.ConfigurationProvider).FirstOrDefault();
//return (pageList, trialTaskConfig);
}
/// <summary>
/// PM阅片跟踪
/// </summary>
/// <param name="queryVisitTask"></param>
/// <returns></returns>
[HttpPost]
public async Task<(PageOutput<ReadingTaskView>, object)> GetReadingTaskList(VisitTaskQuery queryVisitTask)
{
var visitTaskQueryable = _visitTaskRepository.Where(t => t.TrialId == queryVisitTask.TrialId && t.IsAnalysisCreate == false)
.Where(t => t.IsAnalysisCreate == false && t.DoctorUserId != null)
.WhereIf(queryVisitTask.TaskState != null, t => t.TaskState == queryVisitTask.TaskState)
.WhereIf(queryVisitTask.SiteId != null, t => t.Subject.SiteId == queryVisitTask.SiteId)
.WhereIf(queryVisitTask.SubjectId != null, t => t.SubjectId == queryVisitTask.SubjectId)
.WhereIf(queryVisitTask.IsUrgent != null, t => t.IsUrgent == queryVisitTask.IsUrgent)
.WhereIf(queryVisitTask.DoctorUserId != null, t => t.DoctorUserId == queryVisitTask.DoctorUserId)
.WhereIf(queryVisitTask.ReadingCategory != null, t => t.ReadingCategory == queryVisitTask.ReadingCategory)
.WhereIf(queryVisitTask.ReadingTaskState != null, t => t.ReadingTaskState == queryVisitTask.ReadingTaskState)
.WhereIf(queryVisitTask.TaskAllocationState != null, t => t.TaskAllocationState == queryVisitTask.TaskAllocationState)
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TrialSiteCode), t => (t.BlindTrialSiteCode.Contains(queryVisitTask.TrialSiteCode) && t.IsAnalysisCreate) || (t.Subject.TrialSite.TrialSiteCode.Contains(queryVisitTask.TrialSiteCode) && t.IsAnalysisCreate == false))
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TaskName), t => t.TaskName.Contains(queryVisitTask.TaskName) || t.TaskBlindName.Contains(queryVisitTask.TaskName))
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.SubjectCode), t => t.Subject.Code.Contains(queryVisitTask.SubjectCode) || t.BlindSubjectCode.Contains(queryVisitTask.SubjectCode))
.WhereIf(queryVisitTask.BeginAllocateDate != null, t => t.AllocateTime > queryVisitTask.BeginAllocateDate)
.WhereIf(queryVisitTask.EndAllocateDate != null, t => t.AllocateTime < queryVisitTask.EndAllocateDate.Value.AddDays(1))
.ProjectTo<ReadingTaskView>(_mapper.ConfigurationProvider);
var defalutSortArray = new string[] { nameof(VisitTask.IsUrgent) + " desc", nameof(VisitTask.SubjectId), nameof(VisitTask.VisitTaskNum) };
var pageList = await visitTaskQueryable.ToPagedListAsync(queryVisitTask.PageIndex, queryVisitTask.PageSize, queryVisitTask.SortField, queryVisitTask.Asc, string.IsNullOrWhiteSpace(queryVisitTask.SortField), defalutSortArray);
var trialTaskConfig = _trialRepository.Where(t => t.Id == queryVisitTask.TrialId).ProjectTo<TrialTaskConfigView>(_mapper.ConfigurationProvider).FirstOrDefault();
return (pageList, trialTaskConfig);
}
/// <summary>
/// PM 重阅追踪
/// </summary>
/// <param name="queryVisitTask"></param>
/// <returns></returns>
[HttpPost]
public async Task<PageOutput<ReReadingTaskView>> GetReReadingTaskList(VisitTaskQuery queryVisitTask)
{
var visitTaskQueryable = _visitTaskReReadingRepository
.Where(t => t.OriginalReReadingTask.TrialId == queryVisitTask.TrialId /*&& t.OriginalReReadingTask.IsAnalysisCreate == false*/)
.WhereIf(queryVisitTask.RootReReadingTaskId != null, t => t.RootReReadingTaskId == queryVisitTask.RootReReadingTaskId || t.OriginalReReadingTaskId == queryVisitTask.RootReReadingTaskId)
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TaskCode), t => t.OriginalReReadingTask.TaskCode.Contains(queryVisitTask.TaskCode) || t.RootReReadingTask.TaskCode.Contains(queryVisitTask.TaskCode))
.WhereIf(queryVisitTask.SiteId != null, t => t.OriginalReReadingTask.Subject.SiteId == queryVisitTask.SiteId)
.WhereIf(queryVisitTask.TaskState != null, t => t.OriginalReReadingTask.TaskState == queryVisitTask.TaskState)
.WhereIf(queryVisitTask.ReReadingApplyState != null, t => t.OriginalReReadingTask.ReReadingApplyState == queryVisitTask.ReReadingApplyState)
.WhereIf(queryVisitTask.SubjectId != null, t => t.OriginalReReadingTask.SubjectId == queryVisitTask.SubjectId)
.WhereIf(queryVisitTask.IsUrgent != null, t => t.OriginalReReadingTask.IsUrgent == queryVisitTask.IsUrgent)
.WhereIf(queryVisitTask.DoctorUserId != null, t => t.OriginalReReadingTask.DoctorUserId == queryVisitTask.DoctorUserId)
.WhereIf(queryVisitTask.ReadingTaskState != null, t => t.OriginalReReadingTask.ReadingTaskState == queryVisitTask.ReadingTaskState)
.WhereIf(queryVisitTask.TaskAllocationState != null, t => t.OriginalReReadingTask.TaskAllocationState == queryVisitTask.TaskAllocationState)
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TrialSiteCode), t => (t.OriginalReReadingTask.BlindTrialSiteCode.Contains(queryVisitTask.TrialSiteCode) && t.OriginalReReadingTask.IsAnalysisCreate) || (t.OriginalReReadingTask.Subject.TrialSite.TrialSiteCode.Contains(queryVisitTask.TrialSiteCode) && t.OriginalReReadingTask.IsAnalysisCreate == false))
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TaskName), t => t.OriginalReReadingTask.TaskName.Contains(queryVisitTask.TaskName) || t.OriginalReReadingTask.TaskBlindName.Contains(queryVisitTask.TaskName))
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.SubjectCode), t => t.OriginalReReadingTask.Subject.Code.Contains(queryVisitTask.SubjectCode) || t.OriginalReReadingTask.BlindSubjectCode.Contains(queryVisitTask.SubjectCode))
.WhereIf(queryVisitTask.BeginAllocateDate != null, t => t.OriginalReReadingTask.AllocateTime > queryVisitTask.BeginAllocateDate)
.WhereIf(queryVisitTask.EndAllocateDate != null, t => t.OriginalReReadingTask.AllocateTime < queryVisitTask.EndAllocateDate.Value.AddDays(1))
.ProjectTo<ReReadingTaskView>(_mapper.ConfigurationProvider);
var defalutSortArray = new string[] { nameof(ReReadingTaskView.OriginalReReadingTask) + "." + nameof(ReReadingTaskView.OriginalReReadingTask.IsUrgent) + " desc", nameof(ReReadingTaskView.OriginalReReadingTask) + "." + nameof(ReReadingTaskView.OriginalReReadingTask.SubjectId), nameof(ReReadingTaskView.OriginalReReadingTask) + "." + nameof(ReReadingTaskView.OriginalReReadingTask.VisitTaskNum) };
var pageList = await visitTaskQueryable.ToPagedListAsync(queryVisitTask.PageIndex, queryVisitTask.PageSize, queryVisitTask.SortField, queryVisitTask.Asc, string.IsNullOrWhiteSpace(queryVisitTask.SortField), defalutSortArray);
return pageList;
}
/// <summary>
/// 获取IR 重阅影像阅片列表
/// </summary>
/// <param name="queryVisitTask"></param>
/// <returns></returns>
[HttpPost]
public async Task<PageOutput<ReReadingTaskView>> GetIRReReadingTaskList(VisitTaskQuery queryVisitTask)
{
var visitTaskQueryable = _visitTaskReReadingRepository
.Where(t => t.RequestReReadingType == RequestReReadingType.DocotorApply)
.Where(t => t.OriginalReReadingTask.DoctorUserId == _userInfo.Id)
.Where(t => t.OriginalReReadingTask.TrialId == queryVisitTask.TrialId)
.WhereIf(queryVisitTask.RootReReadingTaskId != null, t => t.RootReReadingTaskId == queryVisitTask.RootReReadingTaskId || t.OriginalReReadingTaskId == queryVisitTask.RootReReadingTaskId)
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TaskCode), t => t.OriginalReReadingTask.TaskCode.Contains(queryVisitTask.TaskCode) || t.RootReReadingTask.TaskCode.Contains(queryVisitTask.TaskCode))
.WhereIf(queryVisitTask.TaskState != null, t => t.OriginalReReadingTask.TaskState == queryVisitTask.TaskState)
.WhereIf(queryVisitTask.ReReadingApplyState != null, t => t.OriginalReReadingTask.ReReadingApplyState == queryVisitTask.ReReadingApplyState)
.WhereIf(queryVisitTask.SiteId != null, t => t.OriginalReReadingTask.Subject.SiteId == queryVisitTask.SiteId)
.WhereIf(queryVisitTask.SubjectId != null, t => t.OriginalReReadingTask.SubjectId == queryVisitTask.SubjectId)
.WhereIf(queryVisitTask.IsUrgent != null, t => t.OriginalReReadingTask.IsUrgent == queryVisitTask.IsUrgent)
.WhereIf(queryVisitTask.DoctorUserId != null, t => t.OriginalReReadingTask.DoctorUserId == queryVisitTask.DoctorUserId)
.WhereIf(queryVisitTask.ReadingTaskState != null, t => t.OriginalReReadingTask.ReadingTaskState == queryVisitTask.ReadingTaskState)
.WhereIf(queryVisitTask.TaskAllocationState != null, t => t.OriginalReReadingTask.TaskAllocationState == queryVisitTask.TaskAllocationState)
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TrialSiteCode), t => (t.OriginalReReadingTask.BlindTrialSiteCode.Contains(queryVisitTask.TrialSiteCode) && t.OriginalReReadingTask.IsAnalysisCreate) || (t.OriginalReReadingTask.Subject.TrialSite.TrialSiteCode.Contains(queryVisitTask.TrialSiteCode) && t.OriginalReReadingTask.IsAnalysisCreate == false))
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TaskName), t => t.OriginalReReadingTask.TaskName.Contains(queryVisitTask.TaskName) || t.NewReReadingTask.TaskBlindName.Contains(queryVisitTask.TaskName))
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.SubjectCode), t => t.OriginalReReadingTask.Subject.Code.Contains(queryVisitTask.SubjectCode) || t.OriginalReReadingTask.BlindSubjectCode.Contains(queryVisitTask.SubjectCode))
.WhereIf(queryVisitTask.BeginAllocateDate != null, t => t.OriginalReReadingTask.AllocateTime > queryVisitTask.BeginAllocateDate)
.WhereIf(queryVisitTask.EndAllocateDate != null, t => t.OriginalReReadingTask.AllocateTime < queryVisitTask.EndAllocateDate.Value.AddDays(1))
.ProjectTo<ReReadingTaskView>(_mapper.ConfigurationProvider);
var defalutSortArray = new string[] { nameof(ReReadingTaskView.OriginalReReadingTask) + "." + nameof(ReReadingTaskView.OriginalReReadingTask.IsUrgent) + " desc", nameof(ReReadingTaskView.OriginalReReadingTask) + "." + nameof(ReReadingTaskView.OriginalReReadingTask.SubjectId), nameof(ReReadingTaskView.OriginalReReadingTask) + "." + nameof(ReReadingTaskView.OriginalReReadingTask.VisitTaskNum) };
var pageList = await visitTaskQueryable.ToPagedListAsync(queryVisitTask.PageIndex, queryVisitTask.PageSize, queryVisitTask.SortField, queryVisitTask.Asc, string.IsNullOrWhiteSpace(queryVisitTask.SortField), defalutSortArray);
return pageList;
}
//SPM 能看到项目组申请记录
[HttpPost]
public async Task<PageOutput<ReReadingTaskView>> GetSPMReReadingTaskList(VisitTaskQuery queryVisitTask)
{
var visitTaskQueryable = _visitTaskReReadingRepository.Where(t => t.RequestReReadingType == RequestReReadingType.TrialGroupApply && t.OriginalReReadingTask.IsAnalysisCreate == false)
.Where(t => t.OriginalReReadingTask.TrialId == queryVisitTask.TrialId)
.WhereIf(queryVisitTask.RootReReadingTaskId != null, t => t.RootReReadingTaskId == queryVisitTask.RootReReadingTaskId || t.OriginalReReadingTaskId == queryVisitTask.RootReReadingTaskId)
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TaskCode), t => t.OriginalReReadingTask.TaskCode.Contains(queryVisitTask.TaskCode) || t.RootReReadingTask.TaskCode.Contains(queryVisitTask.TaskCode))
.WhereIf(queryVisitTask.TaskState != null, t => t.OriginalReReadingTask.TaskState == queryVisitTask.TaskState)
.WhereIf(queryVisitTask.SiteId != null, t => t.OriginalReReadingTask.Subject.SiteId == queryVisitTask.SiteId)
.WhereIf(queryVisitTask.SubjectId != null, t => t.OriginalReReadingTask.SubjectId == queryVisitTask.SubjectId)
.WhereIf(queryVisitTask.IsUrgent != null, t => t.OriginalReReadingTask.IsUrgent == queryVisitTask.IsUrgent)
.WhereIf(queryVisitTask.DoctorUserId != null, t => t.OriginalReReadingTask.DoctorUserId == queryVisitTask.DoctorUserId)
.WhereIf(queryVisitTask.ReadingTaskState != null, t => t.OriginalReReadingTask.ReadingTaskState == queryVisitTask.ReadingTaskState)
.WhereIf(queryVisitTask.TaskAllocationState != null, t => t.OriginalReReadingTask.TaskAllocationState == queryVisitTask.TaskAllocationState)
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TrialSiteCode), t => (t.OriginalReReadingTask.BlindTrialSiteCode.Contains(queryVisitTask.TrialSiteCode) && t.OriginalReReadingTask.IsAnalysisCreate) || (t.OriginalReReadingTask.Subject.TrialSite.TrialSiteCode.Contains(queryVisitTask.TrialSiteCode) && t.OriginalReReadingTask.IsAnalysisCreate == false))
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TaskName), t => t.OriginalReReadingTask.TaskName.Contains(queryVisitTask.TaskName) || t.NewReReadingTask.TaskBlindName.Contains(queryVisitTask.TaskName))
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.SubjectCode), t => t.OriginalReReadingTask.Subject.Code.Contains(queryVisitTask.SubjectCode) || t.OriginalReReadingTask.BlindSubjectCode.Contains(queryVisitTask.SubjectCode))
.WhereIf(queryVisitTask.BeginAllocateDate != null, t => t.OriginalReReadingTask.AllocateTime > queryVisitTask.BeginAllocateDate)
.WhereIf(queryVisitTask.EndAllocateDate != null, t => t.OriginalReReadingTask.AllocateTime < queryVisitTask.EndAllocateDate.Value.AddDays(1))
.ProjectTo<ReReadingTaskView>(_mapper.ConfigurationProvider);
var defalutSortArray = new string[] { nameof(ReReadingTaskView.OriginalReReadingTask) + "." + nameof(ReReadingTaskView.OriginalReReadingTask.IsUrgent) + " desc", nameof(ReReadingTaskView.OriginalReReadingTask) + "." + nameof(ReReadingTaskView.OriginalReReadingTask.SubjectId), nameof(ReReadingTaskView.OriginalReReadingTask) + "." + nameof(ReReadingTaskView.OriginalReReadingTask.VisitTaskNum) };
var pageList = await visitTaskQueryable.ToPagedListAsync(queryVisitTask.PageIndex, queryVisitTask.PageSize, queryVisitTask.SortField, queryVisitTask.Asc, string.IsNullOrWhiteSpace(queryVisitTask.SortField), defalutSortArray);
return pageList;
}
/// <summary>
/// IR 待阅片任务列表
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<(PageOutput<IRUnReadSubjectView>, object)> GetIRUnReadSubjectTaskList(IRUnReadSubjectQuery iRUnReadSubjectQuery)
{
var trialId = iRUnReadSubjectQuery.TrialId;
#region 按照任务的维度统计分组
//var query = _visitTaskRepository.Where(t => t.TrialId == trialId)
// .Where(t => t.DoctorUserId == _userInfo.Id && t.ReadingTaskState != ReadingTaskState.HaveSigned)
// .GroupBy(t => new { t.SubjectId, t.Subject.Code })
// .Select(g => new IRUnReadSubjectView()
// {
// SubjectCode = g.Key.Code,
// SubjectId = g.Key.SubjectId,
// UnReadTaskCount = g.Count(),
// UnReadTaskList = g.AsQueryable().Select(c => new IRUnreadTaskView() { Id = c.Id, SuggesteFinishedTime = c.SuggesteFinishedTime, IsUrgent = c.IsUrgent }).ToList()
// });
//return query.ToList();
#endregion
#region 按照Subject 维度
var isReadingTaskViewInOrder = await _trialRepository.Where(x => x.Id == iRUnReadSubjectQuery.TrialId).Select(x => x.IsReadingTaskViewInOrder).FirstOrDefaultAsync();
if (isReadingTaskViewInOrder)
{
// var subjectQuery = _subjectRepository.Where(t => t.TrialId == trialId)
//.Where(t => t.SubjectDoctorList.Any(t => t.DoctorUserId == _userInfo.Id))
//.WhereIf(!string.IsNullOrEmpty(iRUnReadSubjectQuery.SubjectCode), t => t.Code.Contains(iRUnReadSubjectQuery.SubjectCode))
//.Select(s => new IRUnReadSubjectView()
//{
// SubjectId = s.Id,
// SubjectCode = s.Code,
// UnReadTaskCount = s.SubjectVisitTaskList.Count(t => t.ReadingTaskState != ReadingTaskState.HaveSigned && t.DoctorUserId == _userInfo.Id),
// UnReadTaskList = s.SubjectVisitTaskList.Where(t => t.ReadingTaskState != ReadingTaskState.HaveSigned && t.DoctorUserId == _userInfo.Id)
// .Select(u => new IRUnreadTaskView() { Id = u.Id, IsUrgent = u.IsUrgent, SuggesteFinishedTime = u.SuggesteFinishedTime }).ToList(),
//})
// .Where(t => t.UnReadTaskCount > 0);
var visitTaskQuery = GetOrderReadingIQueryable(trialId);
var totalCount = visitTaskQuery.Item1;
var currentPageData = await visitTaskQuery.Item2.Skip((iRUnReadSubjectQuery.PageIndex - 1) * iRUnReadSubjectQuery.PageSize)
.Take(iRUnReadSubjectQuery.PageSize).ToListAsync();
var result = new PageOutput<IRUnReadSubjectView>()
{
PageSize = iRUnReadSubjectQuery.PageSize,
PageIndex = iRUnReadSubjectQuery.PageIndex,
TotalCount = totalCount,
CurrentPageData = currentPageData,
};
// 封装的方法有问题
//var result = await visitQuery.ToPagedListAsync(iRUnReadSubjectQuery.PageIndex, iRUnReadSubjectQuery.PageSize, String.IsNullOrEmpty(iRUnReadSubjectQuery.SortField) ? nameof(IRUnReadSubjectView.SubjectId) : iRUnReadSubjectQuery.SortField, iRUnReadSubjectQuery.Asc);
return (result, new
{
RandomReadInfo = new IRUnReadOutDto(),
IsReadingTaskViewInOrder = isReadingTaskViewInOrder,
});
}
else
{
var taskQuery = _visitTaskRepository.Where(x => x.TrialId == iRUnReadSubjectQuery.TrialId && x.DoctorUserId == _userInfo.Id && x.TaskState == TaskState.Effect)
.Where(x => !x.Subject.IsDeleted);
IRUnReadOutDto iRUnReadOut = new IRUnReadOutDto()
{
FinishJudgeTaskCount = await taskQuery.Where(x => x.ReadingCategory == ReadingCategory.Judge && x.ReadingTaskState == ReadingTaskState.HaveSigned).CountAsync(),
FinishTaskCount = await taskQuery.Where(x => x.ReadingCategory != ReadingCategory.Judge && x.ReadingTaskState == ReadingTaskState.HaveSigned).CountAsync(),
SuggesteFinishedTime = await taskQuery.Where(x => x.ReadingTaskState != ReadingTaskState.HaveSigned).MaxAsync(x => x.SuggesteFinishedTime),
UnReadJudgeTaskCount = await taskQuery.Where(x => x.ReadingCategory == ReadingCategory.Judge && x.ReadingTaskState != ReadingTaskState.HaveSigned).CountAsync(),
UnReadTaskCount = await taskQuery.Where(x => x.ReadingCategory != ReadingCategory.Judge && x.ReadingTaskState != ReadingTaskState.HaveSigned).CountAsync(),
};
return (new PageOutput<IRUnReadSubjectView>(), new
{
IsReadingTaskViewInOrder = isReadingTaskViewInOrder,
RandomReadInfo = iRUnReadOut,
});
}
#endregion
}
/// <summary>
/// 获取有序阅片IQuery对象
/// </summary>
/// <param name="trialId"></param>
/// <returns></returns>
public (int, IOrderedQueryable<IRUnReadSubjectView>) GetOrderReadingIQueryable(Guid trialId)
{
var visitGroupQuery = _visitTaskRepository.Where(x => x.TrialId == trialId && x.DoctorUserId == _userInfo.Id)
.Where(x => !x.Subject.IsDeleted)
.Where(t => (t.ReadingTaskState != ReadingTaskState.HaveSigned && t.TaskState == TaskState.Effect) || t.ReReadingApplyState == ReReadingApplyState.HaveApplyed)
.GroupBy(x => new { x.SubjectId, x.Subject.Code, x.BlindSubjectCode });
var visitTaskQuery = visitGroupQuery.Select(x => new IRUnReadSubjectView()
{
SubjectId = x.Key.SubjectId,
SubjectCode = x.Key.BlindSubjectCode == string.Empty ? x.Key.Code : x.Key.BlindSubjectCode,
UnReadTaskCount = x.Where(y => y.ReReadingApplyState != ReReadingApplyState.HaveApplyed).Count(),
ExistReadingApply = x.Any(y => y.ReReadingApplyState == ReReadingApplyState.HaveApplyed),
UnReadTaskList = x.Where(y => y.ReReadingApplyState != ReReadingApplyState.HaveApplyed).OrderBy(x => x.VisitTaskNum)
.Select(u => new IRUnreadTaskView()
{
Id = u.Id,
IsUrgent = u.IsUrgent,
VisitNum = u.VisitTaskNum,
TaskBlindName = u.TaskBlindName,
VisistId = u.SourceSubjectVisitId,
SuggesteFinishedTime = u.SuggesteFinishedTime,
ReadingCategory = u.ReadingCategory,
}).ToList(),
}).Where(x => x.UnReadTaskCount > 0).OrderBy(x => x.SubjectId);
var count = visitGroupQuery.Select(x => new
{
UnReadTaskCount = x.Where(y => y.ReReadingApplyState != ReReadingApplyState.HaveApplyed).Count(),
}).Where(x => x.UnReadTaskCount > 0).Count();
return (count, visitTaskQuery);
}
/// <summary>
/// IR 已阅片任务
/// </summary>
/// <param name="queryVisitTask"></param>
/// <returns></returns>
[HttpPost]
public async Task<PageOutput<IRHaveReadView>> GetIRHaveReadTaskList(VisitTaskQuery queryVisitTask)
{
var visitTaskQueryable = _visitTaskRepository.Where(t => t.TrialId == queryVisitTask.TrialId)
.Where(t => t.DoctorUserId == _userInfo.Id && t.ReadingTaskState == ReadingTaskState.HaveSigned)//该医生 已经签名的数据
.WhereIf(queryVisitTask.SiteId != null, t => t.Subject.SiteId == queryVisitTask.SiteId)
.WhereIf(queryVisitTask.SubjectId != null, t => t.SubjectId == queryVisitTask.SubjectId)
.WhereIf(queryVisitTask.IsUrgent != null, t => t.IsUrgent == queryVisitTask.IsUrgent)
.WhereIf(queryVisitTask.ReadingCategory != null, t => t.ReadingCategory == queryVisitTask.ReadingCategory)
.WhereIf(queryVisitTask.TaskAllocationState != null, t => t.TaskAllocationState == queryVisitTask.TaskAllocationState)
.WhereIf(queryVisitTask.TaskState != null, t => t.TaskState == queryVisitTask.TaskState)
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TrialSiteCode), t => t.BlindTrialSiteCode.Contains(queryVisitTask.TrialSiteCode) || t.Subject.TrialSite.TrialSiteCode.Contains(queryVisitTask.TrialSiteCode))
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.TaskName), t => t.TaskName.Contains(queryVisitTask.TaskName) || t.TaskBlindName.Contains(queryVisitTask.TaskName))
.WhereIf(!string.IsNullOrEmpty(queryVisitTask.SubjectCode), t => t.Subject.Code.Contains(queryVisitTask.SubjectCode) || t.BlindSubjectCode.Contains(queryVisitTask.SubjectCode))
.WhereIf(queryVisitTask.BeginAllocateDate != null, t => t.AllocateTime > queryVisitTask.BeginAllocateDate)
.WhereIf(queryVisitTask.EndAllocateDate != null, t => t.AllocateTime < queryVisitTask.EndAllocateDate.Value.AddDays(1))
.ProjectTo<IRHaveReadView>(_mapper.ConfigurationProvider);
var defalutSortArray = new string[] { nameof(VisitTask.IsUrgent) + " desc", nameof(VisitTask.SubjectId), nameof(VisitTask.VisitTaskNum) };
var pageList = await visitTaskQueryable.ToPagedListAsync(queryVisitTask.PageIndex, queryVisitTask.PageSize, queryVisitTask.SortField, queryVisitTask.Asc, string.IsNullOrWhiteSpace(queryVisitTask.SortField), defalutSortArray);
return pageList;
}
/// <summary>
/// 申请重阅 1:IR 2:PM
/// </summary>
/// <param name="applyReReadingCommand"></param>
/// <returns></returns>
[HttpPost]
[UnitOfWork]
public async Task<IResponseOutput> ApplyReReading(ApplyReReadingCommand applyReReadingCommand)
{
var taskList = await _visitTaskRepository.Where(t => applyReReadingCommand.TaskIdList.Contains(t.Id), true).Include(t => t.JudgeVisitTask).ToListAsync();
var trialConfig = (await _trialRepository.Where(t => t.Id == applyReReadingCommand.TrialId).Select(t => new { TrialId = t.Id, t.ReadingType, t.IsReadingTaskViewInOrder }).FirstOrDefaultAsync()).IfNullThrowException();
foreach (var task in taskList)
{
if (task.ReadingTaskState != ReadingTaskState.HaveSigned || task.TaskState != TaskState.Effect)
{
throw new BusinessValidationFailedException("未阅片完成,或者未生效的任务不允许申请重阅");
}
if (task.ReReadingApplyState == ReReadingApplyState.HaveApplyed || task.ReReadingApplyState == ReReadingApplyState.Agree)
{
throw new BusinessValidationFailedException("重阅已申请,或者重阅已同意状态下不允许申请重阅");
}
if (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ProjectManager)
{
if (task.IsAnalysisCreate)
{
throw new BusinessValidationFailedException("PM 不允许对一致性分析任务进行申请重阅");
}
if (task.ReadingCategory != ReadingCategory.Visit)
{
throw new BusinessValidationFailedException("PM 仅仅允许对访视类型的任务申请重阅");
}
//// 有序
//if (trialConfig.IsReadingTaskViewInOrder)
//{
// // 当前访视之前 已有任务申请
// if (await _visitTaskRepository.AnyAsync(t => t.TrialId == task.TrialId && t.SubjectId == task.SubjectId && t.TaskState == TaskState.Effect && t.ReadingCategory == ReadingCategory.Visit
// && t.ReadingTaskState == ReadingTaskState.HaveSigned && t.VisitTaskNum <= task.VisitTaskNum && t.Id != task.Id && t.ReReadingApplyState == ReReadingApplyState.HaveApplyed))
// {
// return ResponseOutput.NotOk("当前为有序阅片,之前有访视,或其他IR的本次访视已申请重阅还未处理不允许申请");
// }
//}
//else
//{
//}
}
if (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.IndependentReviewer)
{
// 有序
if (trialConfig.IsReadingTaskViewInOrder)
{
Expression<Func<VisitTask, bool>> filterExpression = t => t.TrialId == task.TrialId && t.SubjectId == task.SubjectId && t.TaskState == TaskState.Effect
&& t.ReadingTaskState == ReadingTaskState.HaveSigned && t.DoctorUserId == task.DoctorUserId && t.IsAnalysisCreate == false && t.VisitTaskNum > task.VisitTaskNum;
if (task.ReadingCategory == ReadingCategory.Judge && await _visitTaskRepository.AnyAsync(filterExpression.And(t => t.ReadingCategory == ReadingCategory.Global)))
{
throw new BusinessValidationFailedException("只允许申请该受试者阅片人最后一次完成全局任务重阅");
}
if (task.ReadingCategory == ReadingCategory.Oncology && await _visitTaskRepository.AnyAsync(filterExpression.And(t => t.ReadingCategory == ReadingCategory.Oncology)))
{
throw new BusinessValidationFailedException("只允许申请该受试者阅片人最后一次完成肿瘤学任务重阅");
}
if (task.ReadingCategory == ReadingCategory.Judge && await _visitTaskRepository.AnyAsync(filterExpression.And(t => t.ReadingCategory == ReadingCategory.Judge)))
{
throw new BusinessValidationFailedException("只允许申请该受试者阅片人最后一次完成裁判的任务重阅");
}
}
else
{
if (task.ReadingCategory != ReadingCategory.Visit && task.ReadingCategory != ReadingCategory.Global)
{
throw new BusinessValidationFailedException("无序阅片仅仅允许IR 申请 全局和访视类型类别的任务进行重阅");
}
}
}
task.ReReadingApplyState = ReReadingApplyState.HaveApplyed;
var rootReReadingTaskId = _visitTaskReReadingRepository.Where(t => t.NewReReadingTaskId == task.Id).Select(u => u.RootReReadingTaskId).FirstOrDefault();
//添加申请记录
var visitTaskReReading = await _visitTaskReReadingRepository.AddAsync(new VisitTaskReReading()
{
RootReReadingTaskId = rootReReadingTaskId == Guid.Empty ? task.Id : rootReReadingTaskId,
OriginalReReadingTaskId = task.Id,
RequestReReadingTime = DateTime.Now,
RequestReReadingUserId = _userInfo.Id,
IsCopyOrigenalForms = applyReReadingCommand.IsCopyOrigenalForms,
IsCopyFollowForms = applyReReadingCommand.IsCopyFollowForms,
RequestReReadingReason = applyReReadingCommand.RequestReReadingReason,
RequestReReadingType = applyReReadingCommand.RequestReReadingType,
}); ;
}
await _visitTaskRepository.SaveChangesAsync();
return ResponseOutput.Ok();
}
/// <summary>
/// 重阅原任务跟踪处理 只会在同意的时候调用这个
/// </summary>
/// <param name="origenalTask"></param>
private void ReReadingTaskTrackingDeal(VisitTask origenalTask, ConfirmReReadingCommand agreeReReadingCommand)
{
origenalTask.ReReadingApplyState = agreeReReadingCommand.RequestReReadingResultEnum == RequestReReadingResult.Agree ? ReReadingApplyState.Agree : ReReadingApplyState.Reject;
origenalTask.TaskState = agreeReReadingCommand.RequestReReadingResultEnum == RequestReReadingResult.Agree ? TaskState.HaveReturned : origenalTask.TaskState;
}
/// <summary>
/// 确认重阅与否 1同意 2 拒绝
/// </summary>
/// <param name="agreeReReadingCommand"></param>
/// <returns></returns>
[HttpPost]
[UnitOfWork]
public async Task<IResponseOutput> ConfirmReReading(ConfirmReReadingCommand agreeReReadingCommand, [FromServices] IVisitTaskHelpeService _visitTaskCommonService)
{
var trialId = agreeReReadingCommand.TrialId;
var trialConfig = (await _trialRepository.Where(t => t.Id == trialId).Select(t => new { TrialId = t.Id, t.IsReadingTaskViewInOrder, t.ReadingType }).FirstOrDefaultAsync()).IfNullThrowException();
foreach (var item in agreeReReadingCommand.ConfirmReReadingList)
{
//var origenalTask = (await _visitTaskRepository.Where(t => item.OriginalReReadingTaskId == t.Id, true).FirstOrDefaultAsync()).IfNullThrowException();
////更新原始任务 当前任务处理
//origenalTask.ReReadingApplyState = agreeReReadingCommand.RequestReReadingResultEnum == RequestReReadingResult.Agree ? ReReadingApplyState.Agree : ReReadingApplyState.Reject;
//origenalTask.TaskState = agreeReReadingCommand.RequestReReadingResultEnum == RequestReReadingResult.Agree ? TaskState.HaveReturned : origenalTask.TaskState;
var origenalTask = (await _visitTaskRepository.Where(t => item.OriginalReReadingTaskId == t.Id).FirstOrDefaultAsync()).IfNullThrowException();
// 当前访视之前 已有任务申请
if (trialConfig.IsReadingTaskViewInOrder)
{
var query = _visitTaskReReadingRepository.Where(t => t.OriginalReReadingTask.SubjectId == origenalTask.SubjectId && t.OriginalReReadingTask.TaskState == TaskState.Effect && t.OriginalReReadingTask.ReadingCategory == ReadingCategory.Visit
&& t.OriginalReReadingTask.ReadingTaskState == ReadingTaskState.HaveSigned && t.OriginalReReadingTask.VisitTaskNum > origenalTask.VisitTaskNum && t.RequestReReadingResultEnum == RequestReReadingResult.Default)
.Where(t => t.OriginalReReadingTask.IsAnalysisCreate == origenalTask.IsAnalysisCreate);
if (await query.AnyAsync())
{
return ResponseOutput.NotOk("当前为有序阅片,当前访视之后,也申请了重阅 必须从后向前处理");
}
}
Expression<Func<VisitTask, bool>> filterExpression = t => t.TrialId == trialId && t.SubjectId == origenalTask.SubjectId && t.TaskState == TaskState.Effect && t.TaskAllocationState == TaskAllocationState.Allocated;
//是否是一致性分析任务
filterExpression = filterExpression.And(t => t.IsAnalysisCreate == origenalTask.IsAnalysisCreate);
//更新申请信息
var visitTaskReReadingAppply = await _visitTaskReReadingRepository.FirstOrDefaultAsync(t => t.Id == item.Id);
visitTaskReReadingAppply.RequestReReadingConfirmUserId = _userInfo.Id;
visitTaskReReadingAppply.RequestReReadingResultEnum = agreeReReadingCommand.RequestReReadingResultEnum;
visitTaskReReadingAppply.RequestReReadingRejectReason = agreeReReadingCommand.RequestReReadingRejectReason;
if (agreeReReadingCommand.RequestReReadingResultEnum == RequestReReadingResult.Agree)
{
////有序 如果先同意访视2 再同意访视3 把访视3改为同意
//if (trialConfig.IsReadingTaskViewInOrder)
//{
// await _visitTaskReReadingRepository.BatchUpdateNoTrackingAsync(t => t.OriginalReReadingTask.SubjectId == visitTaskReReadingAppply.OriginalReReadingTask.SubjectId && t.OriginalReReadingTask.VisitTaskNum > origenalTask.VisitTaskNum, u => new VisitTaskReReading()
// {
// RequestReReadingConfirmUserId = _userInfo.Id,
// RequestReReadingResultEnum = RequestReReadingResult.Agree,
// });
//}
//PM申请 SPM / CPM审批 回退访视,在此不生成访视任务
if (visitTaskReReadingAppply.RequestReReadingType == RequestReReadingType.TrialGroupApply && (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.SPM || _userInfo.UserTypeEnumInt == (int)UserTypeEnum.CPM))
{
// 不管有序 无序 都会 回退访视
if (origenalTask.ReadingCategory == ReadingCategory.Visit)
{
//执行类似一致性核查回退流程
await VisitBackAsync(origenalTask.SourceSubjectVisitId);
}
else
{
throw new BusinessValidationFailedException("仅允许同意访视类型的任务重阅");
}
//有序阅片
if (trialConfig.IsReadingTaskViewInOrder)
{
//访视影响当前以及当前之后的 两个阅片人的
filterExpression = filterExpression.And(t => t.VisitTaskNum >= origenalTask.VisitTaskNum);
#region 影响的任务
var influenceTaskList = await _visitTaskRepository.Where(filterExpression, true).ToListAsync();
var trakingOrigenalTask = influenceTaskList.Where(t => t.Id == origenalTask.Id).First();
foreach (var influenceTask in influenceTaskList)
{
//处理申请的任务
if (influenceTask.Id == origenalTask.Id)
{
ReReadingTaskTrackingDeal(influenceTask, agreeReReadingCommand);
//influenceTaskList.ForEach(t =>
//{
// //记录实际影像的任务
// influenceTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = t.Id });
//});
//将医学审核设置为失效
var taskIdList = influenceTaskList.Select(t => t.Id).ToList();
await _taskMedicalReviewRepository.UpdatePartialFromQueryAsync(t => taskIdList.Contains(t.VisitTaskId) && t.AuditState != MedicalReviewAuditState.HaveSigned, u => new TaskMedicalReview() { IsInvalid = true });
}
//申请的访视 要不是重阅重置,要不就是失效 不会存在取消分配
if (influenceTask.ReadingCategory == ReadingCategory.Visit && influenceTask.VisitTaskNum != origenalTask.VisitTaskNum)
{
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
influenceTask.TaskState = TaskState.HaveReturned;
trakingOrigenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Return });
}
else if (influenceTask.ReadingTaskState == ReadingTaskState.Reading)
{
influenceTask.TaskState = TaskState.Adbandon;
trakingOrigenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Abandon });
}
else
{
influenceTask.DoctorUserId = null;
influenceTask.AllocateTime = null;
influenceTask.SuggesteFinishedTime = null;
influenceTask.TaskAllocationState = TaskAllocationState.NotAllocate;
trakingOrigenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.CancelAssign });
}
}
//当前访视
else
{
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
influenceTask.TaskState = TaskState.HaveReturned;
trakingOrigenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Return });
}
else
{
influenceTask.TaskState = TaskState.Adbandon;
trakingOrigenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Abandon });
}
}
}
#endregion
}
//无序阅片 没有 全局 肿瘤学
else
{
#region old
////阅片任务产生了裁判
//if (origenalTask.JudgeVisitTaskId != null)
//{
// //裁判任务是否已阅片完成
// var judgeTask = await _visitTaskRepository.FirstOrDefaultAsync(t => t.Id == origenalTask.JudgeVisitTaskId);
// if (judgeTask.ReadingTaskState == ReadingTaskState.HaveSigned)
// {
// judgeTask.TaskState = TaskState.HaveReturned;
// }
// //裁判任务未完
// else
// {
// judgeTask.TaskState = TaskState.Adbandon;
// }
//}
////不管是否触发裁判 阅片任务退回,待影像重传后重新分 配给原阅片人
//if (trialConfig.ReadingType == ReadingMethod.Double)
//{
// //考虑该访视 另外一个阅片人的任务也同时退回
// var otherTask = await _visitTaskRepository.FirstOrDefaultAsync(t => t.SourceSubjectVisitId == origenalTask.SourceSubjectVisitId && t.Id != origenalTask.Id && t.TaskState == TaskState.Effect);
// if (otherTask.ReadingTaskState == ReadingTaskState.HaveSigned)
// {
// otherTask.TaskState = TaskState.HaveReturned;
// }
// else
// {
// otherTask.TaskState = TaskState.Adbandon;
// }
//}
#endregion
// 1.当前任务及裁判任务
// 2.影响所有阅片人的任务
var judegTaskNum = origenalTask.VisitTaskNum + ReadingCommon.TaskNumDic[ReadingCategory.Judge];
filterExpression = filterExpression.And(t => t.VisitTaskNum == origenalTask.VisitTaskNum || t.VisitTaskNum == judegTaskNum);
var influenceTaskList = await _visitTaskRepository.Where(filterExpression, true).ToListAsync();
var trakingOrigenalTask = influenceTaskList.Where(t => t.Id == origenalTask.Id).First();
foreach (var influenceTask in influenceTaskList)
{
//处理申请的任务
if (influenceTask.Id == origenalTask.Id)
{
ReReadingTaskTrackingDeal(influenceTask, agreeReReadingCommand);
//influenceTaskList.ForEach(t =>
//{
// //记录实际影像的任务
// influenceTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = t.Id });
//});
//将医学审核设置为失效
var taskIdList = influenceTaskList.Select(t => t.Id).ToList();
await _taskMedicalReviewRepository.UpdatePartialFromQueryAsync(t => taskIdList.Contains(t.VisitTaskId) && t.AuditState != MedicalReviewAuditState.HaveSigned, u => new TaskMedicalReview() { IsInvalid = true });
trakingOrigenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Return });
}
else
{
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
influenceTask.TaskState = TaskState.HaveReturned;
trakingOrigenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Return });
}
else
{
influenceTask.TaskState = TaskState.Adbandon;
trakingOrigenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Abandon });
}
}
}
}
}
//IR申请 PM 审批 注意这里有一致性分析的申请同意 不会回退访视,在此要生成影响的访视任务
else if (visitTaskReReadingAppply.RequestReReadingType == RequestReReadingType.DocotorApply && _userInfo.UserTypeEnumInt == (int)UserTypeEnum.ProjectManager)
{
//有序阅片
if (trialConfig.IsReadingTaskViewInOrder)
{
#region 有序 IR 申请 重阅 影响的其他访视查询
switch (origenalTask.ReadingCategory)
{
case ReadingCategory.Visit:
//影响后续访视已经读完的,未读的不做处理 以及其他类型任务
filterExpression = filterExpression.And(t => t.VisitTaskNum >= origenalTask.VisitTaskNum &&
((t.DoctorUserId == origenalTask.DoctorUserId && ((t.ReadingCategory == ReadingCategory.Visit && t.ReadingTaskState == ReadingTaskState.HaveSigned) || t.ReadingCategory == ReadingCategory.Global || t.ReadingCategory == ReadingCategory.Oncology))
||
(t.ReadingCategory == ReadingCategory.Judge))
)
;
break;
case ReadingCategory.Global:
//全局不影响后续访视任务
filterExpression = filterExpression.And(t => t.VisitTaskNum >= origenalTask.VisitTaskNum &&
(t.DoctorUserId == origenalTask.DoctorUserId && (t.ReadingCategory == ReadingCategory.Global || t.ReadingCategory == ReadingCategory.Oncology) || (t.ReadingCategory == ReadingCategory.Judge)));
break;
case ReadingCategory.Oncology:
//仅仅影响自己 后续任务如果是访视任务、全局任务或裁判任务,均不处理
filterExpression = filterExpression.And(t => t.Id == origenalTask.Id);
break;
case ReadingCategory.Judge:
//裁判的影响自己 和后续肿瘤学阅片
filterExpression = filterExpression.And(t => (t.Id == origenalTask.Id) || t.VisitTaskNum > origenalTask.VisitTaskNum && t.DoctorUserId == origenalTask.DoctorUserId && t.ReadingCategory == ReadingCategory.Oncology);
break;
default:
throw new BusinessValidationFailedException("不支持重阅的任务类型");
}
#endregion
#region 这里时影响其他的任务 /*不包括申请的任务 申请的任务,在上面会统一处理*/
var influenceTaskList = await _visitTaskRepository.Where(filterExpression, true).ToListAsync();
var trakingOrigenalTask = influenceTaskList.Where(t => t.Id == origenalTask.Id).First();
foreach (var influenceTask in influenceTaskList)
{
//处理申请的任务
if (influenceTask.Id == origenalTask.Id)
{
ReReadingTaskTrackingDeal(influenceTask, agreeReReadingCommand);
//influenceTaskList.ForEach(t =>
//{
// //记录实际影像的任务
// influenceTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = t.Id });
//});
//将医学审核设置为失效
var taskIdList = influenceTaskList.Select(t => t.Id).ToList();
await _taskMedicalReviewRepository.UpdatePartialFromQueryAsync(t => taskIdList.Contains(t.VisitTaskId) && t.IsHaveQuestion==false && t.AuditState != MedicalReviewAuditState.HaveSigned, u => new TaskMedicalReview() { IsInvalid = true });
trakingOrigenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Return });
}
//处理其他任务
else
{
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
influenceTask.TaskState = TaskState.HaveReturned;
trakingOrigenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Return });
}
else
{
influenceTask.TaskState = TaskState.Adbandon;
trakingOrigenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Abandon });
}
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned && influenceTask.ReadingCategory == ReadingCategory.Visit)
{
await _visitTaskCommonService.AddTaskAsync(new GenerateTaskCommand()
{
TrialId = trialId,
ReadingCategory = GenerateTaskCategory.ReReading,
ReReadingTask = influenceTask,
//同步才可以
Action = (newTask) =>
{
//申请表 设置新任务Id
visitTaskReReadingAppply.NewReReadingTaskId = newTask.Id;
//生成的任务分配给原始医生
newTask.DoctorUserId = origenalTask.DoctorUserId;
newTask.TaskAllocationState = TaskAllocationState.Allocated;
newTask.AllocateTime = DateTime.Now;
newTask.SuggesteFinishedTime = DateTime.Now.AddDays(7);
//拷贝表单
if (visitTaskReReadingAppply.IsCopyOrigenalForms)
{
if (origenalTask.ReadingCategory == ReadingCategory.Visit)
{
var list = _readingTaskQuestionAnswerRepository.Where(t => t.VisitTaskId == origenalTask.Id).ToList();
foreach (var item in list)
{
item.Id = Guid.Empty;
item.VisitTaskId = newTask.Id;
}
_ = _readingTaskQuestionAnswerRepository.AddRangeAsync(list).Result;
}
else if (origenalTask.ReadingCategory == ReadingCategory.Global)
{
var list = _repository.Where<ReadingGlobalTaskInfo>(t => t.GlobalTaskId == origenalTask.Id).ToList();
foreach (var item in list)
{
item.Id = Guid.Empty;
item.GlobalTaskId = newTask.Id;
}
_ = _repository.AddRangeAsync(list).Result;
}
}
}
});
}
}
}
#endregion
}
//无序阅片 只会申请访视类型和裁判类型的任务 注意这里有一致性分析的申请同意
else
{
//1.当前任务及裁判任务
//2.影响当前阅片人的任务
filterExpression = filterExpression.And(t => t.Id == origenalTask.Id || t.Id == origenalTask.JudgeVisitTaskId);
var influenceTaskList = await _visitTaskRepository.Where(filterExpression, true).ToListAsync();
var trakingOrigenalTask = influenceTaskList.Where(t => t.Id == origenalTask.Id).First();
foreach (var influenceTask in influenceTaskList)
{
//申请原任务处理
if (influenceTask.Id == origenalTask.Id)
{
ReReadingTaskTrackingDeal(influenceTask, agreeReReadingCommand);
//influenceTaskList.ForEach(t =>
//{
// //记录实际影像的任务
// influenceTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = t.Id });
//});
//将医学审核设置为失效
var taskIdList = influenceTaskList.Select(t => t.Id).ToList();
await _taskMedicalReviewRepository.UpdatePartialFromQueryAsync(t => taskIdList.Contains(t.VisitTaskId) && t.IsHaveQuestion == false && t.AuditState != MedicalReviewAuditState.HaveSigned, u => new TaskMedicalReview() { IsInvalid = true });
trakingOrigenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Return });
}
else
{
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
influenceTask.TaskState = TaskState.HaveReturned;
trakingOrigenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Return });
}
else
{
influenceTask.TaskState = TaskState.Adbandon;
trakingOrigenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Abandon });
}
}
}
#region Old
////访视任务产生了裁判
//if (origenalTask.ReadingCategory == ReadingCategory.Visit && origenalTask.JudgeVisitTaskId != null)
//{
// //裁判任务是否已阅片完成
// var judgeTask = await _visitTaskRepository.FirstOrDefaultAsync(t => t.Id == origenalTask.JudgeVisitTaskId);
// if (judgeTask.ReadingTaskState == ReadingTaskState.HaveSigned)
// {
// judgeTask.TaskState = TaskState.HaveReturned;
// }
// //裁判任务未完
// else
// {
// judgeTask.TaskState = TaskState.Adbandon;
// }
//}
#endregion
}
#region 申请任务 重新生成 拷贝表单
await _visitTaskCommonService.AddTaskAsync(new GenerateTaskCommand()
{
TrialId = trialId,
ReadingCategory = GenerateTaskCategory.ReReading,
ReReadingTask = origenalTask,
//同步才可以
Action = (newTask) =>
{
//申请表 设置新任务Id
visitTaskReReadingAppply.NewReReadingTaskId = newTask.Id;
//生成的任务分配给原始医生
newTask.DoctorUserId = origenalTask.DoctorUserId;
newTask.TaskAllocationState = TaskAllocationState.Allocated;
newTask.AllocateTime = DateTime.Now;
newTask.SuggesteFinishedTime = DateTime.Now.AddDays(7);
//拷贝表单
if (visitTaskReReadingAppply.IsCopyOrigenalForms)
{
if (origenalTask.ReadingCategory == ReadingCategory.Visit)
{
var list = _readingTaskQuestionAnswerRepository.Where(t => t.VisitTaskId == origenalTask.Id).ToList();
foreach (var item in list)
{
item.Id = Guid.Empty;
item.VisitTaskId = newTask.Id;
}
_ = _readingTaskQuestionAnswerRepository.AddRangeAsync(list).Result;
}
else if (origenalTask.ReadingCategory == ReadingCategory.Global)
{
var list = _repository.Where<ReadingGlobalTaskInfo>(t => t.GlobalTaskId == origenalTask.Id).ToList();
foreach (var item in list)
{
item.Id = Guid.Empty;
item.GlobalTaskId = newTask.Id;
}
_ = _repository.AddRangeAsync(list).Result;
}
}
}
});
#endregion
}
else
{
throw new BusinessValidationFailedException("不符合 PM申请 SPM / CPM审批 | IR申请 PM 审批 ");
}
}
else if (agreeReReadingCommand.RequestReReadingResultEnum == RequestReReadingResult.Reject)
{
await _visitTaskRepository.BatchUpdateNoTrackingAsync(t => t.Id == origenalTask.Id, u => new VisitTask()
{
ReReadingApplyState = ReReadingApplyState.Reject
});
}
}
await _visitTaskRepository.SaveChangesAsync();
return ResponseOutput.Ok();
}
/// <summary>
/// PM 设置任务 退回
/// </summary>
/// <returns></returns>
[HttpPut("{trialId:guid}/{taskId:guid}")]
[UnitOfWork]
public async Task<IResponseOutput> PMSetTaskBack(Guid trialId, Guid taskId)
{
var trialConfig = (await _trialRepository.Where(t => t.Id == trialId).Select(t => new { TrialId = t.Id, t.IsReadingTaskViewInOrder, t.ReadingType }).FirstOrDefaultAsync()).IfNullThrowException();
var task = (await _visitTaskRepository.Where(t => t.Id == taskId).FirstOrDefaultAsync()).IfNullThrowException();
if (task.TaskState != TaskState.Effect || task.ReadingCategory != ReadingCategory.Visit || task.ReadingTaskState == ReadingTaskState.HaveSigned || task.TaskAllocationState == TaskAllocationState.NotAllocate)
{
return ResponseOutput.NotOk("仅仅允许针对已分配、生效、未完成的访视任务进行退回操作,请刷新页面数据");
}
if (task.IsAnalysisCreate)
{
return ResponseOutput.NotOk("一致性分析的任务,不允许设置退回");
}
//PM 才允许操作
if (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ProjectManager)
{
#region 有序 无序公用流程
//执行类似一致性核查回退流程 回退访视到影像上传流程
await VisitBackAsync(task.SourceSubjectVisitId);
#endregion
//有序
if (trialConfig.IsReadingTaskViewInOrder)
{
Expression<Func<VisitTask, bool>> filterExpression = t => t.TrialId == trialId && t.SubjectId == task.SubjectId && t.TaskState == TaskState.Effect && t.IsAnalysisCreate == false;
//另一个阅片人的任务根据任务进度自动进入PM退回或PM申请重阅
filterExpression = filterExpression.And(t => t.VisitTaskNum >= task.VisitTaskNum);
var influenceTaskList = await _visitTaskRepository.Where(filterExpression, true).ToListAsync();
#region 方式一
//foreach (var influenceTask in influenceTaskList)
//{
// //申请任务的阅片人 后续任务肯定没做, 只有访视任务,没有其他任务 取消分配
// if (influenceTask.DoctorUserId == task.DoctorUserId)
// {
// switch (influenceTask.ReadingCategory)
// {
// case ReadingCategory.Visit:
// influenceTask.DoctorUserId = null;
// influenceTask.AllocateTime = null;
// influenceTask.SuggesteFinishedTime = null;
// influenceTask.TaskAllocationState = TaskAllocationState.NotAllocate;
// break;
// case ReadingCategory.Global:
// case ReadingCategory.Judge:
// case ReadingCategory.Oncology:
// throw new BusinessValidationFailedException("不支持回退任务类型");
// }
// }
// //另外一个阅片人
// else
// {
// //另外一个阅片人 有序PM 申请重阅流程
// if (otherReviewerTask.ReadingTaskState == ReadingTaskState.HaveSigned)
// {
// switch (influenceTask.ReadingCategory)
// {
// case ReadingCategory.Visit:
// if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
// {
// influenceTask.TaskState = TaskState.HaveReturned;
// }
// else if (influenceTask.ReadingTaskState == ReadingTaskState.Reading)
// {
// influenceTask.TaskState = TaskState.Adbandon;
// }
// else
// {
// influenceTask.DoctorUserId = null;
// influenceTask.AllocateTime = null;
// influenceTask.SuggesteFinishedTime = null;
// influenceTask.TaskAllocationState = TaskAllocationState.NotAllocate;
// }
// break;
// case ReadingCategory.Global:
// case ReadingCategory.Oncology:
// if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
// {
// influenceTask.TaskState = TaskState.HaveReturned;
// }
// else
// {
// influenceTask.TaskState = TaskState.Adbandon;
// }
// break;
// case ReadingCategory.Judge:
// default:
// throw new BusinessValidationFailedException("不支持回退任务类型");
// }
// }
// //另外一个阅片人 有序 PM 回退流程
// else
// {
// switch (influenceTask.ReadingCategory)
// {
// case ReadingCategory.Visit:
// influenceTask.DoctorUserId = null;
// influenceTask.AllocateTime = null;
// influenceTask.SuggesteFinishedTime = null;
// influenceTask.TaskAllocationState = TaskAllocationState.NotAllocate;
// break;
// case ReadingCategory.Global:
// case ReadingCategory.Judge:
// case ReadingCategory.Oncology:
// throw new BusinessValidationFailedException("不支持回退任务类型");
// }
// }
// }
//}
#endregion
#region 方式二
var origenalTask = influenceTaskList.Where(t => t.Id == task.Id).First();
foreach (var influenceTask in influenceTaskList)
{
//同意的访视 因为要记录具体的操作,所以废弃
if (influenceTask.Id == task.Id)
{
//influenceTaskList.ForEach(t =>
//{
// //记录实际影像的任务
// influenceTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = t.Id });
//});
influenceTask.IsPMSetBack = true;
}
//申请的访视 要不是重阅重置,要不就是失效 不会存在取消分配
if (influenceTask.ReadingCategory == ReadingCategory.Visit && influenceTask.VisitTaskNum != task.VisitTaskNum)
{
//后续访视处理访视
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
influenceTask.TaskState = TaskState.HaveReturned;
origenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Return });
}
else if (influenceTask.ReadingTaskState == ReadingTaskState.Reading)
{
influenceTask.TaskState = TaskState.Adbandon;
origenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Abandon });
}
else
{
influenceTask.DoctorUserId = null;
influenceTask.AllocateTime = null;
influenceTask.SuggesteFinishedTime = null;
influenceTask.TaskAllocationState = TaskAllocationState.NotAllocate;
origenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.CancelAssign });
}
}
else
{
//申请的访视 全局肿瘤学
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
influenceTask.TaskState = TaskState.HaveReturned;
origenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Return });
}
else
{
influenceTask.TaskState = TaskState.Adbandon;
origenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Abandon });
}
}
}
#endregion
}
else
{
//无序 无序阅片没有 全局 肿瘤学
//// 当前任务标为失效
//task.TaskState = TaskState.Adbandon;
////考虑该访视 另外一个阅片人的任务
//var otherReviewerTask = await _visitTaskRepository.FirstOrDefaultAsync(t => t.SourceSubjectVisitId == task.SourceSubjectVisitId && t.Id != task.Id && t.TaskState == TaskState.Effect);
//if (otherReviewerTask.ReadingTaskState == ReadingTaskState.HaveSigned)
//{
// //另外阅片人完成阅片了 就设置为重阅重置
// otherReviewerTask.TaskState = TaskState.HaveReturned;
//}
//else
//{
// otherReviewerTask.TaskState = TaskState.Adbandon;
//}
// 申请该访视的任务 申请人失效 另外一个人重阅重置或者失效
var currentVisitList = await _visitTaskRepository.Where(t => t.SourceSubjectVisitId == task.SourceSubjectVisitId && t.ReadingCategory == ReadingCategory.Visit && t.TaskState == TaskState.Effect && t.VisitTaskNum == task.VisitTaskNum, true).ToListAsync();
var origenalTask = currentVisitList.Where(t => t.Id == task.Id).First();
foreach (var influenceTask in currentVisitList)
{
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
//另外阅片人完成阅片了 就设置为重阅重置
influenceTask.TaskState = TaskState.HaveReturned;
origenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Return });
}
else
{
influenceTask.TaskState = TaskState.Adbandon;
origenalTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = influenceTask.Id, OptType = ReReadingOrBackOptType.Abandon });
}
////同意的访视
//if (influenceTask.Id == task.Id)
//{
// currentVisitList.ForEach(t =>
// {
// //记录实际影像的任务
// influenceTask.TaskInfluenceList.Add(new TaskInfluence() { InfluenceTaskId = t.Id });
// });
//}
}
}
}
else
{
return ResponseOutput.NotOk("仅PM 可以进行回退操作");
}
await _visitTaskRepository.SaveChangesAsync();
return ResponseOutput.Ok();
}
private async Task VisitBackAsync(Guid? subjectVisitId)
{
var sv = (await _subjectVisitRepository.FirstOrDefaultAsync(t => t.Id == subjectVisitId)).IfNullThrowException();
//需要重新产生任务
sv.IsVisitTaskGenerated = false;
sv.IsPMBackOrReReading = true;
sv.AuditState = AuditStateEnum.None;
sv.SubmitState = SubmitStateEnum.ToSubmit;
sv.ReadingStatus = ReadingStatusEnum.ImageNotSubmit;
//回退后,回退状态恢复
sv.RequestBackState = RequestBackStateEnum.NotRequest;
sv.IsCheckBack = true;
sv.CheckState = CheckStateEnum.None;
sv.CheckChallengeState = CheckChanllengeTypeEnum.None;
sv.SVENDTC = null;
sv.SVSTDTC = null;
sv.PreliminaryAuditTime = null;
sv.SubmitTime = null;
sv.ReviewAuditTime = null;
sv.CurrentActionUserExpireTime = null;
sv.IsTake = false;
sv.CurrentActionUserId = null;
sv.PreliminaryAuditUserId = null;
sv.ReviewAuditUserId = null;
//await _repository.AddAsync(new CheckChallengeDialog() { SubjectVisitId = subjectVisitId, TalkContent = "PM/APM同意一致性核查回退。", UserTypeEnum = (UserTypeEnum)_userInfo.UserTypeEnumInt });
await _repository.BatchDeleteAsync<TrialQCQuestionAnswer>(t => t.SubjectVisitId == subjectVisitId);
await _repository.BatchDeleteAsync<DicomInstance>(t => t.DicomSerie.IsDeleted);
await _repository.BatchDeleteAsync<DicomSeries>(t => t.IsDeleted);
var success = await _subjectVisitRepository.SaveChangesAsync();
}
/// <summary>
/// 影响提示列表 重阅仅仅针对已完成的任务申请 退回针对的是未完成的(退回仅仅针对是访视类型的)
/// </summary>
/// <param name="taskId"></param>
/// <param name="isReReading"> 重阅还是直接回退</param>
/// <param name="applyId"> 申请记录的Id</param>
/// <returns></returns>
[HttpGet("{taskId:guid}/{isReReading:bool}")]
public async Task<List<InfluenceTaskInfo>> GetReReadingOrBackInfluenceTaskList(Guid taskId, bool isReReading, Guid? applyId)
{
var filterObj = (await _visitTaskRepository.FirstOrDefaultNoTrackingAsync(t => t.Id == taskId)).IfNullThrowException();
var trialId = filterObj.TrialId;
var trialConfig = (await _trialRepository.Where(t => t.Id == trialId).Select(t => new { TrialId = t.Id, t.IsReadingTaskViewInOrder, t.ReadingType }).FirstOrDefaultAsync()).IfNullThrowException();
Expression<Func<VisitTask, bool>> filterExpression = t => t.TrialId == trialId && t.SubjectId == filterObj.SubjectId && t.TaskState == TaskState.Effect && t.TaskAllocationState == TaskAllocationState.Allocated;
//是否是一致性分析任务 (一致性分析的任务 不会产生裁判 肿瘤学 仅仅有生成的访视和全局)
filterExpression = filterExpression.And(t => t.IsAnalysisCreate == filterObj.IsAnalysisCreate);
//重阅影响
if (isReReading)
{
//IR 申请 PM 同意 仅仅影响自己
if ((_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ProjectManager && applyId != null && await _visitTaskReReadingRepository.AnyAsync(t => t.Id == applyId && t.CreateUser.UserTypeEnum == UserTypeEnum.IndependentReviewer))
|| (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.IndependentReviewer && applyId == null))
{
//当前任务及其之后的所有访视任务、全局任务、裁判任务、肿瘤学阅片任务
//有序
if (trialConfig.IsReadingTaskViewInOrder)
{
switch (filterObj.ReadingCategory)
{
case ReadingCategory.Visit:
//影响当前医生 以及当前医生之后的 1、访视任务 已经读完的
//2、后续任务如果是全局、肿瘤学阅片任务状态为阅片完成标记为重阅重置若在阅片中则标记为失效若为待阅片则标记为失效
//3、当前任务、后续访视任务或者全局任务触发了裁判任务若裁判任务状态为阅片完成则标记为重阅重置若在阅片中或待阅片则标记为失效
filterExpression = filterExpression.And(t => t.VisitTaskNum >= filterObj.VisitTaskNum &&
((t.DoctorUserId == filterObj.DoctorUserId && ((t.ReadingCategory == ReadingCategory.Visit && t.ReadingTaskState == ReadingTaskState.HaveSigned) || t.ReadingCategory == ReadingCategory.Global || t.ReadingCategory == ReadingCategory.Oncology))
||
(t.ReadingCategory == ReadingCategory.Judge))
)
;
break;
case ReadingCategory.Global:
// 1、后续任务如果是访视任务均不处理
//2、后续任务如果是全局、状态为阅片完成则标记为重阅重置若阅片中或待阅片则不处理
//3、当前任务或者全局任务触发了裁判任务若裁判任务状态为阅片完成则标记为重阅重置若在阅片中或待阅片则标记为失效
//4、后续任务为肿瘤学阅片任务状态为阅片完成标记为重阅重置若在阅片中则标记为失效若为待阅片则标记为失效
//全局不影响后续访视任务
filterExpression = filterExpression.And(t => t.VisitTaskNum >= filterObj.VisitTaskNum &&
(t.DoctorUserId == filterObj.DoctorUserId && (t.ReadingCategory == ReadingCategory.Global || t.ReadingCategory == ReadingCategory.Oncology) || (t.ReadingCategory == ReadingCategory.Judge)));
break;
//1、后续任务如果是访视任务、全局任务或裁判任务均不处理
case ReadingCategory.Oncology:
//仅仅影响自己
filterExpression = filterExpression.And(t => t.Id == filterObj.Id);
break;
//(只允许申请该阅片人最后一次完成裁判的任务重阅)申请的时候做了限制
case ReadingCategory.Judge:
// 1、后续任务如果是访视任务、全局任务均不处理
//2、后续若有肿瘤学阅片若肿瘤学阅片任务状态为阅片完成则标记为重阅重置若为阅片中则标记为失效如为待阅片则取消分配
//裁判的影响自己 和后续肿瘤学阅片
filterExpression = filterExpression.And(t => t.Id == filterObj.Id || t.VisitTaskNum > filterObj.VisitTaskNum && t.DoctorUserId == filterObj.DoctorUserId && t.ReadingCategory == ReadingCategory.Oncology);
break;
default:
throw new BusinessValidationFailedException("不支持重阅的任务类型");
}
}
//无序
else
{
//1.当前任务及裁判任务
//2.影响当前阅片人的任务
filterExpression = filterExpression.And(t => t.Id == filterObj.Id || t.Id == filterObj.JudgeVisitTaskId);
}
//throw new BusinessValidationFailedException("仅允许PM 同意 IR 申请的任务");
}
//PM 影响所有阅片人 仅仅针对访视 SPM CPM 掉用
else if (((_userInfo.UserTypeEnumInt == (int)UserTypeEnum.SPM || _userInfo.UserTypeEnumInt == (int)UserTypeEnum.CPM) && applyId != null && await _visitTaskReReadingRepository.AnyAsync(t => t.Id == applyId && t.CreateUser.UserTypeEnum == UserTypeEnum.ProjectManager) && filterObj.IsAnalysisCreate == false && filterObj.ReadingCategory == ReadingCategory.Visit)
|| (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ProjectManager && applyId == null))
{
//有序
if (trialConfig.IsReadingTaskViewInOrder)
{
switch (filterObj.ReadingCategory)
{
case ReadingCategory.Visit:
//访视影响当前以及当前之后的 两个阅片人的
filterExpression = filterExpression.And(t => t.VisitTaskNum >= filterObj.VisitTaskNum);
break;
default:
throw new BusinessValidationFailedException("不支持重阅的任务类型");
}
}
//无序
else
{
// 1.当前任务及裁判任务
// 2.影响所有阅片人的任务
var judegTaskNum = filterObj.VisitTaskNum + ReadingCommon.TaskNumDic[ReadingCategory.Judge];
filterExpression = filterExpression.And(t => t.VisitTaskNum == filterObj.VisitTaskNum || t.VisitTaskNum == judegTaskNum);
}
//throw new BusinessValidationFailedException("仅允许SPM CPM 同意 PM 申请的非 一致性分析的访视任务");
}
else
{
throw new BusinessValidationFailedException("当前用户查看列表未定义");
}
}
//退回影响 仅仅针对是访视类型的
else
{
if (filterObj.IsAnalysisCreate)
{
throw new BusinessValidationFailedException("不允许退回一致性分析任务");
}
if (filterObj.ReadingCategory == ReadingCategory.Visit && _userInfo.UserTypeEnumInt == (int)UserTypeEnum.ProjectManager)
{
//有序
if (trialConfig.IsReadingTaskViewInOrder)
{
// 当前任务及其之后的所有访视任务 两个阅片人的
// 1.后续访视不处理
//2.当前任务未完成,不会产生全局任务。后续任务均为访视任务,且均为待阅片,取消分配;
filterExpression = filterExpression.And(t => t.VisitTaskNum >= filterObj.VisitTaskNum);
}
//无序
else
{
//自己和另一个人的当前任务 退回针对的是未完成的肯定不会有裁判
filterExpression = filterExpression.And(t => t.VisitTaskNum == filterObj.VisitTaskNum);
}
}
else
{
throw new BusinessValidationFailedException("仅仅访视类型的任务支持PM退回");
}
}
var list = await _visitTaskRepository.Where(filterExpression)
//IR 申请的时候,仅仅看到影响自己的
.WhereIf(_userInfo.UserTypeEnumInt == (int)UserTypeEnum.IndependentReviewer, t => t.DoctorUserId == _userInfo.Id)
.OrderBy(t => t.VisitTaskNum).ProjectTo<InfluenceTaskInfo>(_mapper.ConfigurationProvider).ToListAsync();
#region 影响后的操作
foreach (var influenceTask in list)
{
if (isReReading)
{
if ((_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ProjectManager && applyId != null) || (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.IndependentReviewer && applyId == null))
{
//有序
if (trialConfig.IsReadingTaskViewInOrder)
{
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
influenceTask.OptType = ReReadingOrBackOptType.Return;
}
else
{
influenceTask.OptType = ReReadingOrBackOptType.Abandon;
}
}
else
{
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
influenceTask.OptType = ReReadingOrBackOptType.Return;
}
else
{
influenceTask.OptType = ReReadingOrBackOptType.Abandon;
}
}
}
if (((_userInfo.UserTypeEnumInt == (int)UserTypeEnum.SPM || _userInfo.UserTypeEnumInt == (int)UserTypeEnum.CPM) && applyId != null) || (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ProjectManager && applyId == null))
{
//有序
if (trialConfig.IsReadingTaskViewInOrder)
{
//申请的访视 要不是重阅重置,要不就是失效 不会存在取消分配
if (influenceTask.ReadingCategory == ReadingCategory.Visit && influenceTask.VisitTaskNum != filterObj.VisitTaskNum)
{
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
influenceTask.OptType = ReReadingOrBackOptType.Return;
}
else if (influenceTask.ReadingTaskState == ReadingTaskState.Reading)
{
influenceTask.OptType = ReReadingOrBackOptType.Abandon;
}
else
{
influenceTask.OptType = ReReadingOrBackOptType.CancelAssign;
}
}
else
{
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
influenceTask.OptType = ReReadingOrBackOptType.Return;
}
else
{
influenceTask.OptType = ReReadingOrBackOptType.Abandon;
}
}
}
else
{
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
influenceTask.OptType = ReReadingOrBackOptType.Return;
}
else
{
influenceTask.OptType = ReReadingOrBackOptType.Abandon;
}
}
}
}
//PM退回
else
{
//有序
if (trialConfig.IsReadingTaskViewInOrder)
{
//申请的访视 要不是重阅重置,要不就是失效 不会存在取消分配
if (influenceTask.ReadingCategory == ReadingCategory.Visit && influenceTask.VisitTaskNum != filterObj.VisitTaskNum)
{
//后续访视处理访视
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
influenceTask.OptType = ReReadingOrBackOptType.Return;
}
else if (influenceTask.ReadingTaskState == ReadingTaskState.Reading)
{
influenceTask.OptType = ReReadingOrBackOptType.Abandon;
}
else
{
influenceTask.OptType = ReReadingOrBackOptType.CancelAssign;
}
}
else
{
//申请的访视 全局肿瘤学
if (influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned)
{
influenceTask.OptType = ReReadingOrBackOptType.Return;
}
else
{
influenceTask.OptType = ReReadingOrBackOptType.Abandon;
}
}
}
//无序
else
{
//重阅重置或者失效
influenceTask.OptType = influenceTask.ReadingTaskState == ReadingTaskState.HaveSigned ? ReReadingOrBackOptType.Return : ReReadingOrBackOptType.Abandon;
}
}
}
#endregion
return list;
}
/// <summary>
/// 获取已影响的列表
/// </summary>
/// <returns></returns>
public async Task<List<InfluenceTaskInfo>> GetInfluencedTaskList(Guid taskId)
{
var list = await _repository.Where<TaskInfluence>(t => t.OriginalTaskId == taskId)/*.Select(t => t.InfluenceTask)*/.ProjectTo<InfluenceTaskInfo>(_mapper.ConfigurationProvider).ToListAsync();
return list;
}
}
}