using Furion.DependencyInjection;
using GDZZ.Core.Entity;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace GDZZ.Core.Service;
///
/// 角色数据范围服务
///
public class SysRoleDataScopeService : ISysRoleDataScopeService, ITransient
{
private readonly SqlSugarRepository _sysRoleDataScopeRep; // 角色数据范围表仓储
private readonly SqlSugarRepository _roleRep;
public SysRoleDataScopeService(SqlSugarRepository sysRoleDataScopeRep, SqlSugarRepository roleRep)
{
_sysRoleDataScopeRep = sysRoleDataScopeRep;
_roleRep = roleRep;
}
///
/// 授权角色数据范围
///
///
///
public async Task GrantDataScope(GrantRoleDataInput input)
{
try
{
_sysRoleDataScopeRep.CurrentBeginTran();
await _sysRoleDataScopeRep.DeleteAsync(u => u.SysRoleId == input.Id);
var grantOrgIdList = new List();
input.GrantOrgIdList.ForEach(u =>
{
grantOrgIdList.Add(
new SysRoleDataScope
{
SysRoleId = input.Id,
SysOrgId = u
});
});
await _roleRep.UpdateAsync(m => m.Id == input.Id,m => new SysRole() { DataScopeType = input.DataScopeType });
await _sysRoleDataScopeRep.InsertAsync(grantOrgIdList);
_sysRoleDataScopeRep.CurrentCommitTran();
}
catch (System.Exception)
{
_sysRoleDataScopeRep.CurrentRollbackTran();
throw;
}
}
///
/// 根据角色Id集合获取角色数据范围集合
///
///
///
public async Task> GetRoleDataScopeIdList(List roleIdList)
{
return await _sysRoleDataScopeRep
.Where(u => roleIdList.Contains(u.SysRoleId))
.Select(u => u.SysOrgId).ToListAsync();
}
///
/// 根据机构Id集合删除对应的角色-数据范围关联信息
///
///
///
public async Task DeleteRoleDataScopeListByOrgIdList(List orgIdList)
{
await _sysRoleDataScopeRep.DeleteAsync(u => orgIdList.Contains(u.SysOrgId));
}
///
/// 根据角色Id删除对应的角色-数据范围关联信息
///
///
///
public async Task DeleteRoleDataScopeListByRoleId(long roleId)
{
await _sysRoleDataScopeRep.DeleteAsync(u => u.SysRoleId == roleId);
}
}