using IRaCIS.Application.Interfaces; using IRaCIS.Core.Infra.EFCore; using IRaCIS.Core.Domain.Share; using IRaCIS.Core.Application.Filter; using Microsoft.AspNetCore.Mvc; using IRaCIS.Core.Application.Service.WorkLoad.DTO; using Microsoft.AspNetCore.Authorization; using IRaCIS.Core.Application.Auth; using IRaCIS.Core.Application.Service.Reading.Dto; using MassTransit; using IRaCIS.Core.Application.Service.Reading; using IRaCIS.Core.Infra.EFCore.Common; using Panda.DynamicWebApi.Attributes; using IRaCIS.Core.Application.Contracts; namespace IRaCIS.Application.Services { /// /// 阅片问题.标准 /// [ApiExplorerSettings(GroupName = "Reading")] public class ReadingQuestionService : BaseService, IReadingQuestionService { private readonly IRepository _subjectVisitRepository; private readonly IRepository _readingQuestionCriterionSystemRepository; private readonly IRepository _readingQuestionCriterionTrialRepository; private readonly IRepository _readingQuestionSystemRepository; private readonly IRepository _readingQuestionTrialRepository; private readonly IRepository _clinicalDataTrialSetRepository; private readonly IRepository _clinicalDataSystemSetRepository; private readonly IRepository _dictionaryRepository; private readonly IRepository _readingTaskQuestionAnswer; private readonly IRepository _previousPDFRepository; public ReadingQuestionService( IRepository subjectVisitRepository, IRepository readingQuestionCriterionSystemRepository, IRepository readingQuestionCriterionTrialRepository, IRepository readingQuestionSystemRepository, IRepository readingQuestionTrialRepository, IRepository ClinicalDataTrialSetRepository, IRepository ClinicalDataSystemSetRepository, IRepository dictionaryRepository, IRepository readingTaskQuestionAnswer, IRepository previousPDFRepository ) { this._subjectVisitRepository = subjectVisitRepository; this._readingQuestionCriterionSystemRepository = readingQuestionCriterionSystemRepository; this._readingQuestionCriterionTrialRepository = readingQuestionCriterionTrialRepository; this._readingQuestionSystemRepository = readingQuestionSystemRepository; this._readingQuestionTrialRepository = readingQuestionTrialRepository; this._clinicalDataTrialSetRepository = ClinicalDataTrialSetRepository; this._clinicalDataSystemSetRepository = ClinicalDataSystemSetRepository; this._dictionaryRepository = dictionaryRepository; this._readingTaskQuestionAnswer = readingTaskQuestionAnswer; this._previousPDFRepository = previousPDFRepository; } ///// ///// 新增修改系统问题标准 ///// ///// ///// //[HttpPost] //public async Task AddOrUpdateReadingQuestionCriterionSystem(AddOrUpdateReadingQuestionCriterionSystemInDto indto) //{ // var entity = await _readingQuestionCriterionSystemRepository.InsertOrUpdateAsync(indto,true); // return ResponseOutput.Ok(entity.Id.ToString()); //} /// /// 获取系统问题标准 /// /// [HttpPost] public async Task> GetReadingQuestionCriterionSystemList(ReadingQuestionCriterionSystemViewInDto inDto) { await AddSystemQuestionCriterion(); var query= _readingQuestionCriterionSystemRepository.AsQueryable() .WhereIf(!inDto.CriterionName.IsNullOrEmpty(), x => x.CriterionName.Contains(inDto.CriterionName)) .ProjectTo(_mapper.ConfigurationProvider) .Where(x=>x.IsEnable); return await query.ToPagedListAsync(inDto.PageIndex, inDto.PageSize, inDto.SortField.IsNullOrEmpty() ? nameof(ReadingQuestionCriterionSystemView.ShowOrder) : inDto.SortField, inDto.Asc); } /// /// 设置系统标准被禁用 /// /// [NonDynamicMethod] public async Task SetSystemCriterionDisable(Guid dictionaryId, Guid? parentId) { // 判断是否是阅片 if (await _dictionaryRepository.AnyAsync(x => x.Id == parentId && x.Code == "ReadingStandard")) { // 判断当前阅片是否在项目里面存在 var systemCriterion = await _readingQuestionCriterionSystemRepository.FirstOrDefaultAsync(x => x.CriterionId == dictionaryId); if (systemCriterion != null) { var trialCriterionIds = await _readingQuestionCriterionTrialRepository.Where(x => x.ReadingQuestionCriterionSystemId == systemCriterion.Id).Select(x=>x.ReadingQuestionCriterionSystemId).ToListAsync(); await _readingQuestionCriterionTrialRepository.BatchDeleteNoTrackingAsync(x => x.ReadingQuestionCriterionSystemId == systemCriterion.Id); await _readingQuestionTrialRepository.BatchDeleteNoTrackingAsync(x => trialCriterionIds.Contains(x.ReadingQuestionCriterionTrialId)); return ResponseOutput.Ok(); } } return ResponseOutput.Ok(); } ///// ///// 设置系统问题标准是否生效 ///// ///// ///// //public async Task SetSystemReadingQuestionCriterionIsIsEnable(SetSystemReadingQuestionCriterionIsIsEnable inDto) //{ // await _readingQuestionCriterionSystemRepository.UpdatePartialFromQueryAsync(inDto.Id, x => new ReadingQuestionCriterionSystem() // { // IsEnable = inDto.IsEnable // }); // var result =await _readingQuestionCriterionSystemRepository.SaveChangesAsync(); // return ResponseOutput.Ok(result); //} /// /// 设置系统问题标准是否完成配置 /// /// /// public async Task SetSystemReadingQuestionCriterionIsCompleteConfig(SetSystemReadingQuestionCriterionIsIsCompleteConfig inDto) { //if (!inDto.IsCompleteConfig && await _readingQuestionCriterionTrialRepository.AnyAsync(x => x.ReadingQuestionCriterionSystemId == inDto.Id)) //{ // ResponseOutput.NotOk("当前标准已经运用到项目,操作失败"); //} if (!inDto.IsCompleteConfig) { var trialCriterionIds =await _readingQuestionCriterionTrialRepository.Where(x => x.ReadingQuestionCriterionSystemId == inDto.Id).Select(x => x.Id).ToListAsync(); if (await _readingTaskQuestionAnswer.AnyAsync(x => trialCriterionIds.Contains(x.ReadingQuestionCriterionTrialId))) { return ResponseOutput.NotOk("此标准在项目里面已被使用,操作失败"); } } await _readingQuestionCriterionSystemRepository.UpdatePartialFromQueryAsync(inDto.Id, x => new ReadingQuestionCriterionSystem() { IsCompleteConfig = inDto.IsCompleteConfig }); if (inDto.IsCompleteConfig) { await UpdateTrialCriterion(inDto.Id); } var result = await _readingQuestionCriterionSystemRepository.SaveChangesAsync(); return ResponseOutput.Ok(result); } /// /// 更改项目标准 /// /// /// private async Task UpdateTrialCriterion(Guid systemCriterionId) { await _readingQuestionCriterionTrialRepository.BatchUpdateNoTrackingAsync(x => x.ReadingQuestionCriterionSystemId == systemCriterionId, x => new ReadingQuestionCriterionTrial() { IsCompleteConfig = true, }); var systemCriterionQuestionList = await _readingQuestionSystemRepository.Where(x => x.ReadingQuestionCriterionSystemId == systemCriterionId).ToListAsync(); var trialCriterionList = await _readingQuestionCriterionTrialRepository.Where(x => x.ReadingQuestionCriterionSystemId == systemCriterionId).Select(x => new { x.Id, x.TrialId, }).ToListAsync(); var trialCriterionIdList = trialCriterionList.Select(x => x.Id).ToList(); List needAddQuestionList = new List(); foreach (var item in trialCriterionList) { await _readingQuestionTrialRepository.BatchDeleteNoTrackingAsync(x => trialCriterionIdList.Contains(x.ReadingQuestionCriterionTrialId)); List trialQuestionList = new List(); SetChildParentQuestion(item.Id, item.TrialId, systemCriterionQuestionList, trialQuestionList); needAddQuestionList.AddRange(trialQuestionList); } await _readingQuestionTrialRepository.AddRangeAsync(needAddQuestionList); } /// /// 添加系统qc问题标准 /// /// [NonDynamicMethod] private async Task AddSystemQuestionCriterion() { var useSystemQuestionCriterionIds = _readingQuestionCriterionSystemRepository.Select(x => x.CriterionId); var dictionaryParentId =await _dictionaryRepository.Where(x => x.Code == "ReadingStandard").Select(x => x.Id).FirstOrDefaultAsync(); var criterionList = await _dictionaryRepository.Where(x => x.ParentId == dictionaryParentId && !useSystemQuestionCriterionIds.Contains(x.Id)) .Select(x => new CriterionList() { Id = x.Id, Value = x.Value, ShowOrder=x.ShowOrder, }).ToListAsync(); List needAddCriterionList = new List(); criterionList.ForEach(x => { needAddCriterionList.Add(new ReadingQuestionCriterionSystem() { CriterionId = x.Id, ShowOrder=x.ShowOrder, CriterionName = x.Value, IsEnable = false, }); }); await _readingQuestionCriterionSystemRepository.AddRangeAsync(needAddCriterionList); await _readingQuestionCriterionSystemRepository.SaveChangesAsync(); } ///// ///// 删除系统问题标准 ///// ///// ///// //[HttpDelete("{readingQuestionCriterionSystemId:guid}")] //public async Task DeleteReadingQuestionCriterionSystem(Guid readingQuestionCriterionSystemId) //{ // await _readingQuestionCriterionSystemRepository.DeleteFromQueryAsync(t => t.Id == readingQuestionCriterionSystemId); // var success = await _readingQuestionCriterionSystemRepository.SaveChangesAsync(); // return ResponseOutput.Result(success); //} /// /// 新增修改系统问题 /// /// /// [HttpPost] public async Task AddOrUpdateReadingQuestionSystem(AddOrUpdateReadingQuestionSystemInDto indto) { var entity = await _readingQuestionSystemRepository.InsertOrUpdateAsync(indto, true); return ResponseOutput.Ok(entity.Id.ToString()); } /// /// 获取系统问题 /// /// [HttpPost] public async Task> GetReadingQuestionSystemList(ReadingQuestionSystemViewInDto inDto) { var query = _readingQuestionSystemRepository.AsQueryable() .Where(x=>x.ReadingQuestionCriterionSystemId==inDto.ReadingQuestionCriterionSystemId) .WhereIf(!inDto.QuestionName.IsNullOrEmpty(), x => x.QuestionName.Contains(inDto.QuestionName)) .WhereIf(!inDto.Type.IsNullOrEmpty(), x => x.Type.Contains(inDto.Type)) .ProjectTo(_mapper.ConfigurationProvider); return await query.ToPagedListAsync(inDto.PageIndex, inDto.PageSize, inDto.SortField == null ? nameof(ReadingQuestionSystemView.QuestionName) : inDto.SortField, inDto.Asc); } /// /// 获取系统标准的其他问题 /// /// /// [HttpPost] public async Task> GetSystemCriterionOtherQuestion(GetSystemCriterionOtherQuestionInDto inDto) { var types = new List() { "select","radio" }; var questionList =await _readingQuestionSystemRepository.Where(x => x.ReadingQuestionCriterionSystemId == inDto.ReadingQuestionCriterionSystemId) .Where(x=> types.Contains(x.Type)) .WhereIf(inDto.Id != null, x => x.Id != inDto.Id&&x.ParentId!= inDto.Id) .Select(x => new CriterionOtherQuestionOutDto() { QuestionId = x.Id, QuestionName = x.QuestionName, TypeValue=x.TypeValue, GroupName = x.GroupName, }).ToListAsync(); return questionList; } /// /// 删除系统问题 /// /// /// [HttpDelete("{id:guid}")] public async Task DeleteReadingQuestionSystem(Guid id) { if (await _readingQuestionSystemRepository.AnyAsync(x => x.ParentId == id)) { return ResponseOutput.NotOk("此问题存在子问题,请先删除子问题"); } await _readingQuestionSystemRepository.DeleteFromQueryAsync(t => t.Id == id); var success = await _readingQuestionSystemRepository.SaveChangesAsync(); return ResponseOutput.Result(success); } /// /// 添加系统数据到项目里面 /// /// [NonDynamicMethod] private async Task AddSystemDataToTrila(Guid trialId) { var trialUsrSystemIds = _readingQuestionCriterionTrialRepository.Where(x => x.TrialId == trialId && x.ReadingQuestionCriterionSystemId != null) .Select(x => x.ReadingQuestionCriterionSystemId); var needAddSystemDataList = await _readingQuestionCriterionSystemRepository.Where(x => !trialUsrSystemIds.Contains(x.Id)&&x.Dictionary.IsEnable).Include(x=>x.ReadingQuestionSystemList).ToListAsync(); List needAddCriterionList = new List(); List needAddQuestionList = new List(); needAddSystemDataList.ForEach(x => { var criterion = new ReadingQuestionCriterionTrial() { CriterionName = x.CriterionName, IsEnable = false, ReadingQuestionCriterionSystemId = x.Id, TrialId = trialId, IsCompleteConfig=x.IsCompleteConfig, Id = NewId.NextGuid(), }; List systemQuestionList = x.ReadingQuestionSystemList.Clone(); List readingQuestionTrialList = new List(); SetChildParentQuestion(criterion.Id, trialId, systemQuestionList, readingQuestionTrialList); needAddCriterionList.Add(criterion); needAddQuestionList.AddRange(readingQuestionTrialList); }); await _readingQuestionCriterionTrialRepository.AddRangeAsync(needAddCriterionList); await _readingQuestionTrialRepository.AddRangeAsync(needAddQuestionList); await _readingQuestionTrialRepository.SaveChangesAsync(); } /// /// 设置父子关系 /// /// 项目标准ID /// 项目Id /// 系统问题 /// 需要添加list private void SetChildParentQuestion(Guid ReadingQuestionCriterionTrialId, Guid trialId, List systemQuesitonList,List needQuestionList) { var parentIdIsNullList = systemQuesitonList.Where(x => x.ParentId == null).ToList(); parentIdIsNullList.ForEach(x => { var quesiton = x.Clone(); var oldId = quesiton.Id; var newId = NewId.NextGuid(); needQuestionList.Add(new ReadingQuestionTrial() { Id = newId, ShowOrder = quesiton.ShowOrder, IsEnable = quesiton.IsEnable, IsRequired = quesiton.IsRequired, ParentTriggerValue = quesiton.ParentTriggerValue, QuestionName = quesiton.QuestionName, ReadingQuestionCriterionTrialId = ReadingQuestionCriterionTrialId, TrialId = trialId, AnswerGroup = string.Empty, Type = quesiton.Type, GroupName=quesiton.GroupName, ReadingQuestionSystemId=x.Id, IsJudgeQuestion =quesiton.IsJudgeQuestion, Remark=quesiton.Remark, TypeValue = quesiton.TypeValue, }); CreateQuestionRelation(ReadingQuestionCriterionTrialId, trialId, oldId, newId, systemQuesitonList, needQuestionList); }); } /// /// 递归处理父子关系 /// /// /// /// /// /// /// private void CreateQuestionRelation(Guid ReadingQuestionCriterionTrialId, Guid trialId,Guid oldParentId,Guid newParentId, List systemQuesitonList, List needQuestionList) { var childList = systemQuesitonList.Where(x => x.ParentId == oldParentId).ToList(); childList.ForEach(x => { var quesiton = x.Clone(); var oldId = quesiton.Id; var newId = NewId.NextGuid(); needQuestionList.Add(new ReadingQuestionTrial() { Id = newId, ShowOrder = quesiton.ShowOrder, IsEnable = quesiton.IsEnable, IsRequired = quesiton.IsRequired, ParentId = newParentId, ReadingQuestionSystemId = x.Id, AnswerGroup = string.Empty, ParentTriggerValue = quesiton.ParentTriggerValue, QuestionName = quesiton.QuestionName, ReadingQuestionCriterionTrialId = ReadingQuestionCriterionTrialId, TrialId = trialId, GroupName = quesiton.GroupName, Type = quesiton.Type, IsJudgeQuestion = quesiton.IsJudgeQuestion, Remark = quesiton.Remark, TypeValue = quesiton.TypeValue, }) ; CreateQuestionRelation(ReadingQuestionCriterionTrialId, trialId, oldId, newId, systemQuesitonList, needQuestionList); }); } /// /// 新增修改项目问题标准(项目) /// /// /// [HttpPost] public async Task AddOrUpdateReadingQuestionCriterionTrial(AddOrUpdateReadingQuestionCriterionTrialInDto indto) { var entity = await _readingQuestionCriterionTrialRepository.InsertOrUpdateAsync(indto, true); return ResponseOutput.Ok(entity.Id.ToString()); } ///// ///// 设置项目标准是否生效 ///// ///// ///// //public async Task SetTrialReadingQuestionCriterionIsIsEnable(SetSystemReadingQuestionCriterionIsIsEnable inDto) //{ // await _readingQuestionCriterionTrialRepository.UpdatePartialFromQueryAsync(inDto.Id, x => new ReadingQuestionCriterionTrial() // { // IsEnable = inDto.IsEnable // }); // var result = await _readingQuestionCriterionTrialRepository.SaveChangesAsync(); // return ResponseOutput.Ok(result); //} /// /// 设置项目标准是否完成配置 /// /// /// public async Task SetTrialReadingQuestionCriterionIsIsCompleteConfig(SetSystemReadingQuestionCriterionIsIsCompleteConfig inDto) { await _readingQuestionCriterionTrialRepository.UpdatePartialFromQueryAsync(inDto.Id, x => new ReadingQuestionCriterionTrial() { IsCompleteConfig = inDto.IsCompleteConfig }); var result = await _readingQuestionCriterionTrialRepository.SaveChangesAsync(); return ResponseOutput.Ok(result); } /// /// 获取项目问题标准(项目) /// /// [HttpPost] public async Task> GetReadingQuestionCriterionTrialList(ReadingQuestionCriterionTrialViewInDto inDto) { await AddSystemDataToTrila(inDto.TrialId); var query = _readingQuestionCriterionTrialRepository.AsQueryable() .Where(x=>x.TrialId==inDto.TrialId) .WhereIf(!inDto.CriterionName.IsNullOrEmpty(), x => x.CriterionName.Contains(inDto.CriterionName)) .ProjectTo(_mapper.ConfigurationProvider); return await query.ToPagedListAsync(inDto.PageIndex, inDto.PageSize, inDto.SortField == null ? nameof(ReadingQuestionCriterionTrialView.CriterionName) : inDto.SortField, inDto.Asc); } /// /// 删除项目问题标准(项目) /// /// /// [HttpDelete("{id:guid}")] public async Task DeleteReadingQuestionCriterionTrial(Guid id) { await _readingQuestionCriterionTrialRepository.DeleteFromQueryAsync(t => t.Id == id); var success = await _readingQuestionCriterionTrialRepository.SaveChangesAsync(); return ResponseOutput.Result(success); } /// /// 新增修改项目问题(项目) /// /// /// [HttpPost] public async Task AddOrUpdateReadingQuestionTrial(AddOrUpdateReadingQuestionTrialInDto indto) { var entity = await _readingQuestionTrialRepository.InsertOrUpdateAsync(indto, true); return ResponseOutput.Ok(entity.Id.ToString()); } /// /// 获取项目问题(项目) /// /// [HttpPost] public async Task> GetReadingQuestionTrialList(ReadingQuestionTrialViewInDto inDto) { var query = _readingQuestionTrialRepository.AsQueryable() .Where(x => x.ReadingQuestionCriterionTrialId == inDto.ReadingQuestionCriterionTrialId) .WhereIf(!inDto.QuestionName.IsNullOrEmpty(), x => x.QuestionName.Contains(inDto.QuestionName)) .WhereIf(!inDto.Type.IsNullOrEmpty(), x => x.Type.Contains(inDto.Type)) .ProjectTo(_mapper.ConfigurationProvider); return await query.ToPagedListAsync(inDto.PageIndex, inDto.PageSize, inDto.SortField == null ? nameof(ReadingQuestionTrialView.QuestionName) : inDto.SortField, inDto.Asc); } /// /// 获取项目标准的其他问题(项目) /// /// /// [HttpPost] public async Task> GetTrialCriterionOtherQuestion(GetTrialCriterionOtherQuestionInDto inDto) { var types = new List() { "select","radio" }; var questionList = await _readingQuestionTrialRepository.Where(x => x.ReadingQuestionCriterionTrialId == inDto.ReadingQuestionCriterionTrialId) .Where(x => types.Contains(x.Type)) .WhereIf(inDto.Id != null, x => x.Id != inDto.Id && x.ParentId != inDto.Id) .Select(x => new CriterionOtherQuestionOutDto() { QuestionId = x.Id, QuestionName = x.QuestionName, TypeValue = x.TypeValue, GroupName=x.GroupName, }).ToListAsync(); return questionList; } ///// ///// 设置问题为裁判问题 ///// ///// //public async Task SetTrialQuestionIsIsJudgeQuestion(SetTrialQuestionIsIsJudgeQuestionInDto inDto) //{ // var trialCriterionId = (await _readingQuestionTrialRepository.FirstOrDefaultAsync(x => x.Id == inDto.Id)).IfNullThrowException(); // if (inDto.IsJudgeQuestion) // { // if() // } //} /// /// 删除项目问题(项目) /// /// /// [HttpDelete("{id:guid}")] public async Task DeleteReadingQuestionTrial(Guid id) { if (await _readingQuestionTrialRepository.AnyAsync(x => x.ParentId == id)) { return ResponseOutput.NotOk("此问题存在子问题,请先删除子问题"); } await _readingQuestionTrialRepository.DeleteFromQueryAsync(t => t.Id == id); var success = await _readingQuestionTrialRepository.SaveChangesAsync(); return ResponseOutput.Result(success); } /// /// 获取项目问题分组 /// /// /// [HttpPost] public async Task> GetTrialGroupNameList(GetTrialGroupNameListInDto inDto) { var result = await _readingQuestionTrialRepository.Where(x => x.ReadingQuestionCriterionTrialId == inDto.CriterionId && x.Type == "group") .Select(x => x.GroupName).ToListAsync(); return result; } /// /// 获取系统问题分组 /// /// /// [HttpPost] public async Task> GetSystemGroupNameList(GetTrialGroupNameListInDto inDto) { var result = await _readingQuestionSystemRepository.Where(x => x.ReadingQuestionCriterionSystemId == inDto.CriterionId && x.Type == "group") .Select(x => x.GroupName).ToListAsync(); return result; } } }