123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- using Attribute;
- using Base;
- using Dto;
- using Infrastructure;
- using Model;
- using Model.Base;
- using Repository;
- using Service;
- using System.Collections;
- namespace Services
- {
- /// <summary>
- /// 角色
- /// </summary>
- [AppService(ServiceType = typeof(ISysRoleService), ServiceLifetime = LifeTime.Transient)]
- public class SysRoleService : BaseService<SysRole>, ISysRoleService
- {
- private ISysUserRoleService SysUserRoleService;
- private ISysDeptService DeptService;
- private ISysRoleMenuService RoleMenuService;
- public SysRoleService(
- ISysUserRoleService sysUserRoleService,
- ISysDeptService deptService,
- ISysRoleMenuService roleMenuService)
- {
- SysUserRoleService = sysUserRoleService;
- DeptService = deptService;
- RoleMenuService = roleMenuService;
- }
- /// <summary>
- /// 根据条件分页查询角色数据
- /// </summary>
- /// <param name="sysRole">角色信息</param>
- /// <param name="pager">分页信息</param>
- /// <returns>角色数据集合信息</returns>
- public PagedInfo<SysRole> SelectRoleList(SysRole sysRole, PagerInfo pager)
- {
- var exp = Expressionable.Create<SysRole>();
- exp.And(role => role.DelFlag == "0");
- exp.AndIF(!string.IsNullOrEmpty(sysRole.roleName), role => role.roleName.Contains(sysRole.roleName));
- exp.AndIF(!string.IsNullOrEmpty(sysRole.roleCode), role => role.roleCode == sysRole.roleCode);
- var query = Queryable()
- .Where(exp.ToExpression())
- .OrderBy(x => x.roleId)
- .Select((role) => new SysRole
- {
- userNum = SqlFunc.Subqueryable<SysUserRole>().Where(f => f.RoleId == role.roleId).Count()
- }, true);
- return query.ToPage(pager);
- }
- /// <summary>
- /// 查询所有角色
- /// </summary>
- /// <returns></returns>
- public List<SysRole> SelectRoleAll()
- {
- return Queryable()
- .Where(role => role.DelFlag == "0")
- .OrderBy(role => role.roleId)
- .ToList();
- }
- /// <summary>
- /// 根据用户查询
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- public List<SysRole> SelectRolePermissionByUserId(long userId)
- {
- return Queryable()
- .Where(role => role.DelFlag == "0")
- .Where(it => SqlFunc.Subqueryable<SysUserRole>().Where(s => s.UserId == userId).Any())
- .OrderBy(role => role.roleId)
- .ToList();
- }
- /// <summary>
- /// 通过角色ID查询角色
- /// </summary>
- /// <param name="roleId">角色ID</param>
- /// <returns>角色对象信息</returns>
- public SysRole SelectRoleById(long roleId)
- {
- return GetId(roleId);
- }
- /// <summary>
- /// 批量删除角色信息
- /// </summary>
- /// <param name="roleIds">需要删除的角色ID</param>
- /// <returns></returns>
- public int DeleteRoleByRoleId(long[] roleIds)
- {
- foreach (var item in roleIds)
- {
- CheckRoleAllowed(new SysRole(item));
- SysRole role = SelectRoleById(item);
- if (SysUserRoleService.CountUserRoleByRoleId(item) > 0)
- {
- throw new CustomException($"{role.roleName}已分配,不能删除");
- }
- }
- return Delete(roleIds);
- }
- /// <summary>
- /// 更改角色权限状态
- /// </summary>
- /// <param name="roleDto"></param>
- /// <returns></returns>
- public int UpdateRoleStatus(SysRole roleDto)
- {
- return Update(roleDto, it => new { it.DelFlag }, f => f.roleId == roleDto.roleId);
- }
- /// <summary>
- /// 校验角色权限是否唯一
- /// </summary>
- /// <param name="sysRole">角色信息</param>
- /// <returns></returns>
- public string CheckRoleKeyUnique(SysRole sysRole)
- {
- long roleId = 0 == sysRole.roleId ? -1L : sysRole.roleId;
- SysRole info = GetFirst(it => it.roleCode == sysRole.roleCode);
- if (info != null && info.roleId != roleId)
- {
- return UserConstants.NOT_UNIQUE;
- }
- return UserConstants.UNIQUE;
- }
- /// <summary>
- /// 校验角色是否允许操作
- /// </summary>
- /// <param name="role"></param>
- public void CheckRoleAllowed(SysRole role)
- {
- if (IsRoleAdmin(role.roleId))
- {
- throw new CustomException("不允许操作超级管理员角色");
- }
- }
- /// <summary>
- /// 新增保存角色信息
- /// </summary>
- /// <param name="sysRole">角色信息</param>
- /// <returns></returns>
- public long InsertRole(SysRole sysRole)
- {
- sysRole.CreateTime = DateTime.Now;
- sysRole.roleId = InsertReturnBigIdentity(sysRole);
- //插入角色部门数据
- DeptService.InsertRoleDepts(sysRole);
- return sysRole.roleId;
- }
- /// <summary>
- /// 通过角色ID删除角色和菜单关联
- /// </summary>
- /// <param name="roleId">角色ID</param>
- /// <returns></returns>
- public int DeleteRoleMenuByRoleId(long roleId)
- {
- return RoleMenuService.DeleteRoleMenuByRoleId(roleId);
- }
- /// <summary>
- /// 修改数据权限信息
- /// </summary>
- /// <param name="sysRoleDto"></param>
- /// <returns></returns>
- public bool AuthDataScope(SysRoleDto sysRoleDto)
- {
- return UseTran2(() =>
- {
- //删除角色菜单
- //DeleteRoleMenuByRoleId(sysRoleDto.roleId);
- //InsertRoleMenu(sysRoleDto);
- var oldMenus = SelectUserRoleMenus(sysRoleDto.roleId);
- var newMenus = sysRoleDto.menuIds;
- //并集菜单
- var arr_c = oldMenus.Intersect(newMenus).ToArray();
- //获取减量菜单
- var delMenuIds = oldMenus.Where(c => !arr_c.Contains(c)).ToArray();
- //获取增量
- var addMenuIds = newMenus.Where(c => !arr_c.Contains(c)).ToArray();
- RoleMenuService.DeleteRoleMenuByRoleIdMenuIds(sysRoleDto.roleId, delMenuIds);
- sysRoleDto.menuIds = addMenuIds.ToList();
- sysRoleDto.delMenuIds= delMenuIds.ToList();
- InsertRoleMenu(sysRoleDto);
- Console.WriteLine($"减少了{delMenuIds.Length},增加了{addMenuIds.Length}菜单");
- });
- }
- #region Service
- /// <summary>
- /// 批量新增角色菜单信息
- /// </summary>
- /// <param name="sysRoleDto"></param>
- /// <returns></returns>
- public int InsertRoleMenu(SysRoleDto sysRoleDto)
- {
- int rows = 1;
- // 新增用户与角色管理
- List<SysRoleMenu> sysRoleMenus = new();
- foreach (var item in sysRoleDto.menuIds)
- {
- SysRoleMenu rm = new()
- {
- MenuId = item,
- RoleId = sysRoleDto.roleId,
- };
- sysRoleMenus.Add(rm);
- }
- //添加角色菜单
- if (sysRoleMenus.Count > 0)
- {
- rows = RoleMenuService.AddRoleMenu(sysRoleMenus);
- }
- return rows;
- }
- /// <summary>
- /// 判断是否是管理员
- /// </summary>
- /// <param name="userid"></param>
- /// <returns></returns>
- public bool IsAdmin(long userid)
- {
- List<string> roles = SelectUserRoleKeys(userid);
- return ((IList)roles).Contains(GlobalConstant.AdminRole);
- }
- /// <summary>
- /// 判断是否是管理员
- /// </summary>
- /// <param name="roleid"></param>
- /// <returns></returns>
- public bool IsRoleAdmin(long roleid)
- {
- var roleInfo = GetFirst(x => x.roleId == roleid) ?? new SysRole();
- return roleInfo.roleCode == GlobalConstant.AdminRole;
- }
- /// <summary>
- /// 获取角色菜单id集合
- /// </summary>
- /// <param name="roleId"></param>
- /// <returns></returns>
- public List<long> SelectUserRoleMenus(long roleId)
- {
- var list = RoleMenuService.SelectRoleMenuByRoleId(roleId);
- return list.Select(x => x.MenuId).Distinct().ToList();
- }
- /// <summary>
- /// 根据用户所有角色获取菜单
- /// </summary>
- /// <param name="roleIds"></param>
- /// <returns></returns>
- public List<long> SelectRoleMenuByRoleIds(long[] roleIds)
- {
- return RoleMenuService.SelectRoleMenuByRoleIds(roleIds)
- .Select(x => x.MenuId)
- .Distinct().ToList();
- }
- /// <summary>
- /// 获取用户角色列表
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- public List<SysRole> SelectUserRoleListByUserId(long userId)
- {
- return Context.Queryable<SysUserRole>()
- .LeftJoin<SysRole>((ur, r) => ur.RoleId == r.roleId)
- .Where((ur, r) => ur.UserId == userId && r.roleId > 0)
- .WithCache(60 * 10)
- .Select((ur, r) => r)
- .ToList();
- }
- /// <summary>
- /// 获取用户权限集合
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- public List<long> SelectUserRoles(long userId)
- {
- var list = SelectUserRoleListByUserId(userId).Where(f => f.DelFlag == "0");
- return list.Select(x => x.roleId).ToList();
- }
- /// <summary>
- /// 获取用户权限字符串集合
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- public List<string> SelectUserRoleKeys(long userId)
- {
- var list = SelectUserRoleListByUserId(userId);
- return list.Select(x => x.roleCode).ToList();
- }
- /// <summary>
- /// 获取用户所有角色名
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- public List<string> SelectUserRoleNames(long userId)
- {
- var list = SelectUserRoleListByUserId(userId);
- return list.Select(x => x.roleName).ToList();
- }
- #endregion
- /// <summary>
- /// 修改保存角色信息
- /// </summary>
- /// <param name="sysRole">角色信息</param>
- /// <returns></returns>
- public int UpdateRole(SysRole sysRole)
- {
- sysRole.UpdateTime = DateTime.Now;
- var result = UseTran(() =>
- {
- //修改角色信息
- UpdateSysRole(sysRole);
- //删除角色与部门管理
- // DeptService.DeleteRoleDeptByRoleId(sysRole.roleId);
- // //插入角色部门数据
- // DeptService.InsertRoleDepts(sysRole);
- });
- return result.IsSuccess ? 1 : 0;
- }
- /// <summary>
- /// 修改用户角色
- /// </summary>
- /// <param name="sysRole"></param>
- /// <returns></returns>
- public int UpdateSysRole(SysRole sysRole)
- {
- var db = Context;
- sysRole.UpdateTime = db.GetDate();
- return db.Updateable<SysRole>()
- .SetColumns(it => it.UpdateTime == sysRole.UpdateTime)
- .SetColumns(it => it.roleDesc == sysRole.roleDesc)
- .SetColumns(it => it.UpdateBy == sysRole.UpdateBy)
- .SetColumnsIF(!string.IsNullOrEmpty(sysRole.roleName), it => it.roleName == sysRole.roleName)
- .SetColumnsIF(!string.IsNullOrEmpty(sysRole.roleCode), it => it.roleCode == sysRole.roleCode)
- .Where(it => it.roleId == sysRole.roleId)
- .ExecuteCommand();
- }
- }
- }
|