//-------------------------------------------------------------------- // 此代码由T4模板自动生成 byzhouhang 20210918 // 生成时间 2022-07-01 15:33:04 // 对此文件的更改可能会导致不正确的行为,并且如果重新生成代码,这些更改将会丢失。 //-------------------------------------------------------------------- using IRaCIS.Core.Domain.Models; using Microsoft.AspNetCore.Mvc; using IRaCIS.Core.Application.Interfaces; using IRaCIS.Core.Application.ViewModel; using IRaCIS.Core.Infrastructure; using IRaCIS.Core.Domain.Share; using System.Linq.Expressions; using IRaCIS.Core.Infra.EFCore.Common; using System.Linq; using Nito.AsyncEx; using IRaCIS.Core.Application.Contracts; namespace IRaCIS.Core.Application.Service { /// /// 一致性分析配置表 /// [ApiExplorerSettings(GroupName = "Trial")] public class TaskConsistentRuleService : BaseService, ITaskConsistentRuleService { private readonly IRepository _taskConsistentRuleRepository; private readonly IRepository _visitTaskRepository; private readonly IRepository _subjectUserRepository; private readonly IRepository _subjectRepository; private readonly IRepository _enrollRepository; private readonly AsyncLock _mutex = new AsyncLock(); public TaskConsistentRuleService(IRepository visitTaskRepository, IRepository enrollRepository, IRepository taskConsistentRuleRepository, IRepository subjectUserRepository, IRepository subjectRepository) { _taskConsistentRuleRepository = taskConsistentRuleRepository; _visitTaskRepository = visitTaskRepository; _subjectUserRepository = subjectUserRepository; _subjectRepository = subjectRepository; _enrollRepository = enrollRepository; } /// /// 设置一致性分析任务失效 /// /// /// [HttpPost] public async Task SetAnalysisTaskInvalid(List taskIdList) { await _visitTaskRepository.BatchUpdateNoTrackingAsync(t => taskIdList.Contains(t.Id), u => new VisitTask() { TaskState = TaskState.NotEffect }); await _visitTaskRepository.SaveChangesAsync(); return ResponseOutput.Ok(); } /// /// 一致性分析列表 (自身 组内 最后勾选 产生的任务) /// /// /// [HttpPost] public async Task<(PageOutput, object?)> GetAnalysisTaskList(VisitTaskQuery queryVisitTask) { var visitTaskQueryable = _visitTaskRepository.Where(t => t.TrialId == queryVisitTask.TrialId) .Where(t => t.IsAnalysisCreate) .WhereIf(queryVisitTask.SiteId != null, t => t.Subject.SiteId == queryVisitTask.SiteId) .WhereIf(queryVisitTask.SubjectId != null, t => t.SubjectId == queryVisitTask.SubjectId) .WhereIf(queryVisitTask.TaskState != null, t => t.TaskState == queryVisitTask.TaskState) .WhereIf(queryVisitTask.IsUrgent != null, t => t.IsUrgent == queryVisitTask.IsUrgent) .WhereIf(queryVisitTask.DoctorUserId != null, t => t.DoctorUserId == queryVisitTask.DoctorUserId) .WhereIf(queryVisitTask.ReadingCategory != null, t => t.ReadingCategory == queryVisitTask.ReadingCategory) .WhereIf(queryVisitTask.ReadingTaskState != null, t => t.ReadingTaskState == queryVisitTask.ReadingTaskState) .WhereIf(queryVisitTask.TaskAllocationState != null, t => t.TaskAllocationState == queryVisitTask.TaskAllocationState) .WhereIf(queryVisitTask.IsSelfAnalysis != null, t => t.IsSelfAnalysis == queryVisitTask.IsSelfAnalysis) .WhereIf(queryVisitTask.ArmEnum != null, t => t.ArmEnum == queryVisitTask.ArmEnum) .WhereIf(!string.IsNullOrEmpty(queryVisitTask.TrialSiteCode), t => (t.BlindTrialSiteCode.Contains(queryVisitTask.TrialSiteCode!) && t.IsAnalysisCreate) || (t.Subject.TrialSite.TrialSiteCode.Contains(queryVisitTask.TrialSiteCode!) && t.IsAnalysisCreate == false)) .WhereIf(!string.IsNullOrEmpty(queryVisitTask.TaskName), t => t.TaskName.Contains(queryVisitTask.TaskName) || t.TaskBlindName.Contains(queryVisitTask.TaskName)) .WhereIf(!string.IsNullOrEmpty(queryVisitTask.SubjectCode), t => t.Subject.Code.Contains(queryVisitTask.SubjectCode)) .WhereIf(queryVisitTask.BeginAllocateDate != null, t => t.AllocateTime > queryVisitTask.BeginAllocateDate) .WhereIf(queryVisitTask.EndAllocateDate != null, t => t.AllocateTime < queryVisitTask.EndAllocateDate!.Value.AddDays(1)) .ProjectTo(_mapper.ConfigurationProvider); var defalutSortArray = new string[] { nameof(VisitTask.IsUrgent) + " desc", nameof(VisitTask.SubjectId), nameof(VisitTask.VisitTaskNum) }; var pageList = await visitTaskQueryable.ToPagedListAsync(queryVisitTask.PageIndex, queryVisitTask.PageSize, queryVisitTask.SortField, queryVisitTask.Asc, string.IsNullOrWhiteSpace(queryVisitTask.SortField), defalutSortArray); var trialTaskConfig = _repository.Where(t => t.Id == queryVisitTask.TrialId).ProjectTo(_mapper.ConfigurationProvider).FirstOrDefault(); return (pageList, trialTaskConfig); } /// /// 为自身一致性分析医生,选择Subejct 列表 /// /// /// [HttpPost] public async Task> GetDoctorSelfConsistentRuleSubjectList(ConsistentQuery inQuery) { var filterObj = await _taskConsistentRuleRepository.FirstOrDefaultAsync(t => t.Id == inQuery.TaskConsistentRuleId); var pagedList = await GetIQueryableDoctorSelfConsistentSubjectView(filterObj, inQuery.DoctorUserId).ToPagedListAsync(inQuery.PageIndex, inQuery.PageSize, string.IsNullOrWhiteSpace(inQuery.SortField) ? nameof(DoctorSelfConsistentSubjectView.SubjectCode) : inQuery.SortField, inQuery.Asc); return pagedList; } /// /// 确认生成自身一致性分析任务 /// /// /// /// [HttpPost] [UnitOfWork] public async Task ConfirmGenerateSelfConsistentTask(ConsistentConfirmGenerateCommand inCommand, [FromServices] IVisitTaskHelpeService _visitTaskCommonService) { var filterObj = await _taskConsistentRuleRepository.FirstOrDefaultAsync(t => t.Id == inCommand.TaskConsistentRuleId); var doctorUserId = inCommand.DoctorUserId; var list = await GetIQueryableDoctorSelfConsistentSubjectView(filterObj, doctorUserId, inCommand.SubejctIdList).ToListAsync(); //var (group, query) = GetIQueryableDoctorSelfConsistentRuleSubjectView(filterObj, inCommand.SubejctIdList); //var list = query.OrderByDescending(t => t.IsHaveGeneratedTask).ToList(); using (await _mutex.LockAsync()) { int maxCodeInt = 0; foreach (var subject in list) { //处理 Subject 编号 var blindSubjectCode = string.Empty; var subjectTask = _visitTaskRepository.Where(t => t.SubjectId == subject.SubjectId).OrderByDescending(t => t.BlindSubjectCode).FirstOrDefault().IfNullThrowException(); if (subjectTask.BlindSubjectCode != String.Empty) { blindSubjectCode = subjectTask.BlindSubjectCode; } else { var maxCodeStr = _visitTaskRepository.Where(t => t.TrialId == subject.TrialId).OrderByDescending(t => t.BlindSubjectCode).Select(t => t.BlindSubjectCode).FirstOrDefault(); int.TryParse(maxCodeStr, out maxCodeInt); blindSubjectCode = filterObj.BlindTrialSiteCode + (maxCodeInt + 1).ToString($"D{filterObj.BlindSubjectNumberOfPlaces}"); } subject.VisitTaskList = subject.VisitTaskList.Take(filterObj.PlanVisitCount).ToList(); subject.VisitTaskList.ForEach(t => { t.DoctorUserId = doctorUserId; //t.TaskConsistentRuleId = filterObj.Id; t.BlindTrialSiteCode = filterObj.BlindTrialSiteCode; t.BlindSubjectCode = blindSubjectCode; }); //最后一个访视添加全局 if (filterObj.IsGenerateGlobalTask) { var lastTask = (subject.VisitTaskList.Take(filterObj.PlanVisitCount).Last()).Clone(); var existGlobal = _visitTaskRepository.Where(t => t.SubjectId == lastTask.SubjectId && t.TaskState == TaskState.Effect && t.ReadingCategory == ReadingCategory.Global && t.VisitTaskNum == lastTask.VisitTaskNum + ReadingCommon.TaskNumDic[ReadingCategory.Global]).ProjectTo(_mapper.ConfigurationProvider).FirstOrDefault(); if (existGlobal == null) { existGlobal = new VisitTaskSimpleDTO() { SubjectId = lastTask.SubjectId, TrialId = lastTask.TrialId, ArmEnum = lastTask.ArmEnum, ReadingCategory = ReadingCategory.Global, TaskName = lastTask.VisitTaskNum + "Global", TaskBlindName = lastTask.VisitTaskNum + "Global" }; } existGlobal.BlindSubjectCode = lastTask.BlindSubjectCode; existGlobal.BlindTrialSiteCode = lastTask.BlindTrialSiteCode; subject.VisitTaskList.Add(existGlobal); } await _visitTaskCommonService.AddTaskAsync(new GenerateTaskCommand() { TrialId = filterObj.TrialId, ReadingCategory = GenerateTaskCategory.SelfConsistent, //产生的过滤掉已经生成的 GenerataConsistentTaskList = subject.VisitTaskList.Where(t => t.IsHaveGeneratedTask == false).ToList() }); await _visitTaskRepository.SaveChangesAsync(); } } return ResponseOutput.Ok(); } /// /// 组间一致性分析 选择Subejct 列表 /// /// /// [HttpPost] public async Task> GetGroupConsistentRuleSubjectList(GroupConsistentQuery inQuery) { var trialId = inQuery.TrialId; var filterObj = await _taskConsistentRuleRepository.FirstOrDefaultAsync(t => t.TrialId == trialId && t.IsSelfAnalysis == false); if (filterObj == null) { return new PageOutput(); } var query = await GetGroupConsistentQueryAsync(filterObj); var pagedList = await query.ToPagedListAsync(inQuery.PageIndex, inQuery.PageSize, string.IsNullOrWhiteSpace(inQuery.SortField) ? nameof(DoctorSelfConsistentSubjectView.SubjectCode) : inQuery.SortField, inQuery.Asc); return pagedList; } /// /// 确认生成组间一致性分析任务 /// /// /// /// [HttpPost] [UnitOfWork] public async Task ConfirmGenerateGroupConsistentTask(GroupConsistentConfirmGenrateCommand inCommand, [FromServices] IVisitTaskHelpeService _visitTaskCommonService) { var trialId = inCommand.TrialId; var filterObj = await _taskConsistentRuleRepository.FirstOrDefaultAsync(t => t.TrialId == trialId && t.IsSelfAnalysis == false); var query = await GetGroupConsistentQueryAsync(filterObj, inCommand.SubejctIdList); var subjectList = query.ToList(); var doctorUserIdQuery = from enroll in _repository.Where(t => t.TrialId == trialId).Where(t => t.EnrollReadingCategoryList.Any(c => c.ReadingCategory == ReadingCategory.Global || c.ReadingCategory == ReadingCategory.Visit)) join user in _repository.Where() on enroll.DoctorId equals user.DoctorId select user.Id; var configDoctorUserIdList = await doctorUserIdQuery.ToListAsync(); foreach (var subject in subjectList.Where(t => t.IsHaveGeneratedTask == false)) { var subjectAddTaskList = new List(); //需要处理的医生 var needAddDoctorUserIdList = configDoctorUserIdList.Except(subject.VisitTaskList.Select(t => (Guid)t.DoctorUserId)).ToList(); if (needAddDoctorUserIdList.Count == 0) { throw new BusinessValidationFailedException("请配置一致性分析的医生"); } foreach (var needAddDoctorUserId in needAddDoctorUserIdList) { //每个医生 都生成处理的任务 foreach (var task in subject.SubjectTaskVisitList.Take(filterObj.PlanVisitCount)) { subjectAddTaskList.Add(new VisitTaskGroupSimpleDTO() { ReadingCategory = task.ReadingCategory, ReadingTaskState = task.ReadingTaskState, TaskBlindName = task.TaskBlindName, TaskName = task.TaskName, TaskState = task.TaskState, SubjectId = task.SubjectId, VisitTaskNum = task.VisitTaskNum, TrialId = task.TrialId, DoctorUserId = needAddDoctorUserId, ArmEnum = Arm.GroupConsistentArm, SouceReadModuleId = task.SouceReadModuleId, SourceSubjectVisitId = task.SourceSubjectVisitId, }); } //最后一个访视添加全局 if (filterObj.IsGenerateGlobalTask) { var lastTask = (subjectAddTaskList.Take(filterObj.PlanVisitCount).Last()).Clone(); var existGlobal = _visitTaskRepository.Where(t => t.SubjectId == lastTask.SubjectId && t.TaskState == TaskState.Effect && t.ReadingCategory == ReadingCategory.Global && t.VisitTaskNum == lastTask.VisitTaskNum + ReadingCommon.TaskNumDic[ReadingCategory.Global]).ProjectTo(_mapper.ConfigurationProvider).FirstOrDefault(); if (existGlobal == null) { existGlobal = new VisitTaskSimpleDTO() { SubjectId = lastTask.SubjectId, TrialId = lastTask.TrialId, ReadingCategory = ReadingCategory.Global, TaskBlindName = lastTask.VisitTaskNum + "Global", TaskName = lastTask.VisitTaskNum + "Global" }; } existGlobal.ArmEnum = Arm.GroupConsistentArm; existGlobal.DoctorUserId = needAddDoctorUserId; subjectAddTaskList.Add(existGlobal); } } await _visitTaskCommonService.AddTaskAsync(new GenerateTaskCommand() { TrialId = filterObj.TrialId, ReadingCategory = GenerateTaskCategory.GroupConsistent, GenerataGroupConsistentTaskList = subjectAddTaskList }); await _taskConsistentRuleRepository.SaveChangesAsync(); } return ResponseOutput.Ok(); } /// /// 仅仅自身一致性时使用( /// /// /// /// /// private IQueryable GetIQueryableDoctorSelfConsistentSubjectView(TaskConsistentRule filterObj, Guid doctorUserId, List? subejctIdList = null) { var trialId = filterObj.TrialId; #region Subejct 维度 Expression> comonTaskFilter = u => u.TrialId == trialId && u.IsAnalysisCreate == false && u.TaskState == TaskState.Effect && u.ReadingTaskState == ReadingTaskState.HaveSigned && u.SignTime!.Value.AddDays(filterObj.IntervalWeeks * 7) < DateTime.Now && (u.ReReadingApplyState == ReReadingApplyState.Default || u.ReReadingApplyState == ReReadingApplyState.Reject) && u.DoctorUserId == doctorUserId; if (subejctIdList != null && subejctIdList?.Count > 0) { comonTaskFilter = comonTaskFilter.And(t => subejctIdList.Contains(t.SubjectId)); } Expression> visitTaskFilter = comonTaskFilter.And(t => t.ReadingCategory == ReadingCategory.Visit); ////所选访视数量 的访视 其中必有一个访视后有全局任务 //if (filterObj.IsHaveReadingPeriod == true) //{ // //这里的过滤条件 不能用 where(comonTaskFilter) 会报错,奇怪的问题 只能重新写一遍 // visitTaskFilter = visitTaskFilter.And(c => c.Subject.SubjectVisitTaskList.Any(t => t.VisitTaskNum == c.VisitTaskNum + ReadingCommon.TaskNumDic[ReadingCategory.Global] && t.ReadingCategory == ReadingCategory.Global && t.IsAnalysisCreate == false && t.TaskState == TaskState.Effect && t.ReadingTaskState == ReadingTaskState.HaveSigned && // t.SignTime!.Value.AddDays(filterObj.IntervalWeeks * 7) < DateTime.Now && (t.ReReadingApplyState == ReReadingApplyState.Default || t.ReReadingApplyState == ReReadingApplyState.Reject))); //} var subjectQuery = _subjectRepository.Where(t => t.TrialId == trialId && t.SubjectVisitTaskList.AsQueryable().Where(visitTaskFilter).Count() >= filterObj.PlanVisitCount) .WhereIf(filterObj.IsHaveReadingPeriod == true, u => u.SubjectVisitTaskList.AsQueryable().Where(comonTaskFilter).Where(t => t.ReadingCategory == ReadingCategory.Visit || t.ReadingCategory == ReadingCategory.Global).OrderBy(t => t.VisitTaskNum).Take(filterObj.PlanVisitCount + 1).Any(t => t.ReadingCategory == ReadingCategory.Global)) ; var query = subjectQuery.Select(t => new DoctorSelfConsistentSubjectView() { TrialId = t.TrialId, SiteId = t.SiteId, SubjectCode = t.Code, TrialSiteCode = t.TrialSite.TrialSiteCode, SubjectId = t.Id, BlindSubjectCode = t.SubjectVisitTaskList.Where(t => t.IsAnalysisCreate).OrderByDescending(t => t.BlindSubjectCode).Select(t => t.BlindSubjectCode).FirstOrDefault(), IsHaveGeneratedTask = t.SubjectVisitTaskList.Any(c => c.DoctorUserId == doctorUserId && c.IsSelfAnalysis == true), ValidVisitCount = t.SubjectVisitTaskList.AsQueryable().Where(visitTaskFilter).Count(), VisitTaskList = t.SubjectVisitTaskList.AsQueryable().Where(visitTaskFilter).OrderBy(t => t.VisitTaskNum).Select(c => new VisitTaskSimpleDTO() { Id = c.Id, ReadingCategory = c.ReadingCategory, ReadingTaskState = c.ReadingTaskState, TaskBlindName = c.TaskBlindName, TaskCode = c.TaskCode, TaskName = c.TaskName, TaskState = c.TaskState, ArmEnum = c.ArmEnum, SubjectId = c.SubjectId, VisitTaskNum = c.VisitTaskNum, TrialId = c.TrialId, SourceSubjectVisitId = c.SourceSubjectVisitId, SouceReadModuleId = c.SouceReadModuleId, //自身一致性才有意义 //IsHaveGeneratedTask = c.Subject.SubjectVisitTaskList.Any(t => t.ConsistentAnalysisOriginalTaskId == c.Id), GlobalVisitTaskList = c.Subject.SubjectVisitTaskList.AsQueryable().Where(comonTaskFilter).Where(t => t.VisitTaskNum == c.VisitTaskNum + ReadingCommon.TaskNumDic[ReadingCategory.Global]).Select(c => new VisitTaskSimpleDTO() { Id = c.Id, ReadingCategory = c.ReadingCategory, ReadingTaskState = c.ReadingTaskState, TaskBlindName = c.TaskBlindName, TaskCode = c.TaskCode, TaskName = c.TaskName, TaskState = c.TaskState, ArmEnum = c.ArmEnum, SubjectId = c.SubjectId, VisitTaskNum = c.VisitTaskNum, TrialId = c.TrialId, SourceSubjectVisitId = c.SourceSubjectVisitId, SouceReadModuleId = c.SouceReadModuleId, }).ToList(), }).ToList() }); return query.OrderByDescending(t => t.IsHaveGeneratedTask); #endregion } private async Task> GetGroupConsistentQueryAsync(TaskConsistentRule filterObj, List? subejctIdList = null) { var trialId = filterObj.TrialId; var trialConfig = (await _repository.Where(t => t.Id == trialId).Select(t => new { TrialId = t.Id, t.ReadingType, t.IsReadingTaskViewInOrder }).FirstOrDefaultAsync()).IfNullThrowException(); Expression> comonTaskFilter = u => u.TrialId == trialId && u.IsAnalysisCreate == false && u.TaskState == TaskState.Effect && u.ReadingTaskState == ReadingTaskState.HaveSigned && (u.ReReadingApplyState == ReReadingApplyState.Default || u.ReReadingApplyState == ReReadingApplyState.Reject); if (subejctIdList != null && subejctIdList?.Count > 0) { comonTaskFilter = comonTaskFilter.And(t => subejctIdList.Contains(t.SubjectId)); } Expression> visitTaskFilter = comonTaskFilter.And(t => t.ReadingCategory == ReadingCategory.Visit); ////所选访视数量 的访视 其中必有一个访视后有全局任务 //if (filterObj.IsHaveReadingPeriod == true) //{ // //visitTaskFilter = visitTaskFilter.And(t => t.Subject.SubjectVisitTaskList.AsQueryable().Where(comonTaskFilter).Any(u => u.VisitTaskNum == t.VisitTaskNum + ReadingCommon.TaskNumDic[ReadingCategory.Global] && u.ReadingCategory == ReadingCategory.Global)); // //这里的过滤条件 不能用 where(comonTaskFilter) 会报错,奇怪的问题 只能重新写一遍 // visitTaskFilter = visitTaskFilter.And(c => c.Subject.SubjectVisitTaskList.Any(t => t.VisitTaskNum == c.VisitTaskNum + ReadingCommon.TaskNumDic[ReadingCategory.Global] && t.ReadingCategory == ReadingCategory.Global && t.IsAnalysisCreate == false && t.TaskState == TaskState.Effect && t.ReadingTaskState == ReadingTaskState.HaveSigned && // t.SignTime!.Value.AddDays(filterObj.IntervalWeeks * 7) < DateTime.Now && (t.ReReadingApplyState == ReReadingApplyState.Default || t.ReReadingApplyState == ReReadingApplyState.Reject))); //} IQueryable subjectQuery = default; //单重阅片没有组件一致性 subjectQuery = _subjectRepository.Where(t => t.TrialId == trialId && t.SubjectVisitTaskList.AsQueryable().Where(comonTaskFilter).Where(t => t.ReadingCategory == ReadingCategory.Visit || t.ReadingCategory == ReadingCategory.Global).Select(t => t.DoctorUserId).Distinct().Count() == 2 && t.SubjectVisitTaskList.AsQueryable().Where(visitTaskFilter).GroupBy(t => new { t.SubjectId, t.VisitTaskNum }).Where(g => g.Count() == 2).Count() >= filterObj.PlanVisitCount ) .WhereIf(filterObj.IsHaveReadingPeriod == true, u => u.SubjectVisitTaskList.AsQueryable().Where(comonTaskFilter).Where(t => t.ReadingCategory == ReadingCategory.Visit || t.ReadingCategory == ReadingCategory.Global).OrderBy(t => t.VisitTaskNum).Take(filterObj.PlanVisitCount * 2 + 2).Any(t => t.ReadingCategory == ReadingCategory.Global)) ; var query = subjectQuery.Select(t => new DoctorGroupConsistentSubjectView() { TrialId = t.TrialId, SiteId = t.SiteId, SubjectCode = t.Code, TrialSiteCode = t.TrialSite.TrialSiteCode, SubjectId = t.Id, IsHaveGeneratedTask = t.SubjectVisitTaskList.Any(c => c.IsSelfAnalysis == false), ValidVisitCount = t.SubjectVisitTaskList.AsQueryable().Where(visitTaskFilter).GroupBy(t => new { t.SubjectId, t.VisitTaskNum }).Where(g => g.Count() == 2).Count(), VisitTaskList = t.SubjectVisitTaskList.AsQueryable().Where(visitTaskFilter) .Select(c => new VisitTaskGroupSimpleDTO() { ReadingCategory = c.ReadingCategory, ReadingTaskState = c.ReadingTaskState, TaskBlindName = c.TaskBlindName, TaskName = c.TaskName, TaskState = c.TaskState, SubjectId = c.SubjectId, VisitTaskNum = c.VisitTaskNum, TrialId = c.TrialId, DoctorUserId = c.DoctorUserId, SourceSubjectVisitId = c.SourceSubjectVisitId, SouceReadModuleId = c.SouceReadModuleId, }).ToList() // }); query = query.OrderByDescending(t => t.IsHaveGeneratedTask); return query; } [HttpPost] public async Task GetConsistentRule(TaskConsistentRuleQuery inQuery) { return await _taskConsistentRuleRepository.Where(t => t.TrialId == inQuery.TrialId && t.IsSelfAnalysis == inQuery.IsSelfAnalysis).ProjectTo(_mapper.ConfigurationProvider).FirstOrDefaultAsync(); } /// /// 自身一致性分配 配置+ 统计已经生成数量统计表 /// /// /// [HttpPost] public async Task> GetSelfConsistentDoctorStatList(TaskConsistentRuleQuery inQuery) { var trialId = inQuery.TrialId; var taskConsistentRuleQueryable = from enroll in _repository.Where(t => t.TrialId == trialId) join user in _repository.Where() on enroll.DoctorId equals user.DoctorId join taskConsistentRule in _repository.Where(t => t.TrialId == trialId && t.IsSelfAnalysis) on enroll.TrialId equals taskConsistentRule.TrialId select new TaskConsistentRuleView() { Id = taskConsistentRule.Id, CreateTime = taskConsistentRule.CreateTime, BlindTrialSiteCode = taskConsistentRule.BlindTrialSiteCode, BlindSubjectNumberOfPlaces = taskConsistentRule.BlindSubjectNumberOfPlaces, CreateUserId = taskConsistentRule.CreateUserId, IntervalWeeks = taskConsistentRule.IntervalWeeks, IsEnable = taskConsistentRule.IsEnable, PlanSubjectCount = taskConsistentRule.PlanSubjectCount, Note = taskConsistentRule.Note, TrialId = taskConsistentRule.TrialId, UpdateTime = taskConsistentRule.UpdateTime, UpdateUserId = taskConsistentRule.UpdateUserId, IsGenerateGlobalTask = taskConsistentRule.IsGenerateGlobalTask, IsHaveReadingPeriod = taskConsistentRule.IsHaveReadingPeriod, PlanVisitCount = taskConsistentRule.PlanVisitCount, GeneratedSubjectCount = taskConsistentRule.Trial.VisitTaskList.Where(t => t.IsAnalysisCreate && t.IsSelfAnalysis == true && t.DoctorUserId == user.Id).Select(t => t.SubjectId).Distinct().Count(), AnalysisDoctorUser = new UserSimpleInfo() { UserId = user.Id, UserCode = user.UserCode, FullName = user.FullName, UserName = user.UserName } }; //if (await _taskConsistentRuleRepository.AnyAsync(t => t.TrialId == inQuery.TrialId)) //{ // var rule = await _taskConsistentRuleRepository.Where(t => t.TrialId == inQuery.TrialId).ProjectTo(_mapper.ConfigurationProvider).FirstAsync(); // rule.IsBatchAdd = true; // await BatchAddOrUpdateTaskConsistentRule(rule); //} //#endregion //var taskConsistentRuleQueryable = _taskConsistentRuleRepository.Where(t => t.TrialId == inQuery.TrialId) // .ProjectTo(_mapper.ConfigurationProvider); return await taskConsistentRuleQueryable.ToListAsync(); } public async Task AddOrUpdateTaskConsistentRule(TaskConsistentRuleAddOrEdit addOrEditTaskConsistentRule) { var verifyExp1 = new EntityVerifyExp() { VerifyExp = t => t.TrialId == addOrEditTaskConsistentRule.TrialId && t.IsSelfAnalysis == addOrEditTaskConsistentRule.IsSelfAnalysis, VerifyMsg = "已有该项目配置,不允许继续增加" }; if (await _visitTaskRepository.AnyAsync(t => t.IsSelfAnalysis == addOrEditTaskConsistentRule.IsSelfAnalysis && t.TrialId == addOrEditTaskConsistentRule.TrialId)) { return ResponseOutput.NotOk("已有Subject 生成了任务,不允许修改配置"); } var entity = await _taskConsistentRuleRepository.InsertOrUpdateAsync(addOrEditTaskConsistentRule, true, verifyExp1); return ResponseOutput.Ok(entity.Id.ToString()); } [HttpDelete("{taskConsistentRuleId:guid}")] public async Task DeleteTaskConsistentRule(Guid taskConsistentRuleId) { var config = await _taskConsistentRuleRepository.FirstOrDefaultAsync(t => t.Id == taskConsistentRuleId); if (await _visitTaskRepository.AnyAsync(t => t.IsAnalysisCreate && t.TrialId == config.TrialId && t.IsSelfAnalysis == config.IsSelfAnalysis)) { throw new BusinessValidationFailedException("已产生一致性分析任务,不允许删除"); } var success = await _taskConsistentRuleRepository.DeleteFromQueryAsync(t => t.Id == taskConsistentRuleId, true); return ResponseOutput.Ok(); } } }