RoleService.cs 5.0 KB

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