using IRaCIS.Application.Contracts;
using IRaCIS.Application.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Panda.DynamicWebApi.Attributes;

namespace IRaCIS.Core.Application.Service
{
    [ApiExplorerSettings(GroupName = "Financial")]
    public class ReviewerPayInfoService(IRepository<ReviewerPayInformation> _doctorPayInfoRepository,
        IRepository<Doctor> _doctorRepository,
        IRepository<RankPrice> _rankPriceRepository,
        IRepository<Hospital> _hospitalRepository, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, IReviewerPayInfoService
    {

        [NonDynamicMethod]
        public async Task<IResponseOutput> AddOrUpdateReviewerPayInfo(ReviewerPayInfoCommand addOrUpdateModel, Guid userId)
        {
            var success = false;
            var doctorPayInfoExistedItem = await _doctorPayInfoRepository.FirstOrDefaultAsync(u => u.DoctorId == addOrUpdateModel.DoctorId);
            if (doctorPayInfoExistedItem == null)//insert
            {

                await _doctorPayInfoRepository.InsertFromDTOAsync(addOrUpdateModel);

            }
            else//update
            {
                await _doctorPayInfoRepository.UpdateFromDTOAsync(addOrUpdateModel);

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

        /// <summary>
        /// 获取医生支付信息列表
        /// </summary>
        [HttpPost]
        public async Task<PageOutput<DoctorPayInfoQueryListDTO>> GetReviewerPayInfoList(DoctorPaymentInfoQueryDTO inQuery)
        {


            var doctorQueryable = from doctor in _doctorRepository.AsQueryable()
                                  .WhereIf(inQuery.HospitalId != null, o => o.HospitalId == inQuery.HospitalId)
                                  .WhereIf(!string.IsNullOrEmpty(inQuery.SearchName),
                                  u => u.ChineseName.Contains(inQuery.SearchName) || (u.LastName + u.FirstName).Contains(inQuery.SearchName))
                                  join hospitalItem in _hospitalRepository.AsQueryable() on doctor.HospitalId equals hospitalItem.Id into gt
                                  from hospital in gt.DefaultIfEmpty()
                                  join trialPayInfo in _doctorPayInfoRepository.Where()
                                  on doctor.Id equals trialPayInfo.DoctorId into payInfo
                                  from doctorPayInfo in payInfo.DefaultIfEmpty()
                                  join rankPrice in _rankPriceRepository.Where()
                                  on doctorPayInfo.RankId equals rankPrice.Id into rankPriceInfo
                                  from rankPrice in rankPriceInfo.DefaultIfEmpty()
                                  select new DoctorPayInfoQueryListDTO
                                  {
                                      //Id = doctorPayInfo.Id,
                                      DoctorId = doctor.Id,
                                      Code = doctor.ReviewerCode,
                                      LastName = doctor.LastName,
                                      FirstName = doctor.FirstName,
                                      ChineseName = doctor.ChineseName,
                                      Phone = doctor.Phone,
                                      DoctorNameInBank = doctorPayInfo.DoctorNameInBank,
                                      IDCard = doctorPayInfo.IDCard,
                                      BankCardNumber = doctorPayInfo.BankCardNumber,
                                      BankName = doctorPayInfo.BankName,
                                      RankId = doctorPayInfo.RankId,
                                      RankName = rankPrice.RankName,
                                      Additional = doctorPayInfo.Additional,
                                      Hospital = hospital.HospitalName,
                                      CreateTime = doctor.CreateTime
                                  };

            return await doctorQueryable.ToPagedListAsync(inQuery);


        }


        /// <summary>
        /// 根据医生Id获取支付信息
        /// </summary>
        /// <param name="doctorId">医生Id</param>
        /// <returns></returns>
        [HttpGet("{doctorId:guid}")]
        public async Task<DoctorPayInfoQueryListDTO> GetReviewerPayInfo(Guid doctorId)
        {
            var doctorQueryable = from doctor in _doctorRepository.Where(u => u.Id == doctorId)
                                  join trialPayInfo in _doctorPayInfoRepository.Where()
                                  on doctor.Id equals trialPayInfo.DoctorId into payInfo
                                  from doctorPayInfo in payInfo.DefaultIfEmpty()
                                  join rankPrice in _rankPriceRepository.Where()
                                  on doctorPayInfo.RankId equals rankPrice.Id into rankPriceInfo
                                  from rankPrice in rankPriceInfo.DefaultIfEmpty()
                                  select new DoctorPayInfoQueryListDTO
                                  {
                                      //Id = doctorPayInfo.Id,
                                      DoctorId = doctor.Id,
                                      Code = doctor.ReviewerCode,
                                      LastName = doctor.LastName,
                                      FirstName = doctor.FirstName,
                                      ChineseName = doctor.ChineseName,
                                      Phone = doctor.Phone,
                                      DoctorNameInBank = doctorPayInfo.DoctorNameInBank,
                                      IDCard = doctorPayInfo.IDCard,
                                      BankCardNumber = doctorPayInfo.BankCardNumber,
                                      BankName = doctorPayInfo.BankName,
                                      RankId = doctorPayInfo.RankId,
                                      RankName = rankPrice.RankName,
                                      Additional = doctorPayInfo.Additional,
                                      CreateTime = doctor.CreateTime
                                  };

            return (await doctorQueryable.FirstOrDefaultAsync()).IfNullThrowException();
        }

        /// <summary>
        /// 根据rankId 获取ReviewerId，用于当Rank的单价信息改变时，触发费用计算
        /// </summary>
        /// <param name="rankId"></param>
        /// <returns></returns>
        public async Task<List<Guid>> GetReviewerIdByRankId(Guid rankId)
        {
            return await _doctorPayInfoRepository.Where(u => u.RankId == rankId).Select(u => u.DoctorId).ToListAsync();
        }
    }
}
