package com.persagy.person.manage; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.google.common.collect.Lists; import com.persagy.common.constant.SaasCommonConstant; import com.persagy.common.exception.BusinessException; import com.persagy.common.utils.StringUtil; import com.persagy.person.pojo.dto.SaasAccountRole; import com.persagy.person.pojo.dto.SaasFunction; import com.persagy.person.pojo.dto.SaasMenu; import com.persagy.person.pojo.dto.SaasRole; import com.persagy.person.pojo.dto.SaasRoleMenu; import com.persagy.person.pojo.vo.auth.SaasMenuFunctionVO; import com.persagy.person.pojo.vo.role.SaasRoleDeleteVO; import com.persagy.person.service.ISaasAccountRoleService; import com.persagy.person.service.ISaasFunctionService; import com.persagy.person.service.ISaasMenuService; import com.persagy.person.service.ISaasRoleMenuService; import com.persagy.person.service.ISaasRoleService; import cn.hutool.core.collection.CollectionUtil; /** * 角色菜单管理 * * @version * @description * @company persagy * @author zhangqiankun * @since 2021年3月15日: 上午10:49:15 */ @Component public class SaasRoleMenuHandler { @Autowired private ISaasRoleService saasRoleService; @Autowired private ISaasMenuService saasMenuService; @Autowired private ISaasFunctionService saasFunctionService; @Autowired private ISaasRoleMenuService saasRoleMenuService; @Autowired private ISaasAccountRoleService saasAccountRoleService; /** * 查询账号菜单功能权限树,菜单定义和角色定义编码相同,所以这里应该同步传递 * * @param accountId * @param groupCode * @param appId * @param isAdmin 是否为超管 true-是 * @param isMenu 是否仅查询出菜单树,true-是 * @return 空集合或数据菜单树 */ public List querySaasMenuTree(String accountId, String groupCode, String appId, boolean isAdmin, boolean isMenu) { // 1.获取此账号对应菜单集合 List menuTree = null; if (isAdmin) { LambdaQueryWrapper queryWrapper = new SaasMenu.Builder().createQueryWrapper().appIdEq(appId).menuTypeEq(SaasCommonConstant.STR_STATUS_0).builderQueryWrapper(); menuTree = this.saasMenuService.list(queryWrapper); } else { menuTree = this.saasMenuService.querySaasMenuTree(accountId, groupCode, appId, SaasCommonConstant.STATUS_1); } if (CollectionUtil.isEmpty(menuTree)) { return Lists.newArrayList(); } // 2.转为菜单树 List topMenus = new ArrayList(); for (int i = menuTree.size() - 1; i >= 0; i--) { SaasMenu saasMenu = menuTree.get(i); if (StringUtil.isBlank(saasMenu.getParentId())) { topMenus.add(saasMenu); menuTree.remove(i); } } List parents = topMenus.stream().sorted(Comparator.comparing(SaasMenu::getMenuSort)).collect(Collectors.toList()); this.setMenuChildrens(accountId, menuTree, parents, isAdmin, !isMenu); return parents; } /** * 生成菜单树 * @param accountId 为null时,isAdmin 为true * @param menuTree * @param parents * @param isAdmin 是否为超管 true-是 * @param menuAndFun */ public void setMenuChildrens(String accountId, List menuTree, List parents, boolean isAdmin, boolean menuAndFun) { for (SaasMenu parent : parents) { if (StringUtil.isBlank(parent.getMenuId())) { parent.setMenuId(parent.getId()); } if (menuAndFun && StringUtil.isNotBlank(parent.getMenuUrl())) { // 查询出对应功能点集合 List functions = null; if (isAdmin) { LambdaQueryWrapper queryWrapper = new SaasFunction.Builder().createQueryWrapper().menuIdEq(parent.getMenuId()).builderQueryWrapper(); functions = this.saasFunctionService.list(queryWrapper.orderByAsc(SaasFunction::getFunSort)); } else { functions = this.saasFunctionService.queryAccountFunctionList(accountId, parent.getGroupCode(), parent.getMenuId()); } parent.setFunctions(CollectionUtil.isEmpty(functions) ? Lists.newArrayList() : functions); } List childrens = new ArrayList(); for (int i = menuTree.size() - 1; i >= 0; i--) { SaasMenu saasMenu = menuTree.get(i); if (parent.getMenuId().equals(saasMenu.getParentId())) { childrens.add(saasMenu); menuTree.remove(i); } } List nextTops = childrens.stream().sorted(Comparator.comparing(SaasMenu::getMenuSort)).collect(Collectors.toList()); parent.setChildrens(nextTops); this.setMenuChildrens(accountId, menuTree, nextTops, isAdmin, menuAndFun); } } /** * 删除角色,级联删除账号角色,角色菜单的关联关系 * * @param deleteVO */ @Transactional public boolean deleteSaasRole(SaasRoleDeleteVO deleteVO) { LambdaQueryWrapper updateWrapper = new SaasRole.Builder().createQueryWrapper().idEq(deleteVO.getId()) .validEq(SaasCommonConstant.STATUS_1).builderQueryWrapper(); boolean result = this.saasRoleService.remove(updateWrapper); if (result) { // 1.级联删除账号和角色关联关系 LambdaQueryWrapper accountRoleWrapper = new SaasAccountRole.Builder().createQueryWrapper().roleIdEq(deleteVO.getId()).builderQueryWrapper(); this.saasAccountRoleService.remove(accountRoleWrapper); // 2.级联删除角色和菜单的关联关系 LambdaQueryWrapper roleMenuWrapper = new SaasRoleMenu.Builder().createQueryWrapper().roleIdEq(deleteVO.getId()).builderQueryWrapper(); this.saasRoleMenuService.remove(roleMenuWrapper); } return result; } /** * 删除菜单,级联删除,角色、菜单、功能三者关联关系数据 * * @param menuId * @return */ @Transactional public boolean deleteSaasMenu(String menuId) { boolean result = this.saasMenuService.removeById(menuId); if (result) { // 1.删除此菜单下功能 LambdaQueryWrapper funWrapper = new SaasFunction.Builder().createQueryWrapper().menuIdEq(menuId).builderQueryWrapper(); this.saasFunctionService.remove(funWrapper); // 2.删除此功能对应的菜单、角色关系数据 LambdaQueryWrapper roleMenuWrapper = new SaasRoleMenu.Builder().createQueryWrapper().menuIdEq(menuId).builderQueryWrapper(); this.saasRoleMenuService.remove(roleMenuWrapper); } return result; } /** * 删除功能,级联删除菜单、功能关系 * @param menuId * * @param id * @return */ @Transactional public boolean deleteSaasFunction(String funId, String menuId) { LambdaQueryWrapper queryWrapper = new SaasFunction.Builder().createQueryWrapper().menuIdEq(menuId).idEq(funId).builderQueryWrapper(); boolean result = this.saasFunctionService.removeById(queryWrapper); if (result) { // 1.删除此功能对应的菜单、角色关系数据 LambdaQueryWrapper roleMenuWrapper = new SaasRoleMenu.Builder().createQueryWrapper().menuIdEq(menuId).functionIdEq(funId).builderQueryWrapper(); this.saasRoleMenuService.remove(roleMenuWrapper); } return result; } /** * 先删后新增,仅删除此账号角色下的所有菜单功能点的关联关系,并重新新增 * * @param batchVO * @return */ @Transactional public boolean resetSaasRoleMenu(String groupCode, String roleId, List auths) { LambdaQueryWrapper roleMenuWrapper = new SaasRoleMenu.Builder().createQueryWrapper().roleIdEq(roleId).builderQueryWrapper(); this.saasRoleMenuService.remove(roleMenuWrapper); // 1.批量新增 boolean result = this.saasRoleMenuService.batchCreateSaasRoleMenu(groupCode, roleId, auths); if (!result) { throw new BusinessException("先删后新增角色菜单功能权限失败"); } return result; } /** * 创建角色,同时添加菜单关联关系 * @param saasRole * @param auths * @return */ @Transactional public boolean createSaasRole(SaasRole saasRole, List auths) { // 1.创建角色信息 boolean result = this.saasRoleService.save(saasRole); if (result && CollectionUtil.isNotEmpty(auths)) { // 2.删除并创建角色信息 this.resetSaasRoleMenu(saasRole.getGroupCode(), saasRole.getId(), auths); } return result; } /** * 更新角色,同时更新菜单关联关系 * @param saasRole * @param auths * @return */ @Transactional public boolean updateSaasRole(SaasRole saasRole, List auths) { boolean result = this.saasRoleService.updateById(saasRole); if (result && CollectionUtil.isNotEmpty(auths)) { this.resetSaasRoleMenu(saasRole.getGroupCode(), saasRole.getId(), auths); } return result; } }