104 lines
3.3 KiB
C#
104 lines
3.3 KiB
C#
using LFlow.Base.Interfaces;
|
|
using LFlow.Base.Utils;
|
|
using LFlow.Cache.Interface;
|
|
using LFlow.Permission.Service;
|
|
using LFlow.Role.Model;
|
|
using Mapster;
|
|
|
|
namespace LFlow.Role.Service
|
|
{
|
|
/// <summary>
|
|
/// 角色服务
|
|
/// </summary>
|
|
public class RoleService(IRepo<RoleModel, string> repo, IPermissionService permissionService, ISelfCache cacher) : IRoleService
|
|
{
|
|
/// <summary>
|
|
/// 添加角色
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public Task<RoleDto> AddRoleAsync(RoleDto model)
|
|
{
|
|
var savedRole = repo.SaveOrUpdate(model.Adapt<RoleModel>(), isUpdate: false);
|
|
if (savedRole == null)
|
|
{
|
|
throw new InvalidOperationException("Failed to save or update the role.");
|
|
}
|
|
var roleDto = savedRole.MapTo<RoleDto>();
|
|
if (roleDto == null)
|
|
{
|
|
throw new InvalidOperationException("Failed to map the saved role to RoleDto.");
|
|
}
|
|
return Task.FromResult(roleDto);
|
|
}
|
|
/// <summary>
|
|
/// 删除角色并清理缓存
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public Task<int> DeleteRoleAsync(string id)
|
|
{
|
|
cacher.RemoveAsync(id);
|
|
return Task.FromResult(repo.DeleteById(id));
|
|
}
|
|
|
|
public async Task<RoleDto> GetRolePerminssionListAsync(string roleId)
|
|
{
|
|
var roleDto = GetRoleAsync(roleId).Result;
|
|
if (roleDto == null)
|
|
{
|
|
return new RoleDto();
|
|
}
|
|
var permissions = await permissionService.GetProgPerminssionListAsync(roleId);
|
|
if (permissions != null && permissions.Count >= 0)
|
|
{
|
|
roleDto.Permissions = permissions!;
|
|
return roleDto;
|
|
}
|
|
else
|
|
{
|
|
return new RoleDto();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID获取角色
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<RoleDto> GetRoleAsync(string id)
|
|
{
|
|
var cachedRole = await cacher.GetAsync<RoleDto>(id);
|
|
if (cachedRole != null)
|
|
{
|
|
return cachedRole;
|
|
}
|
|
var role = await Task.FromResult(repo.Get(id));
|
|
if (role != null)
|
|
{
|
|
await cacher.SetAsync(id, role);
|
|
}
|
|
return role?.MapTo<RoleDto>() ?? new RoleDto();
|
|
}
|
|
/// <summary>
|
|
/// 获取角色列表
|
|
/// </summary>
|
|
/// <param name="pageIndex"></param>
|
|
/// <param name="pageSize"></param>
|
|
/// <param name="total"></param>
|
|
/// <returns></returns>
|
|
public Task<List<RoleDto>> GetRoleListAsync(int pageIndex, int pageSize, ref int total)
|
|
{
|
|
var roleModels = repo.GetAll(pageIndex, pageSize, ref total);
|
|
var roleDtos = roleModels?.Select(role => role.MapTo<RoleDto>()).ToList() ?? [];
|
|
return Task.FromResult(roleDtos ?? [])!;
|
|
}
|
|
|
|
public Task<RoleDto> UpdateRoleAsync(RoleDto model)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
}
|
|
}
|