using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhonTai.Admin.Core.Consts;
using ZhonTai.Admin.Core.Dto;
using ZhonTai.Admin.Domain.Banner;
using ZhonTai.Admin.Services.Banner.Dto;
using ZhonTai.DynamicApi;
using ZhonTai.DynamicApi.Attributes;

namespace ZhonTai.Admin.Services.Banner
{
    /// <summary>
    /// 轮播图服务
    /// </summary>
    [DynamicApi(Area = AdminConsts.AreaName)]
    public class BannerService : BaseService, IBannerService, IDynamicApi
    {
        private IBannerRepository _bannerRepository => LazyGetRequiredService<IBannerRepository>();

        public BannerService()
        {
        }

        /// <summary>
        /// 查询模块
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<BannerGetOutput> GetAsync(long id)
        {
            var result = await _bannerRepository.GetAsync<BannerGetOutput>(id);
            return result;
        }

        /// <summary>
        /// 查询分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<BannerListOutput>> GetPageAsync(PageInput<BannerGetPageDto> input)
        {
            //1未开始 2进行中 3已结束
            var status = input.Filter?.Status;
            DateTime dtnow = DateTime.Now;

            var list = await _bannerRepository.Select
            .WhereIf(status.HasValue&&status==1, a => a.BeginTime> dtnow)
            .WhereIf(status.HasValue && status == 2, a => a.BeginTime < dtnow && a.EndTime> dtnow)
            .WhereIf(status.HasValue && status == 3, a => a.EndTime < dtnow)
            .Count(out var total)
            .OrderByDescending(true, c => c.Rank)
            .OrderByDescending(true, c => c.Id)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync<BannerListOutput>();

            var data = new PageOutput<BannerListOutput>()
            {
                List = list,
                Total = total
            };

            return data;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<long> AddAsync(BannerAddInput input)
        {
            var entity = Mapper.Map<BannerEntity>(input);
            await _bannerRepository.InsertAsync(entity);

            return entity.Id;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task UpdateAsync(BannerUpdateInput input)
        {
            var entity = await _bannerRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                throw ResultOutput.Exception("轮播图信息不存在");
            }

            Mapper.Map(input, entity);
            await _bannerRepository.UpdateAsync(entity);
        }        

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task SoftDeleteAsync(long id)
        {
            await _bannerRepository.SoftDeleteAsync(id);
        }       
    }
}