SysRoleService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. using Attribute;
  2. using Base;
  3. using Dto;
  4. using Infrastructure;
  5. using Model;
  6. using Model.Base;
  7. using Repository;
  8. using Service;
  9. using System.Collections;
  10. namespace Services
  11. {
  12. /// <summary>
  13. /// 角色
  14. /// </summary>
  15. [AppService(ServiceType = typeof(ISysRoleService), ServiceLifetime = LifeTime.Transient)]
  16. public class SysRoleService : BaseService<SysRole>, ISysRoleService
  17. {
  18. private ISysUserRoleService SysUserRoleService;
  19. private ISysDeptService DeptService;
  20. private ISysRoleMenuService RoleMenuService;
  21. public SysRoleService(
  22. ISysUserRoleService sysUserRoleService,
  23. ISysDeptService deptService,
  24. ISysRoleMenuService roleMenuService)
  25. {
  26. SysUserRoleService = sysUserRoleService;
  27. DeptService = deptService;
  28. RoleMenuService = roleMenuService;
  29. }
  30. /// <summary>
  31. /// 根据条件分页查询角色数据
  32. /// </summary>
  33. /// <param name="sysRole">角色信息</param>
  34. /// <param name="pager">分页信息</param>
  35. /// <returns>角色数据集合信息</returns>
  36. public PagedInfo<SysRole> SelectRoleList(SysRole sysRole, PagerInfo pager)
  37. {
  38. var exp = Expressionable.Create<SysRole>();
  39. exp.And(role => role.DelFlag == "0");
  40. exp.AndIF(!string.IsNullOrEmpty(sysRole.roleName), role => role.roleName.Contains(sysRole.roleName));
  41. exp.AndIF(!string.IsNullOrEmpty(sysRole.roleCode), role => role.roleCode == sysRole.roleCode);
  42. var query = Queryable()
  43. .Where(exp.ToExpression())
  44. .OrderBy(x => x.roleId)
  45. .Select((role) => new SysRole
  46. {
  47. userNum = SqlFunc.Subqueryable<SysUserRole>().Where(f => f.RoleId == role.roleId).Count()
  48. }, true);
  49. return query.ToPage(pager);
  50. }
  51. /// <summary>
  52. /// 查询所有角色
  53. /// </summary>
  54. /// <returns></returns>
  55. public List<SysRole> SelectRoleAll()
  56. {
  57. return Queryable()
  58. .Where(role => role.DelFlag == "0")
  59. .OrderBy(role => role.roleId)
  60. .ToList();
  61. }
  62. /// <summary>
  63. /// 根据用户查询
  64. /// </summary>
  65. /// <param name="userId"></param>
  66. /// <returns></returns>
  67. public List<SysRole> SelectRolePermissionByUserId(long userId)
  68. {
  69. return Queryable()
  70. .Where(role => role.DelFlag == "0")
  71. .Where(it => SqlFunc.Subqueryable<SysUserRole>().Where(s => s.UserId == userId).Any())
  72. .OrderBy(role => role.roleId)
  73. .ToList();
  74. }
  75. /// <summary>
  76. /// 通过角色ID查询角色
  77. /// </summary>
  78. /// <param name="roleId">角色ID</param>
  79. /// <returns>角色对象信息</returns>
  80. public SysRole SelectRoleById(long roleId)
  81. {
  82. return GetId(roleId);
  83. }
  84. /// <summary>
  85. /// 批量删除角色信息
  86. /// </summary>
  87. /// <param name="roleIds">需要删除的角色ID</param>
  88. /// <returns></returns>
  89. public int DeleteRoleByRoleId(long[] roleIds)
  90. {
  91. foreach (var item in roleIds)
  92. {
  93. CheckRoleAllowed(new SysRole(item));
  94. SysRole role = SelectRoleById(item);
  95. if (SysUserRoleService.CountUserRoleByRoleId(item) > 0)
  96. {
  97. throw new CustomException($"{role.roleName}已分配,不能删除");
  98. }
  99. }
  100. return Delete(roleIds);
  101. }
  102. /// <summary>
  103. /// 更改角色权限状态
  104. /// </summary>
  105. /// <param name="roleDto"></param>
  106. /// <returns></returns>
  107. public int UpdateRoleStatus(SysRole roleDto)
  108. {
  109. return Update(roleDto, it => new { it.DelFlag }, f => f.roleId == roleDto.roleId);
  110. }
  111. /// <summary>
  112. /// 校验角色权限是否唯一
  113. /// </summary>
  114. /// <param name="sysRole">角色信息</param>
  115. /// <returns></returns>
  116. public string CheckRoleKeyUnique(SysRole sysRole)
  117. {
  118. long roleId = 0 == sysRole.roleId ? -1L : sysRole.roleId;
  119. SysRole info = GetFirst(it => it.roleCode == sysRole.roleCode);
  120. if (info != null && info.roleId != roleId)
  121. {
  122. return UserConstants.NOT_UNIQUE;
  123. }
  124. return UserConstants.UNIQUE;
  125. }
  126. /// <summary>
  127. /// 校验角色是否允许操作
  128. /// </summary>
  129. /// <param name="role"></param>
  130. public void CheckRoleAllowed(SysRole role)
  131. {
  132. if (IsRoleAdmin(role.roleId))
  133. {
  134. throw new CustomException("不允许操作超级管理员角色");
  135. }
  136. }
  137. /// <summary>
  138. /// 新增保存角色信息
  139. /// </summary>
  140. /// <param name="sysRole">角色信息</param>
  141. /// <returns></returns>
  142. public long InsertRole(SysRole sysRole)
  143. {
  144. sysRole.CreateTime = DateTime.Now;
  145. sysRole.roleId = InsertReturnBigIdentity(sysRole);
  146. //插入角色部门数据
  147. DeptService.InsertRoleDepts(sysRole);
  148. return sysRole.roleId;
  149. }
  150. /// <summary>
  151. /// 通过角色ID删除角色和菜单关联
  152. /// </summary>
  153. /// <param name="roleId">角色ID</param>
  154. /// <returns></returns>
  155. public int DeleteRoleMenuByRoleId(long roleId)
  156. {
  157. return RoleMenuService.DeleteRoleMenuByRoleId(roleId);
  158. }
  159. /// <summary>
  160. /// 修改数据权限信息
  161. /// </summary>
  162. /// <param name="sysRoleDto"></param>
  163. /// <returns></returns>
  164. public bool AuthDataScope(SysRoleDto sysRoleDto)
  165. {
  166. return UseTran2(() =>
  167. {
  168. //删除角色菜单
  169. //DeleteRoleMenuByRoleId(sysRoleDto.roleId);
  170. //InsertRoleMenu(sysRoleDto);
  171. var oldMenus = SelectUserRoleMenus(sysRoleDto.roleId);
  172. var newMenus = sysRoleDto.menuIds;
  173. //并集菜单
  174. var arr_c = oldMenus.Intersect(newMenus).ToArray();
  175. //获取减量菜单
  176. var delMenuIds = oldMenus.Where(c => !arr_c.Contains(c)).ToArray();
  177. //获取增量
  178. var addMenuIds = newMenus.Where(c => !arr_c.Contains(c)).ToArray();
  179. RoleMenuService.DeleteRoleMenuByRoleIdMenuIds(sysRoleDto.roleId, delMenuIds);
  180. sysRoleDto.menuIds = addMenuIds.ToList();
  181. sysRoleDto.delMenuIds= delMenuIds.ToList();
  182. InsertRoleMenu(sysRoleDto);
  183. Console.WriteLine($"减少了{delMenuIds.Length},增加了{addMenuIds.Length}菜单");
  184. });
  185. }
  186. #region Service
  187. /// <summary>
  188. /// 批量新增角色菜单信息
  189. /// </summary>
  190. /// <param name="sysRoleDto"></param>
  191. /// <returns></returns>
  192. public int InsertRoleMenu(SysRoleDto sysRoleDto)
  193. {
  194. int rows = 1;
  195. // 新增用户与角色管理
  196. List<SysRoleMenu> sysRoleMenus = new();
  197. foreach (var item in sysRoleDto.menuIds)
  198. {
  199. SysRoleMenu rm = new()
  200. {
  201. MenuId = item,
  202. RoleId = sysRoleDto.roleId,
  203. };
  204. sysRoleMenus.Add(rm);
  205. }
  206. //添加角色菜单
  207. if (sysRoleMenus.Count > 0)
  208. {
  209. rows = RoleMenuService.AddRoleMenu(sysRoleMenus);
  210. }
  211. return rows;
  212. }
  213. /// <summary>
  214. /// 判断是否是管理员
  215. /// </summary>
  216. /// <param name="userid"></param>
  217. /// <returns></returns>
  218. public bool IsAdmin(long userid)
  219. {
  220. List<string> roles = SelectUserRoleKeys(userid);
  221. return ((IList)roles).Contains(GlobalConstant.AdminRole);
  222. }
  223. /// <summary>
  224. /// 判断是否是管理员
  225. /// </summary>
  226. /// <param name="roleid"></param>
  227. /// <returns></returns>
  228. public bool IsRoleAdmin(long roleid)
  229. {
  230. var roleInfo = GetFirst(x => x.roleId == roleid) ?? new SysRole();
  231. return roleInfo.roleCode == GlobalConstant.AdminRole;
  232. }
  233. /// <summary>
  234. /// 获取角色菜单id集合
  235. /// </summary>
  236. /// <param name="roleId"></param>
  237. /// <returns></returns>
  238. public List<long> SelectUserRoleMenus(long roleId)
  239. {
  240. var list = RoleMenuService.SelectRoleMenuByRoleId(roleId);
  241. return list.Select(x => x.MenuId).Distinct().ToList();
  242. }
  243. /// <summary>
  244. /// 根据用户所有角色获取菜单
  245. /// </summary>
  246. /// <param name="roleIds"></param>
  247. /// <returns></returns>
  248. public List<long> SelectRoleMenuByRoleIds(long[] roleIds)
  249. {
  250. return RoleMenuService.SelectRoleMenuByRoleIds(roleIds)
  251. .Select(x => x.MenuId)
  252. .Distinct().ToList();
  253. }
  254. /// <summary>
  255. /// 获取用户角色列表
  256. /// </summary>
  257. /// <param name="userId"></param>
  258. /// <returns></returns>
  259. public List<SysRole> SelectUserRoleListByUserId(long userId)
  260. {
  261. return Context.Queryable<SysUserRole>()
  262. .LeftJoin<SysRole>((ur, r) => ur.RoleId == r.roleId)
  263. .Where((ur, r) => ur.UserId == userId && r.roleId > 0)
  264. .WithCache(60 * 10)
  265. .Select((ur, r) => r)
  266. .ToList();
  267. }
  268. /// <summary>
  269. /// 获取用户权限集合
  270. /// </summary>
  271. /// <param name="userId"></param>
  272. /// <returns></returns>
  273. public List<long> SelectUserRoles(long userId)
  274. {
  275. var list = SelectUserRoleListByUserId(userId).Where(f => f.DelFlag == "0");
  276. return list.Select(x => x.roleId).ToList();
  277. }
  278. /// <summary>
  279. /// 获取用户权限字符串集合
  280. /// </summary>
  281. /// <param name="userId"></param>
  282. /// <returns></returns>
  283. public List<string> SelectUserRoleKeys(long userId)
  284. {
  285. var list = SelectUserRoleListByUserId(userId);
  286. return list.Select(x => x.roleCode).ToList();
  287. }
  288. /// <summary>
  289. /// 获取用户所有角色名
  290. /// </summary>
  291. /// <param name="userId"></param>
  292. /// <returns></returns>
  293. public List<string> SelectUserRoleNames(long userId)
  294. {
  295. var list = SelectUserRoleListByUserId(userId);
  296. return list.Select(x => x.roleName).ToList();
  297. }
  298. #endregion
  299. /// <summary>
  300. /// 修改保存角色信息
  301. /// </summary>
  302. /// <param name="sysRole">角色信息</param>
  303. /// <returns></returns>
  304. public int UpdateRole(SysRole sysRole)
  305. {
  306. sysRole.UpdateTime = DateTime.Now;
  307. var result = UseTran(() =>
  308. {
  309. //修改角色信息
  310. UpdateSysRole(sysRole);
  311. //删除角色与部门管理
  312. // DeptService.DeleteRoleDeptByRoleId(sysRole.roleId);
  313. // //插入角色部门数据
  314. // DeptService.InsertRoleDepts(sysRole);
  315. });
  316. return result.IsSuccess ? 1 : 0;
  317. }
  318. /// <summary>
  319. /// 修改用户角色
  320. /// </summary>
  321. /// <param name="sysRole"></param>
  322. /// <returns></returns>
  323. public int UpdateSysRole(SysRole sysRole)
  324. {
  325. var db = Context;
  326. sysRole.UpdateTime = db.GetDate();
  327. return db.Updateable<SysRole>()
  328. .SetColumns(it => it.UpdateTime == sysRole.UpdateTime)
  329. .SetColumns(it => it.roleDesc == sysRole.roleDesc)
  330. .SetColumns(it => it.UpdateBy == sysRole.UpdateBy)
  331. .SetColumnsIF(!string.IsNullOrEmpty(sysRole.roleName), it => it.roleName == sysRole.roleName)
  332. .SetColumnsIF(!string.IsNullOrEmpty(sysRole.roleCode), it => it.roleCode == sysRole.roleCode)
  333. .Where(it => it.roleId == sysRole.roleId)
  334. .ExecuteCommand();
  335. }
  336. }
  337. }