using Myshipping.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Furion.FriendlyException;
using Myshipping.Core.Entity;

namespace Myshipping.Core.Service
{
    /// <summary>
    /// 钉钉客服通知群配置服务
    /// </summary>
    [ApiDescriptionSettings(Name = "DjyDingtalkGroupConfig", Order = 1)]
    public class DjyDingtalkGroupConfigService : IDjyDingtalkGroupConfigService, IDynamicApiController, ITransient
    {
        private readonly SqlSugarRepository<DjyDingtalkGroupConfig> _rep;
        private readonly ILogger<DjyDingtalkGroupConfig> _logger;

        public DjyDingtalkGroupConfigService(SqlSugarRepository<DjyDingtalkGroupConfig> rep, ILogger<DjyDingtalkGroupConfig> logger)
        {
            _rep = rep;
            _logger = logger;
        }

        /// <summary>
        /// 分页查询钉钉客服通知群配置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/DjyDingtalkGroupConfig/page")]
        public async Task<dynamic> Page([FromQuery] QueryDjyDingtalkGroupConfigInput input)
        {
            var entities = await _rep.AsQueryable()
                                     .WhereIF(!string.IsNullOrWhiteSpace(input.Code), u => u.Code == input.Code)
                                     .WhereIF(!string.IsNullOrWhiteSpace(input.Desp), u => u.Desp.Contains(input.Desp.Trim()))
                                     .WhereIF(!string.IsNullOrWhiteSpace(input.Keyword), u => u.Keyword.Contains(input.Keyword.Trim()))
                                     .ToPagedListAsync(input.PageNo, input.PageSize);
            return entities.XnPagedResult();
        }

        /// <summary>
        /// 增加钉钉客服通知群配置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/DjyDingtalkGroupConfig/add")]
        public async Task Add(AddDjyDingtalkGroupConfigInput input)
        {
            var cc = _rep.Count(x => x.Code == input.Code);
            if (cc > 0)
            {
                throw Oops.Bah("代码重复");
            }

            var entity = input.Adapt<DjyDingtalkGroupConfig>();
            await _rep.InsertAsync(entity);
        }

        /// <summary>
        /// 更新钉钉客服通知群配置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/DjyDingtalkGroupConfig/edit")]
        public async Task Update(UpdateDjyDingtalkGroupConfigInput input)
        {
            var cc = _rep.Count(x => x.Code == input.Code && x.Id != input.Id);
            if (cc > 0)
            {
                throw Oops.Bah("代码重复");
            }

            var entity = input.Adapt<DjyDingtalkGroupConfig>();
            await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
        }

        /// <summary>
        /// 删除钉钉客服通知群配置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/DjyDingtalkGroupConfig/delete")]
        public async Task Delete(GetDjyDingtalkGroupConfigInput input)
        {
            var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
            await _rep.DeleteAsync(entity);
        }

        /// <summary>
        /// 获取钉钉客服通知群配置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/DjyDingtalkGroupConfig/detail")]
        public async Task<DjyDingtalkGroupConfig> Get([FromQuery] GetDjyDingtalkGroupConfigInput input)
        {
            return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
        }

        ///// <summary>
        ///// 获取钉钉客服通知群配置列表
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //[HttpGet("/DjyDingtalkGroupConfig/list")]
        //public async Task<dynamic> List([FromQuery] QueryDjyDingtalkGroupConfigInput input)
        //{
        //    return await _rep.ToListAsync();
        //}
    }
}