123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using Attribute;
- using Dto;
- using Extensions;
- using Model;
- using Model.Base;
- using Repository;
- using Service;
- namespace Services
- {
- /// <summary>
- /// 用户角色
- /// </summary>
- [AppService(ServiceType = typeof(ISysUserRoleService), ServiceLifetime = LifeTime.Transient)]
- public class SysUserRoleService : BaseService<SysUserRole>, ISysUserRoleService
- {
- /// <summary>
- /// 通过角色ID查询角色使用数量
- /// </summary>
- /// <param name="roleId"></param>
- /// <returns></returns>
- public int CountUserRoleByRoleId(long roleId)
- {
- return Count(it => it.RoleId == roleId);
- }
- /// <summary>
- /// 删除用户角色
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- public int DeleteUserRoleByUserId(int userId)
- {
- return Delete(it => it.UserId == userId) ? 1 : 0;
- }
- /// <summary>
- /// 批量删除角色对应用户
- /// </summary>
- /// <param name="roleId"></param>
- /// <param name="userIds"></param>
- /// <returns></returns>
- public int DeleteRoleUserByUserIds(long roleId, List<long> userIds)
- {
- return Delete(it => it.RoleId == roleId && userIds.Contains(it.UserId)) ? 1 : 0;
- }
- /// <summary>
- /// 添加用户角色
- /// </summary>
- /// <param name="sysUserRoles"></param>
- /// <returns></returns>
- public int AddUserRole(List<SysUserRole> sysUserRoles)
- {
- return Insert(sysUserRoles);
- }
- /// <summary>
- /// 获取用户数据根据角色id
- /// </summary>
- /// <param name="roleId"></param>
- /// <returns></returns>
- public List<SysUser> GetSysUsersByRoleId(long roleId)
- {
- return Context.Queryable<SysUserRole, SysUser>((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();
- }
- /// <summary>
- /// 获取用户数据根据角色id
- /// </summary>
- /// <param name="roleUserQueryDto"></param>
- /// <returns></returns>
- public PagedInfo<SysUser> GetSysUsersByRoleId(RoleUserQueryDto roleUserQueryDto)
- {
- var query = Context.Queryable<SysUserRole, SysUser>((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);
- }
- /// <summary>
- /// 获取尚未指派的用户数据根据角色id
- /// </summary>
- /// <param name="roleUserQueryDto"></param>
- /// <returns></returns>
- public PagedInfo<SysUser> GetExcludedSysUsersByRoleId(RoleUserQueryDto roleUserQueryDto)
- {
- var query = Context.Queryable<SysUser>()
- .Where(it => it.DelFlag == "0")
- .Where(it => SqlFunc.Subqueryable<SysUserRole>().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);
- }
- /// <summary>
- /// 新增用户角色信息
- /// </summary>
- /// <param name="user"></param>
- /// <returns></returns>
- public int InsertUserRole(SysUser user)
- {
- //if(user.RoleIds == null) return 0;
- List<SysUserRole> userRoles = new();
- foreach (var item in user.RoleIds)
- {
- userRoles.Add(new SysUserRole() { RoleId = item, UserId = user.userId });
- }
- return userRoles.Count > 0 ? AddUserRole(userRoles) : 0;
- }
- /// <summary>
- /// 新增加角色用户
- /// </summary>
- /// <param name="roleUsersCreateDto"></param>
- /// <returns></returns>
- public int InsertRoleUser(RoleUsersCreateDto roleUsersCreateDto)
- {
- List<SysUserRole> userRoles = new();
- foreach (var item in roleUsersCreateDto.UserIds)
- {
- userRoles.Add(new SysUserRole() { RoleId = roleUsersCreateDto.RoleId, UserId = item });
- }
- return userRoles.Count > 0 ? AddUserRole(userRoles) : 0;
- }
- }
- }
|