PrivilegeGroupLogic.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using CZFW.Core;
  2. using CZFW.Framework.Interface;
  3. using CZFW.Framework.Model;
  4. using CZFW.Framework.Model.Entity;
  5. using CZFW.Framework.Model.ViewModel;
  6. using Microsoft.AspNetCore.Mvc;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. namespace CZFW.Framework.Logic
  10. {
  11. public class PrivilegeGroupLogic : LogicBase<PrivilegeGroupEntity>, IPrivilegeGroup
  12. {
  13. public ResultModel AddEntity(PrivilegeGroupEntity entity)
  14. {
  15. entity.Mark = Utility.GenerateTreeMark(entity.ParentMark);
  16. return base.AddEntity(entity);
  17. }
  18. IPrivilege _privilegeLogic;// = new PrivilegeLogic();
  19. public PrivilegeGroupLogic(CZDbContext db, IPrivilege privilegeLogic)
  20. {
  21. _privilegeLogic = privilegeLogic;
  22. }
  23. public IList<PrivilegeModel> GetAllPrivilegeInGroup(string mark)
  24. {
  25. var res = _privilegeLogic.GetSortedQueryable()
  26. .Where(x => x.GroupMark.Contains(mark))
  27. .Select(x => new PrivilegeModel()
  28. {
  29. Id = x.Id,
  30. ActionName = x.ActionName,
  31. AreaName = x.AreaName,
  32. ControllerName = x.ControllerName,
  33. Description = x.Description,
  34. GroupMark = x.GroupMark,
  35. Name = x.Name,
  36. Url = x.Url
  37. }).ToList();
  38. return res;
  39. }
  40. public IList<PrivilegeGroupModel> GetDescendantsById(int id, bool includeSelf = false)
  41. {
  42. var mark = GetMarkById(id);
  43. return GetDescendantsByMark(mark, includeSelf);
  44. }
  45. public IList<PrivilegeGroupModel> GetDescendantsByMark(string mark, bool includeSelf = false)
  46. {
  47. var res = GetSortedQueryable().Where(x => x.ParentMark.Contains(mark))
  48. .Select(x => new PrivilegeGroupModel()
  49. {
  50. Mark = x.Mark,
  51. Name = x.Name,
  52. SortOrder = x.SortOrder ?? 0,
  53. ParentMark = x.ParentMark,
  54. Id = x.Id
  55. }).ToList();
  56. if (!includeSelf)
  57. res = res.Where(x => x.Mark != mark).ToList();
  58. return res;
  59. }
  60. public IList<PrivilegeGroupModel> GetTreeById(int id, bool includeSelf = false)
  61. {
  62. var mark = GetMarkById(id);
  63. var list = GetDescendantsById(id, includeSelf);
  64. var res = SetTree(list, mark);
  65. if (includeSelf)
  66. {
  67. var self = GetModelByMark(mark);
  68. self.Children = res;
  69. res = new List<PrivilegeGroupModel>();
  70. res.Add(self);
  71. }
  72. return res;
  73. }
  74. public IList<PrivilegeGroupModel> GetTreeByMark(string mark, bool includeSelf = false)
  75. {
  76. var list = GetDescendantsByMark(mark, includeSelf);
  77. var res = SetTree(list, mark);
  78. if(includeSelf)
  79. {
  80. var self = GetModelByMark(mark);
  81. self.Children = res;
  82. res = new List<PrivilegeGroupModel>();
  83. res.Add(self);
  84. }
  85. return res;
  86. }
  87. public int GetIdByMark(string mark)
  88. {
  89. var res = GetQueryable().Where(x => x.Mark == mark).Select(x => x.Id).SingleOrDefault();
  90. return res;
  91. }
  92. public string GetMarkById(int id)
  93. {
  94. var res = GetQueryable().Where(x => x.Id == id).Select(x => x.Mark).SingleOrDefault();
  95. return res;
  96. }
  97. public PrivilegeGroupModel GetModelById(int id)
  98. {
  99. var res = GetQueryable().Where(x => x.Id == id).Select(x => new PrivilegeGroupModel()
  100. {
  101. Mark = x.Mark,
  102. Name = x.Name,
  103. ParentMark = x.ParentMark,
  104. SortOrder = (int)x.SortOrder,
  105. Id = x.Id
  106. }).SingleOrDefault();
  107. return res;
  108. }
  109. public PrivilegeGroupModel GetModelByMark(string mark)
  110. {
  111. var queryable = Queryable;
  112. var res =queryable.Where(x => x.Mark == mark).Select(x => new PrivilegeGroupModel()
  113. {
  114. Mark = x.Mark,
  115. Name = x.Name,
  116. ParentMark = x.ParentMark,
  117. SortOrder = x.SortOrder??0,
  118. Id = x.Id
  119. }).SingleOrDefault();
  120. return res;
  121. }
  122. public IList<PrivilegeModel> GetPrivilegeInGroup(string mark)
  123. {
  124. var res = _privilegeLogic.GetSortedQueryable().Where(x => x.GroupMark == mark).Select(x => new PrivilegeModel()
  125. {
  126. ActionName = x.ActionName,
  127. AreaName = x.AreaName,
  128. ControllerName = x.ControllerName,
  129. Description = x.Description,
  130. GroupMark = x.GroupMark,
  131. Id = x.Id,
  132. Name = x.Name,
  133. Url = x.Url
  134. }).ToList();
  135. return res;
  136. }
  137. public IList<PrivilegeGroupModel> SetTree(IList<PrivilegeGroupModel> list, string parentMark)
  138. {
  139. var result = new List<PrivilegeGroupModel>();
  140. foreach (var item in list)
  141. {
  142. if (item.ParentMark == parentMark)
  143. {
  144. result.Add(item);
  145. item.Children = SetTree(list, item.Mark);
  146. }
  147. }
  148. result = result.OrderByDescending(x => x.SortOrder).ToList();
  149. return result;
  150. }
  151. public ResultModel Add(PrivilegeGroupEntity entity)
  152. {
  153. if (string.IsNullOrEmpty(entity.ParentMark))
  154. entity.ParentMark = "0";
  155. entity.Mark = Utility.GenerateTreeMark(entity.ParentMark);
  156. var res = AddEntity(entity);
  157. return res;
  158. }
  159. public IList<PrivilegeGroupModel> GetChildrenById(int id)
  160. {
  161. var item = GetModelById(id);
  162. return GetChildrenByMark(item.Mark);
  163. }
  164. public IList<PrivilegeGroupModel> GetChildrenByMark(string mark)
  165. {
  166. var item = GetModelByMark(mark);
  167. var res = GetSortedQueryable().Where(x => x.ParentMark == mark)
  168. .Select(x => new PrivilegeGroupModel()
  169. {
  170. Mark = x.Mark,
  171. Name = x.Name,
  172. ParentMark = x.ParentMark,
  173. SortOrder = x.SortOrder ?? 0,
  174. ParentName = item.Name,
  175. Id = x.Id
  176. }).ToList();
  177. return res;
  178. }
  179. }
  180. }