using Furion.DependencyInjection;
using GDZZ.Core.Entity;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace GDZZ.Core.Service;
///
/// 用户数据范围服务
///
public class SysUserDataScopeService : ISysUserDataScopeService, ITransient
{
private readonly SqlSugarRepository _sysUserDataScopeRep; // 用户数据范围表仓储
public SysUserDataScopeService(SqlSugarRepository sysUserDataScopeRep)
{
_sysUserDataScopeRep = sysUserDataScopeRep;
}
///
/// 授权用户数据
///
///
///
public async Task GrantData(UpdateUserInput input)
{
try
{
_sysUserDataScopeRep.CurrentBeginTran();
await _sysUserDataScopeRep.DeleteAsync(u => u.SysUserId == input.Id);
var grantOrgIdList = new List();
input.GrantOrgIdList.ForEach(u =>
{
grantOrgIdList.Add(
new SysUserDataScope
{
SysUserId = input.Id,
SysOrgId = u
});
});
await _sysUserDataScopeRep.InsertAsync(grantOrgIdList);
_sysUserDataScopeRep.CurrentCommitTran();
}
catch (System.Exception)
{
_sysUserDataScopeRep.CurrentRollbackTran();
throw;
}
}
///
/// 获取用户的数据范围Id集合
///
///
///
public async Task> GetUserDataScopeIdList(long userId)
{
return await _sysUserDataScopeRep
.Where(u => u.SysUserId == userId)
.Select(u => u.SysOrgId).ToListAsync();
}
///
/// 根据机构Id集合删除对应的用户-数据范围关联信息
///
///
///
public async Task DeleteUserDataScopeListByOrgIdList(List orgIdList)
{
await _sysUserDataScopeRep.DeleteAsync(u => orgIdList.Contains(u.SysOrgId));
}
///
/// 根据用户Id删除对应的用户-数据范围关联信息
///
///
///
public async Task DeleteUserDataScopeListByUserId(long userId)
{
await _sysUserDataScopeRep.DeleteAsync(u => u.SysUserId == userId);
}
}