RoleService.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using GxPress.Repository.Interface.SystemRoleMenus;
  2. using GxPress.Service.Interface.Role;
  3. using GxPress.Repository.Interface.SystemRole;
  4. using GxPress.Repository.Interface.Menus;
  5. using System.Threading.Tasks;
  6. using GxPress.Request.Role;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Transactions;
  10. using AutoMapper;
  11. using GxPress.Request.Menu;
  12. namespace GxPress.Service.Implement.Role
  13. {
  14. public class RoleService : IRoleService
  15. {
  16. private readonly ISystemRoleMenusRepository systemRoleMenusRepository;
  17. private readonly IMenusRepository menusRepository;
  18. private readonly ISystemRoleRepository systemRoleRepository;
  19. private readonly IMapper _mapper;
  20. public RoleService(ISystemRoleMenusRepository systemRoleMenusRepository, IMenusRepository menusRepository, ISystemRoleRepository systemRoleRepository, IMapper _mapper)
  21. {
  22. this.systemRoleRepository = systemRoleRepository;
  23. this.systemRoleMenusRepository = systemRoleMenusRepository;
  24. this.menusRepository = menusRepository;
  25. this._mapper = _mapper;
  26. }
  27. /// <summary>
  28. /// 添加权限菜单关联
  29. /// </summary>
  30. /// <param name="models"></param>
  31. /// <returns></returns>
  32. public async Task<bool> InsertSystemRoleMenusAsync(SystemRoleMenusInRequest models)
  33. {
  34. return await systemRoleMenusRepository.InsertAsync(models);
  35. }
  36. /// <summary>
  37. /// 添加权限
  38. /// </summary>
  39. /// <param name="models"></param>
  40. /// <returns></returns>
  41. public async Task<bool> InsertSystemRoleAsync(Entity.SystemRole.SystemRole model)
  42. {
  43. return await systemRoleRepository.InsertAsync(model) > 0;
  44. }
  45. public async Task<IEnumerable<Entity.SystemRole.SystemRole>> GetSystemRoleAllAsync()
  46. {
  47. return await systemRoleRepository.GetSystemRolesAllAsync();
  48. }
  49. /// <summary>
  50. /// 修改权限
  51. /// </summary>
  52. /// <param name="model"></param>
  53. /// <returns></returns>
  54. public async Task<bool> UpdateSystemRoleAsync(Entity.SystemRole.SystemRole model)
  55. {
  56. return await systemRoleRepository.UpdateAsync(model);
  57. }
  58. /// <summary>
  59. /// 删除权限
  60. /// </summary>
  61. /// <param name="id"></param>
  62. /// <returns></returns>
  63. public async Task<bool> DeleteSystemRoleAsync(int id)
  64. {
  65. return await systemRoleRepository.DeleteAsync(id);
  66. }
  67. /// <summary>
  68. /// 添加菜单
  69. /// </summary>
  70. /// <param name="models"></param>
  71. /// <returns></returns>
  72. public async Task<bool> InsertMenusAsync(Entity.Menus.Menus model)
  73. {
  74. return await menusRepository.InsertAsync(model) > 0;
  75. }
  76. /// <summary>
  77. /// 添加菜单
  78. /// </summary>
  79. /// <param name="models"></param>
  80. /// <returns></returns>
  81. public async Task<bool> InsertMenusAsync(List<MenuInRequest> models)
  82. {
  83. try
  84. {
  85. using (var transactionScope = new TransactionScope())
  86. {
  87. foreach (var item in models)
  88. {
  89. var menuEntity = new Entity.Menus.Menus()
  90. {
  91. Icon = item.Icon,
  92. Name = item.Name,
  93. ParentId = item.ParentId,
  94. Path = item.Path,
  95. Title = item.Title
  96. };
  97. var id = await menusRepository.InsertAsync(menuEntity);
  98. if (item.Children == null)
  99. continue;
  100. foreach (var menu in item.Children)
  101. {
  102. menuEntity = new Entity.Menus.Menus()
  103. {
  104. Icon = menu.Icon,
  105. Name = menu.Name,
  106. ParentId = id,
  107. Path = menu.Path,
  108. Title = menu.Title
  109. };
  110. await menusRepository.InsertAsync(menuEntity);
  111. }
  112. }
  113. transactionScope.Complete();
  114. }
  115. }
  116. catch (System.Exception ex)
  117. {
  118. throw new Common.Exceptions.BusinessException(ex.Message);
  119. }
  120. return true;
  121. }
  122. /// <summary>
  123. /// 修改菜单
  124. /// </summary>
  125. /// <param name="model"></param>
  126. /// <returns></returns>
  127. public async Task<bool> UpdateMenusAsync(Entity.Menus.Menus model)
  128. {
  129. return await menusRepository.UpdateAsync(model);
  130. }
  131. /// <summary>
  132. /// 删除菜单
  133. /// </summary>
  134. /// <param name="id"></param>
  135. /// <returns></returns>
  136. public async Task<bool> DeleteMenusAsync(int id)
  137. {
  138. return await menusRepository.DeleteAsync(id);
  139. }
  140. /// <summary>
  141. /// 根据权限ID获取菜单
  142. /// </summary>
  143. /// <param name="roleId"></param>
  144. /// <returns></returns>
  145. public async Task<IEnumerable<Entity.Menus.Menus>> GetMenusAllAsync(int roleId)
  146. {
  147. //获取
  148. var models = await systemRoleMenusRepository.GetAllAsync(roleId);
  149. return await menusRepository.GetAllAsync(models.Select(n => n.Id).ToList());
  150. }
  151. /// <summary>
  152. /// 获取所有菜单
  153. /// </summary>
  154. /// <param name="roleId"></param>
  155. /// <returns></returns>
  156. public async Task<IEnumerable<Entity.Menus.Menus>> GetMenusAllAsync()
  157. {
  158. return await menusRepository.GetAllAsync();
  159. }
  160. }
  161. }