//--------------------------------------------------------------------
//     此代码由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;
using System.Linq.Expressions;
using IRaCIS.Core.Domain.Share.Reading;

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 IRepository<ReadModule> _readModuleRepository;

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

        private readonly IRepository<ReadingQuestionCriterionTrial> _trialReadingCriterionRepository;

        private readonly IRepository<ClinicalDataTrialSet> _trialClinicalDataSetRepository;
        private readonly IRepository<ReadingClinicalData> _readingClinicalDataRepository;


        public VisitTaskHelpeService(IRepository<VisitTask> visitTaskRepository, IRepository<SubjectUser> subjectUserRepository, IRepository<Trial> trialRepository, IEasyCachingProvider provider,
             IRepository<SubjectVisit> subjectVisitRepository,
             IRepository<ReadModule> readModuleRepository,
             IRepository<ReadingJudgeInfo> readingJudgeInfoRepository,
             IRepository<TaskAllocationRule> taskAllocationRuleRepository, IMapper mapper, IUserInfo userInfo, IRepository<VisitTaskReReading> visitTaskReReadingRepository,
             IRepository<ReadingQuestionCriterionTrial> trialReadingCriterionRepository, IRepository<ClinicalDataTrialSet> trialClinicalDataSetRepository, IRepository<ReadingClinicalData> readingClinicalDataRepository)
        {
            _readingClinicalDataRepository = readingClinicalDataRepository;
            _trialClinicalDataSetRepository = trialClinicalDataSetRepository;

            _visitTaskRepository = visitTaskRepository;
            _trialRepository = trialRepository;
            this._readModuleRepository = readModuleRepository;
            _provider = provider;
            _subjectVisitRepository = subjectVisitRepository;
            this._readingJudgeInfoRepository = readingJudgeInfoRepository;
            _taskAllocationRuleRepository = taskAllocationRuleRepository;
            _subjectUserRepository = subjectUserRepository;
            _mapper = mapper;
            _userInfo = userInfo;
            _visitTaskReReadingRepository = visitTaskReReadingRepository;

            _trialReadingCriterionRepository = trialReadingCriterionRepository;
        }

        //查询列表的时候，一致性核查通过未产生任务的 自动产生任务   如果是一致性核查，那么还会自动分配
        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 BaseCritrionGenerateVisitTask(Guid trialId, Guid confirmedTrialReadingCriterionId)
        {
            //找到一致性核查通过且没有产生任务的检查批次
            var needGenerateVisit = await _subjectVisitRepository.Where(t => t.TrialId == trialId && t.CheckState == CheckStateEnum.CVPassed &&
            !t.VisitTaskList.Any(u => u.TrialReadingCriterionId == confirmedTrialReadingCriterionId && u.SourceSubjectVisitId == t.Id && u.TaskState==TaskState.Effect && u.IsAnalysisCreate==false)).ToListAsync();


            var trialReadingCriterionConfig = await _trialReadingCriterionRepository.Where(t => t.Id == confirmedTrialReadingCriterionId).Select(t => new { TrialReadingCriterionId = t.Id, t.ReadingTool, t.ReadingType, t.IsReadingTaskViewInOrder, t.IsFollowVisitAutoAssign, t.IsFollowGlobalVisitAutoAssign, t.FollowGlobalVisitAutoAssignDefaultState, t.FollowVisitAutoAssignDefaultState, t.TaskAllocateObjEnum, t.CriterionType }).FirstOrDefaultAsync();

            //获取确认的临床数据配置
            var clinicalDataConfirmList = _trialClinicalDataSetRepository.Where(t => t.TrialId == trialId && t.IsConfirm).Include(t => t.TrialClinicalDataSetCriteriaList).ToList();

            var visitBlindConfig = _trialRepository.Where(t => t.Id == trialId).Select(t => new { t.BlindBaseLineName, t.BlindFollowUpPrefix }).FirstOrDefault();

            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;



            foreach (var subjectGroup in needGenerateVisit.GroupBy(t => t.SubjectId).Select(g => new { SubjectId = g.Key, SubjectVisitList = g.ToList() }))
            {
                var assignConfigList = await _subjectUserRepository.Where(t => t.TrialId == trialId && t.TrialReadingCriterionId == trialReadingCriterionConfig.TrialReadingCriterionId && t.SubjectId == subjectGroup.SubjectId && t.OrignalSubjectUserId == null && t.IsConfirmed).Select(u => new { u.DoctorUserId, u.ArmEnum }).ToListAsync();



                var visitNumList = _subjectVisitRepository.Where(t => t.SubjectId == subjectGroup.SubjectId && t.IsLostVisit == false).OrderBy(t => t.VisitNum).Select(t => t.VisitNum).ToList();

                foreach (var subjectVisit in subjectGroup.SubjectVisitList)
                {
                    //var blindTaskName = string.Empty;

                    var blindTaskName = subjectVisit.VisitName;

                    var isNeedClinicalDataSign = IsNeedClinicalDataSign(ReadingCategory.Visit, subjectVisit.IsBaseLine, trialReadingCriterionConfig.TrialReadingCriterionId, clinicalDataConfirmList);
                    var isClinicalDataSign = IsClinicalDataSign(ReadingCategory.Visit, subjectVisit.IsBaseLine, trialReadingCriterionConfig.TrialReadingCriterionId, clinicalDataConfirmList, subjectVisit.Id, trialId);

                    //if (visitNumList.IndexOf(subjectVisit.VisitNum) == 0)
                    //{
                    //    blindTaskName = visitBlindConfig.BlindBaseLineName;
                    //}
                    //else
                    //{
                    //    if (trialReadingCriterionConfig.IsReadingTaskViewInOrder)
                    //    {
                    //        blindTaskName = visitBlindConfig.BlindFollowUpPrefix + " " + visitNumList.IndexOf(subjectVisit.VisitNum);
                    //    }
                    //    else
                    //    {
                    //        blindTaskName = visitBlindConfig.BlindFollowUpPrefix;
                    //    }
                    //}

                    //每个检查批次 根据项目配置生成任务  双审生成两个 

                    TaskUrgentType? taskUrgentType = null;

                    if (subjectVisit.PDState == PDStateEnum.PDProgress)
                    {
                        taskUrgentType = TaskUrgentType.PDProgress;
                    }
                    else if (subjectVisit.IsEnrollmentConfirm)
                    {
                        taskUrgentType = TaskUrgentType.EnrollmentConfirm;
                    }
                    else if (subjectVisit.IsUrgent)
                    {
                        taskUrgentType = TaskUrgentType.VisitUrgent;
                    }

                    bool isCanEditUrgentState = taskUrgentType == TaskUrgentType.EnrollmentConfirm || taskUrgentType == TaskUrgentType.PDProgress ? false : true;

                    if (trialReadingCriterionConfig.ReadingType == ReadingMethod.Double)
                    {
                        //未防止脏数据 这里也多判断一次  
                        var existCurrentVisitTaskList = _visitTaskRepository.Where(t => t.TrialId == trialId && t.SubjectId == subjectVisit.SubjectId
                                    && t.TrialReadingCriterionId == trialReadingCriterionConfig.TrialReadingCriterionId && t.TaskState == TaskState.Effect
                                    && t.TaskAllocationState == TaskAllocationState.NotAllocate && t.DoctorUserId == null && t.SourceSubjectVisitId == subjectVisit.Id).ToList();

                        VisitTask? task1 = null;
                        VisitTask? task2 = null;
                     
                        if (!existCurrentVisitTaskList.Any(t => t.ArmEnum == Arm.DoubleReadingArm1))
                        {
                            currentMaxCodeInt = currentMaxCodeInt + 1;


                           

                            task1 = await _visitTaskRepository.AddAsync(new VisitTask()
                            {
                                TrialId = trialId,
                                SubjectId = subjectVisit.SubjectId,
                                IsUrgent = subjectVisit.IsUrgent,
                                TaskBlindName = blindTaskName,
                                TaskName = subjectVisit.VisitName,
                                VisitTaskNum = subjectVisit.VisitNum,
                                TaskUrgentType = taskUrgentType,
                                IsCanEditUrgentState= isCanEditUrgentState,
                                //CheckPassedTime = subjectVisit.CheckPassedTime,
                                ArmEnum = Arm.DoubleReadingArm1,//特殊
                                Code = currentMaxCodeInt,
                                SourceSubjectVisitId = subjectVisit.Id,
                                TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt, nameof(VisitTask)),
                                ReadingCategory = ReadingCategory.Visit,

                                TrialReadingCriterionId = trialReadingCriterionConfig.TrialReadingCriterionId,
                                IsNeedClinicalDataSign = isNeedClinicalDataSign,
                                IsClinicalDataSign = isClinicalDataSign
                            }) ;

                        }

                        if (!existCurrentVisitTaskList.Any(t => t.ArmEnum == Arm.DoubleReadingArm2))
                        {
                            currentMaxCodeInt = currentMaxCodeInt + 1;


                            task2 = await _visitTaskRepository.AddAsync(new VisitTask()
                            {
                                TrialId = trialId,
                                SubjectId = subjectVisit.SubjectId,
                                IsUrgent = subjectVisit.IsUrgent,
                                TaskBlindName = blindTaskName,
                                TaskName = subjectVisit.VisitName,
                                TaskUrgentType = taskUrgentType,
                                IsCanEditUrgentState = isCanEditUrgentState,
                                VisitTaskNum = subjectVisit.VisitNum,
                                //CheckPassedTime = subjectVisit.CheckPassedTime,
                                ArmEnum = Arm.DoubleReadingArm2,//特殊
                                Code = currentMaxCodeInt,
                                SourceSubjectVisitId = subjectVisit.Id,
                                TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt, nameof(VisitTask)),
                                ReadingCategory = ReadingCategory.Visit,

                                TrialReadingCriterionId = trialReadingCriterionConfig.TrialReadingCriterionId,
                                IsNeedClinicalDataSign = isNeedClinicalDataSign,
                                IsClinicalDataSign = isClinicalDataSign
                            });


                        }

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




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

                        if (assignConfigList.Any(t => t.ArmEnum == Arm.DoubleReadingArm1) && task1 != null)
                        {
                            task1.TaskAllocationState = defaultState;
                            //分配给对应Arm的人
                            task1.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == Arm.DoubleReadingArm1).DoctorUserId;
                            task1.AllocateTime = DateTime.Now;

                            task1.SuggesteFinishedTime = GetSuggessFinishTime(true,UrgentType.NotUrget);
                        }

                        if (assignConfigList.Any(t => t.ArmEnum == Arm.DoubleReadingArm2) && task2 != null)
                        {
                            task2.TaskAllocationState = defaultState;
                            task2.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == Arm.DoubleReadingArm2).DoctorUserId;
                            task2.AllocateTime = DateTime.Now;
                            task2.SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget);
                        }

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

                        var singleTask = await _visitTaskRepository.AddAsync(new VisitTask()
                        {
                            TrialId = trialId,
                            SubjectId = subjectVisit.SubjectId,
                            IsUrgent = subjectVisit.IsUrgent,
                            TaskBlindName = blindTaskName,
                            TaskName = subjectVisit.VisitName,
                            VisitTaskNum = subjectVisit.VisitNum,
                            TaskUrgentType = taskUrgentType,
                            IsCanEditUrgentState = isCanEditUrgentState,
                            //CheckPassedTime = subjectVisit.CheckPassedTime,
                            ArmEnum = Arm.SingleReadingArm, //特殊
                            Code = currentMaxCodeInt + 1,
                            SourceSubjectVisitId = subjectVisit.Id,
                            TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),
                            ReadingCategory = ReadingCategory.Visit,
                            TrialReadingCriterionId = trialReadingCriterionConfig.TrialReadingCriterionId,
                            IsNeedClinicalDataSign = isNeedClinicalDataSign,
                            IsClinicalDataSign = isClinicalDataSign
                        });


                        currentMaxCodeInt = currentMaxCodeInt + 1;

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



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

                        if (assignConfigList.Any(t => t.ArmEnum == Arm.SingleReadingArm))
                        {
                            singleTask.TaskAllocationState = defaultState;

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

                            singleTask.AllocateTime = DateTime.Now;

                            singleTask.SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget);
                        }




                    }
                    await _visitTaskRepository.SaveChangesAsync();

                }

            }


        }



        private bool IsNeedClinicalDataSign(ReadingCategory readingCategory, bool isbaseLine, Guid trialReadingCriterionId, List<ClinicalDataTrialSet> trialClinicalDataSetList)
        {
            //检查批次
            if (readingCategory == ReadingCategory.Visit)
            {
                if (isbaseLine)
                {
                    //return  trialClinicalDataSetList.Where(t=>t.CriterionEnumList.Any(c=>c==(int)criterionType)  && (t.ClinicalDataLevel == ClinicalLevel.Subject || t.ClinicalDataLevel == ClinicalLevel.SubjectVisit)).Count()> 0;

                    return trialClinicalDataSetList.Any(t => t.TrialClinicalDataSetCriteriaList.Any(c => c.TrialReadingCriterionId == trialReadingCriterionId) && (t.ClinicalDataLevel == ClinicalLevel.Subject || t.ClinicalDataLevel == ClinicalLevel.SubjectVisit));
                }
                else
                {
                    return trialClinicalDataSetList.Any(t => t.TrialClinicalDataSetCriteriaList.Any(c => c.TrialReadingCriterionId == trialReadingCriterionId) && t.ClinicalDataLevel == ClinicalLevel.SubjectVisit);
                }
            }

            else if (readingCategory == ReadingCategory.Judge)
            {
                return false;
            }
            //阅片期
            else if (readingCategory == ReadingCategory.Global)
            {
                return trialClinicalDataSetList.Any(t => t.TrialClinicalDataSetCriteriaList.Any(c => c.TrialReadingCriterionId == trialReadingCriterionId) && t.ClinicalDataLevel == ClinicalLevel.ImageRead);
            }
            // 肿瘤学
            else if (readingCategory == ReadingCategory.Oncology)
            {
                return trialClinicalDataSetList.Any(t => t.TrialClinicalDataSetCriteriaList.Any(c => c.TrialReadingCriterionId == trialReadingCriterionId) && t.ClinicalDataLevel == ClinicalLevel.OncologyRead);
            }
            else
            {
                return false;
            }

        }


        // 有可能在任务生成之前 就签名完了临床数据
        private bool IsClinicalDataSign(ReadingCategory readingCategory, bool isbaseLine, Guid trialReadingCriterionId, List<ClinicalDataTrialSet> trialClinicalDataSetList, Guid readingId, Guid trialId)
        {

            //不需要签名的 直接返回
            if (IsNeedClinicalDataSign(readingCategory, isbaseLine, trialReadingCriterionId, trialClinicalDataSetList) == false)
            {
                return false;
            }


            var isClinicalDataSign = false;

            var needSignCount = 0;

            //IC 的自动签名  不用管  只用处理PM的就好
            var haveSignedCount = _readingClinicalDataRepository
                .Where(t => t.TrialId == trialId && t.IsSign && t.ReadingClinicalDataState == ReadingClinicalDataStatus.HaveSigned && t.ReadingId == readingId && t.ClinicalDataTrialSet.UploadRole == UploadRole.PM).Count();

            //检查批次
            if (readingCategory == ReadingCategory.Visit)
            {
                if (isbaseLine)
                {
                    //IC 的自动签名   不用管 只用处理PM 的就好
                    needSignCount = trialClinicalDataSetList.Where(t => t.TrialClinicalDataSetCriteriaList.Any(c => c.TrialReadingCriterionId == trialReadingCriterionId) && (t.ClinicalDataLevel == ClinicalLevel.Subject || t.ClinicalDataLevel == ClinicalLevel.SubjectVisit) && t.UploadRole == UploadRole.PM).Count();
                }
                else
                {
                    needSignCount = trialClinicalDataSetList.Where(t => t.TrialClinicalDataSetCriteriaList.Any(c => c.TrialReadingCriterionId == trialReadingCriterionId) && t.ClinicalDataLevel == ClinicalLevel.SubjectVisit && t.UploadRole == UploadRole.PM).Count();

                }
            }

            else if (readingCategory == ReadingCategory.Judge)
            {

            }
            //阅片期
            else if (readingCategory == ReadingCategory.Global)
            {
                needSignCount = trialClinicalDataSetList.Where(t => t.TrialClinicalDataSetCriteriaList.Any(c => c.TrialReadingCriterionId == trialReadingCriterionId) && t.ClinicalDataLevel == ClinicalLevel.ImageRead && t.UploadRole == UploadRole.PM).Count();

            }
            // 肿瘤学
            else if (readingCategory == ReadingCategory.Oncology)
            {
                return trialClinicalDataSetList.Any(t => t.TrialClinicalDataSetCriteriaList.Any(c => c.TrialReadingCriterionId == trialReadingCriterionId) && t.ClinicalDataLevel == ClinicalLevel.OncologyRead);
            }
            else
            {
                needSignCount = trialClinicalDataSetList.Where(t => t.TrialClinicalDataSetCriteriaList.Any(c => c.TrialReadingCriterionId == trialReadingCriterionId) && t.ClinicalDataLevel == ClinicalLevel.OncologyRead && t.UploadRole == UploadRole.PM).Count();
            }

            //可能仅仅IC 基线   没有PM
            if (needSignCount == haveSignedCount /*&& needSignCount != 0*/)
            {
                isClinicalDataSign = true;

            }



            return isClinicalDataSign;
        }


        public DateTime GetSuggessFinishTime(bool isInOrder,  UrgentType urgentType )
        {

            var datetime= new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day , 22, 0, 0).AddDays(7);


            //switch (urgentType)
            //{
            //    case UrgentType.NotUrget:

            //        datetime= new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + 7, 22, 0, 0);

            //        break;
            //    case UrgentType.EnrollConfirm:

            //        datetime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day , 22, 0, 0);
            //        break;

            //    case UrgentType.EnrollConfirmOtherNotUrgent:

            //        datetime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day+5, 22, 0, 0);
            //        break;

            //    case UrgentType.PDVProgressVisitOrGlobal:
            //        datetime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 22, 0, 0);
            //        break;
            //    case UrgentType.PDVProgressJudge:
            //        datetime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day+1, 22, 0, 0);
            //        break;

            //        //不加急 7天
            //    case UrgentType.NormalUrgent:

            //        datetime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + 7, 22, 0, 0);
            //        break;
               
            //    default:
            //        break;
            //}

            return datetime;



        }


        public async Task AddTaskAsync(GenerateTaskCommand generateTaskCommand)
        {

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

            var trialReadingCriterionConfigList = _trialReadingCriterionRepository.Where(t => t.TrialId == trialId && t.ReadingInfoSignTime != null).Select(t => new { TrialReadingCriterionId = t.Id, t.ReadingTool, t.ReadingType, t.IsReadingTaskViewInOrder, t.IsFollowVisitAutoAssign, t.IsFollowGlobalVisitAutoAssign, t.FollowGlobalVisitAutoAssignDefaultState, t.FollowVisitAutoAssignDefaultState, t.TaskAllocateObjEnum, t.CriterionType }).ToList();

            var visitBlindConfig = _trialRepository.Where(t => t.Id == trialId).Select(t => new { t.BlindBaseLineName, t.BlindFollowUpPrefix }).FirstOrDefault();

            //获取确认的临床数据配置
            var clinicalDataConfirmList = _trialClinicalDataSetRepository.Where(t => t.TrialId == trialId && t.IsConfirm).Include(t => t.TrialClinicalDataSetCriteriaList).ToList();


            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:

                    //每个Subject   的每个检查批次   都要根据每个标准进行任务的生成  和分配(考虑回退后 的分配)


                    foreach (var subjectGroup in generateTaskCommand.VisitGenerataTaskList.GroupBy(t => t.SubjectId).Select(g => new { SubjectId = g.Key, SubjectVisitList = g.ToList() }))
                    {


                        foreach (var subjectVisit in subjectGroup.SubjectVisitList)
                        {

                            var subjectVisitInfo = await _subjectVisitRepository.Where(x => x.Id == subjectVisit.Id).Select(x => new
                            {
                                x.PDState,
                                x.IsEnrollmentConfirm,
                                x.IsUrgent,
                            }).FirstNotNullAsync();

                            TaskUrgentType? taskUrgentType = null;

                            if (subjectVisitInfo.PDState == PDStateEnum.PDProgress)
                            {
                                taskUrgentType = TaskUrgentType.PDProgress;
                            }
                            else if (subjectVisitInfo.IsEnrollmentConfirm)
                            {
                                taskUrgentType = TaskUrgentType.EnrollmentConfirm;
                            }
                            else if (subjectVisitInfo.IsUrgent)
                            {
                                taskUrgentType = TaskUrgentType.VisitUrgent;
                            }

                            bool isCanEditUrgentState = taskUrgentType == TaskUrgentType.EnrollmentConfirm || taskUrgentType == TaskUrgentType.PDProgress ? false : true;

                            var visitNumList = _subjectVisitRepository.Where(t => t.SubjectId == subjectGroup.SubjectId && t.IsLostVisit == false).OrderBy(t => t.VisitNum).Select(t => t.VisitNum).ToList();


                            foreach (var trialReadingCriterionConfig in trialReadingCriterionConfigList)
                            {

                                var assignConfigList = await _subjectUserRepository.Where(t => t.TrialId == trialId && t.TrialReadingCriterionId == trialReadingCriterionConfig.TrialReadingCriterionId && t.SubjectId == subjectVisit.SubjectId && t.OrignalSubjectUserId == null && t.IsConfirmed).Select(u => new { u.DoctorUserId, u.ArmEnum }).ToListAsync();


                                var blindTaskName = subjectVisit.VisitName;

                                var isNeedClinicalDataSign = IsNeedClinicalDataSign(ReadingCategory.Visit, subjectVisit.IsBaseLine, trialReadingCriterionConfig.TrialReadingCriterionId, clinicalDataConfirmList);
                                var isClinicalDataSign = IsClinicalDataSign(ReadingCategory.Visit, subjectVisit.IsBaseLine, trialReadingCriterionConfig.TrialReadingCriterionId, clinicalDataConfirmList, subjectVisit.Id, trialId);


                                //if (visitNumList.IndexOf(subjectVisit.VisitNum) == 0)
                                //{
                                //    blindTaskName = visitBlindConfig.BlindBaseLineName;
                                //}
                                //else
                                //{
                                //    if (trialReadingCriterionConfig.IsReadingTaskViewInOrder)
                                //    {
                                //        blindTaskName = visitBlindConfig.BlindFollowUpPrefix + " " + visitNumList.IndexOf(subjectVisit.VisitNum);
                                //    }
                                //    else
                                //    {
                                //        blindTaskName = visitBlindConfig.BlindFollowUpPrefix;
                                //    }
                                //}


                                //每个检查批次 根据项目配置生成任务  双审生成两个 

                                //双重 可能有一个人的任务没分配，然后影像回退不会影响未分配的任务，所以生成的时候 要进行判断 不能每次都生成两个
                                if (trialReadingCriterionConfig.ReadingType == ReadingMethod.Double)
                                {

                                    var existCurrentVisitTaskList = _visitTaskRepository.Where(t => t.TrialId == trialId && t.SubjectId == subjectVisit.SubjectId
                                    && t.TrialReadingCriterionId == trialReadingCriterionConfig.TrialReadingCriterionId && t.TaskState == TaskState.Effect
                                    && t.TaskAllocationState == TaskAllocationState.NotAllocate && t.DoctorUserId == null && t.SourceSubjectVisitId == subjectVisit.Id).ToList();

                                    VisitTask? task1 = null;
                                    VisitTask? task2 = null;


                                    if (!existCurrentVisitTaskList.Any(t => t.ArmEnum == Arm.DoubleReadingArm1))
                                    {
                                        currentMaxCodeInt = currentMaxCodeInt + 1;

                                        task1 = await _visitTaskRepository.AddAsync(new VisitTask()
                                        {
                                            TrialId = trialId,
                                            SubjectId = subjectVisit.SubjectId,
                                            TaskBlindName = blindTaskName,

                                            IsUrgent = subjectVisit.IsUrgent,
                                            TaskUrgentType = taskUrgentType,
                                            IsCanEditUrgentState = isCanEditUrgentState,
                                            TaskName = subjectVisit.VisitName,
                                            VisitTaskNum = subjectVisit.VisitNum,
                                            //CheckPassedTime = subjectVisit.CheckPassedTime,
                                            ArmEnum = Arm.DoubleReadingArm1,//特殊
                                            Code = currentMaxCodeInt,
                                            SourceSubjectVisitId = subjectVisit.Id,
                                            TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt, nameof(VisitTask)),
                                            ReadingCategory = ReadingCategory.Visit,

                                            TrialReadingCriterionId = trialReadingCriterionConfig.TrialReadingCriterionId,
                                            IsNeedClinicalDataSign = isNeedClinicalDataSign,
                                            IsClinicalDataSign = isClinicalDataSign
                                        });
                                    }

                                    if (!existCurrentVisitTaskList.Any(t => t.ArmEnum == Arm.DoubleReadingArm2))
                                    {
                                        currentMaxCodeInt = currentMaxCodeInt + 1;
                                        task2 = await _visitTaskRepository.AddAsync(new VisitTask()
                                        {
                                            TrialId = trialId,
                                            SubjectId = subjectVisit.SubjectId,
                                            TaskBlindName = blindTaskName,
                                            TaskName = subjectVisit.VisitName,
                                            IsUrgent = subjectVisit.IsUrgent,
                     
                                            TaskUrgentType = taskUrgentType,
                                            IsCanEditUrgentState = isCanEditUrgentState,
                                            VisitTaskNum = subjectVisit.VisitNum,
                                            //CheckPassedTime = subjectVisit.CheckPassedTime,
                                            ArmEnum = Arm.DoubleReadingArm2,//特殊
                                            Code = currentMaxCodeInt,
                                            SourceSubjectVisitId = subjectVisit.Id,
                                            TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt, nameof(VisitTask)),
                                            ReadingCategory = ReadingCategory.Visit,

                                            TrialReadingCriterionId = trialReadingCriterionConfig.TrialReadingCriterionId,
                                            IsNeedClinicalDataSign = isNeedClinicalDataSign,
                                            IsClinicalDataSign = isClinicalDataSign
                                        });
                                    }




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


                                    #region 分配
                                    if (isAssignSubjectToDoctor)
                                    {


                                        if (trialReadingCriterionConfig.TaskAllocateObjEnum == TaskAllocateObj.Subject)
                                        {
                                            var allocateSubjectArmList = _visitTaskRepository.Where(t => t.TrialReadingCriterionId == trialReadingCriterionConfig.TrialReadingCriterionId && 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)
                                            {

                                            }
                                            else
                                            {
                                                //并且配置了医生 
                                                if (assignConfigList.Count > 0 && trialReadingCriterionConfig.IsFollowVisitAutoAssign)
                                                {

                                                    #region 后续检查批次 未分配的进行再次分配，重置的或者失效的 需要重新生成新的任务  (PM 有序退回  或者PM  有序 申请重阅)


                                                    if (trialReadingCriterionConfig.IsReadingTaskViewInOrder)
                                                    {
                                                        //之前有回退到影像上传的检查批次 那么当前检查批次一致性核查通过的时候，当前检查批次生成但是不分配出去(排除失访的)

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

                                                        //之前有回退的，那么当前检查批次任务生成但是不分配
                                                        if (beforeBackVisitTask != null)
                                                        {
                                                            //不用进行额外处理

                                                            //检查批次2 PM 回退 基线回退  检查批次2先一致性核查通过，生成检查批次2任务，但是不分配
                                                        }
                                                        else
                                                        {
                                                            #region 当前检查批次根据配置规则分配出去

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

                                                            if (assignConfigList.Any(t => t.ArmEnum == Arm.DoubleReadingArm1) && task1 != null)
                                                            {
                                                                task1.TaskAllocationState = defaultState;
                                                                //分配给对应Arm的人
                                                                task1.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == Arm.DoubleReadingArm1).DoctorUserId;
                                                                task1.AllocateTime = DateTime.Now;

                                                                task1.SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget);
                                                            }

                                                            if (assignConfigList.Any(t => t.ArmEnum == Arm.DoubleReadingArm2) && task2 != null)
                                                            {
                                                                task2.TaskAllocationState = defaultState;
                                                                task2.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == Arm.DoubleReadingArm2).DoctorUserId;
                                                                task2.AllocateTime = DateTime.Now;
                                                                task2.SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget);
                                                            }

                                                            #endregion
                                                        }

                                                        //后续最近的未一致性核查通过的检查批次任务
                                                        var followBackVisitTask = await _visitTaskRepository.Where(t => t.TrialId == trialId && t.TrialReadingCriterionId == trialReadingCriterionConfig.TrialReadingCriterionId && t.SubjectId == subjectVisit.SubjectId && t.VisitTaskNum > subjectVisit.VisitNum && t.ReadingCategory == ReadingCategory.Visit && t.SourceSubjectVisit.CheckState != CheckStateEnum.CVPassed && t.SourceSubjectVisit.IsLostVisit == false).OrderBy(t => t.VisitTaskNum).FirstOrDefaultAsync();

                                                        //大于当前检查批次  同时小于最近的未一致性核查通过的检查批次任务分配 或者生成

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

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

                                                        //每个检查批次去判断  是分配还是生成（因为影响哪里有些是取消分配，有些是重阅重置需要重新生成）
                                                        foreach (var visitGroup in followVisitGroup)
                                                        {

                                                            var visit = await _subjectVisitRepository.Where(x => x.Id == visitGroup.First().SourceSubjectVisitId).Select(x => new
                                                            {
                                                                x.PDState,
                                                                x.IsEnrollmentConfirm,
                                                                x.IsUrgent,
                                                            }).FirstNotNullAsync();


                                                            TaskUrgentType? urgentType = null;

                                                            if (subjectVisitInfo.PDState == PDStateEnum.PDProgress)
                                                            {
                                                                urgentType = TaskUrgentType.PDProgress;
                                                            }
                                                            else if (subjectVisitInfo.IsEnrollmentConfirm)
                                                            {
                                                                urgentType = TaskUrgentType.EnrollmentConfirm;
                                                            }
                                                            else if (subjectVisitInfo.IsUrgent)
                                                            {
                                                                urgentType = TaskUrgentType.VisitUrgent;
                                                            }

                                                            bool isCanEdit = urgentType == TaskUrgentType.EnrollmentConfirm || urgentType == TaskUrgentType.PDProgress ? false : true;

                                                            //如果后续检查批次已分配有效 就不用处理
                                                            if (visitGroup.Any(t => t.TaskState == TaskState.Effect && t.TaskAllocationState == TaskAllocationState.Allocated && t.ArmEnum == Arm.DoubleReadingArm1))
                                                            {
                                                                //不做处理
                                                            }
                                                            else
                                                            {
                                                                var arm1 = visitGroup.FirstOrDefault(t => t.TaskState == TaskState.Effect && t.TaskAllocationState == TaskAllocationState.NotAllocate && t.DoctorUserId == null && t.ArmEnum == Arm.DoubleReadingArm1);

                                                                if (arm1 != null)
                                                                {

                                                                    //有可能仅仅只分配了一个Subject   未分配  那么
                                                                    if (assignConfigList.Any(t => t.ArmEnum == Arm.DoubleReadingArm1) && task1 != null)
                                                                    {
                                                                        arm1.IsUrgent = visit.IsUrgent;
                                                                        arm1.TaskUrgentType = urgentType;
                                                                        arm1.IsCanEditUrgentState = isCanEdit;
                                                                        arm1.TaskAllocationState = TaskAllocationState.Allocated;
                                                                        arm1.AllocateTime = DateTime.Now;
                                                                        arm1.DoctorUserId = task1.DoctorUserId;
                                                                        arm1.SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget);
                                                                    }

                                                                }
                                                                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 = visit.IsUrgent,
                                                                        TaskUrgentType = urgentType,
                                                                        IsCanEditUrgentState = isCanEdit,
                                                                        ArmEnum = Arm.DoubleReadingArm1,//特殊
                                                                        Code = currentMaxCodeInt + 1,
                                                                        TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),
                                                                        ReadingCategory = ReadingCategory.Visit,

                                                                        SourceSubjectVisitId = latestTask.SourceSubjectVisitId,
                                                                        VisitTaskNum = latestTask.VisitTaskNum,
                                                                        TaskBlindName = visitBlindConfig.BlindFollowUpPrefix + " " + visitNumList.IndexOf(latestTask.VisitTaskNum),
                                                                        TaskName = latestTask.TaskName,

                                                                        BlindSubjectCode = latestTask.BlindSubjectCode,
                                                                        BlindTrialSiteCode = latestTask.BlindTrialSiteCode,
                                                                        IsAnalysisCreate = latestTask.IsAnalysisCreate,
                                                                        IsSelfAnalysis = latestTask.IsSelfAnalysis,
                                                                        TaskAllocationState = TaskAllocationState.Allocated,
                                                                        AllocateTime = DateTime.Now,
                                                                        DoctorUserId = task1.DoctorUserId,
                                                                        SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget),
                                                                        TrialReadingCriterionId = latestTask.TrialReadingCriterionId,
                                                                        IsNeedClinicalDataSign = latestTask.IsNeedClinicalDataSign,
                                                                        IsClinicalDataSign = latestTask.IsClinicalDataSign
                                                                    });

                                                                    currentMaxCodeInt = currentMaxCodeInt + 1;

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

                                                            }

                                                            //如果后续检查批次已分配有效 就不用处理
                                                            if (visitGroup.Any(t => t.TaskState == TaskState.Effect && t.TaskAllocationState == TaskAllocationState.Allocated && t.ArmEnum == Arm.DoubleReadingArm2))
                                                            {
                                                                //不做处理
                                                            }
                                                            else
                                                            {
                                                                var arm2 = visitGroup.FirstOrDefault(t => t.TaskState == TaskState.Effect && t.TaskAllocationState == TaskAllocationState.NotAllocate && t.DoctorUserId == null && t.ArmEnum == Arm.DoubleReadingArm2);
                                                                if (arm2 != null)
                                                                {
                                                                    //有可能仅仅只分配了一个Subject
                                                                    if (assignConfigList.Any(t => t.ArmEnum == Arm.DoubleReadingArm2) && task2 != null)
                                                                    {
                                                                        arm2.IsUrgent = visit.IsUrgent;
                                                                        arm2.TaskUrgentType = urgentType;
                                                                        arm2.IsCanEditUrgentState = isCanEdit;
                                                                        arm2.TaskAllocationState = TaskAllocationState.Allocated;
                                                                        arm2.AllocateTime = DateTime.Now;
                                                                        arm2.DoctorUserId = task2.DoctorUserId;
                                                                        arm2.SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget);
                                                                    }

                                                                }
                                                                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 = visit.IsUrgent,
                                                                        TaskUrgentType = urgentType,
                                                                        IsCanEditUrgentState = isCanEdit,

                                                                        //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 = visitBlindConfig.BlindFollowUpPrefix + " " + visitNumList.IndexOf(latestTask.VisitTaskNum),
                                                                        TaskName = latestTask.TaskName,

                                                                        BlindSubjectCode = latestTask.BlindSubjectCode,
                                                                        BlindTrialSiteCode = latestTask.BlindTrialSiteCode,
                                                                        IsAnalysisCreate = latestTask.IsAnalysisCreate,
                                                                        IsSelfAnalysis = latestTask.IsSelfAnalysis,
                                                                        TaskAllocationState = TaskAllocationState.Allocated,

                                                                        AllocateTime = DateTime.Now,
                                                                        DoctorUserId = task2.DoctorUserId,
                                                                        SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget),

                                                                        TrialReadingCriterionId = latestTask.TrialReadingCriterionId,
                                                                        IsNeedClinicalDataSign = latestTask.IsNeedClinicalDataSign,
                                                                        IsClinicalDataSign = latestTask.IsClinicalDataSign
                                                                    });

                                                                    currentMaxCodeInt = currentMaxCodeInt + 1;

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

                                                        }


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

                                                        if (assignConfigList.Any(t => t.ArmEnum == Arm.DoubleReadingArm1) && task1 != null)
                                                        {
                                                            task1.TaskAllocationState = defaultState;
                                                            //分配给对应Arm的人
                                                            task1.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == Arm.DoubleReadingArm1).DoctorUserId;
                                                            task1.AllocateTime = DateTime.Now;

                                                            task1.SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget);
                                                        }

                                                        if (assignConfigList.Any(t => t.ArmEnum == Arm.DoubleReadingArm2) && task2 != null)
                                                        {
                                                            task2.TaskAllocationState = defaultState;
                                                            task2.DoctorUserId = assignConfigList.FirstOrDefault(t => t.ArmEnum == Arm.DoubleReadingArm2).DoctorUserId;
                                                            task2.AllocateTime = DateTime.Now;
                                                            task2.SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget);
                                                        }

                                                    }


                                                    #endregion

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

                                                }

                                            }


                                        }


                                    }
                                    #endregion

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


                                    var singleTask = await _visitTaskRepository.AddAsync(new VisitTask()
                                    {
                                        TrialId = trialId,
                                        SubjectId = subjectVisit.SubjectId,
                                        IsUrgent = subjectVisit.IsUrgent,
                                        TaskBlindName = blindTaskName,
                                        TaskName = subjectVisit.VisitName,
                                        TaskUrgentType = taskUrgentType,
                                        IsCanEditUrgentState = isCanEditUrgentState,
                                        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,

                                        TrialReadingCriterionId = trialReadingCriterionConfig.TrialReadingCriterionId,
                                        IsNeedClinicalDataSign = isNeedClinicalDataSign,
                                        IsClinicalDataSign = isClinicalDataSign
                                    });


                                    currentMaxCodeInt = currentMaxCodeInt + 1;

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

                                    #region 分配

                                    if (isAssignSubjectToDoctor)
                                    {

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

                                            if (trialReadingCriterionConfig.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 (trialReadingCriterionConfig.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 && t.SourceSubjectVisit.IsLostVisit == false).OrderBy(t => t.VisitTaskNum).FirstOrDefaultAsync();


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

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


                                                                if (assignConfigList.Any(t => t.ArmEnum == Arm.SingleReadingArm))
                                                                {
                                                                    singleTask.TaskAllocationState = defaultState;

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

                                                                    singleTask.AllocateTime = DateTime.Now;

                                                                    singleTask.SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget);
                                                                }


                                                                #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 && t.SourceSubjectVisit.IsLostVisit == false).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 && t.IsAnalysisCreate == false, true)
                                                                     .WhereIf(followBackVisitTask != null, t => t.VisitTaskNum < followBackVisitTask.VisitTaskNum)
                                                                     .ToListAsync();

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

                                                                //每个检查批次去判断  是分配还是生成


                                                                foreach (var visitGroup in followVisitGroup)
                                                                {
                                                                    //如果后续检查批次已分配有效 就不用处理
                                                                    if (visitGroup.Any(t => t.TaskState == TaskState.Effect && t.TaskAllocationState == TaskAllocationState.Allocated && t.ArmEnum == Arm.SingleReadingArm))
                                                                    {
                                                                        //不做处理
                                                                    }

                                                                    else
                                                                    {
                                                                        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;
                                                                            arm.SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget);
                                                                        }
                                                                        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,
                                                                                TaskUrgentType = latestTask.TaskUrgentType,
                                                                                SourceSubjectVisitId = latestTask.SourceSubjectVisitId,
                                                                                VisitTaskNum = latestTask.VisitTaskNum,
                                                                                TaskBlindName = visitBlindConfig.BlindFollowUpPrefix + " " + visitNumList.IndexOf(latestTask.VisitTaskNum),
                                                                                TaskName = latestTask.TaskName,

                                                                                BlindSubjectCode = latestTask.BlindSubjectCode,
                                                                                BlindTrialSiteCode = latestTask.BlindTrialSiteCode,
                                                                                IsAnalysisCreate = latestTask.IsAnalysisCreate,
                                                                                IsSelfAnalysis = latestTask.IsSelfAnalysis,
                                                                                TaskAllocationState = TaskAllocationState.Allocated,
                                                                                AllocateTime = DateTime.Now,
                                                                                DoctorUserId = singleTask.DoctorUserId,
                                                                                SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget),

                                                                            TrialReadingCriterionId = latestTask.TrialReadingCriterionId,
                                                                                IsNeedClinicalDataSign = latestTask.IsNeedClinicalDataSign,
                                                                                IsClinicalDataSign = latestTask.IsClinicalDataSign

                                                                            });

                                                                            currentMaxCodeInt = currentMaxCodeInt + 1;

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


                                                                }

                                                            }
                                                            //之前有回退的    后续检查批次不生成或者分配   当前检查批次生成但是不分配出去
                                                            else
                                                            {

                                                                //不用进行额外处理
                                                            }



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


                                                            if (assignConfigList.Any(t => t.ArmEnum == Arm.SingleReadingArm))
                                                            {
                                                                singleTask.TaskAllocationState = defaultState;

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

                                                                singleTask.AllocateTime = DateTime.Now;

                                                                singleTask.SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget);
                                                            }


                                                        }

                                                        #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;

                    Guid? subjectVisitId = Guid.Empty;

                    if (reReadingVisitTask.SourceSubjectVisitId == null)
                    {
                        subjectVisitId= _readModuleRepository.Where(t=>t.Id==reReadingVisitTask.SouceReadModuleId).Select(t=>t.SubjectVisitId).FirstOrDefault();
                    }
                    else
                    {
                        subjectVisitId = reReadingVisitTask.SourceSubjectVisitId;
                    }

                    var subjectVisitUrgentInfo = await _subjectVisitRepository.Where(x => x.Id == subjectVisitId).Select(x => new
                    {
                        x.PDState,
                        x.IsEnrollmentConfirm,
                        x.IsUrgent,
                    }).FirstNotNullAsync();

                    TaskUrgentType? taskUrgent = null;

                    if (subjectVisitUrgentInfo.PDState == PDStateEnum.PDProgress)
                    {
                        taskUrgent = TaskUrgentType.PDProgress;
                    }
                    else if (subjectVisitUrgentInfo.IsEnrollmentConfirm)
                    {
                        taskUrgent = TaskUrgentType.EnrollmentConfirm;
                    }
                    else if (subjectVisitUrgentInfo.IsUrgent)
                    {
                        taskUrgent = TaskUrgentType.VisitUrgent;
                    }
                    bool isCanEditUrgent = taskUrgent == TaskUrgentType.EnrollmentConfirm || taskUrgent == TaskUrgentType.PDProgress ? false : true;
                    var newTask = await _visitTaskRepository.AddAsync(new VisitTask()
                    {

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

                        IsUrgent = reReadingVisitTask.IsAnalysisCreate?false: subjectVisitUrgentInfo.IsUrgent,
                        TaskUrgentType = reReadingVisitTask.IsAnalysisCreate ? null : taskUrgent,
                        IsCanEditUrgentState = isCanEditUrgent,
                        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,


                        TrialReadingCriterionId = reReadingVisitTask.TrialReadingCriterionId,
                        IsNeedClinicalDataSign = reReadingVisitTask.IsNeedClinicalDataSign,
                        IsClinicalDataSign = reReadingVisitTask.IsClinicalDataSign,

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

                    });

                    generateTaskCommand.Action(newTask);


                    if (reReadingVisitTask.ReadingCategory == ReadingCategory.Judge)
                    {
                        var judgeRecord = await _readingJudgeInfoRepository.Where(t => t.JudgeTaskId == reReadingVisitTask.Id).FirstOrDefaultAsync();

                        var origenalTaskIdList = new Guid[] { judgeRecord.TaskIdOne, judgeRecord.TaskIdTwo };

                        await _visitTaskRepository.BatchUpdateNoTrackingAsync(x => origenalTaskIdList.Contains(x.Id), x => new VisitTask()
                        {
                            JudgeVisitTaskId = newTask.Id
                        });

                        await _readingJudgeInfoRepository.AddAsync(new ReadingJudgeInfo()
                        {
                            JudgeTaskId = newTask.Id,
                            SubjectId = judgeRecord.SubjectId,
                            TrialId = judgeRecord.TrialId,
                            TaskIdOne = judgeRecord.TaskIdOne,
                            TaskIdTwo = judgeRecord.TaskIdTwo
                        });
                    }


                    //是否增加任务类别

                    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,
                            SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget),

                            SouceReadModuleId = task.SouceReadModuleId,
                            SourceSubjectVisitId = task.SourceSubjectVisitId,

                            ConsistentAnalysisOriginalTaskId = task.Id,
                            IsAnalysisCreate = true,
                            IsSelfAnalysis = true,

                            TrialReadingCriterionId = task.TrialReadingCriterionId,
                            BlindTrialSiteCode = task.BlindTrialSiteCode,
                            BlindSubjectCode = task.BlindSubjectCode,
                            IsNeedClinicalDataSign = task.IsNeedClinicalDataSign,
                            IsClinicalDataSign = task.IsClinicalDataSign
                        };

                        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,
                            SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget),

                            IsAnalysisCreate = true,
                            IsSelfAnalysis = false,

                            TrialReadingCriterionId = task.TrialReadingCriterionId,
                            BlindTrialSiteCode = task.BlindTrialSiteCode,
                            BlindSubjectCode = task.BlindSubjectCode,
                            IsNeedClinicalDataSign = task.IsNeedClinicalDataSign,
                            IsClinicalDataSign = task.IsClinicalDataSign

                        };

                        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 && x.IsConfirmed && x.TrialReadingCriterionId == firstTask.TrialReadingCriterionId).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,
                        IsCanEditUrgentState= firstTask.IsCanEditUrgentState,
                        SourceSubjectVisitId = firstTask.SourceSubjectVisitId,
                        SouceReadModuleId = firstTask.SouceReadModuleId,
                        TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),
                        TaskState = TaskState.Effect,
                        TaskBlindName = firstTask.TaskBlindName,
                        DoctorUserId = subjectUser == null ? null : subjectUser.DoctorUserId,
                        TaskAllocationState = subjectUser == null ? TaskAllocationState.NotAllocate : TaskAllocationState.Allocated,
                        AllocateTime = subjectUser == null ? null : DateTime.Now,
                        SuggesteFinishedTime = subjectUser == null ? null : GetSuggessFinishTime(true, UrgentType.NotUrget),

                        TrialReadingCriterionId = firstTask.TrialReadingCriterionId,

                    };
                    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).Include(x => x.TrialReadingCriterion).FirstNotNullAsync();

                    //var criterionType = await _trialReadingCriterionRepository.Where(t => t.Id == originalTaskInfo.TrialReadingCriterionId).Select(t => t.CriterionType).FirstOrDefaultAsync();

                    // 需要添加全局任务再添加
                    if (originalTaskInfo.TrialReadingCriterion.IsGlobalReading)
                    {
                        var visitNumList = _subjectVisitRepository.Where(t => t.SubjectId == originalTaskInfo.SubjectId && t.IsLostVisit == false).OrderBy(t => t.VisitNum).Select(t => t.VisitNum).ToList();
                        foreach (var item in generateTaskCommand.ReadingGenerataTaskList)
                        {
                            item.VisitNum = originalTaskInfo.VisitTaskNum;
                            var task1 = await _visitTaskRepository.AddAsync(new VisitTask()
                            {
                                TrialId = trialId,
                                SubjectId = item.SubjectId,
                                IsUrgent = originalTaskInfo.IsUrgent,
                                VisitTaskNum = item.VisitNum + ReadingCommon.TaskNumDic[ReadingCategory.Global],
                                TaskName = item.ReadingName,
                                ArmEnum = originalTaskInfo.ArmEnum,//特殊
                                TaskUrgentType= originalTaskInfo.TaskUrgentType,
                                IsCanEditUrgentState= originalTaskInfo.IsCanEditUrgentState,
                                TaskUrgentRemake = originalTaskInfo.TaskUrgentRemake,
                                DoctorUserId = originalTaskInfo.DoctorUserId,
                                AllocateTime = DateTime.Now,
                                TaskAllocationState = TaskAllocationState.Allocated,
                                SuggesteFinishedTime = GetSuggessFinishTime(true, UrgentType.NotUrget),

                                Code = currentMaxCodeInt + 1,
                                TaskBlindName = "G-"+ visitNumList.IndexOf(originalTaskInfo.VisitTaskNum),
                                SouceReadModuleId = item.ReadModuleId,
                                TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),
                                ReadingCategory = item.ReadingCategory,

                                TrialReadingCriterionId = originalTaskInfo.TrialReadingCriterionId,
                                IsNeedClinicalDataSign = IsNeedClinicalDataSign(ReadingCategory.Global, false, originalTaskInfo.TrialReadingCriterionId, clinicalDataConfirmList),
                                IsClinicalDataSign = IsClinicalDataSign(ReadingCategory.Global, false, originalTaskInfo.TrialReadingCriterionId, clinicalDataConfirmList, item.ReadModuleId, trialId)
                            });

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


                    break;
                case GenerateTaskCategory.Oncology:

                    originalTaskInfo = await _visitTaskRepository.Where(x => x.Id == generateTaskCommand.OriginalVisitId).FirstNotNullAsync();

                    //criterionType = await _trialReadingCriterionRepository.Where(t => t.Id == originalTaskInfo.TrialReadingCriterionId).Select(t => t.CriterionType).FirstOrDefaultAsync();

                    foreach (var item in generateTaskCommand.ReadingGenerataTaskList)
                    {
                        //需要 根据标准筛选
                        var oncologySubjectUser = await _subjectUserRepository.Where(x => x.SubjectId == item.SubjectId && x.ArmEnum == Arm.TumorArm && x.IsConfirmed && x.TrialReadingCriterionId == originalTaskInfo.TrialReadingCriterionId).FirstOrDefaultAsync();

                        item.VisitNum = await _readModuleRepository.Where(x => x.Id == item.ReadModuleId).Select(x => x.SubjectVisit.VisitNum).FirstOrDefaultAsync();


                        var singleTask = await _visitTaskRepository.AddAsync(new VisitTask()
                        {
                            TrialId = trialId,
                            SubjectId = item.SubjectId,
                            IsUrgent = originalTaskInfo.IsUrgent,
                            TaskName = item.ReadingName,
                            // 原任务是全局任务  加0.03 就好
                            VisitTaskNum = originalTaskInfo.VisitTaskNum + 0.03m,
                            ArmEnum = Arm.TumorArm, //特殊
                            Code = currentMaxCodeInt + 1,
                            SouceReadModuleId = item.ReadModuleId,
                            TaskBlindName = item.ReadingName,

                            DoctorUserId = oncologySubjectUser == null ? null : oncologySubjectUser.DoctorUserId,
                            AllocateTime = oncologySubjectUser == null ? null : DateTime.Now,
                            TaskAllocationState = oncologySubjectUser == null ? TaskAllocationState.NotAllocate : TaskAllocationState.Allocated,
                            SuggesteFinishedTime = oncologySubjectUser == null ? null : GetSuggessFinishTime(true, UrgentType.NotUrget),

                            TaskCode = AppSettings.GetCodeStr(currentMaxCodeInt + 1, nameof(VisitTask)),
                            ReadingCategory = item.ReadingCategory,

                            TrialReadingCriterionId = originalTaskInfo.TrialReadingCriterionId,

                            IsNeedClinicalDataSign = IsNeedClinicalDataSign(ReadingCategory.Oncology, false, originalTaskInfo.TrialReadingCriterionId, clinicalDataConfirmList),
                            IsClinicalDataSign = IsClinicalDataSign(ReadingCategory.Oncology, false, originalTaskInfo.TrialReadingCriterionId, clinicalDataConfirmList, item.ReadModuleId, trialId)

                        });

                        singleTask.AllocateTime = DateTime.Now;

                        currentMaxCodeInt = currentMaxCodeInt + 1;

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



            await _visitTaskRepository.SaveChangesAsync();
        }


    }



}
