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 System.ComponentModel.DataAnnotations;
using System.Reflection;
using IRaCIS.Core.Infra.EFCore.Common;
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Application.Service.Inspection.DTO;
using Panda.DynamicWebApi.Attributes;
using Microsoft.Extensions.DependencyInjection;

namespace IRaCIS.Application.Services
{
    /// <summary>
    /// 阅片临床数据
    /// </summary>
    [ApiExplorerSettings(GroupName = "Reading")]
    public class ReadingClinicalDataService : BaseService, IReadingClinicalDataService
    {
        private readonly IRepository<ReadingClinicalData> _readingClinicalDataRepository;
        private readonly IRepository<ClinicalDataTrialSet> _clinicalDataTrialSetRepository;
        private readonly IRepository<VisitTask> _visitTaskRepository;
        private readonly IRepository<PreviousPDF> _previousPDFRepository;
        private readonly IRepository<SubjectVisit> _subjectVisitRepository;
        private readonly IRepository<PreviousHistory> _previousHistoryRepository;
        private readonly IRepository<PreviousOther> _previousOtherRepository;
        private readonly IRepository<PreviousSurgery> _previousSurgeryRepository;
        private readonly IServiceProvider iServiceProvider;
        private readonly IRepository<Subject> _subjectRepository;
        private readonly IRepository<ReadModule> _readModuleRepository;
        private readonly IRepository<ReadingClinicalDataPDF> _readingClinicalDataPDFRepository;

        public ReadingClinicalDataService(IRepository<ReadingClinicalData> readingClinicalDataRepository,
            IRepository<ClinicalDataTrialSet> clinicalDataTrialSetRepository,
             IRepository<PreviousPDF> previousPDFRepository,
             IRepository<SubjectVisit> subjectVisitRepository,
             IRepository<PreviousHistory> previousHistoryRepository,
             IRepository<PreviousOther> previousOtherRepository,
             IRepository<PreviousSurgery> previousSurgeryRepository,
             IServiceProvider IServiceProvider,
               IRepository<Subject> subjectRepository,
                 IRepository<ReadModule> readModuleRepository,
              IRepository<ReadingClinicalDataPDF> readingClinicalDataPDFRepository,
              IRepository<VisitTask> visitTaskRepository)
        {
            this._readingClinicalDataRepository = readingClinicalDataRepository;
            this._clinicalDataTrialSetRepository = clinicalDataTrialSetRepository;
            this._previousPDFRepository = previousPDFRepository;
            this._subjectVisitRepository = subjectVisitRepository;
            this._previousHistoryRepository = previousHistoryRepository;
            this._previousOtherRepository = previousOtherRepository;
            this._previousSurgeryRepository = previousSurgeryRepository;
            this.iServiceProvider = IServiceProvider;
            this._subjectRepository = subjectRepository;
            this._readModuleRepository = readModuleRepository;
            this._readingClinicalDataPDFRepository = readingClinicalDataPDFRepository;
            this._visitTaskRepository = visitTaskRepository;
        }


