//--------------------------------------------------------------------
//     此代码由T4模板自动生成  byzhouhang 20210918
//	   生成时间 2021-11-22 11:29:44 
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------
using Microsoft.AspNetCore.Mvc;
using IRaCIS.Core.Application.Service;

namespace IRaCIS.Core.Application.Contracts
{
    /// <summary>
    ///受试者临床信息
    /// </summary>	
    [ApiExplorerSettings(GroupName = "Image")]
    public class ClinicalDataService : BaseService, IClinicalDataService
    {
        public IRepository<PreviousOther> _previousOtherRepository { get; }
        private readonly IRepository<PreviousHistory> _previousHistoryRepository;
        private readonly IRepository<PreviousSurgery> _previousSurgeryRepository;
        private readonly IRepository<PreviousPDF> _previousPdfRepository;
        private readonly IRepository<SubjectVisit> _subjectVisitRepository;
        private readonly QCCommon _qCCommon;

      public ClinicalDataService(IRepository<PreviousHistory> previousHistoryRepository, 
            IRepository<PreviousOther> previousOtherRepository,
            IRepository<PreviousSurgery> previousSurgeryRepository,
            IRepository<PreviousPDF> previousPDFRepository,
             IRepository<SubjectVisit> subjectVisitRepository,
             QCCommon qCCommon
            )
        {
            _qCCommon = qCCommon;
            _previousOtherRepository = previousOtherRepository;
            _previousHistoryRepository = previousHistoryRepository;
            _previousSurgeryRepository = previousSurgeryRepository;
            _previousPdfRepository = previousPDFRepository;
            _subjectVisitRepository = subjectVisitRepository;
        }




        /// <summary>
        /// 获取访视+受试者级别的数据
        /// </summary>
        /// <param ></param>
        /// <returns></returns>
        [HttpGet("{subjectVisitId:guid}")]
        public async Task<SubjectClinicalDataDto> GetSubjectVisitClinicalData(Guid subjectVisitId)
        {

            // 最完美方式  其中TrialSite 通过导航属性 两个字段连接出来

            var clinicalObj = await _repository.Where<SubjectVisit>(t => t.Id == subjectVisitId)
            .ProjectTo<SubjectClinicalDataDto>(_mapper.ConfigurationProvider, new { subjectVisitId = subjectVisitId, token = _userInfo.UserToken }).FirstOrDefaultAsync().IfNullThrowException();

            return clinicalObj;

            #region 老方式  linq join 而且需要映射  


            //var query = from sv in _subjectVisitRepository.GetAll()
            //            join subject in _subjectRepository.GetAll() on sv.SubjectId equals subject.Id
            //            join trialSite in _trialSiteRepository.Find(t => t.TrialId == subjectClinicalDataQuery.TrialId) on sv.SiteId equals trialSite.SiteId
            //            select new SubjectClinicalDataDto()
            //            {
            //                SubjectId = sv.SubjectId,
            //                SubjectCode = subject.Code,
            //                SubjectVisitId = sv.Id,
            //                VisitName = sv.VisitName,
            //                VisitNum = sv.VisitNum,
            //                TrialSiteCode = trialSite.TrialSiteCode
            //            };

            //var clinicalObj = query.FirstOrDefault(t => t.SubjectVisitId == subjectClinicalDataQuery.SubjectVisitId);

            //clinicalObj.PreviousHistoryList = _previousHistoryRepository.Find(t => t.SubjectVisitId == subjectClinicalDataQuery.SubjectVisitId)
            //                                   .ProjectTo<PreviousHistoryView>(_mapper.ConfigurationProvider).ToList();
            //clinicalObj.PreviousOtherList = _previousOtherRepository.Find(t => t.SubjectVisitId == subjectClinicalDataQuery.SubjectVisitId)
            //                                   .ProjectTo<PreviousOtherView>(_mapper.ConfigurationProvider).ToList();
            //clinicalObj.PreviousSurgeryList = _previousSurgeryRepository.Find(t => t.SubjectVisitId == subjectClinicalDataQuery.SubjectVisitId)
            //                                   .ProjectTo<PreviousSurgeryView>(_mapper.ConfigurationProvider).ToList();

            #endregion

        }


        public async Task<List<PreviousHistoryView>> GetPreviousHistoryList(PreviousHistoryQuery queryPreviousHistory)
        {

            var previousHistoryQueryable = _previousHistoryRepository.WhereIf(queryPreviousHistory.IsSubjectLevel != null, t => t.IsSubjectLevel == queryPreviousHistory.IsSubjectLevel)
             .ProjectTo<PreviousHistoryView>(_mapper.ConfigurationProvider, new { token = _userInfo.UserToken });

            return await previousHistoryQueryable.ToListAsync();
        }

        [HttpPost("{trialId:guid}")]
        public async Task<IResponseOutput<Guid>> AddOrUpdatePreviousHistory(PreviousHistoryAddOrEdit addOrEditPreviousHistory)
        {
            await _qCCommon.VerifyIsCRCSubmmitAsync(_repository, _userInfo, addOrEditPreviousHistory.SubjectVisitId);

            var entity = await _previousHistoryRepository.InsertOrUpdateAsync(addOrEditPreviousHistory, true);
            return ResponseOutput.Ok(entity.Id);
        }


