From aa4573fdf1c1b6fd2d9b0749f0e8c99707821a7d Mon Sep 17 00:00:00 2001
From: hang <872297557@qq.com>
Date: Mon, 13 Jun 2022 17:41:42 +0800
Subject: [PATCH] =?UTF-8?q?=E5=8C=BB=E7=94=9F=E5=88=86=E9=85=8D=E7=BB=84?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../Allocation/DTO/VisitTaskViewModel.cs      |  2 +
 .../Allocation/VisitTaskHelpeService.cs       | 53 ++++++++++++-------
 2 files changed, 37 insertions(+), 18 deletions(-)

diff --git a/IRaCIS.Core.Application/Service/Allocation/DTO/VisitTaskViewModel.cs b/IRaCIS.Core.Application/Service/Allocation/DTO/VisitTaskViewModel.cs
index e6dce518b..c31136560 100644
--- a/IRaCIS.Core.Application/Service/Allocation/DTO/VisitTaskViewModel.cs
+++ b/IRaCIS.Core.Application/Service/Allocation/DTO/VisitTaskViewModel.cs
@@ -135,6 +135,8 @@ namespace IRaCIS.Core.Application.ViewModel
 
         public DateTime? AssignTime { get; set; }
 
+        public int ArmEnum { get; set; }
+
         public bool IsAssignDoctorApplyedTask { get; set; }
     }
 
diff --git a/IRaCIS.Core.Application/Service/Allocation/VisitTaskHelpeService.cs b/IRaCIS.Core.Application/Service/Allocation/VisitTaskHelpeService.cs
index 619b9a182..4cfe9c342 100644
--- a/IRaCIS.Core.Application/Service/Allocation/VisitTaskHelpeService.cs
+++ b/IRaCIS.Core.Application/Service/Allocation/VisitTaskHelpeService.cs
@@ -26,29 +26,31 @@ namespace IRaCIS.Core.Application.Service
         private readonly IEasyCachingProvider _provider;
         private readonly IRepository<SubjectVisit> _subjectVisitRepository;
         private readonly IRepository<TaskAllocationRule> _taskAllocationRuleRepository;
+        private readonly IRepository<SubjectUser> _subjectUserRepository;
 
 
-        public VisitTaskHelpeService(IRepository<VisitTask> visitTaskRepository, IRepository<Trial> trialRepository, IEasyCachingProvider provider, IRepository<SubjectVisit> subjectVisitRepository, IRepository<TaskAllocationRule> taskAllocationRuleRepository)
+        public VisitTaskHelpeService(IRepository<VisitTask> visitTaskRepository, IRepository<SubjectUser> subjectUserRepository, IRepository<Trial> trialRepository, IEasyCachingProvider provider, IRepository<SubjectVisit> subjectVisitRepository, IRepository<TaskAllocationRule> taskAllocationRuleRepository)
         {
             _visitTaskRepository = visitTaskRepository;
             _trialRepository = trialRepository;
             _provider = provider;
             _subjectVisitRepository = subjectVisitRepository;
             _taskAllocationRuleRepository = taskAllocationRuleRepository;
+            _subjectUserRepository = subjectUserRepository;
         }
 
         //查询列表的时候,一致性核查通过未产生任务的 自动产生任务