        /// <summary>
        /// 新增或者修改
        /// </summary>
        /// <param name="indto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> AddOrUpdateReadingClinicalData(AddOrUpdateReadingClinicalDataDto indto)
        {
            var existsQuery = _readingClinicalDataRepository
                .WhereIf(indto.Id != null, x => x.Id != indto.Id)
                .Where(x => x.ClinicalDataTrialSetId == indto.ClinicalDataTrialSetId && x.ReadingId == indto.ReadingId);

            if (await existsQuery.AnyAsync())
            {
                return ResponseOutput.NotOk("存在同类型的临床数据");
            }
            var clinicalDataTrialSet = (await _clinicalDataTrialSetRepository.Where(x => x.Id == indto.ClinicalDataTrialSetId).FirstOrDefaultAsync()).IfNullThrowException();
            if (indto.Id == null)
            {
                var entity = _mapper.Map<ReadingClinicalData>(indto);
                entity.ReadingClinicalDataPDFList = indto.AddFileList.Select(x => new ReadingClinicalDataPDF()
                {
                    TrialId = entity.TrialId,
                    SubjectId = indto.SubjectId,
                    FileName = x.FileName,
                    Path = x.Path,
                    ClinicalDataTrialSetId = indto.ClinicalDataTrialSetId,
                    IsVisit = indto.IsVisit,
                    ClinicalDataLevel = clinicalDataTrialSet.ClinicalDataLevel,
                    ReadingId = indto.ReadingId
                }).ToList();

                entity.ReadingClinicalDataState = ReadingClinicalDataStatus.HaveUploaded;
                entity.IsBlind = null;
                entity.IsComplete = null;
                entity.FileCount = entity.ReadingClinicalDataPDFList.Count();
                await _readingClinicalDataRepository.AddAsync(entity, true);
                var success = await _readingClinicalDataRepository.SaveChangesAsync();
                return ResponseOutput.Ok(entity.Id);
            }
            else
            {
                var entity = (await _readingClinicalDataRepository.Where(t => t.Id == indto.Id, true).FirstOrDefaultAsync()).IfNullThrowException();
                _mapper.Map(indto, entity);
                await _readingClinicalDataPDFRepository.BatchDeleteNoTrackingAsync(x => indto.DeleteFileIds.Contains(x.Id));

                var addFileList = indto.AddFileList.Select(x => new ReadingClinicalDataPDF()
                {
                    TrialId = entity.TrialId,
                    SubjectId = indto.SubjectId,
                    FileName = x.FileName,
                    Path = x.Path,
                    ClinicalDataTrialSetId = indto.ClinicalDataTrialSetId,
                    IsVisit = indto.IsVisit,
                    ClinicalDataLevel = clinicalDataTrialSet.ClinicalDataLevel,
                    ReadingId = indto.ReadingId,
                    ReadingClinicalDataId = entity.Id,
                }).ToList();
                await _readingClinicalDataPDFRepository.AddRangeAsync(addFileList);

                await _readingClinicalDataPDFRepository.SaveChangesAsync();

                var fileCount = await _readingClinicalDataPDFRepository.Where(t => t.ReadingClinicalDataId == indto.Id).CountAsync();
                entity.ReadingClinicalDataState = ReadingClinicalDataStatus.HaveUploaded;
                entity.FileCount = fileCount;
                var success = await _readingClinicalDataRepository.SaveChangesAsync();
                return ResponseOutput.Ok(entity.Id);

            }

        }

