RoleService.cs 8.8 KB

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