using IRaCIS.Application.Interfaces;
using IRaCIS.Core.Domain.Models;
using System.Linq.Expressions;
using Microsoft.AspNetCore.Mvc;
using IRaCIS.Core.Infra.EFCore;
using IRaCIS.Core.Application.Contracts;

namespace IRaCIS.Core.Application.Services
{
    /// <summary>
    /// Financial---项目收入价格验证
    /// </summary>
    [ ApiExplorerSettings(GroupName = "Financial")]
    public class TrialRevenuesPriceVerificationService : BaseService, ITrialRevenuesPriceVerificationService
    {
        private readonly IRepository<TrialRevenuesPriceVerification> _trialRevenuesPriceVerificationRepository;
        private readonly IRepository<Trial> _trialRepository;
        private readonly IRepository<Doctor> _doctorRepository;
        private readonly IRepository<Payment> _paymentRepository;

        public TrialRevenuesPriceVerificationService(IRepository<TrialRevenuesPriceVerification> trialRevenuesPriceVerificationRepository, 
            IRepository<Trial> trialRepository,IRepository<Doctor> doctorRepository,IRepository<Payment> paymentRepository)
        {
            _trialRevenuesPriceVerificationRepository = trialRevenuesPriceVerificationRepository;
            _trialRepository = trialRepository;
            _doctorRepository = doctorRepository;
            _paymentRepository = paymentRepository;
        }
        [HttpPost]
        public async Task<AnalysisVerifyResultDTO> GetAnalysisVerifyList(RevenusVerifyQueryDTO param)
        {
            AnalysisVerifyResultDTO result=new AnalysisVerifyResultDTO();
            result.RevenuesVerifyList = await GetRevenuesVerifyList(param);


            var bDate = new DateTime(param.BeginDate.Year, param.BeginDate.Month, 1);
            var eDate = new DateTime(param.EndDate.Year, param.EndDate.Month, 1);
            eDate = eDate.AddMonths(1).AddSeconds(-1);

            Expression<Func<Payment, bool>> paymentLambda = x => x.YearMonthDate >= bDate && x.YearMonthDate <= eDate&&!x.IsLock;

            var query = from payment in _paymentRepository.Where(paymentLambda)
                        join doctor in _doctorRepository.AsQueryable() on payment.DoctorId equals doctor.Id
                        select new AnalysisNeedLockDTO()
                        {
                            YearMonth = payment.YearMonth,
                            ReviewerCode = doctor.ReviewerCode,
                            ReviewerName = doctor.LastName + " / " + doctor.FirstName,
                            ReviewerNameCN = doctor.ChineseName
                        };

            result.MonthVerifyResult = (await query.ToListAsync()).GroupBy(t => t.YearMonth).Select(g => new MonthlyResult
            {
                YearMonth = g.Key,
                ReviewerNameList = g.Select(t => t.ReviewerName).ToList(),
                ReviewerNameCNList = g.Select(t => t.ReviewerNameCN).ToList(),
                ReviewerCodeList = g.Select(t => t.ReviewerCode).ToList()

            }).OrderBy(t=>t.YearMonth).ToList();

            return result;
        }


        [HttpPost]
        public async Task<List<RevenusVerifyDTO>> GetRevenuesVerifyList(RevenusVerifyQueryDTO param)
        {
            var bDate = new DateTime(param.BeginDate.Year, param.BeginDate.Month, 1);
            var eDate = new DateTime(param.EndDate.Year, param.EndDate.Month, 1);
            Expression<Func<TrialRevenuesPriceVerification, bool>> trialRevenuesPriceVerificationLambda = x => x.WorkLoadDate >= bDate && x.WorkLoadDate <= eDate;

            var query = (from trialVerify in _trialRevenuesPriceVerificationRepository.Where(trialRevenuesPriceVerificationLambda)
                join trail in _trialRepository.AsQueryable() on trialVerify.TrialId equals trail.Id
                select new RevenusVerifyDTO
                {
                    TrialCode = trail.TrialCode,
                    Timepoint = trialVerify.Timepoint,
                    TimepointIn24H = trialVerify.TimepointIn24H,
                    TimepointIn48H = trialVerify.TimepointIn48H,
                    Adjudication = trialVerify.Adjudication,
                    AdjudicationIn24H = trialVerify.AdjudicationIn24H,
                    AdjudicationIn48H = trialVerify.AdjudicationIn48H,
                    Global = trialVerify.Global,
                    Downtime = trialVerify.Downtime,
                    Training = trialVerify.Training
                }).Distinct();


            return await query.ToListAsync();
        }



