123 lines
4.1 KiB
C#
123 lines
4.1 KiB
C#
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);
|
|
if (exisit)
|
|
{
|
|
result.code = 1;
|
|
result.msg = $"sn:{input.sn}已经存在白名单中";
|
|
return result;
|
|
}
|
|
var id = await _db.Insertable(entity).ExecuteReturnSnowflakeIdAsync();
|
|
await _redisService.SetAsync(entity.sn, 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();
|
|
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, int pageSize, string key)
|
|
{
|
|
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);
|
|
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>();
|
|
await _db.Updateable(entity).ExecuteCommandAsync();
|
|
if (entity != null)
|
|
{
|
|
entity.Adapt(input);
|
|
await _db.Updateable(entity).ExecuteCommandAsync();
|
|
return new ApiResult() { code = 0 };
|
|
}
|
|
return new ApiResult() { code = 1, msg = "更新失败" };
|
|
}
|
|
}
|
|
}
|