//--------------------------------------------------------------------
//     此代码由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 EasyCaching.Core;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure;
using AutoMapper;
using MassTransit;
using IRaCIS.Core.Infra.EFCore.Common;

namespace IRaCIS.Core.Application.Service
{
    /// <summary>
    /// 访视读片任务
    /// </summary>	
    [ApiExplorerSettings(GroupName = "Trial")]
    public class VisitTaskHelpeService : IVisitTaskHelpeService
    {

        private readonly IRepository<VisitTask> _visitTaskRepository;
        private readonly IRepository<Trial> _trialRepository;
        private readonly IEasyCachingProvider _provider;
        private readonly IRepository<SubjectVisit> _subjectVisitRepository;
        private readonly IRepository<ReadingJudgeInfo> _readingJudgeInfoRepository;
        private readonly IRepository<TaskAllocationRule> _taskAllocationRuleRepository;
        private readonly IRepository<SubjectUser> _subjectUserRepository;

        private readonly IMapper _mapper;
        private readonly IUserInfo _userInfo;
        private readonly IRepository<VisitTaskReReading> _visitTaskReReadingRepository;


        public VisitTaskHelpeService(IRepository<VisitTask> visitTaskRepository, IRepository<SubjectUser> subjectUserRepository, IRepository<Trial> trialRepository, IEasyCachingProvider provider,
            IRepository<SubjectVisit> subjectVisitRepository,
             IRepository<ReadingJudgeInfo> readingJudgeInfoRepository,
            IRepository<TaskAllocationRule> taskAllocationRuleRepository, IMapper mapper, IUserInfo userInfo, IRepository<VisitTaskReReading> visitTaskReReadingRepository)
        {
            _visitTaskRepository = visitTaskRepository;
            _trialRepository = trialRepository;
            _provider = provider;
            _subjectVisitRepository = subjectVisitRepository;
            this._readingJudgeInfoRepository = readingJudgeInfoRepository;
            _taskAllocationRuleRepository = taskAllocationRuleRepository;
            _subjectUserRepository = subjectUserRepository;
            _mapper = mapper;
            _userInfo = userInfo;
            _visitTaskReReadingRepository = visitTaskReReadingRepository;
        }

        //查询列表的时候，一致性核查通过未产生任务的 自动产生任务   如果是一致性核查，那么还会自动分配
        public async Task GenerateVisitTaskAsync(Guid trialId, List<Guid> subjectVisitIdList, bool isAssignSubjectToDoctor = false)
        {

            if (subjectVisitIdList.Count == 0)
            {
                return;
            }

            var subjectVisitList = _subjectVisitRepository.Where(t => subjectVisitIdList.Contains(t.Id)).ProjectTo<VisitGenerataTaskDTO>(_mapper.ConfigurationProvider).Distinct().ToList();


            await AddTaskAsync(new GenerateTaskCommand() { TrialId = trialId, IsAssignSubjectToDoctor = isAssignSubjectToDoctor, VisitGenerataTaskList = subjectVisitList, ReadingCategory = GenerateTaskCategory.Visit });


        }



