RoleService.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /// <summary>
  46. /// 修改权限
  47. /// </summary>
  48. /// <param name="model"></param>
  49. /// <returns></returns>
  50. public async Task<bool> UpdateSystemRoleAsync(Entity.SystemRole.SystemRole model)
  51. {
  52. return await systemRoleRepository.UpdateAsync(model);
  53. }
  54. /// <summary>
  55. /// 删除权限
  56. /// </summary>
  57. /// <param name="id"></param>
  58. /// <returns></returns>
  59. public async Task<bool> DeleteSystemRoleAsync(int id)
  60. {
  61. return await systemRoleRepository.DeleteAsync(id);
  62. }
  63. /// <summary>
  64. /// 添加菜单
  65. /// </summary>
  66. /// <param name="models"></param>
  67. /// <returns></returns>
  68. public async Task<bool> InsertMenusAsync(Entity.Menus.Menus model)
  69. {
  70. return await menusRepository.InsertAsync(model) > 0;
  71. }
  72. /// <summary>
  73. /// 添加菜单
  74. /// </summary>
  75. /// <param name="models"></param>
  76. /// <returns></returns>
  77. public async Task<bool> InsertMenusAsync(List<MenuInRequest> models)
  78. {
  79. try
  80. {
  81. using (var transactionScope = new TransactionScope())
  82. {
  83. foreach (var item in models)
  84. {
  85. var menuEntity = new Entity.Menus.Menus()
  86. {
  87. Icon = item.Icon,
  88. Name = item.Name,
  89. ParentId = item.ParentId,
  90. Path = item.Path,
  91. Title = item.Title
  92. };
  93. var id = await menusRepository.InsertAsync(menuEntity);
  94. if (item.Children == null)
  95. continue;
  96. foreach (var menu in item.Children)
  97. {
  98. menuEntity = new Entity.Menus.Menus()
  99. {
  100. Icon = item.Icon,
  101. Name = item.Name,
  102. ParentId = id,
  103. Path = item.Path,
  104. Title = item.Title
  105. };
  106. await menusRepository.InsertAsync(menuEntity);
  107. }
  108. }
  109. transactionScope.Complete();
  110. }
  111. }
  112. catch (System.Exception ex)
  113. {
  114. throw new Common.Exceptions.BusinessException(ex.Message);
  115. }
  116. return true;
  117. }
  118. /// <summary>
  119. /// 修改菜单
  120. /// </summary>
  121. /// <param name="model"></param>
  122. /// <returns></returns>
  123. public async Task<bool> UpdateMenusAsync(Entity.Menus.Menus model)
  124. {
  125. return await menusRepository.UpdateAsync(model);
  126. }
  127. /// <summary>
  128. /// 删除菜单
  129. /// </summary>
  130. /// <param name="id"></param>
  131. /// <returns></returns>
  132. public async Task<bool> DeleteMenusAsync(int id)
  133. {
  134. return await menusRepository.DeleteAsync(id);
  135. }
  136. /// <summary>
  137. /// 根据权限ID获取菜单
  138. /// </summary>
  139. /// <param name="roleId"></param>
  140. /// <returns></returns>
  141. public async Task<IEnumerable<Entity.Menus.Menus>> GetMenusAllAsync(int roleId)
  142. {
  143. //获取
  144. var models = await systemRoleMenusRepository.GetAllAsync(roleId);
  145. return await menusRepository.GetAllAsync(models.Select(n => n.Id).ToList());
  146. }
  147. /// <summary>
  148. /// 获取所有菜单
  149. /// </summary>
  150. /// <param name="roleId"></param>
  151. /// <returns></returns>
  152. public async Task<IEnumerable<Entity.Menus.Menus>> GetMenusAllAsync()
  153. {
  154. return await menusRepository.GetAllAsync();
  155. }
  156. }
  157. }