SaasRoleMenuHandler.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package com.persagy.person.manage;
  2. import java.util.ArrayList;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.transaction.annotation.Transactional;
  9. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  10. import com.google.common.collect.Lists;
  11. import com.persagy.common.constant.SaasCommonConstant;
  12. import com.persagy.common.exception.BusinessException;
  13. import com.persagy.common.utils.StringUtil;
  14. import com.persagy.person.pojo.dto.SaasAccountRole;
  15. import com.persagy.person.pojo.dto.SaasFunction;
  16. import com.persagy.person.pojo.dto.SaasMenu;
  17. import com.persagy.person.pojo.dto.SaasRole;
  18. import com.persagy.person.pojo.dto.SaasRoleMenu;
  19. import com.persagy.person.pojo.vo.auth.SaasMenuFunctionVO;
  20. import com.persagy.person.pojo.vo.role.SaasRoleDeleteVO;
  21. import com.persagy.person.service.ISaasAccountRoleService;
  22. import com.persagy.person.service.ISaasFunctionService;
  23. import com.persagy.person.service.ISaasMenuService;
  24. import com.persagy.person.service.ISaasRoleMenuService;
  25. import com.persagy.person.service.ISaasRoleService;
  26. import cn.hutool.core.collection.CollectionUtil;
  27. /**
  28. * 角色菜单管理
  29. *
  30. * @version
  31. * @description
  32. * @company persagy
  33. * @author zhangqiankun
  34. * @since 2021年3月15日: 上午10:49:15
  35. */
  36. @Component
  37. public class SaasRoleMenuHandler {
  38. @Autowired
  39. private ISaasRoleService saasRoleService;
  40. @Autowired
  41. private ISaasMenuService saasMenuService;
  42. @Autowired
  43. private ISaasFunctionService saasFunctionService;
  44. @Autowired
  45. private ISaasRoleMenuService saasRoleMenuService;
  46. @Autowired
  47. private ISaasAccountRoleService saasAccountRoleService;
  48. /**
  49. * 查询账号菜单功能权限树,菜单定义和角色定义编码相同,所以这里应该同步传递
  50. *
  51. * @param accountId
  52. * @param groupCode
  53. * @param appId
  54. * @param isAdmin 是否为超管 true-是
  55. * @param isMenu 是否仅查询出菜单树,true-是
  56. * @return 空集合或数据菜单树
  57. */
  58. public List<SaasMenu> querySaasMenuTree(String accountId, String groupCode, String appId, boolean isAdmin, boolean isMenu) {
  59. // 1.获取此账号对应菜单集合
  60. List<SaasMenu> menuTree = null;
  61. if (isAdmin) {
  62. LambdaQueryWrapper<SaasMenu> queryWrapper = new SaasMenu.Builder().createQueryWrapper().appIdEq(appId).menuTypeEq(SaasCommonConstant.STR_STATUS_0).builderQueryWrapper();
  63. menuTree = this.saasMenuService.list(queryWrapper);
  64. } else {
  65. menuTree = this.saasMenuService.querySaasMenuTree(accountId, groupCode, appId, SaasCommonConstant.STATUS_1);
  66. }
  67. if (CollectionUtil.isEmpty(menuTree)) {
  68. return Lists.newArrayList();
  69. }
  70. // 2.转为菜单树
  71. List<SaasMenu> topMenus = new ArrayList<SaasMenu>();
  72. for (int i = menuTree.size() - 1; i >= 0; i--) {
  73. SaasMenu saasMenu = menuTree.get(i);
  74. if (StringUtil.isBlank(saasMenu.getParentId())) {
  75. topMenus.add(saasMenu);
  76. menuTree.remove(i);
  77. }
  78. }
  79. List<SaasMenu> parents = topMenus.stream().sorted(Comparator.comparing(SaasMenu::getMenuSort)).collect(Collectors.toList());
  80. this.setMenuChildrens(accountId, menuTree, parents, isAdmin, !isMenu);
  81. return parents;
  82. }
  83. /**
  84. * 生成菜单树
  85. * @param accountId 为null时,isAdmin 为true
  86. * @param menuTree
  87. * @param parents
  88. * @param isAdmin 是否为超管 true-是
  89. * @param menuAndFun
  90. */
  91. public void setMenuChildrens(String accountId, List<SaasMenu> menuTree, List<SaasMenu> parents, boolean isAdmin, boolean menuAndFun) {
  92. for (SaasMenu parent : parents) {
  93. if (StringUtil.isBlank(parent.getMenuId())) {
  94. parent.setMenuId(parent.getId());
  95. }
  96. if (menuAndFun && StringUtil.isNotBlank(parent.getMenuUrl())) {
  97. // 查询出对应功能点集合
  98. List<SaasFunction> functions = null;
  99. if (isAdmin) {
  100. LambdaQueryWrapper<SaasFunction> queryWrapper = new SaasFunction.Builder().createQueryWrapper().menuIdEq(parent.getMenuId()).builderQueryWrapper();
  101. functions = this.saasFunctionService.list(queryWrapper.orderByAsc(SaasFunction::getFunSort));
  102. } else {
  103. functions = this.saasFunctionService.queryAccountFunctionList(accountId, parent.getGroupCode(), parent.getMenuId());
  104. }
  105. parent.setFunctions(CollectionUtil.isEmpty(functions) ? Lists.newArrayList() : functions);
  106. }
  107. List<SaasMenu> childrens = new ArrayList<SaasMenu>();
  108. for (int i = menuTree.size() - 1; i >= 0; i--) {
  109. SaasMenu saasMenu = menuTree.get(i);
  110. if (parent.getMenuId().equals(saasMenu.getParentId())) {
  111. childrens.add(saasMenu);
  112. menuTree.remove(i);
  113. }
  114. }
  115. List<SaasMenu> nextTops = childrens.stream().sorted(Comparator.comparing(SaasMenu::getMenuSort)).collect(Collectors.toList());
  116. parent.setChildrens(nextTops);
  117. this.setMenuChildrens(accountId, menuTree, nextTops, isAdmin, menuAndFun);
  118. }
  119. }
  120. /**
  121. * 删除角色,级联删除账号角色,角色菜单的关联关系
  122. *
  123. * @param deleteVO
  124. */
  125. @Transactional
  126. public boolean deleteSaasRole(SaasRoleDeleteVO deleteVO) {
  127. LambdaQueryWrapper<SaasRole> updateWrapper = new SaasRole.Builder().createQueryWrapper().idEq(deleteVO.getId())
  128. .validEq(SaasCommonConstant.STATUS_1).builderQueryWrapper();
  129. boolean result = this.saasRoleService.remove(updateWrapper);
  130. if (result) {
  131. // 1.级联删除账号和角色关联关系
  132. LambdaQueryWrapper<SaasAccountRole> accountRoleWrapper = new SaasAccountRole.Builder().createQueryWrapper().roleIdEq(deleteVO.getId()).builderQueryWrapper();
  133. this.saasAccountRoleService.remove(accountRoleWrapper);
  134. // 2.级联删除角色和菜单的关联关系
  135. LambdaQueryWrapper<SaasRoleMenu> roleMenuWrapper = new SaasRoleMenu.Builder().createQueryWrapper().roleIdEq(deleteVO.getId()).builderQueryWrapper();
  136. this.saasRoleMenuService.remove(roleMenuWrapper);
  137. }
  138. return result;
  139. }
  140. /**
  141. * 删除菜单,级联删除,角色、菜单、功能三者关联关系数据
  142. *
  143. * @param menuId
  144. * @return
  145. */
  146. @Transactional
  147. public boolean deleteSaasMenu(String menuId) {
  148. boolean result = this.saasMenuService.removeById(menuId);
  149. if (result) {
  150. // 1.删除此菜单下功能
  151. LambdaQueryWrapper<SaasFunction> funWrapper = new SaasFunction.Builder().createQueryWrapper().menuIdEq(menuId).builderQueryWrapper();
  152. this.saasFunctionService.remove(funWrapper);
  153. // 2.删除此功能对应的菜单、角色关系数据
  154. LambdaQueryWrapper<SaasRoleMenu> roleMenuWrapper = new SaasRoleMenu.Builder().createQueryWrapper().menuIdEq(menuId).builderQueryWrapper();
  155. this.saasRoleMenuService.remove(roleMenuWrapper);
  156. }
  157. return result;
  158. }
  159. /**
  160. * 删除功能,级联删除菜单、功能关系
  161. * @param menuId
  162. *
  163. * @param id
  164. * @return
  165. */
  166. @Transactional
  167. public boolean deleteSaasFunction(String funId, String menuId) {
  168. LambdaQueryWrapper<SaasFunction> queryWrapper = new SaasFunction.Builder().createQueryWrapper().menuIdEq(menuId).idEq(funId).builderQueryWrapper();
  169. boolean result = this.saasFunctionService.removeById(queryWrapper);
  170. if (result) {
  171. // 1.删除此功能对应的菜单、角色关系数据
  172. LambdaQueryWrapper<SaasRoleMenu> roleMenuWrapper = new SaasRoleMenu.Builder().createQueryWrapper().menuIdEq(menuId).functionIdEq(funId).builderQueryWrapper();
  173. this.saasRoleMenuService.remove(roleMenuWrapper);
  174. }
  175. return result;
  176. }
  177. /**
  178. * 先删后新增,仅删除此账号角色下的所有菜单功能点的关联关系,并重新新增
  179. *
  180. * @param batchVO
  181. * @return
  182. */
  183. @Transactional
  184. public boolean resetSaasRoleMenu(String groupCode, String roleId, List<SaasMenuFunctionVO> auths) {
  185. LambdaQueryWrapper<SaasRoleMenu> roleMenuWrapper = new SaasRoleMenu.Builder().createQueryWrapper().roleIdEq(roleId).builderQueryWrapper();
  186. this.saasRoleMenuService.remove(roleMenuWrapper);
  187. // 1.批量新增
  188. boolean result = this.saasRoleMenuService.batchCreateSaasRoleMenu(groupCode, roleId, auths);
  189. if (!result) {
  190. throw new BusinessException("先删后新增角色菜单功能权限失败");
  191. }
  192. return result;
  193. }
  194. /**
  195. * 创建角色,同时添加菜单关联关系
  196. * @param saasRole
  197. * @param auths
  198. * @return
  199. */
  200. @Transactional
  201. public boolean createSaasRole(SaasRole saasRole, List<SaasMenuFunctionVO> auths) {
  202. // 1.创建角色信息
  203. boolean result = this.saasRoleService.save(saasRole);
  204. if (result && CollectionUtil.isNotEmpty(auths)) {
  205. // 2.删除并创建角色信息
  206. this.resetSaasRoleMenu(saasRole.getGroupCode(), saasRole.getId(), auths);
  207. }
  208. return result;
  209. }
  210. /**
  211. * 更新角色,同时更新菜单关联关系
  212. * @param saasRole
  213. * @param auths
  214. * @return
  215. */
  216. @Transactional
  217. public boolean updateSaasRole(SaasRole saasRole, List<SaasMenuFunctionVO> auths) {
  218. boolean result = this.saasRoleService.updateById(saasRole);
  219. if (result && CollectionUtil.isNotEmpty(auths)) {
  220. this.resetSaasRoleMenu(saasRole.getGroupCode(), saasRole.getId(), auths);
  221. }
  222. return result;
  223. }
  224. }