using AutoMapper;
using IRaCIS.Application.Interfaces;
using IRaCIS.Application.Contracts;
using System.Linq.Expressions;
using IRaCIS.Core.Infra.EFCore;
using IRaCIS.Core.Domain.Models;
using IRaCIS.Core.Infrastructure.Extention;
using Microsoft.AspNetCore.Mvc;
using Panda.DynamicWebApi.Attributes;

namespace IRaCIS.Application.Services
{
    [ ApiExplorerSettings(GroupName = "Financial")]
    public class TrialRevenuesPriceService : BaseService, ITrialRevenuesPriceService
    {
        private readonly IRepository<Trial> _trialRepository;
        private readonly IRepository<TrialRevenuesPrice> _trialRevenuesPriceRepository;
        private readonly IRepository<CRO> _croRepository;
        private readonly IRepository<Dictionary> _dictionaryRepository;
        private readonly IRepository<TrialRevenuesPriceVerification> _trialRevenuesPriceVerificationRepository;
        

        public TrialRevenuesPriceService(IRepository<Trial> trialRepository, IRepository<TrialRevenuesPrice> trialCostRepository, IRepository<CRO> croCompanyRepository, IRepository<Dictionary> dictionaryRepository, IRepository<TrialRevenuesPriceVerification> trialRevenuesPriceVerificationRepository, IMapper mapper)
        {
            _trialRepository = trialRepository;
            _trialRevenuesPriceRepository = trialCostRepository;
            _croRepository = croCompanyRepository;
            _dictionaryRepository = dictionaryRepository;
            _trialRevenuesPriceVerificationRepository = trialRevenuesPriceVerificationRepository;
            
        }

        public async Task<IResponseOutput> AddOrUpdateTrialRevenuesPrice(TrialRevenuesPriceDTO model)
        {
            var count = model.Timepoint +
                        model.TimepointIn24H +
                        model.TimepointIn48H +
                        model.Adjudication +
                        model.AdjudicationIn24H +
                        model.AdjudicationIn48H +
                        model.Downtime +
                        model.Global +
                        model.Training;

            if (count <= 0)
            {
                return ResponseOutput.NotOk("Please add meaningful data");
            }
            var trialExistedItem = await _trialRevenuesPriceRepository.FirstOrDefaultAsync(u => u.TrialId == model.TrialId);
            if (trialExistedItem == null)//insert
            {
                var trialCost = _mapper.Map<TrialRevenuesPrice>(model);
                await _trialRevenuesPriceRepository.AddAsync(trialCost);
                 var success = await _trialRevenuesPriceRepository.SaveChangesAsync();
                return ResponseOutput.Result(success, trialCost.Id.ToString());
            }
            else//update
            {
                //var trialRevenuesPrice = (await _trialRevenuesPriceRepository.AsQueryable().FirstOrDefaultAsync(u => u.TrialId == model.TrialId)).IfNullThrowException();

                await _trialRevenuesPriceRepository.UpdateFromDTOAsync(model);

                // 完善价格的 将对应的列设置为true 变更为有价格了

                var aaa = await _trialRevenuesPriceVerificationRepository.BatchUpdateNoTrackingAsync(t => t.TrialId == model.TrialId, u => new TrialRevenuesPriceVerification()
                {
                    //有价格 则设置为true  否则 该列不变
                    Timepoint = model.Timepoint > 0 || u.Timepoint,
                    TimepointIn24H = model.TimepointIn24H > 0 || u.TimepointIn24H,
                    TimepointIn48H = model.TimepointIn48H > 0 || u.TimepointIn48H,
                    Adjudication = model.Adjudication > 0 || u.Adjudication,
                    AdjudicationIn24H =
                    model.AdjudicationIn24H > 0 || u.AdjudicationIn24H,
                    AdjudicationIn48H =
                    model.AdjudicationIn48H > 0 || u.AdjudicationIn48H,
                    Global = model.Global > 0 || u.Global,
                    Downtime = model.Downtime > 0 || u.Downtime,
                    Training = model.Training > 0 || u.Training,
                    RefresherTraining = model.RefresherTraining > 0 || u.RefresherTraining,
                });

                //删除所有有价格的记录   为true 表示有价格或者不需要价格  缺价格的为false
               await _trialRevenuesPriceVerificationRepository.BatchDeleteNoTrackingAsync(t => t.TrialId == model.TrialId &&
                                                                      t.Timepoint&&
                                                                      t.TimepointIn24H&&
                                                                      t.TimepointIn48H &&
                                                                      t.Adjudication &&
                                                                      t.AdjudicationIn24H &&
                                                                      t.AdjudicationIn48H &&
                                                                      t.Global &&
                                                                      t.Training &&t.RefresherTraining);



                var success = await _trialRevenuesPriceRepository.SaveChangesAsync();
                return ResponseOutput.Result(success);
            }
        }