        public async Task AddTaskAsync(GenerateTaskCommand generateTaskCommand)
        {

            var trialId = generateTaskCommand.TrialId;
            var isAssignSubjectToDoctor = generateTaskCommand.IsAssignSubjectToDoctor;

            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,t.IsReadingTaskViewInOrder }).FirstOrDefaultAsync()).IfNullThrowException();

            var dbMaxCode = _visitTaskRepository.Where(t => t.TrialId == trialId).Select(t => t.Code).DefaultIfEmpty().Max();

            var cacheMaxCodeInt = _provider.Get<int>($"{trialId}_{StaticData.CacheKey.TaskMaxCode}").Value;

            int currentMaxCodeInt = cacheMaxCodeInt > dbMaxCode ? cacheMaxCodeInt : dbMaxCode;

            switch (generateTaskCommand.ReadingCategory)
            {
                case GenerateTaskCategory.Visit:
                    foreach (var subjectVisit in generateTaskCommand.VisitGenerataTaskList)
                    {
                        var assignConfigList = await _subjectUserRepository.Where(t => t.TrialId == trialId && t.SubjectId == subjectVisit.SubjectId && t.OrignalSubjectUserId == null && t.IsConfirmed).Select(u => new { u.DoctorUserId, u.ArmEnum }).ToListAsync();


                        if (trialConfig.ReadingType == ReadingMethod.Double)
                        {
                            //每个访视 根据项目配置生成任务  双审生成两个 
                            var task1 = await _visitTaskRepository.AddAsync(new VisitTask()
                            {
                                TrialId = trialId,
                                SubjectId = subjectVisit.SubjectId,
                                IsUrgent = subjectVisit.IsUrgent,
                                TaskBlindName = subjectVisit.BlindName,
                                TaskName = subjectVisit.VisitName,
                                VisitTaskNum = subjectVisit.VisitNum,
                                //CheckPassedTime = subjectVisit.CheckPassedTime,
                                ArmEnum = Arm.DoubleReadingArm1,//特殊
                                Code = currentMaxCodeInt + 1,
                                SourceSubjectVisitId = subjectVisit.Id,
                                TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),
                                ReadingCategory = ReadingCategory.Visit
                            });

                            var task2 = await _visitTaskRepository.AddAsync(new VisitTask()
                            {
                                TrialId = trialId,
                                SubjectId = subjectVisit.SubjectId,
                                IsUrgent = subjectVisit.IsUrgent,
                                TaskBlindName = subjectVisit.BlindName,
                                TaskName = subjectVisit.VisitName,
                                VisitTaskNum = subjectVisit.VisitNum,
                                //CheckPassedTime = subjectVisit.CheckPassedTime,
                                ArmEnum = Arm.DoubleReadingArm2,//特殊
                                Code = currentMaxCodeInt + 2,
                                SourceSubjectVisitId = subjectVisit.Id,
                                TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 2, nameof(VisitTask)),
                                ReadingCategory = ReadingCategory.Visit
                            });

                            currentMaxCodeInt = currentMaxCodeInt + 2;

                            _provider.Set<int>($"{trialId}_{StaticData.CacheKey.TaskMaxCode}", currentMaxCodeInt, TimeSpan.FromMinutes(30));


                            #region 分配
                            if (isAssignSubjectToDoctor)
                            {


                                if (trialConfig.TaskAllocateObjEnum == TaskAllocateObj.Subject)
                                {
                                    var allocateSubjectArmList = _visitTaskRepository.Where(t => t.SubjectId == subjectVisit.SubjectId && t.TrialId == trialId && t.DoctorUserId != null && t.ArmEnum != Arm.JudgeArm)
                                           .Select(t => new { t.DoctorUserId, t.ArmEnum }).Distinct().ToList();



                                    //不是初次分配  直接分配给Subject 之前的医生
                                    if (allocateSubjectArmList.Count != 0)
                                    {
                                        #region 验证历史任务

                                        if (_taskAllocationRuleRepository.Where(t => t.TrialId == trialId && t.IsEnable && t.IsJudgeDoctor == false).Count() < 2)
                                        {
                                            throw new BusinessValidationFailedException("能参与读片的医生数量必须大于2,自动分配任务中止");
                                        }



                                        if (!(assignConfigList.Any(t => t.ArmEnum == Arm.DoubleReadingArm1) && allocateSubjectArmList.Any(t => t.ArmEnum == Arm.DoubleReadingArm2)))
                                        {
                                            throw new BusinessValidationFailedException("该受试者阅片医生未配置完成");
                                        }

                                        #endregion


                                        //并且配置了医生 
                                        if (assignConfigList.Count > 0 && trialConfig.IsFollowVisitAutoAssign)
                                        {
                                            
                                            #region 后续访视 未分配的进行再次分配，重置的或者失效的 需要重新生成新的任务  (PM 有序退回  或者PM  有序 申请重阅)


                                            if (trialConfig.IsReadingTaskViewInOrder)
                                            {
                                                //之前有回退的访视 那么当前访视一致性核查通过的时候，对后续访视不生成或者分配   当前访视生成但是不分配出去

                                                var beforeBackVisitTask = await _visitTaskRepository.Where(t => t.TrialId == trialId && t.SubjectId == subjectVisit.SubjectId && t.VisitTaskNum < subjectVisit.VisitNum && t.ReadingCategory == ReadingCategory.Visit && t.SourceSubjectVisit.CheckState != CheckStateEnum.CVPassed).OrderBy(t => t.VisitTaskNum).FirstOrDefaultAsync();


                                                if (beforeBackVisitTask == null)
                                                {
                                                    #region 访视2 PM 回退  基线回退  访视2先一致性核查通过，生成访视2任务，但是不分配

                                                    var defaultState = trialConfig.FollowVisitAutoAssignDefaultState == TaskAllocateDefaultState.InitAllocated ? TaskAllocationState.InitAllocated : TaskAllocationState.Allocated;

                                                    task1.TaskAllocationState = defaultState;
                                                    task2.TaskAllocationState = defaultState;

                                                    //分配给对应Arm的人
                                                    task1.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == Arm.DoubleReadingArm1).DoctorUserId;
                                                    task2.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == Arm.DoubleReadingArm2).DoctorUserId;

                                                    task1.AllocateTime = DateTime.Now;
                                                    task2.AllocateTime = DateTime.Now;

                                                    #endregion


                                                    var followBackVisitTask = await _visitTaskRepository.Where(t => t.TrialId == trialId && t.SubjectId == subjectVisit.SubjectId && t.VisitTaskNum > subjectVisit.VisitNum && t.ReadingCategory == ReadingCategory.Visit && t.SourceSubjectVisit.CheckState != CheckStateEnum.CVPassed).OrderBy(t => t.VisitTaskNum).FirstOrDefaultAsync();

                                                    //存在退回访视1  又退回基线 这种情况   生成任务  考虑基线先一致性核查通过，但是访视1还未通过时  生成任务
                                                    var followVisitTaskList = await _visitTaskRepository
                                                         .Where(t => t.TrialId == trialId && t.SubjectId == subjectVisit.SubjectId && t.VisitTaskNum > subjectVisit.VisitNum && t.SourceSubjectVisit.CheckState == CheckStateEnum.CVPassed && t.ReadingCategory == ReadingCategory.Visit, true)
                                                         .WhereIf(followBackVisitTask != null, t => t.VisitTaskNum < followBackVisitTask.VisitTaskNum)
                                                         .ToListAsync();

                                                    var followVisitGroup = followVisitTaskList.GroupBy(t => t.VisitTaskNum);

                                                    //每个访视去判断  是分配还是生成


                                                    foreach (var visitGroup in followVisitGroup)
                                                    {

                                                        var arm1 = visitGroup.FirstOrDefault(t => t.TaskState == TaskState.Effect && t.TaskAllocationState==TaskAllocationState.NotAllocate && t.DoctorUserId == null && t.ArmEnum == Arm.DoubleReadingArm1);
                                                        if (arm1 != null)
                                                        {
                                                            arm1.TaskAllocationState = TaskAllocationState.Allocated;
                                                            arm1.AllocateTime = DateTime.Now;
                                                            arm1.DoctorUserId = task1.DoctorUserId;
                                                        }
                                                        else
                                                        {
                                                            var latestTask = visitGroup.Where(t => t.ArmEnum == Arm.DoubleReadingArm1).OrderByDescending(t => t.CreateTime).First();

                                                            var taskOne = await _visitTaskRepository.AddAsync(new VisitTask()
                                                            {
                                                                TrialId = trialId,
                                                                SubjectId = subjectVisit.SubjectId,
                                                                IsUrgent = subjectVisit.IsUrgent,
                                                                ArmEnum = Arm.DoubleReadingArm1,//特殊
                                                                Code = currentMaxCodeInt + 1,
                                                                TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),
                                                                ReadingCategory = ReadingCategory.Visit,

                                                                SourceSubjectVisitId = latestTask.SourceSubjectVisitId,
                                                                VisitTaskNum = latestTask.VisitTaskNum,
                                                                TaskBlindName = latestTask.TaskBlindName,
                                                                TaskName = latestTask.TaskName,

                                                                BlindSubjectCode = latestTask.BlindSubjectCode,
                                                                BlindTrialSiteCode = latestTask.BlindTrialSiteCode,
                                                                IsAnalysisCreate = latestTask.IsAnalysisCreate,
                                                                IsSelfAnalysis = latestTask.IsSelfAnalysis,
                                                                TaskAllocationState = TaskAllocationState.Allocated,
                                                                AllocateTime = DateTime.Now,
                                                                DoctorUserId = task1.DoctorUserId
                                                            });

                                                            currentMaxCodeInt = currentMaxCodeInt + 1;

                                                            _provider.Set<int>($"{trialId}_{StaticData.CacheKey.TaskMaxCode}", currentMaxCodeInt, TimeSpan.FromMinutes(30));
                                                        }

                                                        var arm2 = visitGroup.FirstOrDefault(t => t.TaskState == TaskState.Effect && t.TaskAllocationState == TaskAllocationState.NotAllocate && t.DoctorUserId == null && t.ArmEnum == Arm.DoubleReadingArm2);
                                                        if (arm2 != null)
                                                        {
                                                            arm2.TaskAllocationState = TaskAllocationState.Allocated;
                                                            arm2.AllocateTime = DateTime.Now;
                                                            arm2.DoctorUserId = task2.DoctorUserId;
                                                        }
                                                        else
                                                        {
                                                            var latestTask = visitGroup.Where(t => t.ArmEnum == Arm.DoubleReadingArm2).OrderByDescending(t => t.CreateTime).First();

                                                            var taskTwo = await _visitTaskRepository.AddAsync(new VisitTask()
                                                            {
                                                                TrialId = trialId,
                                                                SubjectId = subjectVisit.SubjectId,
                                                                IsUrgent = subjectVisit.IsUrgent,

                                                                //CheckPassedTime = subjectVisit.CheckPassedTime,
                                                                ArmEnum = Arm.DoubleReadingArm2,//特殊
                                                                Code = currentMaxCodeInt + 1,
                                                                TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),
                                                                ReadingCategory = ReadingCategory.Visit,

                                                                SourceSubjectVisitId = latestTask.SourceSubjectVisitId,
                                                                VisitTaskNum = latestTask.VisitTaskNum,
                                                                TaskBlindName = latestTask.TaskBlindName,
                                                                TaskName = latestTask.TaskName,

                                                                BlindSubjectCode = latestTask.BlindSubjectCode,
                                                                BlindTrialSiteCode = latestTask.BlindTrialSiteCode,
                                                                IsAnalysisCreate = latestTask.IsAnalysisCreate,
                                                                IsSelfAnalysis = latestTask.IsSelfAnalysis,
                                                                TaskAllocationState = TaskAllocationState.Allocated,
                                                                AllocateTime = DateTime.Now,
                                                                DoctorUserId = task2.DoctorUserId
                                                            });

                                                            currentMaxCodeInt = currentMaxCodeInt + 1;

                                                            _provider.Set<int>($"{trialId}_{StaticData.CacheKey.TaskMaxCode}", currentMaxCodeInt, TimeSpan.FromMinutes(30));
                                                        }
                                                    }

                                                }
                                                //之前有回退的    后续访视不生成或者分配   当前访视生成但是不分配出去
                                                else
                                                {
                                                    //不用进行额外处理
                                                }



                                            }
                                            //无序的时候    生成任务并分配出去
                                            else
                                            {
                                                var defaultState = trialConfig.FollowVisitAutoAssignDefaultState == TaskAllocateDefaultState.InitAllocated ? TaskAllocationState.InitAllocated : TaskAllocationState.Allocated;

                                                task1.TaskAllocationState = defaultState;
                                                task2.TaskAllocationState = defaultState;

                                                //分配给对应Arm的人
                                                task1.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == Arm.DoubleReadingArm1).DoctorUserId;
                                                task2.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == Arm.DoubleReadingArm2).DoctorUserId;

                                                task1.AllocateTime = DateTime.Now;
                                                task2.AllocateTime = DateTime.Now;
                                            }


                                            #endregion

                                        }
                                        else
                                        //后续访视不自动分配，或者配置的医生数量不足，就不进行分配
                                        {

                                        }

                                    }
                                    else
                                    // 当前任务没有分配医生，初次分配
                                    {
                                        //return;
                                    }
                                }


                            }
                            #endregion

                        }
                        else if (trialConfig.ReadingType == ReadingMethod.Single)
                        {


                            var singleTask = await _visitTaskRepository.AddAsync(new VisitTask()
                            {
                                TrialId = trialId,
                                SubjectId = subjectVisit.SubjectId,
                                IsUrgent = subjectVisit.IsUrgent,
                                TaskBlindName = subjectVisit.BlindName,
                                TaskName = subjectVisit.VisitName,
                                VisitTaskNum = subjectVisit.VisitNum,
                                //CheckPassedTime = subjectVisit.CheckPassedTime,
                                ArmEnum = Arm.SingleReadingArm, //特殊
                                Code = currentMaxCodeInt + 1,
                                SourceSubjectVisitId = subjectVisit.Id,
                                TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),
                                ReadingCategory = ReadingCategory.Visit
                            });


                            currentMaxCodeInt = currentMaxCodeInt + 1;

                            _provider.Set<int>($"{trialId}_{StaticData.CacheKey.TaskMaxCode}", currentMaxCodeInt, TimeSpan.FromMinutes(30));

                            #region 分配

                            if (isAssignSubjectToDoctor)
                            {

                                if (trialConfig.TaskAllocateObjEnum == TaskAllocateObj.Subject)
                                {

                                    if (trialConfig.IsFollowVisitAutoAssign)
                                    {


                                        //该Subject 之前是否有已分配的   如果改变配置  可能会出现  一个Subject 分配的同一个医生 有的在Arm1 有的在Arm2
                                        var allocateSubjectArmList = _visitTaskRepository.Where(t => t.SubjectId == subjectVisit.SubjectId && t.TrialId == trialId && t.DoctorUserId != null && t.ArmEnum != Arm.JudgeArm)
                                            .Select(t => new { t.DoctorUserId, t.ArmEnum }).Distinct().ToList();

                                        //不是初次分配
                                        if (allocateSubjectArmList.Count != 0)
                                        {
                                            if (_taskAllocationRuleRepository.Where(t => t.TrialId == trialId && t.IsEnable).Count() < 2)
                                            {
                                                throw new BusinessValidationFailedException("能参与读片的医生数量必须大于2,自动分配任务中止");
                                            }

                                            //配置了医生 
                                            if (assignConfigList.Count > 0)
                                            {
                         


                                                #region 重阅/退回的时候，需要将取消分配的访视类型的  任务重新分配


                                                if (trialConfig.IsReadingTaskViewInOrder)
                                                {
                                                    //之前有回退的访视 那么当前访视一致性核查通过的时候，对后续访视不生成或者分配   当前访视生成但是不分配出去

                                                    var beforeBackVisitTask = await _visitTaskRepository.Where(t => t.TrialId == trialId && t.SubjectId == subjectVisit.SubjectId && t.VisitTaskNum < subjectVisit.VisitNum && t.ReadingCategory == ReadingCategory.Visit && t.SourceSubjectVisit.CheckState != CheckStateEnum.CVPassed).OrderBy(t => t.VisitTaskNum).FirstOrDefaultAsync();


                                                    if (beforeBackVisitTask == null)
                                                    {
                                                        #region 访视2 PM 回退  基线回退  访视2先一致性核查通过，生成访视2任务，但是不分配

                                                        var defaultState = trialConfig.FollowVisitAutoAssignDefaultState == TaskAllocateDefaultState.InitAllocated ? TaskAllocationState.InitAllocated : TaskAllocationState.Allocated;

                                                        singleTask.TaskAllocationState = defaultState;

                                                        singleTask.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == 0).DoctorUserId;

                                                        singleTask.AllocateTime = DateTime.Now;

                                                        #endregion


                                                        var followBackVisitTask = await _visitTaskRepository.Where(t => t.TrialId == trialId && t.SubjectId == subjectVisit.SubjectId && t.VisitTaskNum > subjectVisit.VisitNum && t.ReadingCategory == ReadingCategory.Visit && t.SourceSubjectVisit.CheckState != CheckStateEnum.CVPassed).OrderBy(t => t.VisitTaskNum).FirstOrDefaultAsync();

                                                        //存在退回访视1  又退回基线 这种情况   生成任务  考虑基线先一致性核查通过，但是访视1还未通过时  生成任务
                                                        var followVisitTaskList = await _visitTaskRepository
                                                             .Where(t => t.TrialId == trialId && t.SubjectId == subjectVisit.SubjectId && t.VisitTaskNum > subjectVisit.VisitNum && t.SourceSubjectVisit.CheckState == CheckStateEnum.CVPassed && t.ReadingCategory == ReadingCategory.Visit, true)
                                                             .WhereIf(followBackVisitTask != null, t => t.VisitTaskNum < followBackVisitTask.VisitTaskNum)
                                                             .ToListAsync();

                                                        var followVisitGroup = followVisitTaskList.GroupBy(t => t.VisitTaskNum);

                                                        //每个访视去判断  是分配还是生成


                                                        foreach (var visitGroup in followVisitGroup)
                                                        {

                                                            var arm = visitGroup.FirstOrDefault(t => t.TaskState == TaskState.Effect && t.TaskAllocationState == TaskAllocationState.NotAllocate && t.DoctorUserId == null && t.ArmEnum == Arm.SingleReadingArm);
                                                            if (arm != null)
                                                            {
                                                                arm.TaskAllocationState = TaskAllocationState.Allocated;
                                                                arm.AllocateTime = DateTime.Now;
                                                                arm.DoctorUserId = singleTask.DoctorUserId;
                                                            }
                                                            else
                                                            {
                                                                var latestTask = visitGroup.Where(t => t.ArmEnum == Arm.SingleReadingArm).OrderByDescending(t => t.CreateTime).First();

                                                                var taskOne = await _visitTaskRepository.AddAsync(new VisitTask()
                                                                {
                                                                    TrialId = trialId,
                                                                    SubjectId = subjectVisit.SubjectId,
                                                                    IsUrgent = subjectVisit.IsUrgent,
                                                                    ArmEnum = Arm.SingleReadingArm,//特殊
                                                                    Code = currentMaxCodeInt + 1,
                                                                    TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),
                                                                    ReadingCategory = ReadingCategory.Visit,

                                                                    SourceSubjectVisitId = latestTask.SourceSubjectVisitId,
                                                                    VisitTaskNum = latestTask.VisitTaskNum,
                                                                    TaskBlindName = latestTask.TaskBlindName,
                                                                    TaskName = latestTask.TaskName,

                                                                    BlindSubjectCode = latestTask.BlindSubjectCode,
                                                                    BlindTrialSiteCode = latestTask.BlindTrialSiteCode,
                                                                    IsAnalysisCreate = latestTask.IsAnalysisCreate,
                                                                    IsSelfAnalysis = latestTask.IsSelfAnalysis,
                                                                    TaskAllocationState = TaskAllocationState.Allocated,
                                                                    AllocateTime = DateTime.Now,
                                                                    DoctorUserId = singleTask.DoctorUserId
                                                                });

                                                                currentMaxCodeInt = currentMaxCodeInt + 1;

                                                                _provider.Set<int>($"{trialId}_{StaticData.CacheKey.TaskMaxCode}", currentMaxCodeInt, TimeSpan.FromMinutes(30));
                                                            }

                                                        }

                                                    }
                                                    //之前有回退的    后续访视不生成或者分配   当前访视生成但是不分配出去
                                                    else
                                                    {
                                                        //不用进行额外处理
                                                    }



                                                }
                                                //无序的时候    生成任务并分配出去
                                                else
                                                {
                                                    var defaultState = trialConfig.FollowVisitAutoAssignDefaultState == TaskAllocateDefaultState.InitAllocated ? TaskAllocationState.InitAllocated : TaskAllocationState.Allocated;

                                                    singleTask.TaskAllocationState = defaultState;

                                                    singleTask.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == 0).DoctorUserId;

                                                    singleTask.AllocateTime = DateTime.Now;

                                                }

                                                #endregion

                                            }
                                        }
                                    }
                                    else
                                    {
                                        //后续Subect  不自动分配  不处理
                                    }




                                }


                            }
                            #endregion


                        }


                        await _subjectVisitRepository.BatchUpdateNoTrackingAsync(t => t.TrialId == trialId && t.Id == subjectVisit.Id, u => new SubjectVisit() { IsVisitTaskGenerated = true });
                    }
                    break;
                case GenerateTaskCategory.ReReading:

                    var reReadingVisitTask = generateTaskCommand.ReReadingTask;

                    //foreach (var reReadingVisitTask in generateTaskCommand.ReReadingTaskList)
                    //{
                    //reReadingVisitTask.OriginalReReadingId = reReadingVisitTask.Id;
                    //reReadingVisitTask.Id = Guid.Empty;
                    //reReadingVisitTask.SignTime = null;
                    //reReadingVisitTask.AllocateTime = null;

                    //reReadingVisitTask.RequestReReadingReason = generateTaskCommand.ReReadingApplyGenerateTaskCommand.RequestReReadingReason;
                    //reReadingVisitTask.RequestReReadingType = generateTaskCommand.ReReadingApplyGenerateTaskCommand.RequestReReadingType;
                    //reReadingVisitTask.RequestReReadingUserId = _userInfo.Id;

                    //reReadingVisitTask.Code = currentMaxCodeInt + 1;
                    //reReadingVisitTask.TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask));

                    // await _visitTaskRepository.AddAsync(reReadingVisitTask);


                    var newTask = await _visitTaskRepository.AddAsync(new VisitTask()
                    {


                        TrialId = reReadingVisitTask.TrialId,
                        SubjectId = reReadingVisitTask.SubjectId,
                        ArmEnum = reReadingVisitTask.ArmEnum,
                        TaskName = reReadingVisitTask.TaskName,
                        TaskBlindName = reReadingVisitTask.TaskBlindName,

                        VisitTaskNum = reReadingVisitTask.VisitTaskNum,
                        ReadingCategory = reReadingVisitTask.ReadingCategory,
                        SourceSubjectVisitId = reReadingVisitTask.SourceSubjectVisitId,
                        SouceReadModuleId = reReadingVisitTask.SouceReadModuleId,

                        IsReReadingCreate = true,
                        TaskState = TaskState.Effect,
                        Code = currentMaxCodeInt + 1,
                        TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),

                        IsAnalysisCreate = reReadingVisitTask.IsAnalysisCreate,
                        IsSelfAnalysis = reReadingVisitTask.IsSelfAnalysis,
                        BlindSubjectCode = reReadingVisitTask.BlindSubjectCode,
                        BlindTrialSiteCode = reReadingVisitTask.BlindTrialSiteCode

                        // TaskAllocationState = reReadingVisitTask.TaskAllocationState,
                        // AllocateTime = DateTime.Now,
                        //DoctorUserId = reReadingVisitTask.DoctorUserId,

                    });

                    generateTaskCommand.Action(newTask);


                    //await  _visitTaskReReadingRepository.BatchUpdateNoTrackingAsync(t=>t.)

                    //是否增加任务类别

                    currentMaxCodeInt = currentMaxCodeInt + 1;

                    _provider.Set<int>($"{trialId}_{StaticData.CacheKey.TaskMaxCode}", currentMaxCodeInt, TimeSpan.FromMinutes(30));

                    //}

                    break;
                case GenerateTaskCategory.SelfConsistent:

                    foreach (var task in generateTaskCommand.GenerataConsistentTaskList)
                    {
                        var consistentTask = new VisitTask()
                        {
                            TrialId = task.TrialId,
                            SubjectId = task.SubjectId,
                            ArmEnum = task.ArmEnum,
                            TaskName = task.TaskName,
                            TaskBlindName = task.TaskBlindName,

                            VisitTaskNum = task.VisitTaskNum,
                            ReadingCategory = task.ReadingCategory,

                            TaskState = TaskState.Effect,
                            Code = currentMaxCodeInt + 1,
                            TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),

                            DoctorUserId = task.DoctorUserId,
                            TaskAllocationState = TaskAllocationState.Allocated,
                            AllocateTime = DateTime.Now,

                            SouceReadModuleId = task.SouceReadModuleId,
                            SourceSubjectVisitId = task.SourceSubjectVisitId,
                            BlindTrialSiteCode = task.BlindTrialSiteCode,
                            BlindSubjectCode = task.BlindSubjectCode,
                            ConsistentAnalysisOriginalTaskId = task.Id,
                            IsAnalysisCreate = true,
                            IsSelfAnalysis = true,

                        };

                        await _visitTaskRepository.AddAsync(consistentTask);

                        currentMaxCodeInt = currentMaxCodeInt + 1;

                        _provider.Set<int>($"{trialId}_{StaticData.CacheKey.TaskMaxCode}", currentMaxCodeInt, TimeSpan.FromMinutes(30));
                    }

                    break;

                case GenerateTaskCategory.GroupConsistent:

                    foreach (var task in generateTaskCommand.GenerataGroupConsistentTaskList)
                    {
                        var consistentTask = new VisitTask()
                        {
                            TrialId = task.TrialId,
                            SubjectId = task.SubjectId,
                            ArmEnum = task.ArmEnum,
                            TaskName = task.TaskName,
                            TaskBlindName = task.TaskBlindName,

                            VisitTaskNum = task.VisitTaskNum,
                            ReadingCategory = task.ReadingCategory,

                            TaskState = TaskState.Effect,
                            Code = currentMaxCodeInt + 1,
                            TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),

                            SouceReadModuleId = task.SouceReadModuleId,
                            SourceSubjectVisitId = task.SourceSubjectVisitId,
                            DoctorUserId = task.DoctorUserId,
                            TaskAllocationState = TaskAllocationState.Allocated,
                            AllocateTime = DateTime.Now,


                            IsAnalysisCreate = true,
                            IsSelfAnalysis = false,

                        };

                        await _visitTaskRepository.AddAsync(consistentTask);

                        currentMaxCodeInt = currentMaxCodeInt + 1;

                        _provider.Set<int>($"{trialId}_{StaticData.CacheKey.TaskMaxCode}", currentMaxCodeInt, TimeSpan.FromMinutes(30));
                    }

                    break;

                case GenerateTaskCategory.Judge:
                    var firstTask = await _visitTaskRepository.Where(x => generateTaskCommand.JudgeVisitTaskIdList.Contains(x.Id)).FirstOrDefaultAsync();

                    var subjectUser = await _subjectUserRepository.Where(x => x.SubjectId == firstTask.SubjectId && x.ArmEnum == Arm.JudgeArm).FirstOrDefaultAsync();

                    VisitTask visitTask = new VisitTask()
                    {
                        ArmEnum = Arm.JudgeArm,
                        Id = NewId.NextGuid(),
                        SubjectId = firstTask.SubjectId,
                        ReadingTaskState = ReadingTaskState.WaitReading,
                        TaskName = firstTask.TaskName,
                        ReadingCategory = ReadingCategory.Judge,
                        VisitTaskNum = firstTask.VisitTaskNum + ReadingCommon.TaskNumDic[ReadingCategory.Judge],
                        TrialId = firstTask.TrialId,
                        Code = currentMaxCodeInt + 1,
                        SourceSubjectVisitId = firstTask.SourceSubjectVisitId,
                        SouceReadModuleId = firstTask.SouceReadModuleId,
                        TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),
                        TaskState = TaskState.Effect,
                        DoctorUserId = subjectUser == null ? null : subjectUser.DoctorUserId,
                        TaskAllocationState = subjectUser == null ? TaskAllocationState.NotAllocate : TaskAllocationState.Allocated,
                        AllocateTime = subjectUser == null ? null : DateTime.Now,


                    };
                    await _visitTaskRepository.AddAsync(visitTask);
                    currentMaxCodeInt = currentMaxCodeInt + 1;

                    _provider.Set<int>($"{trialId}_{StaticData.CacheKey.TaskMaxCode}", currentMaxCodeInt, TimeSpan.FromMinutes(30));

                    await _visitTaskRepository.BatchUpdateNoTrackingAsync(x => generateTaskCommand.JudgeVisitTaskIdList.Contains(x.Id), x => new VisitTask()
                    {
                        JudgeVisitTaskId = visitTask.Id
                    });

                    await _readingJudgeInfoRepository.AddAsync(new ReadingJudgeInfo()
                    {
                        JudgeTaskId = visitTask.Id,
                        SubjectId = firstTask.SubjectId,
                        TrialId = firstTask.TrialId,
                        TaskIdOne = generateTaskCommand.JudgeVisitTaskIdList[0],
                        TaskIdTwo = generateTaskCommand.JudgeVisitTaskIdList[1]
                    });
                    break;

                //case ReadingCategory.ReadingPeriod:
                case GenerateTaskCategory.Global:

                    var originalTaskInfo = await _visitTaskRepository.Where(x => x.Id == generateTaskCommand.OriginalVisitId).FirstNotNullAsync();
                    foreach (var item in generateTaskCommand.ReadingGenerataTaskList)
                    {
                     
                        var task1 = await _visitTaskRepository.AddAsync(new VisitTask()
                        {
                            TrialId = trialId,
                            SubjectId = item.SubjectId,
                            IsUrgent = item.IsUrgent,
                            VisitTaskNum = item.VisitNum + ReadingCommon.TaskNumDic[ReadingCategory.Global],
                            TaskName = item.ReadingName,
                            DoctorUserId = originalTaskInfo.DoctorUserId,
                            ArmEnum = originalTaskInfo.ArmEnum,//特殊
                            AllocateTime = DateTime.Now,
                            TaskAllocationState = TaskAllocationState.Allocated,
                            Code = currentMaxCodeInt + 1,
                            TaskBlindName = item.ReadingName,
                            SouceReadModuleId = item.ReadModuleId,
                            TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),
                            ReadingCategory = item.ReadingCategory
                        });

                        currentMaxCodeInt = currentMaxCodeInt + 1;
                        _provider.Set<int>($"{trialId}_{StaticData.CacheKey.TaskMaxCode}", currentMaxCodeInt, TimeSpan.FromMinutes(30));
                    }
                    break;
                case GenerateTaskCategory.Oncology:
                    foreach (var item in generateTaskCommand.ReadingGenerataTaskList)
                    {
                        var singleTask = await _visitTaskRepository.AddAsync(new VisitTask()
                        {
                            TrialId = trialId,
                            SubjectId = item.SubjectId,
                            IsUrgent = item.IsUrgent,
                            TaskName = item.ReadingName,
                            VisitTaskNum = item.VisitNum + ReadingCommon.TaskNumDic[ReadingCategory.Oncology],
                            ArmEnum = Arm.TumorArm, //特殊
                            Code = currentMaxCodeInt + 1,
                            SouceReadModuleId = item.ReadModuleId,
                            TaskBlindName = item.ReadingName,
                            TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),
                            ReadingCategory = item.ReadingCategory
                        });

                        singleTask.AllocateTime = DateTime.Now;

                        currentMaxCodeInt = currentMaxCodeInt + 1;

                        _provider.Set<int>($"{trialId}_{StaticData.CacheKey.TaskMaxCode}", currentMaxCodeInt, TimeSpan.FromMinutes(30));
                    }
                    break;
            }



            await _visitTaskRepository.SaveChangesAsync();
        }


    }



}