        //废弃
      [Obsolete]
        public async Task<List<RevenusVerifyDTO>> GetRevenuesVerifyResultList(RevenusVerifyQueryDTO param)
        {
            var bDate = new DateTime(param.BeginDate.Year, param.BeginDate.Month, 1);
            var eDate = new DateTime(param.EndDate.Year, param.EndDate.Month, 1);
            Expression<Func<TrialRevenuesPriceVerification, bool>> trialRevenuesPriceVerificationLambda = x => x.WorkLoadDate >= bDate && x.WorkLoadDate <= eDate;

            var query = (from trialVerify in _trialRevenuesPriceVerificationRepository.Where(trialRevenuesPriceVerificationLambda)
                         join trail in _trialRepository.AsQueryable() on trialVerify.TrialId equals trail.Id
                         select new RevenusVerifyDTO
                         {
                             TrialCode = trail.TrialCode,
                             Timepoint = trialVerify.Timepoint,
                             TimepointIn24H = trialVerify.TimepointIn24H,
                             TimepointIn48H = trialVerify.TimepointIn48H,
                             Adjudication = trialVerify.Adjudication,
                             AdjudicationIn24H = trialVerify.AdjudicationIn24H,
                             AdjudicationIn48H = trialVerify.AdjudicationIn48H,
                             Global = trialVerify.Global,
                             Downtime = trialVerify.Downtime,
                             Training = trialVerify.Training
                         }).Distinct();


            return await query.ToListAsync();
            #region 提示 old
            //query = from trialVerify in _trialRevenuesPriceVerificationRepository.GetAll()
            //    join trail in _trialRepository.GetAll() on trialVerify.TrialId equals trail.Id
            //    join reviewer in _doctorRepository.GetAll() on trialVerify.ReviewerId equals reviewer.Id
            //    select new RevenusVerifyDTO()
            //    {
            //        ReviewerCode = reviewer.Code,
            //        TrialCode = trail.Code,
            //        YearMonth = trialVerify.YearMonth
            //    };










            ////0是Detail   1是按照项目   2是按照人  3按照月份
            //if (param.StatType == 0)
            //{
            //    query = from trialVerify in _trialRevenuesPriceVerificationRepository.GetAll()
            //            join trail in _trialRepository.GetAll() on trialVerify.TrialId equals trail.Id
            //            join reviewer in _doctorRepository.GetAll() on trialVerify.ReviewerId equals reviewer.Id
            //            select new RevenusVerifyDTO()
            //            {
            //                ReviewerCode = reviewer.Code,
            //                TrialCode = trail.Code,
            //                YearMonth = trialVerify.YearMonth
            //            };

            //}
            //else if (param.StatType == 1)
            //{
            //    query = (from trialVerify in _trialRevenuesPriceVerificationRepository.GetAll()
            //        join trail in _trialRepository.GetAll() on trialVerify.TrialId equals trail.Id
            //        select new RevenusVerifyDTO()
            //        {
            //            ReviewerCode = "",
            //            TrialCode = trail.Code,
            //            YearMonth = ""
            //        }).Distinct();
            //}
            //else if (param.StatType == 2)
            //{
            //    query = (from trialVerify in _trialRevenuesPriceVerificationRepository.GetAll()
            //        join trail in _trialRepository.GetAll() on trialVerify.TrialId equals trail.Id
            //        join reviewer in _doctorRepository.GetAll() on trialVerify.ReviewerId equals reviewer.Id
            //        select new RevenusVerifyDTO()
            //        {
            //            ReviewerCode = reviewer.Code,
            //            TrialCode = trail.Code,
            //            YearMonth = ""
            //        }).Distinct();
            //}
            //else
            //{
            //    query = from trialVerify in _trialRevenuesPriceVerificationRepository.GetAll()
            //        join trail in _trialRepository.GetAll() on trialVerify.TrialId equals trail.Id
            //        join reviewer in _doctorRepository.GetAll() on trialVerify.ReviewerId equals reviewer.Id
            //        select new RevenusVerifyDTO()
            //        {
            //            ReviewerCode = reviewer.Code,
            //            TrialCode = trail.Code,
            //            YearMonth = trialVerify.YearMonth
            //        };
            //}


            #endregion



        }
    }
}