        /// <summary>
        /// 获取CRC上传的文件
        /// </summary>
        /// <param name="inDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<GetCRCClinicalDataOutDto>> GetCRCClinicalData(GetCRCClinicalDataInDto inDto)
        {
            await this.AddCRCClinicalData(inDto);

            List<GetCRCClinicalDataOutDto> cRCClinicalDataList = await _readingClinicalDataRepository.Where(x => x.ReadingId == inDto.SubjectVisitId)
                .Where(x => x.ClinicalDataTrialSet.TrialId == inDto.TrialId && x.ClinicalDataTrialSet.UploadRole == UploadRole.CRC && x.ClinicalDataTrialSet.ClinicalDataLevel != ClinicalLevel.ImageRead && x.ClinicalDataTrialSet.ClinicalDataLevel != ClinicalLevel.OncologyRead && x.ClinicalDataTrialSet.IsConfirm)
                .Select(x => new GetCRCClinicalDataOutDto()
                {
                    Id = x.Id,
                    ClinicalDataSetName = x.ClinicalDataTrialSet.ClinicalDataSetName,
                    ClinicalUploadType = x.ClinicalDataTrialSet.ClinicalUploadType,
                    ClinicalDataTrialSetId = x.ClinicalDataTrialSet.Id,
                    FileName = x.ClinicalDataTrialSet.FileName,
                    UploadRole = x.ClinicalDataTrialSet.UploadRole,
                    Path = x.ClinicalDataTrialSet.Path,
                    IsBlind=x.IsBlind,
                    IsComplete=x.IsComplete
                }).ToListAsync();

            List<GetReadingClinicalDataListOutDto> clinicalData = (await this.GetReadingClinicalDataList(new GetReadingClinicalDataListIndto()
            {
                SubjectId = inDto.SubjectId,
                ReadingId = inDto.SubjectVisitId,
                TrialId = inDto.TrialId,
            })).Item1;

            var previousHistoryList = await _previousHistoryRepository.Where(x => x.SubjectVisitId == inDto.SubjectVisitId).ProjectTo<PreviousHistoryView>(_mapper.ConfigurationProvider).ToListAsync();
            var previousOtherList = await _previousOtherRepository.Where(x => x.SubjectVisitId == inDto.SubjectVisitId).ProjectTo<PreviousOtherView>(_mapper.ConfigurationProvider).ToListAsync();
            var previousSurgeryList = await _previousSurgeryRepository.Where(x => x.SubjectVisitId == inDto.SubjectVisitId).ProjectTo<PreviousSurgeryView>(_mapper.ConfigurationProvider).ToListAsync();

            foreach (var item in cRCClinicalDataList)
            {
                item.PDFFileList = clinicalData.Where(y => y.ClinicalDataTrialSetId == item.ClinicalDataTrialSetId).SelectMany(x => x.FileList).ToList();
                item.ClinicalTableData = new ClinicalDataTable()
                {
                    PreviousHistoryList = previousHistoryList.Where(x => x.ClinicalDataTrialSetId == item.ClinicalDataTrialSetId).ToList(),
                    PreviousOtherList = previousOtherList.Where(x => x.ClinicalDataTrialSetId == item.ClinicalDataTrialSetId).ToList(),
                    PreviousSurgeryList = previousSurgeryList.Where(x => x.ClinicalDataTrialSetId == item.ClinicalDataTrialSetId).ToList(),
                };

            }
            return cRCClinicalDataList;
        }



 


        /// <summary>
        /// 添加CRC数据类型
        /// </summary>
        /// <returns></returns>
        private async Task AddCRCClinicalData(GetCRCClinicalDataInDto inDto)
        {
            var cRCClinicalDataIds = await _clinicalDataTrialSetRepository.Where(x => x.TrialId == inDto.TrialId && x.UploadRole == UploadRole.CRC && x.ClinicalDataLevel != ClinicalLevel.ImageRead && x.ClinicalDataLevel != ClinicalLevel.OncologyRead && x.IsConfirm).Select(x => x.Id).ToListAsync();

            var needAddIds = cRCClinicalDataIds.Where(x => _readingClinicalDataRepository.Where(y => y.ReadingId == inDto.SubjectVisitId && x == y.ClinicalDataTrialSetId).Count() == 0).ToList();

            List<ReadingClinicalData> readingClinicals = needAddIds.Select(x => new ReadingClinicalData()
            {
                ClinicalDataTrialSetId = x,
                IsVisit = true,
                SubjectId = inDto.SubjectId,
                ReadingId = inDto.SubjectVisitId,
                TrialId = inDto.TrialId
            }).ToList();

            await _readingClinicalDataRepository.AddRangeAsync(readingClinicals);

            await _readingClinicalDataRepository.SaveChangesAsync();

        }

