RoleService.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. using GxPress.Result.SystemRole;
  13. namespace GxPress.Service.Implement.Role
  14. {
  15. public class RoleService : IRoleService
  16. {
  17. private readonly ISystemRoleMenusRepository systemRoleMenusRepository;
  18. private readonly IMenusRepository menusRepository;
  19. private readonly ISystemRoleRepository systemRoleRepository;
  20. private readonly IMapper _mapper;
  21. public RoleService(ISystemRoleMenusRepository systemRoleMenusRepository, IMenusRepository menusRepository, ISystemRoleRepository systemRoleRepository, IMapper _mapper)
  22. {
  23. this.systemRoleRepository = systemRoleRepository;
  24. this.systemRoleMenusRepository = systemRoleMenusRepository;
  25. this.menusRepository = menusRepository;
  26. this._mapper = _mapper;
  27. }
  28. /// <summary>
  29. /// 添加权限菜单关联
  30. /// </summary>
  31. /// <param name="models"></param>
  32. /// <returns></returns>
  33. public async Task<bool> InsertSystemRoleMenusAsync(SystemRoleMenusInRequest models)
  34. {
  35. return await systemRoleMenusRepository.InsertAsync(models);
  36. }
  37. /// <summary>
  38. /// 添加权限
  39. /// </summary>
  40. /// <param name="models"></param>
  41. /// <returns></returns>
  42. public async Task<bool> InsertSystemRoleAsync(RoleInRequest request)
  43. {
  44. try
  45. {
  46. using (var transactionScope = new TransactionScope())
  47. {
  48. var role = new Entity.SystemRole.SystemRole();
  49. role.Name = request.Name;
  50. var roleId = await systemRoleRepository.InsertAsync(role);
  51. var models = new SystemRoleMenusInRequest();
  52. models.RoleId = roleId;
  53. models.SystemRoleMenuInRequest = request.SystemRoleMenuInRequest;
  54. await systemRoleMenusRepository.InsertAsync(models);
  55. transactionScope.Complete();
  56. }
  57. }
  58. catch (System.Exception ex)
  59. {
  60. throw new Common.Exceptions.BusinessException(ex.Message);
  61. }
  62. return true;
  63. }
  64. public async Task<List<SystemRoleResult>> GetSystemRoleAllAsync()
  65. {
  66. var result = new List<SystemRoleResult>();
  67. var systemRoles = await systemRoleRepository.GetSystemRolesAllAsync();
  68. foreach (var systemRole in systemRoles)
  69. {
  70. var models = await systemRoleMenusRepository.GetAllAsync(systemRole.Id);
  71. var item = await menusRepository.GetAllMenusAsync(models.Select(n => n.MenuId).ToList());
  72. var systemRoleResult = new SystemRoleResult();
  73. systemRoleResult.SystemRole = systemRole;
  74. var model = item.Where(n => n.ParentId == 0).Select(n => _mapper.Map<MenuInResult>(n)).ToList();
  75. foreach (var request in model)
  76. {
  77. request.Children = item.Where(n => n.ParentId == request.Id).Select(n => _mapper.Map<MenuInResult>(n)).ToList();
  78. }
  79. systemRoleResult.MenuInResult = model;
  80. result.Add(systemRoleResult);
  81. }
  82. return result;
  83. }
  84. /// <summary>
  85. /// 修改权限
  86. /// </summary>
  87. /// <param name="model"></param>
  88. /// <returns></returns>
  89. public async Task<bool> UpdateSystemRoleAsync(RoleInRequest request)
  90. {
  91. try
  92. {
  93. using (var transactionScope = new TransactionScope())
  94. {
  95. var role = await systemRoleRepository.GetRoleAsync(request.Id);
  96. role.Name = request.Name;
  97. await systemRoleRepository.UpdateAsync(role);
  98. var models = new SystemRoleMenusInRequest();
  99. models.RoleId = request.Id;
  100. models.SystemRoleMenuInRequest = request.SystemRoleMenuInRequest;
  101. await systemRoleMenusRepository.InsertAsync(models);
  102. transactionScope.Complete();
  103. }
  104. }
  105. catch (System.Exception ex)
  106. {
  107. throw new Common.Exceptions.BusinessException(ex.Message);
  108. }
  109. return true;
  110. }
  111. /// <summary>
  112. /// 删除权限
  113. /// </summary>
  114. /// <param name="id"></param>
  115. /// <returns></returns>
  116. public async Task<bool> DeleteSystemRoleAsync(int id)
  117. {
  118. await systemRoleMenusRepository.DeleteAsync(id);
  119. return await systemRoleRepository.DeleteAsync(id);
  120. }
  121. /// <summary>
  122. /// 添加菜单
  123. /// </summary>
  124. /// <param name="models"></param>
  125. /// <returns></returns>
  126. public async Task<bool> InsertMenusAsync(Entity.Menus.Menus model)
  127. {
  128. return await menusRepository.InsertAsync(model) > 0;
  129. }
  130. /// <summary>
  131. /// 添加菜单
  132. /// </summary>
  133. /// <param name="models"></param>
  134. /// <returns></returns>
  135. public async Task<bool> InsertMenusAsync(List<MenuInRequest> models)
  136. {
  137. try
  138. {
  139. using (var transactionScope = new TransactionScope())
  140. {
  141. foreach (var item in models)
  142. {
  143. var menuEntity = new Entity.Menus.Menus()
  144. {
  145. Icon = item.Icon,
  146. Name = item.Name,
  147. ParentId = item.ParentId,
  148. Path = item.Path,
  149. Title = item.Title
  150. };
  151. var id = await menusRepository.InsertAsync(menuEntity);
  152. if (item.Children == null)
  153. continue;
  154. foreach (var menu in item.Children)
  155. {
  156. menuEntity = new Entity.Menus.Menus()
  157. {
  158. Icon = menu.Icon,
  159. Name = menu.Name,
  160. ParentId = id,
  161. Path = menu.Path,
  162. Title = menu.Title
  163. };
  164. await menusRepository.InsertAsync(menuEntity);
  165. }
  166. }
  167. transactionScope.Complete();
  168. }
  169. }
  170. catch (System.Exception ex)
  171. {
  172. throw new Common.Exceptions.BusinessException(ex.Message);
  173. }
  174. return true;
  175. }
  176. /// <summary>
  177. /// 修改菜单
  178. /// </summary>
  179. /// <param name="model"></param>
  180. /// <returns></returns>
  181. public async Task<bool> UpdateMenusAsync(Entity.Menus.Menus model)
  182. {
  183. return await menusRepository.UpdateAsync(model);
  184. }
  185. /// <summary>
  186. /// 删除菜单
  187. /// </summary>
  188. /// <param name="id"></param>
  189. /// <returns></returns>
  190. public async Task<bool> DeleteMenusAsync(int id)
  191. {
  192. return await menusRepository.DeleteAsync(id);
  193. }
  194. /// <summary>
  195. /// 根据权限ID获取菜单
  196. /// </summary>
  197. /// <param name="roleId"></param>
  198. /// <returns></returns>
  199. public async Task<List<MenuInResult>> GetMenusAllAsync(int roleId)
  200. {
  201. //获取
  202. var models = await systemRoleMenusRepository.GetAllAsync(roleId);
  203. var item = await menusRepository.GetAllAsync(models.Select(n => n.MenuId).ToList());
  204. var result = new List<MenuInResult>();
  205. result = item.Select(n=>_mapper.Map<MenuInResult>(n)).ToList();
  206. return result;
  207. }
  208. /// <summary>
  209. /// 获取所有菜单
  210. /// </summary>
  211. /// <param name="roleId"></param>
  212. /// <returns></returns>
  213. public async Task<IEnumerable<Entity.Menus.Menus>> GetMenusAllAsync()
  214. {
  215. return await menusRepository.GetAllAsync();
  216. }
  217. }
  218. }