using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XYY.Core.Standard.Mvc;
using XYY.Service.Standard.Finance.Charging.Base;
using XYY.Service.Standard.Finance.Charging.CustomerOrderFee;
using XYY.Service.Standard.Finance.Charging.Dto;
using XYY.Service.Standard.Finance.Charging.EUExchange;
using XYY.Service.Standard.Finance.Charging.Express;
using XYY.Service.Standard.Finance.Charging.FeeFormula;
using XYY.Service.Standard.Finance.Charging.UserRate;

namespace XYY.Api.Finance.Charges.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RedisCacheMangeController : ApiControllerBase
    {
        IExpressFeeChargesService _expressFeeChargesService;
        IEUExchangeService _exchangeService;
        IUserRateService _userRateService;
        IFormulaService _formulaService;
        IAttachedService _attachedService;
        public RedisCacheMangeController(
            IEUExchangeService exchangeService,
            IUserRateService userRateService,
            IExpressFeeChargesService expressFeeChargesService,
            IFormulaService formulaService, IAttachedService attachedService)
        {
            _exchangeService = exchangeService;
            _userRateService = userRateService;
            _expressFeeChargesService = expressFeeChargesService;
            _formulaService = formulaService;
            _attachedService = attachedService;
        }

        public async Task<IActionResult> RefreshExchangeRedisCache()
        {
            ApiJsonModel jsonModel = await _exchangeService.RefreshRedisCache();
            return Ok(jsonModel.error ?? jsonModel.message);
        }

        public async Task<IActionResult> RefreshUserRateRedisCache()
        {
            ApiJsonModel jsonModel = await _userRateService.RefreshRedisCache();
            return Ok(jsonModel.error ?? jsonModel.message);
        }

        /// <summary>
        /// 更新资费版本
        /// 更新缓存信息，当客户Id与渠道Id都为0时系统将会更新全部值
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="expressId"></param>
        /// <returns></returns>
        public async Task<IActionResult> RefreshExpressBaseRedisCache(int customerId, int expressId)
        {
            ApiJsonModel jsonModel = await _expressFeeChargesService.LoadExpressFeeBases(expressId, customerId);
            return Ok(jsonModel.error ?? jsonModel.message);
        }

        public async Task<IActionResult> RefreshAttachedRedisCache()
        {
            ApiJsonModel jsonModel = await _attachedService.RefreshRedisCache();
            return Ok(jsonModel.error ?? jsonModel.message);
        }
        public async Task<IActionResult> Refresh20230324AttachedRedisCache()
        {
            ApiJsonModel jsonModel = await _attachedService.RefreshRedis20230324Cache();
            return Ok(jsonModel.error ?? jsonModel.message);
        }

        [HttpPost]
        public async Task<IActionResult> BatchRefreshExpressBaseRedisCache(BatchRefreshFeeBaseCustomerParam param)
        {
            if (param.CustomerIds?.Any() != true)
                return Error("参数不能为空");

            foreach (var customerId in param.CustomerIds)
            {
                await _expressFeeChargesService.LoadExpressFeeBases(param.ExpressId, customerId);
            }
            return Ok();
        }

        [HttpPost]
        public async Task<IActionResult> BatchRefreshExpressBaseRedisCacheByExpressId(BatchRefreshFeeBaseChannelParam param)
        {
            if (param.ExpressIds?.Any() != true)
                return Error("参数不能为空");

            foreach (var expressId in param.ExpressIds)
            {
                await _expressFeeChargesService.LoadExpressFeeBases(expressId, param.CustomerId);
            }
            return Ok();
        }
         

        /// <summary>
        /// 快递资费公式刷新
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="expressId"></param>
        /// <returns></returns>
        public async Task<IActionResult> RefreshFormulaRedisCache(int customerId, int expressId)
        {
            ApiJsonModel jsonModel = await _formulaService.LoadFormula(expressId, customerId);
            return Ok(jsonModel.error ?? jsonModel.message);
        }

        /// <summary>
        /// 快递资费刷新
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="expressId"></param>
        /// <returns></returns>
        public async Task<IActionResult> RefreshOtherRedisCache(int customerId, int expressId)
        {
            ApiJsonModel jsonModel = await _formulaService.LoadOther(expressId, customerId);
            return Ok(jsonModel.error ?? jsonModel.message);
        }
        [HttpPost]
        public async Task<IActionResult> BatchRefreshOtherRedisCache(BatchRefreshFeeBaseCustomerParam param)
        {
            if (param.CustomerIds?.Any() != true)
                return Error("参数不能为空");

            foreach (var customerId in param.CustomerIds)
            {
                await _formulaService.LoadOther(param.ExpressId, customerId);
            }
            return Ok();
        }

        [HttpPost]
        public async Task<IActionResult> RefreshOtherRedisCacheByChannels(List<int> expressIds)
        {
            var jsonModel = await _formulaService.LoadOtherByChannelIds(expressIds);
            return Ok(jsonModel.error ?? jsonModel.message);
        }

        [HttpPost]
        public async Task<IActionResult> RefreshSupplement()
        {
             var jsonModel = await _expressFeeChargesService.LoadSupplement();
            return Ok(jsonModel.error ?? jsonModel.message);      
        }
    }
}
