using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.PTG;
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.Ariticle;
using ZhonTai.DynamicApi.Attributes;
using ZhonTai.DynamicApi;
using ZhonTai.Admin.Services.Article.Dto;

namespace ZhonTai.Admin.Services.Article
{    
   /// <summary>
   /// 文章服务
   /// </summary>
    [DynamicApi(Area = AdminConsts.AreaName)]
    public class ArticleService : BaseService, IArticleService, IDynamicApi
    {
        private IAriticleRepository  _ariticleRepository => LazyGetRequiredService<IAriticleRepository>();

        public ArticleService()
        {
        }

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

        /// <summary>
        /// 查询分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<AriticleListOutput>> GetPageAsync(PageInput<AriticleGetPageDto> input)
        {            
            var list = await _ariticleRepository.Select            
            .Count(out var total)
            .OrderByDescending(true, c => c.Rank)
            .OrderByDescending(true, c => c.Id)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync<AriticleListOutput>();

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

            return data;
        }

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

            return entity.Id;
        }

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

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

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