132 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			132 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			C#
		
	
	
	
using GraphQL;
 | 
						|
using LY.App.Common.Redis;
 | 
						|
using LY.App.Extensions.DI;
 | 
						|
using LY.App.Model;
 | 
						|
using Mapster;
 | 
						|
using SqlSugar;
 | 
						|
 | 
						|
namespace LY.App.Service
 | 
						|
{
 | 
						|
    /// <summary>
 | 
						|
    /// 业务层白名单服务
 | 
						|
    /// </summary>
 | 
						|
    [ServiceInjection(InjectionType.Transient)]
 | 
						|
    public class WhitListService
 | 
						|
    {
 | 
						|
        private readonly SqlSugarClient _db;
 | 
						|
        private readonly RedisService _redisService;
 | 
						|
        /// <summary>
 | 
						|
        /// 构造函数
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sqlSugarClient"></param>
 | 
						|
        /// <param name="redisService"></param>
 | 
						|
        public WhitListService(SqlSugarClient sqlSugarClient, RedisService redisService)
 | 
						|
        {
 | 
						|
            _db = sqlSugarClient;
 | 
						|
            _redisService = redisService;
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 新增白名单
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="input"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<ApiResult> Add(AddWhitelist input)
 | 
						|
        {
 | 
						|
            var result = new ApiResult();
 | 
						|
            var entity = input.Adapt<Whitelist>();
 | 
						|
            var exisit = await _db.Queryable<Whitelist>().AnyAsync(x => x.sn == entity.sn && x.IsDeleted == false);
 | 
						|
            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;
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 删除白名单
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="id"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<ApiResult> Delete(long id)
 | 
						|
        {
 | 
						|
            var result = new ApiResult();
 | 
						|
            var entity = await _db.Queryable<Whitelist>().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;
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 获取白名单列表
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="pageNum"></param>
 | 
						|
        /// <param name="pageSize"></param>
 | 
						|
        /// <param name="key"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<ApiResult> GetList(int pageNum = 1, int pageSize = 10, string key = null)
 | 
						|
        {
 | 
						|
            var result = new ApiResult();
 | 
						|
            RefAsync<int> total = 0;
 | 
						|
            var items = await _db.Queryable<Whitelist>()
 | 
						|
                .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
 | 
						|
                }
 | 
						|
            };
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 获取白名单详情
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="id"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<ApiResult> GetById(long id)
 | 
						|
        {
 | 
						|
            var entity = await _db.Queryable<Whitelist>().FirstAsync(x => x.Id == id);
 | 
						|
            if (entity != null)
 | 
						|
            {
 | 
						|
                return new ApiResult(true, null);
 | 
						|
            }
 | 
						|
            return new ApiResult(false, "未找到该白名单");
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 更新白名单
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="input"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<ApiResult> Update(UpdateWhitelist input)
 | 
						|
        {
 | 
						|
            var entity = input.Adapt<Whitelist>();
 | 
						|
 | 
						|
            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 = "更新失败" };
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |