From bf50cdb1fd68f12338b4770b01883085ecbde310 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Tue, 21 Jun 2022 10:59:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A3=81=E5=88=A4=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=88=86=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Allocation/DTO/VisitTaskViewModel.cs | 2 + .../Service/Allocation/VisitTaskService.cs | 243 ++++++++++-------- 2 files changed, 141 insertions(+), 104 deletions(-) diff --git a/IRaCIS.Core.Application/Service/Allocation/DTO/VisitTaskViewModel.cs b/IRaCIS.Core.Application/Service/Allocation/DTO/VisitTaskViewModel.cs index 0f34ad099..e3ef59ace 100644 --- a/IRaCIS.Core.Application/Service/Allocation/DTO/VisitTaskViewModel.cs +++ b/IRaCIS.Core.Application/Service/Allocation/DTO/VisitTaskViewModel.cs @@ -343,6 +343,8 @@ namespace IRaCIS.Core.Application.ViewModel public class AutoSubjectAssignCommand { public Guid TrialId { get; set; } + + public bool IsJudgeDoctor { get; set; } } public class AssignSubjectTaskToDoctorCommand diff --git a/IRaCIS.Core.Application/Service/Allocation/VisitTaskService.cs b/IRaCIS.Core.Application/Service/Allocation/VisitTaskService.cs index d92919f4e..711c4aa59 100644 --- a/IRaCIS.Core.Application/Service/Allocation/VisitTaskService.cs +++ b/IRaCIS.Core.Application/Service/Allocation/VisitTaskService.cs @@ -392,15 +392,16 @@ namespace IRaCIS.Core.Application.Service 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)) + { + throw new BusinessValidationFailedException("有Subject 在其他Arm组已有该医生,不允许在新的组添加该医生"); + } if (await _subjectUserRepository.AnyAsync(t => t.TrialId == trialId && t.SubjectId == subjectId && t.DoctorUserId == doctorUserId && t.ArmEnum == armEnum)) { throw new BusinessValidationFailedException("有Subject 已有该Arm组的医生,不允许继续分配,请刷新页面,确认页面数据是否过期"); } - if (await _subjectUserRepository.AnyAsync(t => t.TrialId == trialId && t.SubjectId == subjectId && t.DoctorUserId == doctorUserId && t.ArmEnum != armEnum)) - { - throw new BusinessValidationFailedException("有Subject 在其他Arm组已有该医生,不允许在新的组添加该医生"); - } else { await _subjectUserRepository.AddAsync(new SubjectUser() { TrialId = trialId, SubjectId = subjectId, DoctorUserId = doctorUserId, ArmEnum = armEnum, AssignTime = DateTime.Now }); @@ -545,30 +546,37 @@ namespace IRaCIS.Core.Application.Service var trialId = autoSubjectAssignCommand.TrialId; - - - //获取 已产生任务的Subject 目前分配情况 - var subjectList = _subjectRepository.Where(t => t.TrialId == trialId).Where(t => t.SubjectVisitTaskList.Any()) - .Select(t => new { SubjectId = t.Id, DoctorUserList = t.SubjectDoctorList.Select(t => new { t.DoctorUserId, t.ArmEnum }), IsApplyed = t.SubjectVisitTaskList.Any(c => c.DoctorUserId != null) }).ToList(); - + 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(); - #region 完全按照Subject 遍历去分 + //获取 已产生任务的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()) + .Select(t => new { + SubjectId = t.Id, + DoctorUserList = t.SubjectDoctorList.Where(t => isJudge? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).Select(t => new { t.DoctorUserId, t.ArmEnum }), + IsApplyed = t.SubjectVisitTaskList.Where(t => isJudge? t.ArmEnum == Arm.JudgeArm : t.ArmEnum != Arm.JudgeArm).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(); + //已分配的 医生的情况 var haveAssignedSubjectDoctorList = subjectList.Clone().SelectMany(t => t.DoctorUserList.Select(c => new { t.SubjectId, DoctorUserId = c.DoctorUserId, c.ArmEnum })).ToList(); - + //将目前已分配的情况 换到医生的维度 foreach (var waitAllocationDoctor in waitAllocationDoctorList) { waitAllocationDoctor.SubjectArmList = haveAssignedSubjectDoctorList.Where(t => t.DoctorUserId == waitAllocationDoctor.DoctorUserId) @@ -580,115 +588,139 @@ namespace IRaCIS.Core.Application.Service } - //仅仅分配未应用的 而且 没有没有分配医生的 + #region 完全按照Subject 遍历去分 + + + + //仅仅分配未应用的 而且 没有分配医生的 foreach (var subject in subjectList.Where(t => t.IsApplyed == false && !t.DoctorUserList.Any())) { //该Subject 已经分配的医生数量 var hasAssignDoctorCount = subject.DoctorUserList.Count(); - //分配两个医生 - 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(); - - - //将分配结果记录 - - - //看阅片人之前在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; - } - - - //if(!waitAllocationDoctorList.Where(t=>t.DoctorUserId== allocateDoctorList[0].DoctorUserId).SelectMany(u => u.SubjectArmList).Any(t => t.SubjectId == subject.SubjectId)) - //{ - 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 - }); - //} - - //if (!waitAllocationDoctorList.Where(t => t.DoctorUserId == allocateDoctorList[1].DoctorUserId).SelectMany(u => u.SubjectArmList).Any(t => t.SubjectId == subject.SubjectId)) - //{ - 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 (isJudge) { if (hasAssignDoctorCount > 1) { - throw new BusinessValidationFailedException("单重阅片当前有Subject绑定医生数量大于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 + ArmEnum = Arm.JudgeArm }); - await _subjectUserRepository.AddAsync(new SubjectUser() { TrialId = trialId, SubjectId = subject.SubjectId, DoctorUserId = allocateDoctor.DoctorUserId, ArmEnum = Arm.SingleReadingArm, AssignTime = DateTime.Now }); + 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 }); + + } } @@ -700,6 +732,9 @@ namespace IRaCIS.Core.Application.Service + + + await _subjectUserRepository.SaveChangesAsync(); return ResponseOutput.Ok();