using Attribute;
using Dto;
using Extensions;
using Model;
using Model.Base;
using Repository;
using Service;
namespace Services
{
///
/// 用户角色
///
[AppService(ServiceType = typeof(ISysUserRoleService), ServiceLifetime = LifeTime.Transient)]
public class SysUserRoleService : BaseService, ISysUserRoleService
{
///
/// 通过角色ID查询角色使用数量
///
///
///
public int CountUserRoleByRoleId(long roleId)
{
return Count(it => it.RoleId == roleId);
}
///
/// 删除用户角色
///
///
///
public int DeleteUserRoleByUserId(int userId)
{
return Delete(it => it.UserId == userId) ? 1 : 0;
}
///
/// 批量删除角色对应用户
///
///
///
///
public int DeleteRoleUserByUserIds(long roleId, List userIds)
{
return Delete(it => it.RoleId == roleId && userIds.Contains(it.UserId)) ? 1 : 0;
}
///
/// 添加用户角色
///
///
///
public int AddUserRole(List sysUserRoles)
{
return Insert(sysUserRoles);
}
///
/// 获取用户数据根据角色id
///
///
///
public List GetSysUsersByRoleId(long roleId)
{
return Context.Queryable((t1, u) => new JoinQueryInfos(
JoinType.Left, t1.UserId == u.userId))
.WithCache(60 * 10)
.Where((t1, u) => t1.RoleId == roleId && u.DelFlag == "0")
.Select((t1, u) => u)
.ToList();
}
///
/// 获取用户数据根据角色id
///
///
///
public PagedInfo GetSysUsersByRoleId(RoleUserQueryDto roleUserQueryDto)
{
var query = Context.Queryable((t1, u) => new JoinQueryInfos(
JoinType.Left, t1.UserId == u.userId))
.Where((t1, u) => t1.RoleId == roleUserQueryDto.RoleId && u.DelFlag == "0");
if (!string.IsNullOrEmpty(roleUserQueryDto.UserName))
{
query = query.Where((t1, u) => u.username.Contains(roleUserQueryDto.UserName));
}
return query.Select((t1, u) => u).ToPage(roleUserQueryDto);
}
///
/// 获取尚未指派的用户数据根据角色id
///
///
///
public PagedInfo GetExcludedSysUsersByRoleId(RoleUserQueryDto roleUserQueryDto)
{
var query = Context.Queryable()
.Where(it => it.DelFlag == "0")
.Where(it => SqlFunc.Subqueryable().Where(s => s.UserId == it.userId && s.RoleId == roleUserQueryDto.RoleId).NotAny())
.WhereIF(roleUserQueryDto.UserName.IsNotEmpty(), it => it.username.Contains(roleUserQueryDto.UserName));
return query.ToPage(roleUserQueryDto);
}
///
/// 新增用户角色信息
///
///
///
public int InsertUserRole(SysUser user)
{
//if(user.RoleIds == null) return 0;
List userRoles = new();
foreach (var item in user.RoleIds)
{
userRoles.Add(new SysUserRole() { RoleId = item, UserId = user.userId });
}
return userRoles.Count > 0 ? AddUserRole(userRoles) : 0;
}
///
/// 新增加角色用户
///
///
///
public int InsertRoleUser(RoleUsersCreateDto roleUsersCreateDto)
{
List userRoles = new();
foreach (var item in roleUsersCreateDto.UserIds)
{
userRoles.Add(new SysUserRole() { RoleId = roleUsersCreateDto.RoleId, UserId = item });
}
return userRoles.Count > 0 ? AddUserRole(userRoles) : 0;
}
}
}