飞星
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

217 行
7.2 KiB

  1. using System.Collections.Generic;
  2. using Permission.Service.IServices;
  3. using Permission.Infrastructure.Repositories;
  4. using Permission.Entity.System;
  5. using System;
  6. using Permission.Entity.DbContext;
  7. using Microsoft.EntityFrameworkCore;
  8. using System.Linq;
  9. using Permission.Infrastructure.WebControls;
  10. using Permission.Utils.Validate;
  11. namespace Permission.Service.Services
  12. {
  13. public class SysRoleService : ISysRoleService
  14. {
  15. private readonly IUnitOfWork _unitOfWork;
  16. private readonly PermissionContext _dbContext;
  17. private readonly IBaseRepository<SysRole> _sysRoleRepository;
  18. private readonly IBaseRepository<SysMenu> _sysMenuRepository;
  19. public SysRoleService(IUnitOfWork unitOfWork, PermissionContext dbContext, IBaseRepository<SysRole> sysRoleRepository, IBaseRepository<SysMenu> sysMenuRepository)
  20. {
  21. this._unitOfWork = unitOfWork;
  22. this._dbContext = dbContext;
  23. this._sysRoleRepository = sysRoleRepository;
  24. this._sysMenuRepository = sysMenuRepository;
  25. }
  26. public bool AddModel(SysRole model)
  27. {
  28. try
  29. {
  30. model.GuidId = Guid.NewGuid().ToString();
  31. model.IsEnabled = model.IsEnabled != null;
  32. model.DeleteMark = false;
  33. model.CreateTime = DateTime.Now;
  34. var res = _sysRoleRepository.Save(model);
  35. return res;
  36. }
  37. catch (Exception ex)
  38. {
  39. throw ex;
  40. }
  41. }
  42. public bool ModifyModel(SysRole model)
  43. {
  44. try
  45. {
  46. var role = _sysRoleRepository.Get(model.Id);
  47. role.EnCode = model.EnCode;
  48. role.Type = model.Type;
  49. role.OrganizeId = model.OrganizeId;
  50. role.RoleName = model.RoleName;
  51. role.AllowEdit = model.AllowEdit;
  52. role.IsEnabled = model.IsEnabled;
  53. role.ModifyTime = DateTime.Now;
  54. role.ModifyUser = model.ModifyUser;
  55. role.Remark = model.Remark;
  56. var res = _sysRoleRepository.Update(role);
  57. return res;
  58. }
  59. catch (Exception ex)
  60. {
  61. throw ex;
  62. }
  63. }
  64. public SysRole GetRole(int key)
  65. {
  66. try
  67. {
  68. if (!key.IsEmpty() || key > 0)
  69. {
  70. var data = _sysRoleRepository.Get(key);
  71. return data;
  72. }
  73. return null;
  74. }
  75. catch (Exception e)
  76. {
  77. throw e;
  78. }
  79. }
  80. public SysRole GetRoleByKey(int key)
  81. {
  82. try
  83. {
  84. if (!key.IsEmpty() || key > 0)
  85. {
  86. var data = _customerOrgById(_dbContext, key);
  87. return data;
  88. }
  89. return null;
  90. }
  91. catch (Exception e)
  92. {
  93. throw e;
  94. }
  95. }
  96. public List<int> GetMenuIdListByRoleId(int roleId)
  97. {
  98. try
  99. {
  100. var currentRole = _customerById(_dbContext, roleId);
  101. //var data = currentRole.RoleAuthorizeList.Where(m => m.RoleId == roleId).Select(m => m.MenuId).ToList();
  102. var data = currentRole.RoleAuthorizes.Where(m => m.RoleId == roleId).Select(m => m.MenuId).ToList();
  103. return data;
  104. }
  105. catch (Exception e)
  106. {
  107. throw e;
  108. }
  109. }
  110. public Page<SysRole> PageList(string keyword, int pagesize, int pageindex)
  111. {
  112. try
  113. {
  114. int total = 0;
  115. if (keyword.IsEmpty())
  116. {
  117. var data = _sysRoleRepository.PageList(pageindex, pagesize, us => us.OrderBy(ur => ur.SortCode),
  118. u => _customerOrgById(_dbContext, u.Id),
  119. out total,
  120. u => u.DeleteMark == false);
  121. return data;
  122. }
  123. else
  124. {
  125. var data = _sysRoleRepository.PageList(pageindex, pagesize, us => us.OrderBy(ur => ur.SortCode), u => _customerOrgById(_dbContext, u.Id),
  126. out total,
  127. u => (u.RoleName.Contains(keyword) || u.EnCode.Contains(keyword)) && u.DeleteMark == false);
  128. return data;
  129. }
  130. }
  131. catch (Exception e)
  132. {
  133. throw e;
  134. }
  135. }
  136. public List<SysRole> GetSysRoleList()
  137. {
  138. try
  139. {
  140. var data = _sysRoleRepository.LoadAll(m => m.IsEnabled.Value == true && m.DeleteMark.Value == false)
  141. .OrderBy(ms => ms.SortCode)
  142. .AsNoTracking()
  143. .ToList();
  144. return data;
  145. }
  146. catch (Exception e)
  147. {
  148. throw e;
  149. }
  150. }
  151. public void SetRoleAuthorize(int roleId, params int[] menuIds)
  152. {
  153. var currentRole = _customerById(_dbContext, roleId);
  154. if (menuIds == null)
  155. {
  156. //清空当前角色下所有权限
  157. currentRole.RoleAuthorizes.Clear();
  158. }
  159. else
  160. {
  161. //a.角色需要重新设置的权限ID集合。
  162. var newMenuIdList = menuIds.ToList();
  163. var newMenuList = _sysMenuRepository.LoadAll(m => newMenuIdList.Contains(m.Id) && m.IsEnable == true && m.DeleteMark == false).ToList();
  164. //b.角色原有的授权信息。
  165. //var oldMenuList = currentRole.RoleAuthorizeList.Where(m => m.RoleId == roleId).ToList();
  166. //c.清空当前角色下所有权限
  167. currentRole.RoleAuthorizes.Clear();
  168. //d.新集合中剩下的授权信息新增到数据库。
  169. RoleAuthorize model;
  170. newMenuList.ForEach((menu) =>
  171. {
  172. model = new RoleAuthorize
  173. {
  174. GuidId = Guid.NewGuid().ToString(),
  175. RoleId = roleId,
  176. MenuId = menu.Id,
  177. SortCode = menu.SortCode,
  178. CreateTime = DateTime.Now
  179. //CreateUser = "",
  180. };
  181. currentRole.RoleAuthorizes.Add(model);
  182. });
  183. }
  184. _unitOfWork.SaveChanges();
  185. }
  186. #region 私有方法
  187. private static Func<PermissionContext, int, SysRole> _customerById =
  188. EF.CompileQuery((PermissionContext db, int roleId) => db.SysRoles
  189. .Include(u => u.RoleAuthorizes)
  190. .FirstOrDefault(c => c.Id == roleId));
  191. private static Func<PermissionContext, int, SysRole> _customerOrgById =
  192. EF.CompileQuery((PermissionContext db, int roleId) => db.SysRoles
  193. .Include(u => u.Organize)
  194. .FirstOrDefault(c => c.Id == roleId));
  195. #endregion
  196. }
  197. }