using LifePayment.Application.Contracts;
using LifePayment.Domain.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Caching;
using Volo.Abp.Uow;

namespace LifePayment.HttpApi
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class AreaController : AbpController
    {
        private readonly IAreaService _areaService;
        private readonly IDistributedCache<List<AreaInfo>> _distributedCache;
        private readonly IDistributedCache<List<AreaDto>> _distributedAreaCache;

        public AreaController(
               IDistributedCache<List<AreaInfo>> distributedCache,
               IDistributedCache<List<AreaDto>> distributedAreaCache,
               IAreaService areaService)
        {
            _areaService = areaService;
            _distributedCache = distributedCache;
            _distributedAreaCache = distributedAreaCache;
        }

        /// <summary>
        /// 获取省市区
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<List<AreaDto>> GetAreaList()
        {
            return await _distributedAreaCache.GetOrAddAsync(LifePaymentConstant.区域信息缓存key, async () =>
            {
                return await _areaService.GetAreaList(new GetAreaListInput());
            });
        }

        /// <summary>
        /// 资讯管理--适用区域
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<AreaDto>> GetApplicableAreaList()
        {
            return await _areaService.GetAreaList(new GetAreaListInput
            {
                Layer = LifePaymentConstant.AreaLayerType.省,
            });
        }

        /// <summary>
        /// 获取区域列表(省、市、区)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [UnitOfWork(IsDisabled = true)]
        public async Task<List<AreaDto>> GetAreas(GetAreaListInput input)
        {
            return await _areaService.GetAreaList(input);
        }

        /// <summary>
        /// 搜索管理--列表(区域管理)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<List<AreaInfo>> GetRegionalManagementList()
        {
            return await _distributedCache.GetOrAddAsync(LifePaymentConstant.区域信息缓存key2, async () =>
            {
                return await _areaService.GetRegionalManagementList();
            });
        }
    }
}