using Furion.DependencyInjection;
using Myshipping.Core.Entity;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Myshipping.Core.Service;
///
/// 角色菜单
///
public class SysRoleMenuService : ISysRoleMenuService, ITransient
{
private readonly SqlSugarRepository _sysRoleMenuRep; // 角色菜单表仓储
private readonly ISysCacheService _sysCacheService;
private readonly SqlSugarRepository _sysRoleRep;
public SysRoleMenuService(SqlSugarRepository sysRoleMenuRep, ISysCacheService sysCacheService, SqlSugarRepository sysRoleRep)
{
_sysRoleMenuRep = sysRoleMenuRep;
_sysCacheService = sysCacheService;
_sysRoleRep = sysRoleRep;
}
///
/// 获取角色的菜单Id集合
///
///
///
public async Task> GetRoleMenuIdList(List roleIdList)
{
return await _sysRoleMenuRep
.Where(u => roleIdList.Contains(u.SysRoleId))
.Select(u => u.SysMenuId).ToListAsync();
}
///
/// 授权角色菜单
///
///
///
public async Task GrantMenu(GrantRoleMenuInput input)
{
try
{
_sysRoleMenuRep.CurrentBeginTran();
await _sysRoleMenuRep.DeleteAsync(u => u.SysRoleId == input.Id);
var grantMenuIdList = new List();
input.GrantMenuIdList.ForEach(u =>
{
grantMenuIdList.Add(
new SysRoleMenu
{
SysRoleId = input.Id,
SysMenuId = u
});
});
await _sysRoleMenuRep.InsertAsync(grantMenuIdList);
_sysRoleMenuRep.CurrentCommitTran();
}
catch (System.Exception)
{
_sysRoleMenuRep.CurrentRollbackTran();
throw;
}
// 清除缓存
await _sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_MENU);
await _sysCacheService.DelByPatternAsync(CommonConst.CACHE_KEY_PERMISSION);
}
///
/// 根据菜单Id集合删除对应的角色-菜单表信息
///
///
///
public async Task DeleteRoleMenuListByMenuIdList(List menuIdList)
{
await _sysRoleMenuRep.DeleteAsync(u => menuIdList.Contains(u.SysMenuId));
}
///
/// 根据角色Id删除对应的角色-菜单表关联信息
///
///
///
public async Task DeleteRoleMenuListByRoleId(long roleId)
{
await _sysRoleMenuRep.DeleteAsync(u => u.SysRoleId == roleId);
}
///
/// 清理租户下角色的权限
///
/// 租户id
/// 租户管理员的角色id
///
public async Task ClearRoleMenuListByTenantId(long tenantId, long manageRoleId)
{
List roleIds = await _sysRoleRep.Where(m => m.TenantId == tenantId).Select(m => m.Id).ToListAsync();
roleIds.Remove(manageRoleId);
var managePermissionList = await _sysRoleMenuRep.Where(m => m.SysRoleId == manageRoleId).Select(m => m.SysMenuId).ToListAsync();
await _sysRoleMenuRep.DeleteAsync(m => roleIds.Contains(m.SysRoleId) && !managePermissionList.Contains(m.SysMenuId));
}
}