        [HttpDelete("{trialId:guid}/{subjectVisitId:guid}/{previousHistoryId:guid}")]
        public async Task<IResponseOutput> DeletePreviousHistory(Guid previousHistoryId,Guid subjectVisitId)
        {
            await _qCCommon.VerifyIsCRCSubmmitAsync(_repository, _userInfo, subjectVisitId);
            await _previousHistoryRepository.DeleteFromQueryAsync(t => t.Id == previousHistoryId,true);
            return ResponseOutput.Ok();
        }


        public async Task<List<PreviousOtherView>> GetPreviousOtherList(PreviousOtherQuery queryPreviousOther)
        {
            var previousOtherQueryable = _previousOtherRepository.WhereIf(queryPreviousOther.IsSubjectLevel != null, t => t.IsSubjectLevel == queryPreviousOther.IsSubjectLevel)
                .ProjectTo<PreviousOtherView>(_mapper.ConfigurationProvider, new { token = _userInfo.UserToken });
            return await previousOtherQueryable.ToListAsync();
        }


        [HttpPost("{trialId:guid}")]
        public async Task<IResponseOutput<Guid>> AddOrUpdatePreviousOther(PreviousOtherAddOrEdit addOrEditPreviousOther)
        {
            await _qCCommon.VerifyIsCRCSubmmitAsync(_repository, _userInfo, addOrEditPreviousOther.SubjectVisitId);
            var entity = await _previousOtherRepository.InsertOrUpdateAsync(addOrEditPreviousOther, true);
            return ResponseOutput.Ok(entity.Id);

        }


        [HttpDelete("{trialId:guid}/{subjectVisitId:guid}/{previousOtherId:guid}")]
        public async Task<IResponseOutput> DeletePreviousOther(Guid previousOtherId, Guid subjectVisitId)
        {
            await _qCCommon.VerifyIsCRCSubmmitAsync(_repository, _userInfo, subjectVisitId);

            await _previousOtherRepository.DeleteFromQueryAsync(t => t.Id == previousOtherId,true);
            return ResponseOutput.Ok();
        }


        public async Task<List<PreviousSurgeryView>> GetPreviousSurgeryList(PreviousSurgeryQuery queryPreviousSurgery)
        {
            var previousSurgeryQueryable = _previousSurgeryRepository.WhereIf(queryPreviousSurgery.IsSubjectLevel != null, t => t.IsSubjectLevel == queryPreviousSurgery.IsSubjectLevel)
             .ProjectTo<PreviousSurgeryView>(_mapper.ConfigurationProvider, new { token = _userInfo.UserToken });

            return await previousSurgeryQueryable.ToListAsync();
        }

        [HttpPost("{trialId:guid}")]
        public async Task<IResponseOutput<Guid>> AddOrUpdatePreviousSurgery(PreviousSurgeryAddOrEdit addOrEditPreviousSurgery)
        {
            await _qCCommon.VerifyIsCRCSubmmitAsync(_repository, _userInfo, addOrEditPreviousSurgery.SubjectVisitId);
            var entity = await _previousSurgeryRepository.InsertOrUpdateAsync(addOrEditPreviousSurgery, true);
            return ResponseOutput.Ok(entity.Id);
        }


        [HttpDelete("{trialId:guid}/{subjectVisitId:guid}/{previousSurgeryId:guid}")]
        public async Task<IResponseOutput> DeletePreviousSurgery(Guid previousSurgeryId, Guid subjectVisitId)
        {
            await _qCCommon.VerifyIsCRCSubmmitAsync(_repository, _userInfo, subjectVisitId);
            var success = await _previousSurgeryRepository.DeleteFromQueryAsync(t => t.Id == previousSurgeryId,true);
            return ResponseOutput.Ok();
        }

        [HttpGet("{subjectVisitId:guid}")]
        public async Task<List<PreviousPDFView>> GetPreviousPDFList(Guid subjectVisitId)
        {

            var previousPDFQueryable = _previousPdfRepository.Where(t => t.SubjectVisitId == subjectVisitId).ProjectTo<PreviousPDFView>(_mapper.ConfigurationProvider);

            return await previousPDFQueryable.ToListAsync();
        }


        public async Task<IResponseOutput> AddOrUpdatePreviousPDF(PreviousPDFAddOrEdit addOrEditPreviousPDF)
        {
            await _qCCommon.VerifyIsCRCSubmmitAsync(_repository, _userInfo, addOrEditPreviousPDF.SubjectVisitId);
            var entity = await _previousPdfRepository.InsertOrUpdateAsync(addOrEditPreviousPDF, true);
            return ResponseOutput.Ok(entity.Id);

        }

        [HttpDelete("{trialId:guid}/{subjectVisitId:guid}/{previousPDFId:guid}")]
        public async Task<IResponseOutput> DeletePreviousPDF(Guid previousPDFId, Guid subjectVisitId)
        {
            await _qCCommon.VerifyIsCRCSubmmitAsync(_repository, _userInfo, subjectVisitId);

            await _previousPdfRepository.DeleteFromQueryAsync(t => t.Id == previousPDFId,true);
            return ResponseOutput.Ok();
        }




    }
}