        [NonDynamicMethod]
        public async Task<bool> DeleteTrialCost(Guid id)
        {
            return await _trialRevenuesPriceRepository.BatchDeleteNoTrackingAsync(u => u.Id == id);
        }

        /// <summary>
        /// 获取项目收入费用信息列表[New]
        /// </summary>
        [HttpPost]
        public async Task<PageOutput<TrialRevenuesPriceDetialDTO>> GetTrialRevenuesPriceList(TrialRevenuesPriceQueryDTO queryParam)
        {
      
            var trialQueryable = from trial in _trialRepository.AsQueryable()
                                 .Where(u => u.TrialCode.Contains(queryParam.KeyWord)|| u.Indication.Contains(queryParam.KeyWord))
                                 .WhereIf(queryParam.CroId != null, o => o.CROId == queryParam.CroId)
                                 join cro in _croRepository.AsQueryable() on trial.CROId equals cro.Id into CRO
                                 from croInfo in CRO.DefaultIfEmpty()
                                 join dic in _dictionaryRepository.AsQueryable() on trial.ReviewModeId equals dic.Id into dict
                                 from dic in dict.DefaultIfEmpty()
                                 join trialCost in _trialRevenuesPriceRepository.AsQueryable()
                                 on trial.Id equals trialCost.TrialId into trialInfo
                                 from trialCostItem in trialInfo.DefaultIfEmpty()
                                 select new TrialRevenuesPriceDetialDTO
                                 {
                                     Id = trialCostItem == null ? Guid.Empty : trialCostItem.Id,
                                     TrialId = trial.Id,
                                     TrialCode = trial.TrialCode,
                                     Indication = trial.Indication,
                                     Cro = croInfo == null ? string.Empty : croInfo.CROName,
                                     ReviewMode = dic == null ? string.Empty : dic.Value,
                                     Timepoint = trialCostItem == null ? 0 : trialCostItem.Timepoint,
                                     TimepointIn24H = trialCostItem == null ? 0 : trialCostItem.TimepointIn24H,
                                     TimepointIn48H = trialCostItem == null ? 0 : trialCostItem.TimepointIn48H,
                                     Adjudication = trialCostItem == null ? 0 : trialCostItem.Adjudication,
                                     AdjudicationIn24H = trialCostItem == null ? 0 : trialCostItem.AdjudicationIn24H,
                                     AdjudicationIn48H = trialCostItem == null ? 0 : trialCostItem.AdjudicationIn48H,
                                     Downtime = trialCostItem == null ? 0 : trialCostItem.Downtime,
                                     Global = trialCostItem == null ? 0 : trialCostItem.Global,
                                     Training = trialCostItem == null ? 0 : trialCostItem.Training,
                                     RefresherTraining= trialCostItem == null ? 0 : trialCostItem.RefresherTraining,
                                     Expedited = trial.Expedited

                                 };

            return await trialQueryable.ToPagedListAsync(queryParam.PageIndex, queryParam.PageSize, "TrialCode", queryParam.Asc);
           
        }


    }
}