        ///// <summary>
        ///// 设置临床数据是否盲化
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost]
        //public async Task<IResponseOutput> SetReadingClinicalDataIsBlind(SetReadingClinicalDataIsBlind inDto)
        //{
        //    await _readingClinicalDataRepository.UpdatePartialFromQueryAsync(inDto.Id, x=>new ReadingClinicalData() { 
        //         IsBlind=inDto.IsBlind,
        //    });
        //    return ResponseOutput.Ok(await _readingClinicalDataRepository.SaveChangesAsync());
        //}

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id:guid}")]
        public async Task<IResponseOutput> DeleteReadingClinicalData(Guid id)
        {

            await _readingClinicalDataRepository.DeleteFromQueryAsync(x => x.Id == id, true);
            await _readingClinicalDataPDFRepository.DeleteFromQueryAsync(x => x.ReadingClinicalDataId == id, true);
            return ResponseOutput.Result(true);
        }


        /// <summary>
        /// 获取下拉菜单
        /// </summary>
        /// <param name="inDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<GetTrialClinicalDataSelectOutDto>> GetTrialClinicalDataSelect(GetTrialClinicalDataSelectIndto inDto)
        {
            var userTypes = new List<int>() {
             (int)UserTypeEnum.ProjectManager,
             (int)UserTypeEnum.SPM,
             (int)UserTypeEnum.CPM,
             (int)UserTypeEnum.IndependentReviewer,
             };



            if (userTypes.Contains(_userInfo.UserTypeEnumInt))
            {
                inDto.UploadRole = UploadRole.PM;
            }

            ReadModule readModule = null;
            if (inDto.ReadingId != null)
            {
                readModule = await _readModuleRepository.Where(x => x.Id ==inDto.ReadingId).FirstOrDefaultAsync();
            }

            Dictionary<ModuleTypeEnum, ClinicalLevel> keyValuePairs = new Dictionary<ModuleTypeEnum, ClinicalLevel>();
            keyValuePairs.Add(ModuleTypeEnum.Global, ClinicalLevel.ImageRead);
            keyValuePairs.Add(ModuleTypeEnum.Oncology, ClinicalLevel.OncologyRead);

            var usedIdsQuery = _readingClinicalDataRepository.Where(x => x.ReadingId == inDto.ReadingId && x.Id != inDto.ReadingClinicalDataId).Select(x => x.ClinicalDataTrialSetId);
            List<GetTrialClinicalDataSelectOutDto> clinicalList = await _clinicalDataTrialSetRepository.Where(x => x.TrialId == inDto.TrialId && x.IsConfirm)
                .WhereIf(inDto.UploadRole != null, x => x.UploadRole == inDto.UploadRole)
                .Where(x => !usedIdsQuery.Contains(x.Id))
                .WhereIf(inDto.IsVisit && inDto.IsBaseLine, x => x.ClinicalDataLevel == ClinicalLevel.Subject || x.ClinicalDataLevel == ClinicalLevel.SubjectVisit)
                .WhereIf(inDto.IsVisit && !inDto.IsBaseLine, x => x.ClinicalDataLevel == ClinicalLevel.SubjectVisit)
                .WhereIf(!inDto.IsVisit, x => x.ClinicalDataLevel == ClinicalLevel.ImageRead||x.ClinicalDataLevel==ClinicalLevel.OncologyRead)
                .WhereIf(readModule!=null, x => x.ClinicalDataLevel == keyValuePairs[readModule.ModuleType])
                .Select(x => new GetTrialClinicalDataSelectOutDto()
                {
                    ClinicalDataLevel = x.ClinicalDataLevel,
                    ClinicalDataSetName = x.ClinicalDataSetName,
                    ClinicalUploadType = x.ClinicalUploadType,
                    FileName = x.FileName,
                    Path = x.Path,
                    Id = x.Id
                }).ToListAsync();
            return clinicalList;
        }


        /// <summary>
        /// 影像阅片临床数据签名
        /// </summary>
        // [HttpPost]
       
        public async Task<IResponseOutput> ReadClinicalDataSign(ReadingClinicalDataSignIndto inDto)
        {
            await _readingClinicalDataRepository.UpdatePartialFromQueryAsync(x => x.Id == inDto.ReadingClinicalDataId, x => new ReadingClinicalData()
            {
                IsSign = true,
                ReadingClinicalDataState=ReadingClinicalDataStatus.HaveSigned
            });

            var result = await _readingClinicalDataRepository.SaveChangesAsync();


            var readingId = await _readingClinicalDataRepository.Where(x => x.Id == inDto.ReadingClinicalDataId).Select(x => x.ReadingId).FirstOrDefaultAsync();

            await this.iServiceProvider.GetService<IReadingImageTaskService>().AddOncologyTask(readingId);

            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 获取访视 阅片或任务临床数据
        /// </summary>
        /// <param name="inDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<(List<GetReadingClinicalDataListOutDto>, object)> GetReadingOrTaskClinicalDataList(GetReadingOrTaskClinicalDataListInDto inDto)
        {
            var readingNameOrTaskBlindName = string.Empty;
            if (inDto.ReadingId == null)
            {
                var visitTask = await _visitTaskRepository.FirstOrDefaultAsync(x => x.Id == inDto.VisitTaskId);
                inDto.ReadingId = visitTask.SouceReadModuleId ?? visitTask.SourceSubjectVisitId;
                readingNameOrTaskBlindName = visitTask.TaskBlindName;
            }

            if (readingNameOrTaskBlindName.IsNullOrEmpty())
            {
                readingNameOrTaskBlindName = await _subjectVisitRepository.Where(x => x.Id == inDto.ReadingId).Select(x => x.VisitName).FirstOrDefaultAsync();
                if (readingNameOrTaskBlindName.IsNullOrEmpty())
                {
                    readingNameOrTaskBlindName = await _readModuleRepository.Where(x => x.Id == inDto.ReadingId).Select(x => x.ModuleName).FirstOrDefaultAsync();

                }
            }

            var result = await GetClinicalDataList(inDto);

            var previousHistoryList = await _previousHistoryRepository.Where(x => x.SubjectVisitId == inDto.ReadingId).ProjectTo<PreviousHistoryView>(_mapper.ConfigurationProvider).ToListAsync();
            var previousOtherList = await _previousOtherRepository.Where(x => x.SubjectVisitId == inDto.ReadingId).ProjectTo<PreviousOtherView>(_mapper.ConfigurationProvider).ToListAsync();
            var previousSurgeryList = await _previousSurgeryRepository.Where(x => x.SubjectVisitId == inDto.ReadingId).ProjectTo<PreviousSurgeryView>(_mapper.ConfigurationProvider).ToListAsync();
            foreach (var item in result)
            {
                item.ClinicalTableData = new ClinicalDataTable()
                {
                    PreviousHistoryList = previousHistoryList.Where(x => x.ClinicalDataTrialSetId == item.ClinicalDataTrialSetId).ToList(),
                    PreviousOtherList = previousOtherList.Where(x => x.ClinicalDataTrialSetId == item.ClinicalDataTrialSetId).ToList(),
                    PreviousSurgeryList = previousSurgeryList.Where(x => x.ClinicalDataTrialSetId == item.ClinicalDataTrialSetId).ToList(),
                };

            }

            return (result, new
            {
                SubjectCode = await _subjectRepository.Where(x => x.Id == inDto.SubjectId).Select(x => x.Code).FirstOrDefaultAsync(),
                ReadingNameOrTaskBlindName = readingNameOrTaskBlindName,
            });
        }

        [NonDynamicMethod]
        public async Task<List<GetReadingClinicalDataListOutDto>> GetClinicalDataList(GetReadingOrTaskClinicalDataListInDto inDto)
        {

            if (inDto.ReadingId == null)
            {
                var visitTask = await _visitTaskRepository.FirstOrDefaultAsync(x => x.Id == inDto.VisitTaskId);
                inDto.ReadingId = visitTask.SouceReadModuleId ?? visitTask.SourceSubjectVisitId;
            }

            var resultQuery = _readingClinicalDataRepository.Where(x => x.SubjectId == inDto.SubjectId)
                 .WhereIf(inDto.ReadingClinicalDataId != null, x => x.Id == inDto.ReadingClinicalDataId)
                 .Where(x => x.ReadingId == inDto.ReadingId || (x.SubjectId == inDto.SubjectId && x.ClinicalDataTrialSet.ClinicalDataLevel == ClinicalLevel.Subject))
                 .Select(x => new GetReadingClinicalDataListOutDto()
                 {
                     ClinicalDataLevel = x.ClinicalDataTrialSet.ClinicalDataLevel,
                     SubjectId = x.SubjectId,
                     ClinicalDataSetName = x.ClinicalDataTrialSet.ClinicalDataSetName,
                     ClinicalDataTrialSetId = x.ClinicalDataTrialSetId,
                     IsSign = x.IsSign,
                     ClinicalUploadType = x.ClinicalDataTrialSet.ClinicalUploadType,
                     Id = x.Id,
                     UploadRole = x.ClinicalDataTrialSet.UploadRole,
                     IsCRCUpload = x.ClinicalDataTrialSet.UploadRole == UploadRole.CRC,
                     FileList = x.ReadingClinicalDataPDFList.Select(y => new GetFileDto()
                     {
                         Id = y.Id,
                         FileName = y.FileName,
                         Path = y.Path,
                         CreateTime = y.CreateTime,
                     }).ToList()
                 });


            var result = await resultQuery.ToListAsync();
            result = result.Where(x => !(x.ClinicalUploadType == ClinicalUploadType.PDF && x.FileList.Count() == 0)).ToList();
            return result;
        }

        /// <summary>
        /// 获取阅片临床数据列表
        /// </summary>
        /// <param name="inDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<(List<GetReadingClinicalDataListOutDto>,object)> GetReadingClinicalDataList(GetReadingClinicalDataListIndto inDto)
        {
            GetTrialClinicalDataSelectIndto getTrialClinicalDataSelectIndto = new GetTrialClinicalDataSelectIndto()
            {
                ReadingId = inDto.ReadingId,
                TrialId = inDto.TrialId,
                IsBaseLine = inDto.IsBaseLine,
                SubjectId = inDto.SubjectId,
                IsVisit = inDto.IsVisit,
            };

            var clinicalDataList = await this.GetTrialClinicalDataSelect(getTrialClinicalDataSelectIndto);

            if (_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ClinicalResearchCoordinator || _userInfo.UserTypeEnumInt == (int)UserTypeEnum.IQC)
            {
                inDto.UploadRole = UploadRole.CRC;
            }
            var isBaseLine = await _subjectVisitRepository.AnyAsync(x => x.Id == inDto.ReadingId && x.IsBaseLine);
           

            var result =await  this.GetReadingClinicalList(inDto);
        

            var previousHistoryList = await _previousHistoryRepository.Where(x => x.SubjectVisitId == inDto.ReadingId).ProjectTo<PreviousHistoryView>(_mapper.ConfigurationProvider).ToListAsync();
            var previousOtherList = await _previousOtherRepository.Where(x => x.SubjectVisitId == inDto.ReadingId).ProjectTo<PreviousOtherView>(_mapper.ConfigurationProvider).ToListAsync();
            var previousSurgeryList = await _previousSurgeryRepository.Where(x => x.SubjectVisitId == inDto.ReadingId).ProjectTo<PreviousSurgeryView>(_mapper.ConfigurationProvider).ToListAsync();
            foreach (var item in result)
            {
                item.ClinicalTableData = new ClinicalDataTable()
                {
                    PreviousHistoryList = previousHistoryList.Where(x => x.ClinicalDataTrialSetId == item.ClinicalDataTrialSetId).ToList(),
                    PreviousOtherList = previousOtherList.Where(x => x.ClinicalDataTrialSetId == item.ClinicalDataTrialSetId).ToList(),
                    PreviousSurgeryList = previousSurgeryList.Where(x => x.ClinicalDataTrialSetId == item.ClinicalDataTrialSetId).ToList(),
                };

            }

            return (result, new {
                IsCanAddClinicalData= clinicalDataList.Count()>0,
            }) ;
        }


        [HttpPut]
        public async Task<IResponseOutput> PMClinicalDataConfirm(PMClinicalDataConfirmCommand command)
        {
            await _readingClinicalDataRepository.UpdatePartialFromQueryAsync(t => t.Id == command.Id, u => new ReadingClinicalData()
            {
                IsBlind=command.IsBlind,
                IsComplete=command.IsComplete,
                ReadingClinicalDataState=ReadingClinicalDataStatus.HaveChecked
            });
            await _readingClinicalDataRepository.SaveChangesAsync();

            return ResponseOutput.Ok();
        }
        /// <summary>
        /// 获取临床数据集合
        /// </summary>
        /// <returns></returns>
        [NonDynamicMethod]
        public async Task<List<GetReadingClinicalDataListOutDto>> GetReadingClinicalList(GetReadingClinicalDataListIndto inDto)
        {
            var resultQuery = _readingClinicalDataRepository.Where(x => x.SubjectId == inDto.SubjectId)
              .Where(x => x.ReadingId == inDto.ReadingId)
              .WhereIf(inDto.UploadRole == UploadRole.CRC, x => x.ClinicalDataTrialSet.UploadRole == UploadRole.CRC)
              .Select(x => new GetReadingClinicalDataListOutDto()
              {
                  ClinicalDataLevel = x.ClinicalDataTrialSet.ClinicalDataLevel,
                  SubjectId = x.SubjectId,
                  ClinicalDataSetName = x.ClinicalDataTrialSet.ClinicalDataSetName,
                  ClinicalDataTrialSetId = x.ClinicalDataTrialSetId,
                  IsSign = x.IsSign,
                  ClinicalUploadType = x.ClinicalDataTrialSet.ClinicalUploadType,
                  Id = x.Id,
                  UploadRole = x.ClinicalDataTrialSet.UploadRole,
                  IsCRCUpload = x.ClinicalDataTrialSet.UploadRole == UploadRole.CRC,
                  IsBlind=x.IsBlind,
                  IsComplete=x.IsComplete,
                  FileCount=x.FileCount,

                  ReadingClinicalDataState=x.ReadingClinicalDataState,

                  FileList = x.ReadingClinicalDataPDFList.Select(y => new GetFileDto()
                  {
                      Id = y.Id,
                      FileName = y.FileName,
                      Path = y.Path,
                      CreateTime = y.CreateTime,
                  }).ToList()
              });

            var result = await resultQuery.ToListAsync();
            result = result.Where(x => !(x.UploadRole == UploadRole.CRC && x.ClinicalUploadType == ClinicalUploadType.PDF && x.FileList.Count() == 0)).ToList();
            return result;
        }

        /// <summary>
        /// 获取单个阅片临床数据的所有文件
        /// </summary>
        /// <param name="inDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<GetReadingClinicalDataPDFListOutDto>> GetReadingClinicalDataPDFList(GetReadingClinicalDataPDFListIndto inDto) {

            var result = await _readingClinicalDataPDFRepository.Where(x => x.ReadingClinicalDataId == inDto.ReadingClinicalDataId).ProjectTo<GetReadingClinicalDataPDFListOutDto>(_mapper.ConfigurationProvider)
                .ToPagedListAsync(inDto.PageIndex, inDto.PageSize, inDto.SortField == null ? nameof(GetReadingClinicalDataPDFListOutDto.FileName) : inDto.SortField,
                inDto.Asc);
            return result;
        }

        /// <summary>
        /// 删除PDF单个文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id:guid}")]
        public async Task<IResponseOutput> DeleteReadingClinicalDataPDF(Guid id)
        {
            await _readingClinicalDataPDFRepository.DeleteFromQueryAsync(x => x.Id == id, true);
            return ResponseOutput.Result(true);
        }


    }
}
