101 lines
3.6 KiB
C#
101 lines
3.6 KiB
C#
using LFlow.Base.Interfaces;
|
|
using LFlow.Base.Utils;
|
|
using LFlow.Cache.Interface;
|
|
using LFlow.Permission.Model;
|
|
using Mapster;
|
|
using SqlSugar;
|
|
|
|
namespace LFlow.Permission.Service
|
|
{
|
|
/// <summary>
|
|
/// 角色服务
|
|
/// </summary>
|
|
public class PermissionService(IRepo<PermissionModel, string> repo, ISelfCache cacher) : IPermissionService
|
|
{
|
|
/// <summary>
|
|
/// 添加权限项
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public Task<PermissionDto> AddPermissionAsync(PermissionDto model)
|
|
{
|
|
var savedModel = repo.SaveOrUpdate(model.Adapt<PermissionModel>(), isUpdate: false);
|
|
var result = savedModel?.MapTo<PermissionDto>() ?? throw new InvalidOperationException("Failed to add the permission model.");
|
|
return Task.FromResult(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除权限项并清理缓存
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public Task<int> DeletePermissionAsync(string id)
|
|
{
|
|
cacher.RemoveAsync(id);
|
|
return Task.FromResult(repo.DeleteById(id));
|
|
}
|
|
/// <summary>
|
|
/// 根据ID获取权限
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<PermissionDto> GetPermissionAsync(string id)
|
|
{
|
|
var cachedPermission = await cacher.GetAsync<PermissionModel>(id);
|
|
if (cachedPermission != null)
|
|
{
|
|
return cachedPermission!.MapTo<PermissionDto>()!;
|
|
}
|
|
var permission = await Task.FromResult(repo.Get(id));
|
|
if (permission != null)
|
|
{
|
|
await cacher.SetAsync(id, permission);
|
|
}
|
|
return permission?.MapTo<PermissionDto>() ?? new PermissionDto();
|
|
}
|
|
/// <summary>
|
|
/// 获取权限列表
|
|
/// </summary>
|
|
/// <param name="pageIndex"></param>
|
|
/// <param name="pageSize"></param>
|
|
/// <param name="total"></param>
|
|
/// <returns></returns>
|
|
public Task<List<PermissionDto>> GetPermissionListAsync(int pageIndex, int pageSize, ref int total)
|
|
{
|
|
return Task.FromResult(repo.GetAll(pageIndex, pageSize, ref total).Adapt<List<PermissionDto>>());
|
|
}
|
|
/// <summary>
|
|
/// 获取程序权限列表
|
|
/// </summary>
|
|
/// <param name="progID"></param>
|
|
/// <returns></returns>
|
|
public Task<List<PermissionDto>> GetProgPerminssionListAsync(string progID)
|
|
{
|
|
return Task.FromResult(repo.Search(new PermissionModel
|
|
{
|
|
PermissionProgID = progID
|
|
}).Adapt<List<PermissionDto>>());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新权限项
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public Task<PermissionDto> UpdatePermissionAsync(PermissionDto model)
|
|
{
|
|
if (model != null && !string.IsNullOrEmpty(model.ID))
|
|
{
|
|
cacher.RemoveAsync(model.ID);
|
|
}
|
|
if (model != null)
|
|
{
|
|
var permissionModel = model?.Adapt<PermissionModel>() ?? throw new ArgumentNullException(nameof(model));
|
|
var savedModel = repo.SaveOrUpdate(permissionModel, isUpdate: true);
|
|
return Task.FromResult(savedModel?.MapTo<PermissionDto>() ?? throw new InvalidOperationException("Failed to update the permission model."));
|
|
}
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
}
|
|
}
|