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.Notice;
using ZhonTai.Admin.Services.Notice.Dto;
using ZhonTai.DynamicApi;
using ZhonTai.DynamicApi.Attributes;

namespace ZhonTai.Admin.Services.Notice
{    
        
    /// <summary>
    /// 公告服务
    /// </summary>
    [DynamicApi(Area = AdminConsts.AreaName)]
    public class NoticeService : BaseService, INoticeService, IDynamicApi
    {
        private INoticeRepository _moduleRepository => LazyGetRequiredService<INoticeRepository>();

        public NoticeService()
        {
        }

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

        /// <summary>
        /// 查询分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<NoticeListOutput>> GetPageAsync(PageInput<NoticeGetPageDto> input)
        {
            var list = await _moduleRepository.Select.Where(m=>m.IsAlter==0)            
            .Count(out var total)
            .OrderByDescending(true,c=>c.IsAlter)
            .OrderByDescending(true, c => c.Rank)
            .OrderByDescending(true, c => c.Id)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync<NoticeListOutput>();
            var data = new PageOutput<NoticeListOutput>()
            {
                List = list,
                Total = total
            };

            return data;
        }

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

            return entity.Id;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task UpdateAsync(NoticeUpdateInput input)
        {
            var entity = await _moduleRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                throw ResultOutput.Exception("模块不存在");
            }

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

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

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task BatchSoftDeleteAsync(long[] ids)
        {
            await _moduleRepository.SoftDeleteAsync(ids);
        }        
        /// <summary>
        /// 查询弹窗模块
        /// </summary>        
        /// <returns></returns>
        public async Task<NoticeGetOutput> GetWeightAsync()
        {
            var result = await _moduleRepository.Where(m=>m.IsAlter==1).ToOneAsync<NoticeGetOutput>();
            return result;
        }                
    }
}
