using GraphQL;
using LY.App.Common.Redis;
using LY.App.Extensions.DI;
using LY.App.Model;
using Mapster;
using SqlSugar;
namespace LY.App.Service
{
    /// 
    /// 业务层白名单服务
    /// 
    [ServiceInjection(InjectionType.Transient)]
    public class WhitListService
    {
        private readonly SqlSugarClient _db;
        private readonly RedisService _redisService;
        /// 
        /// 构造函数
        /// 
        /// 
        /// 
        public WhitListService(SqlSugarClient sqlSugarClient, RedisService redisService)
        {
            _db = sqlSugarClient;
            _redisService = redisService;
        }
        /// 
        /// 新增白名单
        /// 
        /// 
        /// 
        public async Task Add(AddWhitelist input)
        {
            var result = new ApiResult();
            var entity = input.Adapt();
            var exisit = await _db.Queryable().AnyAsync(x => x.sn == entity.sn);
            if (exisit)
            {
                result.code = 1;
                result.msg = $"sn:{input.sn}已经存在白名单中";
                return result;
            }
            var id = await _db.Insertable(entity).ExecuteReturnSnowflakeIdAsync();
            var key = RedisKeyList.white_list(input.sn);
            await _redisService.SetAsync(key, entity);
            result.data = id.ToString();
            return result;
        }
        /// 
        /// 删除白名单
        /// 
        /// 
        /// 
        public async Task Delete(long id)
        {
            var result = new ApiResult();
            var entity = await _db.Queryable().FirstAsync(x => x.Id == id);
            if (entity != null)
            {
                entity.IsDeleted = true;
                await _db.Updateable(entity).ExecuteCommandAsync();
                var key = RedisKeyList.white_list(entity.sn);
                await _redisService.DeleteAsync(key);
                return result;
            }
            return result;
        }
        /// 
        /// 获取白名单列表
        /// 
        /// 
        /// 
        /// 
        /// 
        public async Task GetList(int pageNum = 1, int pageSize = 10, string key = null)
        {
            var result = new ApiResult();
            RefAsync total = 0;
            var items = await _db.Queryable()
                .Where(s => s.IsDeleted == false)
                .WhereIF(!string.IsNullOrEmpty(key), s => s.sn.Contains(key))
                .OrderByDescending(s => s.Id)
                .ToPageListAsync(pageNum, pageSize, total);
            items.ForEach(x =>
            {
                x.positionIds = x.positionId.Select(s => s.ToString()).ToList();
            });
            return new ApiResult()
            {
                data = new
                {
                    total = total.Value,
                    items
                }
            };
        }
        /// 
        /// 获取白名单详情
        /// 
        /// 
        /// 
        public async Task GetById(long id)
        {
            var entity = await _db.Queryable().FirstAsync(x => x.Id == id);
            if (entity != null)
            {
                return new ApiResult(true, null);
            }
            return new ApiResult(false, "未找到该白名单");
        }
        /// 
        /// 更新白名单
        /// 
        /// 
        /// 
        public async Task Update(UpdateWhitelist input)
        {
            var entity = input.Adapt();
     
            if (entity != null)
            {
                await _db.Updateable(entity).ExecuteCommandAsync();
                var key = RedisKeyList.white_list(input.sn);
                await _redisService.SetAsync(key, entity);
                return new ApiResult() { code = 0 };
            }
            return new ApiResult() { code = 1, msg = "更新失败" };
        }
    }
}