| | |
| | | package com.iailab.module.system.service.app; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.iailab.framework.common.pojo.PageResult; |
| | |
| | | import com.iailab.framework.mybatis.core.query.LambdaQueryWrapperX; |
| | | import com.iailab.framework.security.core.util.SecurityFrameworkUtils; |
| | | import com.iailab.framework.tenant.core.aop.TenantIgnore; |
| | | import com.iailab.module.system.api.app.dto.AppMenuRespDTO; |
| | | import com.iailab.module.system.controller.admin.app.vo.AppPageReqVO; |
| | | import com.iailab.module.system.controller.admin.app.vo.AppSaveReqVO; |
| | | import com.iailab.module.system.controller.admin.auth.vo.AuthPermissionInfoRespVO; |
| | | import com.iailab.module.system.convert.auth.AuthConvert; |
| | | import com.iailab.module.system.dal.dataobject.app.AppDO; |
| | | import com.iailab.module.system.dal.dataobject.permission.MenuDO; |
| | | import com.iailab.module.system.dal.dataobject.permission.RoleDO; |
| | |
| | | import com.iailab.module.system.dal.mysql.tenant.TenantMapper; |
| | | import com.iailab.module.system.dal.mysql.tenant.TenantPackageMapper; |
| | | import com.iailab.module.system.enums.permission.MenuTypeEnum; |
| | | import com.iailab.module.system.service.permission.MenuService; |
| | | import com.iailab.module.system.service.permission.PermissionService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.ObjectUtils; |
| | |
| | | import java.util.*; |
| | | |
| | | import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static com.iailab.framework.common.util.collection.CollectionUtils.convertSet; |
| | | import static com.iailab.framework.tenant.core.context.TenantContextHolder.getTenantId; |
| | | import static com.iailab.module.system.enums.ErrorCodeConstants.MENU_EXISTS_CHILDREN; |
| | | import static com.iailab.module.system.enums.ErrorCodeConstants.MENU_NOT_EXISTS; |
| | |
| | | |
| | | @Resource |
| | | private TenantMapper tenantMapper; |
| | | |
| | | @Resource |
| | | private MenuService menuService; |
| | | |
| | | |
| | | @Override |
| | |
| | | //查询系统应用菜单 |
| | | // List<MenuDO> menuDOS = menuMapper.selectList(menuDOLambdaQueryWrapperX); |
| | | // List<AppDO> systemApps = convertMenuToApp(menuDOS); |
| | | //创建一个系统管理应用菜单 |
| | | AppDO aDo = new AppDO(); |
| | | aDo.setAppType(1); |
| | | aDo.setAppName("系统管理"); |
| | | aDo.setOrderNum(0); |
| | | List<AppDO> systemApps = new ArrayList<>(); |
| | | systemApps.add(aDo); |
| | | List<AppDO> appDOS = appMapper.selectList(); |
| | | //暂时先遍历处理应用菜单和应用类型 |
| | | appDOS.stream().forEach(appDO -> { |
| | |
| | | } |
| | | } |
| | | |
| | | // @Override |
| | | // public List<MenuRespDTO> getAppMenu(Long id) { |
| | | // |
| | | // List<MenuDO> children = new LinkedList<>(); |
| | | // // 遍历每一层 |
| | | // Collection<Long> parentIds = Collections.singleton(id); |
| | | // for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下,存在死循环 |
| | | // // 查询当前层,所有的子应用菜单 |
| | | // List<MenuDO> menus = menuMapper.selectListByParentId(parentIds); |
| | | // // 1. 如果没有子菜单,则结束遍历 |
| | | // if (CollUtil.isEmpty(menus)) { |
| | | // break; |
| | | // } |
| | | // // 2. 如果有子应用菜单,继续遍历 |
| | | // children.addAll(menus); |
| | | // parentIds = convertSet(menus, MenuDO::getId); |
| | | // } |
| | | // children = menuService.filterDisableMenus(children); |
| | | // return AuthConvert.INSTANCE.buildMenuTree(id, children); |
| | | // |
| | | // } |
| | | @Override |
| | | public List<AuthPermissionInfoRespVO.MenuVO> getAppMenu(Long id) { |
| | | MenuDO menuDO = menuMapper.selectById(id); |
| | | List<MenuDO> children = new LinkedList<>(); |
| | | // 遍历每一层 |
| | | Collection<Long> parentIds = Collections.singleton(id); |
| | | for (int i = 0; i < Short.MAX_VALUE; i++) { // 使用 Short.MAX_VALUE 避免 bug 场景下,存在死循环 |
| | | // 查询当前层,所有的子应用菜单 |
| | | List<MenuDO> menus = menuMapper.selectListByParentId(parentIds); |
| | | // 1. 如果没有子菜单,则结束遍历 |
| | | if (CollUtil.isEmpty(menus)) { |
| | | break; |
| | | } |
| | | // 2. 如果有子应用菜单,继续遍历 |
| | | children.addAll(menus); |
| | | parentIds = convertSet(menus, MenuDO::getId); |
| | | } |
| | | children = menuService.filterDisableMenus(children); |
| | | return AuthConvert.INSTANCE.buildMenuTree(children, id, menuDO.getPath()); |
| | | |
| | | } |
| | | |
| | | private void dealAppMenu(Integer type, AppDO app){ |
| | | String loginUserNickname = SecurityFrameworkUtils.getLoginUserNickname(); |
| | |
| | | if(type == 1){ |
| | | menuDO.setCreator(loginUserNickname); |
| | | menuDO.setCreateTime(app.getCreateTime()); |
| | | menuDO.setIcon("fa-solid:border-none"); //默认icon |
| | | menuMapper.insert(menuDO); |
| | | //内置租户角色分配菜单 |
| | | assignRoleMenu(menuDO.getId(), app.getTenantId()); |
| | | // //内置租户角色分配菜单 |
| | | // assignRoleMenu(menuDO.getId(), app.getTenantId()); |
| | | } else if(type == 2){ |
| | | LambdaUpdateWrapper<MenuDO> updateWrapper = new LambdaUpdateWrapper<>(); |
| | | updateWrapper.eq(MenuDO::getAppId, app.getId()); |
| | |
| | | menuDO.setCreator(loginUserNickname); |
| | | menuDO.setCreateTime(app.getCreateTime()); |
| | | menuMapper.insert(menuDO); |
| | | //内置租户角色分配菜单 |
| | | assignRoleMenu(menuDO.getId(), app.getTenantId()); |
| | | // //内置租户角色分配菜单 |
| | | // assignRoleMenu(menuDO.getId(), app.getTenantId()); |
| | | } |
| | | } else if(type == 3){ |
| | | //删除租户、角色权限 |
| | |
| | | LambdaQueryWrapperX<MenuDO> menuWrapper = new LambdaQueryWrapperX<>(); |
| | | menuWrapper.eq(MenuDO::getAppId, app.getId()); |
| | | menuWrapper.eq(MenuDO::getType, MenuTypeEnum.DIR.getType()); |
| | | menuWrapper.eq(MenuDO::getParentId, 0); |
| | | MenuDO menu = menuMapper.selectOne(menuWrapper); |
| | | TenantDO tenantDO = tenantMapper.selectById(app.getTenantId()); |
| | | TenantPackageDO tenantPackageDO = tenantPackageMapper.selectById(tenantDO.getPackageId()); |
| | | Set<Long> menuIds = tenantPackageDO.getMenuIds(); |
| | | menuIds.remove(menu.getId()); |
| | | // 校验是否还有子菜单 |
| | | if (menuMapper.selectCountByParentId(menu.getId()) > 0) { |
| | | throw exception(MENU_EXISTS_CHILDREN); |
| | | if(ObjectUtils.isNotEmpty(menu) && ObjectUtils.isNotEmpty(tenantDO)) { |
| | | TenantPackageDO tenantPackageDO = tenantPackageMapper.selectById(tenantDO.getPackageId()); |
| | | Set<Long> menuIds = tenantPackageDO.getMenuIds(); |
| | | menuIds.remove(menu.getId()); |
| | | // 校验是否还有子菜单 |
| | | if (menuMapper.selectCountByParentId(menu.getId()) > 0) { |
| | | throw exception(MENU_EXISTS_CHILDREN); |
| | | } |
| | | // 校验删除的菜单是否存在 |
| | | if (menuMapper.selectById(menu.getId()) == null) { |
| | | throw exception(MENU_NOT_EXISTS); |
| | | } |
| | | // 标记删除 |
| | | menuMapper.deleteById(menu.getId()); |
| | | // 删除授予给角色的权限 |
| | | permissionService.processMenuDeleted(menu.getId()); |
| | | //删除菜单 |
| | | menuMapper.delete(menuWrapper); |
| | | } |
| | | // 校验删除的菜单是否存在 |
| | | if (menuMapper.selectById(menu.getId()) == null) { |
| | | throw exception(MENU_NOT_EXISTS); |
| | | } |
| | | // 标记删除 |
| | | menuMapper.deleteById(menu.getId()); |
| | | // 删除授予给角色的权限 |
| | | permissionService.processMenuDeleted(menu.getId()); |
| | | //删除菜单 |
| | | menuMapper.delete(menuWrapper); |
| | | } |
| | | } |
| | | |
| | |
| | | return appDOS; |
| | | } |
| | | |
| | | public List<AppDO> selectBatchIds(List<Long> ids) { |
| | | List<AppDO> appDOS = appMapper.selectBatchIds(ids); |
| | | return appDOS; |
| | | } |
| | | |
| | | } |