//using IRaCIS.Core.Application.Contracts;
//using System.Linq.Dynamic.Core;
//using AutoMapper;
//using IRaCIS.Application.Contracts;
//using IRaCIS.Core.Infra.EFCore;
//using IRaCIS.Core.Domain.Models;
//using IRaCIS.Core.Infrastructure.Extention;
//using Microsoft.AspNetCore.Mvc;

//namespace IRaCIS.Core.Application.Services
//{
//    [ ApiExplorerSettings(GroupName = "Reading")]
//    public class GlobalReportService : BaseService, IGlobalService
//    {
//        private readonly IRepository<GlobalRS> _globalRsRepository;
//        private readonly IRepository<GlobalResult> _globalResultRepository;
//        private readonly IRSRepository _rsRepository;
//        private readonly IRepository<VisitStage> _visitStageRepository;
        
//        private readonly IRepository<SubjectVisit> _subjectVisitRepository;
//        private readonly IWorkloadADRepository _workloadAdRepository;
//        private readonly IWorkloadGlobalRepository _workloadGlobalRepository;
//        private readonly IRepository<Trial> _trialRepository;
//        private readonly IRepository<Subject> _subjectRepository;

//        public GlobalReportService(IRepository<GlobalRS> globalRsRepository, IRepository<GlobalResult> globalResultRepository,
//            IRSRepository rsRepository, IRepository<VisitStage> visitStageRepository,  IRepository<SubjectVisit> subjectVisitRepository, IWorkloadADRepository workloadAdRepository,
//            IWorkloadGlobalRepository workloadGlobalRepository, IRepository<Trial> trialRepository, IRepository<Subject> subjectRepository)
//        {
//            _globalRsRepository = globalRsRepository;
//            _globalResultRepository = globalResultRepository;
//            _rsRepository = rsRepository;
//            _visitStageRepository = visitStageRepository;
            
//            _subjectVisitRepository = subjectVisitRepository;
//            _workloadAdRepository = workloadAdRepository;
//            _workloadGlobalRepository = workloadGlobalRepository;
//            _trialRepository = trialRepository;
//            _subjectRepository = subjectRepository;
//        }

//        [HttpGet("{trialId:guid}/{subjectId:guid}/{visitNum:decimal}/{globalId:guid}/{globalCode}")]
//        public IEnumerable<HistoryVisitRSDTO> GetHistoryVisitRsList(Guid trialId, Guid subjectId, decimal visitNum, Guid globalId, string globalCode)
//        {
            
//            var tpGroup = 'T' + globalCode.Trim().Substring(globalCode.Trim().Length - 2);


//            var query = from rs in _rsRepository.AsQueryable()
//                    .Where(t => t.SubjectGuid == subjectId && t.TrialGuid == trialId && t.RSTESTCD == "OVRLRESP" && t.VISITNUM <= visitNum && t.TpCode.Contains(tpGroup))

//                        join globalRs in _globalRsRepository.Where(t => t.GlobalId == globalId) on new { rs.TpCode, VisitNum = rs.VISITNUM } equals new { globalRs.TpCode, globalRs.VisitNum } into cc
//                        from globalRs in cc.DefaultIfEmpty()

//                        join visitStage in _visitStageRepository.Where(t => t.TrialId == trialId) on rs.VISITNUM equals visitStage.VisitNum
//                        select new HistoryVisitRSDTO
//                        {
//                            StudyId = rs.StudyGuid,
//                            StudyCode = rs.STUDYID,
//                            VisitNum = visitStage.VisitNum,
//                            VisitName = visitStage.VisitName,
//                            OverallResponse = rs.RSORRES,
//                            TpCode = rs.TpCode,
//                            GlobalRSSelect = new GlobalRSSelectView()
//                            {
//                                Agree = globalRs.Agree,
//                                NewRS = globalRs.NewRS,
//                                Note = globalRs.Note
//                            }
//                        };

//            var visitRsList = query.OrderBy(t => t.VisitNum).ToList();

//            return visitRsList;

//        }
//        #pragma warning disable
//        [HttpGet("{trialId:guid}/{subjectId:guid}/{visitNum:decimal}/{globalId:guid}")]
//        public PreviousGlobalReadsView GetHistoryGlobalRsList(Guid trialId, Guid subjectId, decimal visitNum,
//            Guid globalId)
//        {
//            //subjectCode = subjectCode.Trim();

