//using IRaCIS.Application.Interfaces;
//using IRaCIS.Application.Contracts;
//using IRaCIS.Core.Infra.EFCore;

//using IRaCIS.Core.Domain.Models;
//using System.Linq.Expressions;
//using IRaCIS.Core.Infrastructure.ExpressionExtend;
//using IRaCIS.Core.Infrastructure.Extention;
//using Microsoft.AspNetCore.Mvc;

//namespace IRaCIS.Core.Application.Services
//{
//    [ApiExplorerSettings(GroupName = "Reading")]
//    public class ReadingService : BaseService, IReviewerReadingService
//    {
//        private readonly IWorkloadTPRepository _workloadTPRepository;
//        private readonly IWorkloadGlobalRepository _workloadGlobalRepository;
//        private readonly IWorkloadADRepository _workloadADRepository;
//        private readonly IRepository<Trial> _trialRepository;

//        private readonly IRepository<Subject> _subjectRepository;
//        private readonly IRepository<SubjectVisit> _subjectVisitRepository;
       
//        public ReadingService(IWorkloadTPRepository workloadTPRepository,
//            IWorkloadGlobalRepository workloadGlobalRepository,
//            IWorkloadADRepository workloadADRepository,
//            IRepository<Trial> trialRepository,
//            IRepository<Subject> subjectRepository,
//            IRepository<SubjectVisit> subjectVisitRepository,
//            IUserInfo userInfo)
//        {
//            _workloadTPRepository = workloadTPRepository;
//            _workloadGlobalRepository = workloadGlobalRepository;
//            _workloadADRepository = workloadADRepository;
//            _trialRepository = trialRepository;

//            _subjectRepository = subjectRepository;
//            _subjectVisitRepository = subjectVisitRepository;
            
//        }
//        /// <summary>
//        /// 获取医生个人任务列表
//        /// WorkloadType 1-tp,2-global,3-ad
//        /// </summary>
//        /// <param name="param"></param>
//        /// <returns></returns>
//        [HttpPost]
//        public PageOutput<WorkloadReadingDTO> GetWorkloadList(WorkloadQueryParam param)
//        {
//            #pragma warning disable
//            IQueryable<WorkloadReadingDTO> query = null;
//            Expression<Func<Subject, bool>> subjectLambda = x => true;
//            if (param.SubjectCode != string.Empty)
//            {
//                subjectLambda = subjectLambda.And(o => o.Code.Contains(param.SubjectCode));
//            }
//            Expression<Func<Trial, bool>> trialLambda = x => true;
//            if (param.Expedited != null)
//            {
//                trialLambda = trialLambda.And(o => o.Expedited == param.Expedited);
//            }
//            if (param.TrialId != null && param.TrialId != Guid.Empty)
//            {
//                trialLambda = trialLambda.And(o => o.Id == param.TrialId);
//            }


