package com.tourguide.modules.system.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.tourguide.common.exception.BusinessException; import com.tourguide.modules.system.entity.SystemMenu; import com.tourguide.modules.system.entity.SystemRole; import com.tourguide.modules.system.entity.SystemRoleMenu; import com.tourguide.modules.system.mapper.SystemMenuMapper; import com.tourguide.modules.system.mapper.SystemRoleMapper; import com.tourguide.modules.system.mapper.SystemRoleMenuMapper; import org.springframework.stereotype.Service; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; /** 统一从角色菜单关系中加载后台权限,供登录响应和接口鉴权复用。 */ @Service public class AdminPermissionService { private static final String SUPER_ADMIN_ROLE = "ADMIN"; private final SystemRoleMapper roleMapper; private final SystemMenuMapper menuMapper; private final SystemRoleMenuMapper roleMenuMapper; public AdminPermissionService(SystemRoleMapper roleMapper, SystemMenuMapper menuMapper, SystemRoleMenuMapper roleMenuMapper) { this.roleMapper = roleMapper; this.menuMapper = menuMapper; this.roleMenuMapper = roleMenuMapper; } public List getMenuCodes(String roleCode) { SystemRole role = roleMapper.selectOne(new LambdaQueryWrapper() .eq(SystemRole::getRoleCode, roleCode) .eq(SystemRole::getStatus, 1) .last("LIMIT 1")); if (role == null) { throw new BusinessException(401, "后台角色不存在或已停用"); } if (SUPER_ADMIN_ROLE.equals(roleCode)) { return toMenuCodes(menuMapper.selectList(new LambdaQueryWrapper() .eq(SystemMenu::getStatus, 1) .orderByAsc(SystemMenu::getSort, SystemMenu::getId))); } List menuIds = roleMenuMapper.selectList(new LambdaQueryWrapper() .eq(SystemRoleMenu::getRoleId, role.getId())) .stream() .map(SystemRoleMenu::getMenuId) .collect(Collectors.toList()); if (menuIds.isEmpty()) { return Collections.emptyList(); } return toMenuCodes(menuMapper.selectList(new LambdaQueryWrapper() .in(SystemMenu::getId, menuIds) .eq(SystemMenu::getStatus, 1) .orderByAsc(SystemMenu::getSort, SystemMenu::getId))); } private List toMenuCodes(List menus) { return menus.stream().map(SystemMenu::getMenuCode).collect(Collectors.toList()); } }