namespace Admin.NET.Core.Service;
///
/// 系统用户角色服务
///
public class SysUserRoleService : ITransient
{
private readonly SqlSugarRepository _sysUserRoleRep;
public SysUserRoleService(SqlSugarRepository sysUserRoleRep)
{
_sysUserRoleRep = sysUserRoleRep;
}
///
/// 授权用户角色
///
///
///
[UnitOfWork]
public async Task GrantUserRole(UserRoleInput input)
{
await _sysUserRoleRep.DeleteAsync(u => u.UserId == input.UserId);
if (input.RoleIdList == null || input.RoleIdList.Count < 1) return;
var roles = input.RoleIdList.Select(u => new SysUserRole
{
UserId = input.UserId,
RoleId = u
}).ToList();
await _sysUserRoleRep.InsertRangeAsync(roles);
}
///
/// 根据角色Id删除用户角色
///
///
///
public async Task DeleteUserRoleByRoleId(long roleId)
{
await _sysUserRoleRep.DeleteAsync(u => u.RoleId == roleId);
}
///
/// 根据用户Id删除用户角色
///
///
///
public async Task DeleteUserRoleByUserId(long userId)
{
await _sysUserRoleRep.DeleteAsync(u => u.UserId == userId);
}
///
/// 根据用户Id获取角色集合
///
///
///
public async Task> GetUserRoleList(long userId)
{
var sysUserRoleList = await _sysUserRoleRep.AsQueryable()
.Mapper(u => u.SysRole, u => u.RoleId)
.Where(u => u.UserId == userId).ToListAsync();
return sysUserRoleList.Where(u => u.SysRole != null).Select(u => u.SysRole).ToList();
}
///
/// 根据用户Id获取角色Id集合
///
///
///
public async Task> GetUserRoleIdList(long userId)
{
return await _sysUserRoleRep.AsQueryable()
.Where(u => u.UserId == userId).Select(u => u.RoleId).ToListAsync();
}
}