UserRoleNavigationLogic.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using CZFW.Framework.Interface;
  2. using CZFW.Framework.Model.Entity;
  3. using CZFW.Framework.Model.ViewModel;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using CZFW.Framework.Model;
  7. namespace CZFW.Framework.Logic
  8. {
  9. public class UserRoleNavigationLogic : LogicBase<UserRoleNavigationEntity>, IUserRoleNavigation
  10. {
  11. public IList<NavigationModel> GetNavigationTreeByRoleId(int roleId)
  12. {
  13. var list = (from rn in GetQueryable()
  14. join nav in DbContext.Set<NavigationEntity>()
  15. on rn.NavigationId equals nav.Id
  16. where rn.RoleId == roleId
  17. select new NavigationModel()
  18. {
  19. Description = nav.Description,
  20. ParentMark = nav.ParentMark,
  21. Id = nav.Id,
  22. Icon = nav.Icon,
  23. Mark = nav.Mark,
  24. Name = nav.Name,
  25. SortOrder = nav.SortOrder??0,
  26. Thumbnail = nav.Thumbnail,
  27. Url = nav.Url
  28. }).ToList();
  29. var res = SetTree(list, "0");
  30. return res;
  31. }
  32. public IList<NavigationModel> GetNavigationTreeByUserId(int userId)
  33. {
  34. var roles = DbContext.Set<UserRoleEntity>()
  35. .Where(x => x.UserId == userId)
  36. .Select(x => x.RoleId).ToList();
  37. var list = (from rn in GetQueryable()
  38. join nav in DbContext.Set<NavigationEntity>()
  39. on rn.NavigationId equals nav.Id
  40. where roles.Contains(rn.RoleId)||rn.UserId==userId
  41. select new NavigationModel()
  42. {
  43. Description = nav.Description,
  44. ParentMark = nav.ParentMark,
  45. Id = nav.Id,
  46. Icon = nav.Icon,
  47. Mark = nav.Mark,
  48. Name = nav.Name,
  49. SortOrder = nav.SortOrder??0,
  50. Thumbnail = nav.Thumbnail,
  51. Url = nav.Url
  52. }).Distinct().ToList();
  53. var res = SetTree(list, "0");
  54. return res;
  55. }
  56. public ResultModel GrantRole(int roleId, int navId)
  57. {
  58. var res = new ResultModel();
  59. var count = GetCount(x => x.RoleId == roleId && x.NavigationId == navId && roleId != 0);
  60. if (count > 0)
  61. {
  62. res.Success = false;
  63. res.Message = "该角色已经觉有该授权!";
  64. return res;
  65. }
  66. var item = new UserRoleNavigationEntity()
  67. {
  68. RoleId = roleId,
  69. NavigationId = navId
  70. };
  71. var tp = AddEntity(item);
  72. return tp;
  73. }
  74. public ResultModel GrantRoleNavigations(int roleId, IList<int> navIds)
  75. {
  76. var exisitingIds = GetQueryable()
  77. .Where(x => x.RoleId == roleId && roleId != 0 && navIds.Contains(x.NavigationId))
  78. .Select(x => x.NavigationId).ToList();
  79. var nonExisitingIds = navIds.Where(x => !exisitingIds.Contains(x)).ToList();
  80. var roleNavs = new List<UserRoleNavigationEntity>();
  81. foreach (var item in nonExisitingIds)
  82. {
  83. var entity = new UserRoleNavigationEntity()
  84. {
  85. NavigationId = item,
  86. RoleId = roleId
  87. };
  88. roleNavs.Add(entity);
  89. }
  90. var res = AddEntities(roleNavs);
  91. return res;
  92. }
  93. public ResultModel GrantUser(int userId, int navId)
  94. {
  95. var res = new ResultModel();
  96. var count = GetCount(x => x.UserId == userId && x.NavigationId == navId && userId != 0);
  97. if (count > 0)
  98. {
  99. res.Success = false;
  100. res.Message = "该角色已经觉有该授权!";
  101. return res;
  102. }
  103. var item = new UserRoleNavigationEntity()
  104. {
  105. UserId = userId,
  106. NavigationId = navId
  107. };
  108. var tp = AddEntity(item);
  109. return tp;
  110. }
  111. public ResultModel GrantUserNavigations(int userId, IList<int> navIds)
  112. {
  113. var exisitingIds = GetQueryable()
  114. .Where(x => x.UserId == userId && userId != 0 && navIds.Contains(x.NavigationId))
  115. .Select(x => x.NavigationId).ToList();
  116. var nonExisitingIds = navIds.Where(x => !exisitingIds.Contains(x)).ToList();
  117. var roleNavs = new List<UserRoleNavigationEntity>();
  118. foreach (var item in nonExisitingIds)
  119. {
  120. var entity = new UserRoleNavigationEntity()
  121. {
  122. NavigationId = item,
  123. UserId = userId
  124. };
  125. roleNavs.Add(entity);
  126. }
  127. var res = AddEntities(roleNavs);
  128. return res;
  129. }
  130. public ResultModel RevokeRole(int roleId, int navId)
  131. {
  132. if (roleId == 0) return new ResultModel("请指定角色Id");
  133. var res = RemoveEntities(x => x.RoleId == roleId && navId == x.NavigationId);
  134. return res;
  135. }
  136. public ResultModel RevokeRoleNavigations(int roleId, IList<int> navIds)
  137. {
  138. if (roleId == 0)
  139. return new ResultModel() { Success = false, Message = "请提供正确角色Id" };
  140. var res = RemoveEntities(x => x.RoleId == roleId && navIds.Contains(x.NavigationId));
  141. return res;
  142. }
  143. public ResultModel RevokeUser(int userId, int navId)
  144. {
  145. if (userId == 0) return new ResultModel("请指定用户Id");
  146. var res = RemoveEntities(x => x.UserId == userId && navId == x.NavigationId);
  147. return res;
  148. }
  149. public ResultModel RevokeUserNavigations(int userId, IList<int> navIds)
  150. {
  151. if (userId == 0)
  152. return new ResultModel() { Success = false, Message = "请提供正确用户Id" };
  153. var res = RemoveEntities(x => x.UserId == userId && navIds.Contains(x.NavigationId));
  154. return res;
  155. }
  156. public IList<NavigationModel> SetTree(IList<NavigationModel> list, string parentMark)
  157. {
  158. var result = new List<NavigationModel>();
  159. foreach (var item in list)
  160. {
  161. if (item.Mark!="0"&&item.ParentMark == parentMark)
  162. {
  163. result.Add(item);
  164. item.Children = SetTree(list, item.Mark);
  165. }
  166. }
  167. result = result.OrderByDescending(x => x.SortOrder).ToList();
  168. return result;
  169. }
  170. }
  171. }