using AutoMapper;
using AutoMapper.QueryableExtensions;
using IRaCIS.Application.Interfaces;
using IRaCIS.Core.Domain.Share;
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 VolumeRewardService : BaseService, IVolumeRewardService
    {
        private readonly IRepository<VolumeReward> _volumeRewardRepository;
        

        public VolumeRewardService(IRepository<VolumeReward> volumeRewardRepository,IMapper mapper)
        {
            _volumeRewardRepository = volumeRewardRepository;
            
        }
        /// <summary>
        /// 批量添加或更新奖励费用单价
        /// </summary>
        [NonDynamicMethod]
        public async Task<IResponseOutput> AddOrUpdateVolumeRewardPriceList(IEnumerable<AwardPriceCommand> addOrUpdateModel)
        {
            await  _volumeRewardRepository.BatchDeleteNoTrackingAsync(t => t.Id != Guid.Empty);
            var temp = _mapper.Map<List<VolumeReward>>(addOrUpdateModel);
           
            await _volumeRewardRepository.AddRangeAsync(temp);
            var success = await _volumeRewardRepository.SaveChangesAsync();

            return ResponseOutput.Result(success);
        }


        /// <summary>
        /// 获取所有奖励单价列表-用于计算时，一次性获取所有
        /// </summary>
        [NonDynamicMethod]
        public async Task<List<AwardPriceCalculateDTO>> GetVolumeRewardPriceList()
        {
            return await _volumeRewardRepository.ProjectTo<AwardPriceCalculateDTO>(_mapper.ConfigurationProvider).OrderBy(t => t.Min).ToListAsync();
        }

        /// <summary>
        /// 分页获取奖励单价列表
        /// </summary>
        [HttpPost]
        public async Task<PageOutput<AwardPriceDTO>> GetVolumeRewardPriceList(AwardPriceQueryDTO queryParam)
        {
            var awardPriceQueryable = _volumeRewardRepository.ProjectTo<AwardPriceDTO>(_mapper.ConfigurationProvider);

            return await awardPriceQueryable.ToPagedListAsync(queryParam.PageIndex, queryParam.PageSize, "Min");
          
        }
    }
}