-        public async Task GenerateVisitTaskAsync(Guid trialId, List<Guid> subjectVisitIdList, bool isAssignSubjectToDoctor=false)
+        public async Task GenerateVisitTaskAsync(Guid trialId, List<Guid> subjectVisitIdList, bool isAssignSubjectToDoctor = false)
         {
 
             if (subjectVisitIdList.Count == 0)
             {
                 return;
             }
-           
+
             //以前访视未产生任务的,在查询这里要产生  后期维护到一块 
 
-            var trialConfig = (await _trialRepository.Where(t => t.Id == trialId).Select(t => new { TrialId = t.Id, t.ReadingType,  t.IsFollowVisitAutoAssign, t.IsFollowGlobalVisitAutoAssign,t.FollowGlobalVisitAutoAssignDefaultState,t.FollowVisitAutoAssignDefaultState, t.TaskAllocateObjEnum }).FirstOrDefaultAsync()).IfNullThrowException();
+            var trialConfig = (await _trialRepository.Where(t => t.Id == trialId).Select(t => new { TrialId = t.Id, t.ReadingType, t.IsFollowVisitAutoAssign, t.IsFollowGlobalVisitAutoAssign, t.FollowGlobalVisitAutoAssignDefaultState, t.FollowVisitAutoAssignDefaultState, t.TaskAllocateObjEnum }).FirstOrDefaultAsync()).IfNullThrowException();
 
             var dbMaxCode = _visitTaskRepository.Where(t => t.TrialId == trialId).Select(t => t.Code).DefaultIfEmpty().Max();
 
@@ -56,12 +58,15 @@ namespace IRaCIS.Core.Application.Service
 
             int currentMaxCodeInt = cacheMaxCodeInt > dbMaxCode ? cacheMaxCodeInt : dbMaxCode;
 
-            var subjectVisitList = _subjectVisitRepository.Where(t => subjectVisitIdList.Contains(t.Id)).Select(t => new {t.Id, t.SubjectId, t.IsUrgent, t.BlindName, t.VisitName, t.CheckPassedTime, t.TrialId }).Distinct().ToList();
+            var subjectVisitList = _subjectVisitRepository.Where(t => subjectVisitIdList.Contains(t.Id)).Select(t => new { t.Id, t.SubjectId, t.IsUrgent, t.BlindName, t.VisitName, t.CheckPassedTime, t.TrialId }).Distinct().ToList();
 
 
             foreach (var subjectVisit in subjectVisitList)
             {
 
+                var assignConfigList = await _subjectUserRepository.Where(t => t.TrialId == trialId && t.SubjectId == subjectVisit.SubjectId).Select(u => new { u.DoctorUserId, u.ArmEnum).ToListAsync();
+
+
                 if (trialConfig.ReadingType == ReadingMethod.Double)
                 {
                     //每个访视 根据项目配置生成任务  双审生成两个 
@@ -106,22 +111,24 @@ namespace IRaCIS.Core.Application.Service
                             throw new BusinessValidationFailedException("能参与读片的医生数量必须大于2,自动分配任务中止");
                         }
 
-                                       
-                        if (trialConfig.TaskAllocateObjEnum == TaskAllocateObj.Subject )
+
+                        if (trialConfig.TaskAllocateObjEnum == TaskAllocateObj.Subject)
                         {
 
-                            if (trialConfig.IsFollowVisitAutoAssign )
+                            if (trialConfig.IsFollowVisitAutoAssign)
                             {
-                                var defaultState = trialConfig.FollowVisitAutoAssignDefaultState==TaskAllocateDefaultState.InitAllocated?TaskState.InitAllocated:TaskState.Allocated;
+                                var defaultState = trialConfig.FollowVisitAutoAssignDefaultState == TaskAllocateDefaultState.InitAllocated ? TaskState.InitAllocated : TaskState.Allocated;
 
 
                                 task1.TaskState = defaultState;
                                 task2.TaskState = defaultState;
 
 
+                                #region 验证历史任务
 
                                 var allocateSubjectArmList = _visitTaskRepository.Where(t => t.SubjectId == subjectVisit.SubjectId && t.TrialId == subjectVisit.TrialId && t.DoctorUserId != null).Select(t => new { t.DoctorUserId, t.ArmEnum }).Distinct().ToList();
 
+
                                 //不是初次分配  直接分配给Subject 之前的医生
                                 if (allocateSubjectArmList.Count != 0)
                                 {
@@ -138,10 +145,20 @@ namespace IRaCIS.Core.Application.Service
                                     }
 
 
-                                    //分配给对应Arm的人
-                                    task1.DoctorUserId = allocateSubjectArmList.FirstOrDefault(t => t.ArmEnum == 1).DoctorUserId;
-                                    task2.DoctorUserId = allocateSubjectArmList.FirstOrDefault(t => t.ArmEnum == 2).DoctorUserId;
+
                                 }
+                                #endregion
+
+
+                                //分配给对应Arm的人
+                                task1.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == 1).DoctorUserId;
+                                task2.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == 2).DoctorUserId;
+
+                            }
+
+                            //找到配置规则表 进行分配
+
+
                             else
                             {
                                 //后续Subect  不自动分配  不处理
@@ -156,11 +173,11 @@ namespace IRaCIS.Core.Application.Service
                             //}
 
 
-                   
 
-                            }
+
                         }
 
+
                         task1.AllocateTime = DateTime.Now;
                         task2.AllocateTime = DateTime.Now;
                     }
@@ -219,7 +236,7 @@ namespace IRaCIS.Core.Application.Service
                                 //不是初次分配
                                 if (allocateSubjectArmList.Count != 0)
                                 {
-                                    singleTask.DoctorUserId = allocateSubjectArmList.FirstOrDefault(t => t.ArmEnum == 0).DoctorUserId;
+                                    singleTask.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == 0).DoctorUserId;
 
                                 }
                             }
@@ -230,12 +247,12 @@ namespace IRaCIS.Core.Application.Service
                             }
                         }
 
-                      
+
 
                     }
                     #endregion
 
-                   
+
 
                 }
 
@@ -248,7 +265,7 @@ namespace IRaCIS.Core.Application.Service
 
         }
 
- 
+