//            var query = from globalResult in _globalResultRepository.AsQueryable()
//                    .Where(t => t.SubjectId == subjectId && t.VisitNum < visitNum)
//                        join visitStage in _visitStageRepository.Where(t => t.TrialId == trialId) on globalResult.VisitNum equals visitStage.VisitNum
//                        select new HistoryGlobalRsDTO
//                        {
//                            VisitNum = visitStage.VisitNum,
//                            VisitName = visitStage.VisitName,
//                            OverallResponse = globalResult.Result
//                        };

//            var subjectNote = _globalResultRepository.FirstOrDefault(t => t.GlobalId == globalId)?.SubjectNote;

//            return new PreviousGlobalReadsView()
//            {
//                PreviousGlobalReadsList = query.ToList(),
//                SubjectNote = subjectNote
//            };

//        }

//        public IResponseOutput AddGlobalReport(GlobalTaskReportCommand globalTaskReportCommand)
//        {
//            //删除上一次保存得记录
//            _globalResultRepository.Delete(t => t.GlobalId == globalTaskReportCommand.GlobalId);
//            _globalRsRepository.Delete(t => t.GlobalId == globalTaskReportCommand.GlobalId);


//            var globalRsList = _mapper.Map<List<GlobalRS>>(globalTaskReportCommand.GlobalRSReportList);

//            _globalRsRepository.AddRange(globalRsList);

//            var first = globalRsList.OrderByDescending(t => t.VisitNum).First();

//            var globalResult = new GlobalResult()
//            {
//                VisitNum = first.VisitNum,
//                SubjectCode = globalTaskReportCommand.SubjectCode,
//                SubjectNote = globalTaskReportCommand.SubjectNote,
//                SubjectId = globalTaskReportCommand.SubjectId,

//                GlobalId = first.GlobalId,
//                Result = first.NewRS
//            };

//            _globalResultRepository.Add(globalResult);

//            return  ResponseOutput.Result(_globalResultRepository.SaveChanges()) ;

//        }

//        //public string GetCommentsForSubject(Guid globalId)
//        //{
//        //  return  _globalResultRepository.FirstOrDefault(t => t.GlobalId == globalId)?.SubjectNote;
//        //}
//        [HttpGet("{adId}")]
//        public AdReportDTO GetAdReport(Guid adId)
//        {
//            var adReport = new AdReportDTO();
//            var ad = _workloadAdRepository.FirstOrDefault(t => t.Id == adId);
//            var globalId1 = ad.Global1Id;
//            var globalId2 = ad.Global2Id;

//            var query = from global in _workloadGlobalRepository.Where(t => t.Id == globalId1 || t.Id == globalId2)
//                        join trial in _trialRepository.Where(t => t.Id == ad.TrialId) on global.TrialId equals trial.Id
//                        join subject in _subjectRepository.AsQueryable() on global.SubjectId equals subject.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
//                        };

//            var globalList = query.ToList();

//            var global1 = globalList.First(t => t.Id == globalId1);
//            var global2 = globalList.First(t => t.Id == globalId2);

//            adReport.Global1VisitRS = GetHistoryVisitRsList(global1.TrialId, global1.SubjectId, 
//                 global1.VisitNum, global1.Id, global1.WorkloadCode);

//            adReport.Global2VisitRS = GetHistoryVisitRsList(global2.TrialId, global2.SubjectId
//                ,global2.VisitNum, global2.Id, global2.WorkloadCode);

//            adReport.Global1 = GetHistoryGlobalRsList(global1.TrialId, global1.SubjectId, global1.VisitNum, global1.Id);
//            adReport.Global2 = GetHistoryGlobalRsList(global2.TrialId, global2.SubjectId, global2.VisitNum, global2.Id);

//            adReport.ADInfo = ad;

//            return adReport;

//        }

//        public IResponseOutput AddAdjudicationReport(ADReportCommand adReportCommand)
//        {
//          return ResponseOutput.Result(_workloadAdRepository.Update(t => t.Id == adReportCommand.AdId,
//              u => new WorkloadAD()
//                  { AdNote = adReportCommand.ADNote, SelectGlobalId = adReportCommand.SelectGlobalId }))  ;
//        }
//    }
//}