//            if (param.WorkloadType == 1)// TP
//            {
//                Expression<Func<WorkloadTP, bool>> workloadLambda = x => true;
//                workloadLambda = workloadLambda.And(u => u.ReviewerId == _userInfo.Id);
//                if (param.Status != null)
//                {
//                    if (param.Status == 30)//30 的时候，待读和正在读（未提交的）
//                    {
//                        workloadLambda = workloadLambda.And(o => o.Status == 30 || o.Status == 40);
//                    }
//                    else
//                        workloadLambda = workloadLambda.And(o => o.Status == param.Status);
//                }
//                query = from tp in _workloadTPRepository.Where(workloadLambda)
//                        join trial in _trialRepository.Where(trialLambda) on tp.TrialId equals trial.Id
//                        join subject in _subjectRepository.Where(subjectLambda) on tp.SubjectId equals subject.Id
//                        join visit in _subjectVisitRepository.AsQueryable() on tp.SubjectVisitId equals visit.Id
//                        select new WorkloadReadingDTO
//                        {
//                            Id = tp.Id,
//                            StudyId = tp.StudyId,
//                            WorkloadId = tp.Id,
//                            WorkloadType = 1,
//                            Status = tp.Status,
//                            UpdateTime = tp.UpdateTime,
//                            TrialId = tp.TrialId,
//                            SubjectId = tp.SubjectId,
//                            SubjectCode = subject.Code,
//                            VisitNum = visit.VisitNum,
//                            VisitName = visit.VisitName,
//                            Expedited = trial.Expedited,
//                            TrialCode = trial.TrialCode,
//                            TrialIndication = trial.Indication,
//                            WorkloadCode = tp.TimepointCode
//                        };
//            }
//            else if (param.WorkloadType == 2)//Global
//            {
//                Expression<Func<WorkloadGlobal, bool>> workloadLambda = x => true;
//                workloadLambda = workloadLambda.And(u => u.ReviewerId == _userInfo.Id);
//                if (param.Status != null)
//                {
//                    if (param.Status == 30)
//                    {
//                        workloadLambda = workloadLambda.And(o => o.Status == 30 || o.Status == 40);
//                    }
//                    else
//                        workloadLambda = workloadLambda.And(o => o.Status == param.Status);
//                }
//                query = from global in _workloadGlobalRepository.Where(workloadLambda)
//                        join trial in _trialRepository.Where(trialLambda) on global.TrialId equals trial.Id
//                        join subject in _subjectRepository.Where(subjectLambda) on global.SubjectId equals subject.Id
//                        //join visit in _subjectVisitRepository.GetAll() on global.VisitId equals visit.Id
//                        select new WorkloadReadingDTO
//                        {
//                            Id = global.Id,
//                            WorkloadId = global.Id,
//                            WorkloadType = 1,
//                            Status = global.Status,
//                            UpdateTime = global.UpdateTime,
//                            TrialId = global.TrialId,
//                            SubjectId = global.SubjectId,
//                            SubjectCode = subject.Code,
//                            //VisitNum = visit.VisitNum,
//                            //VisitName = visit.VisitName,
//                            VisitNum = global.VisitNum,
//                            VisitName = global.VisitName,
                            
//                            Expedited = trial.Expedited,
//                            TrialCode = trial.TrialCode,
//                            TrialIndication = trial.Indication,
//                            WorkloadCode = global.GlobalCode
//                        };
//            }
//            else if (param.WorkloadType == 3)//AD
//            {
//                Expression<Func<WorkloadAD, bool>> workloadLambda = x => true;
//                workloadLambda = workloadLambda.And(u => u.ReviewerId == _userInfo.Id);
//                if (param.Status != null)
//                {
//                    if (param.Status == 30)//30 的时候，待读和正在读（未提交的）
//                    {
//                        workloadLambda = workloadLambda.And(o => o.Status == 30 || o.Status == 40);
//                    }
//                    else
//                        workloadLambda = workloadLambda.And(o => o.Status == param.Status);
//                }
//                query = from ad in _workloadADRepository.Where(workloadLambda)
//                        join trial in _trialRepository.Where(trialLambda) on ad.TrialId equals trial.Id
//                        join subject in _subjectRepository.Where(subjectLambda) on ad.SubjectId equals subject.Id
//                        //join visit in _subjectVisitRepository.GetAll() on ad.visi equals visit.Id
//                        select new WorkloadReadingDTO
//                        {
//                            Id = ad.Id,
//                            WorkloadId = ad.Id,
//                            WorkloadType = 1,
//                            Status = ad.Status,
//                            UpdateTime = ad.UpdateTime,
//                            TrialId = ad.TrialId,
//                            SubjectId = ad.SubjectId,
//                            SubjectCode = subject.Code,
//                            //VisitNum = visit.VisitNum,
//                            //VisitName = visit.VisitName,
//                            Expedited = trial.Expedited,
//                            TrialCode = trial.TrialCode,
//                            TrialIndication = trial.Indication,
//                            WorkloadCode = ad.ADCode
//                        };
//            }
//            query.OrderByDescending(u => u.UpdateTime);
//            var count = query.Count();
//            query = query.Skip((param.PageIndex - 1) * param.PageSize).Take(param.PageSize);
//            return new PageOutput<WorkloadReadingDTO>(param.PageIndex,
//                param.PageSize, count, query.ToList());
//        }
//    }
//}
