zzw
1 天以前 781b8b3e072a4e00b1cee56f961280c753cbb460
emergency-repair-server/src/main/java/com/emergencyrepair/system/service/SystemService.java
@@ -14,6 +14,7 @@
import com.emergencyrepair.business.mapper.TeamMajorMapper;
import com.emergencyrepair.common.BusinessException;
import com.emergencyrepair.system.entity.SysMenu;
import com.emergencyrepair.system.entity.SysErrorLog;
import com.emergencyrepair.system.entity.SysLoginLog;
import com.emergencyrepair.system.entity.SysOperationLog;
import com.emergencyrepair.system.entity.SysRole;
@@ -21,6 +22,7 @@
import com.emergencyrepair.system.entity.SysRoleUser;
import com.emergencyrepair.system.entity.SysTenantMenu;
import com.emergencyrepair.system.mapper.SysMenuMapper;
import com.emergencyrepair.system.mapper.SysErrorLogMapper;
import com.emergencyrepair.system.mapper.SysLoginLogMapper;
import com.emergencyrepair.system.mapper.SysOperationLogMapper;
import com.emergencyrepair.system.mapper.SysRoleMapper;
@@ -59,6 +61,7 @@
    private final SysRoleUserMapper roleUserMapper;
    private final SysLoginLogMapper loginLogMapper;
    private final SysOperationLogMapper operationLogMapper;
    private final SysErrorLogMapper errorLogMapper;
    private final ButtonPermissionCatalog buttonPermissionCatalog;
    private final JdbcTemplate jdbcTemplate;
    private final EngineeringMajorMapper engineeringMajorMapper;
@@ -105,11 +108,20 @@
            menu.setMenuType(menu.getId() == null ? 1 : menu.getMenuType());
        }
        boolean isRoot = menu.getPid() == null || menu.getPid() == 0L;
        if (menu.getMenuType() == 2) {
            if (isRoot) {
                throw new BusinessException("按钮类型只能建在菜单类型下级");
        if (menu.getId() != null && !isRoot && menu.getId().equals(menu.getPid())) {
            throw new BusinessException("上级菜单不能选择当前菜单");
        }
        SysMenu parent = null;
        if (!isRoot) {
            parent = menuMapper.selectById(menu.getPid());
            if (parent == null) {
                throw new BusinessException("上级菜单不存在");
            }
            SysMenu parent = menuMapper.selectById(menu.getPid());
            if (menu.getId() != null && isMenuDescendant(menu.getPid(), menu.getId())) {
                throw new BusinessException("上级菜单不能选择当前菜单的下级");
            }
        }
        if (menu.getMenuType() == 2) {
            if (parent == null || parent.getMenuType() == null || parent.getMenuType() != 1) {
                throw new BusinessException("按钮类型只能建在菜单类型下级");
            }
@@ -118,11 +130,8 @@
            }
            menu.setUrl(null);
            menu.setIcon(null);
        } else if (!isRoot) {
            SysMenu parent = menuMapper.selectById(menu.getPid());
            if (parent != null && parent.getMenuType() != null && parent.getMenuType() == 2) {
                throw new BusinessException("按钮类型下级不能再建菜单");
            }
        } else if (parent != null && parent.getMenuType() != null && parent.getMenuType() == 2) {
            throw new BusinessException("按钮类型下级不能再建菜单");
        }
        LocalDateTime now = LocalDateTime.now();
        menu.setUpdateTime(now);
@@ -139,6 +148,19 @@
            menuMapper.updateById(menu);
        }
        return menuMapper.selectById(menu.getId());
    }
    private boolean isMenuDescendant(Long candidateId, Long ancestorId) {
        Long cursor = candidateId;
        int guard = 0;
        while (cursor != null && cursor != 0L && guard++ < 64) {
            if (cursor.equals(ancestorId)) {
                return true;
            }
            SysMenu current = menuMapper.selectById(cursor);
            cursor = current == null ? null : current.getPid();
        }
        return false;
    }
    @Transactional
@@ -566,47 +588,32 @@
        baseEquipmentMapper.deleteById(id);
    }
    public List<Map<String, Object>> tenants() {
        return Collections.singletonList(row("id", 1L, "name", "临抢修租户"));
    }
    public Map<String, Object> menuPermissions(Long roleId, Long tenantId) {
        List<Long> roleMenuIds = roleMenuMapper.selectList(Wrappers.<SysRoleMenu>lambdaQuery()
                        .eq(roleId != null, SysRoleMenu::getRoleId, roleId))
                .stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        List<Long> tenantMenuIds = tenantMenuMapper.selectList(Wrappers.<SysTenantMenu>lambdaQuery()
                        .eq(tenantId != null, SysTenantMenu::getTenantId, tenantId))
                .stream().map(SysTenantMenu::getMenuId).collect(Collectors.toList());
        List<Long> effective = roleMenuIds.stream().filter(tenantMenuIds::contains).collect(Collectors.toList());
    public Map<String, Object> menuPermissions(Long roleId) {
        if (roleId == null) {
            throw new BusinessException("请选择角色");
        }
        List<String> roleMenuIds = roleMenuMapper.selectList(Wrappers.<SysRoleMenu>lambdaQuery()
                        .eq(SysRoleMenu::getRoleId, roleId))
                .stream().map(SysRoleMenu::getMenuId).map(String::valueOf).collect(Collectors.toList());
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("roleMenuIds", roleMenuIds);
        result.put("tenantMenuIds", tenantMenuIds);
        result.put("effectiveMenuIds", effective);
        result.put("effectiveMenuIds", roleMenuIds);
        return result;
    }
    @Transactional
    public void saveMenuPermissions(Long roleId, Long tenantId, List<Long> roleMenuIds, List<Long> tenantMenuIds) {
        if (roleId == null || tenantId == null) {
            throw new BusinessException("请选择角色和租户");
    public void saveMenuPermissions(Long roleId, List<Long> roleMenuIds) {
        if (roleId == null) {
            throw new BusinessException("请选择角色");
        }
        roleMenuMapper.delete(Wrappers.<SysRoleMenu>lambdaQuery().eq(SysRoleMenu::getRoleId, roleId));
        tenantMenuMapper.delete(Wrappers.<SysTenantMenu>lambdaQuery().eq(SysTenantMenu::getTenantId, tenantId));
        List<Long> effectiveRoleMenuIds = expandWithAncestors(roleMenuIds);
        List<Long> effectiveTenantMenuIds = expandWithAncestors(tenantMenuIds);
        for (Long menuId : effectiveRoleMenuIds) {
            SysRoleMenu relation = new SysRoleMenu();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            relation.setCreateTime(LocalDateTime.now());
            roleMenuMapper.insert(relation);
        }
        for (Long menuId : effectiveTenantMenuIds) {
            SysTenantMenu relation = new SysTenantMenu();
            relation.setTenantId(tenantId);
            relation.setMenuId(menuId);
            relation.setCreateTime(LocalDateTime.now());
            tenantMenuMapper.insert(relation);
        }
    }
@@ -888,18 +895,7 @@
        Set<Long> roleMenuIds = new LinkedHashSet<>(roleMenuMapper.selectList(Wrappers.<SysRoleMenu>lambdaQuery()
                        .in(SysRoleMenu::getRoleId, roleIds))
                .stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList()));
        Set<Long> tenantMenuIds = new LinkedHashSet<>(tenantMenuMapper.selectList(Wrappers.<SysTenantMenu>lambdaQuery()
                        .eq(SysTenantMenu::getTenantId, 1L))
                .stream().map(SysTenantMenu::getMenuId).collect(Collectors.toList()));
        if (tenantMenuIds.isEmpty()) {
            result.addAll(roleMenuIds);
        } else {
            for (Long menuId : roleMenuIds) {
                if (tenantMenuIds.contains(menuId)) {
                    result.add(menuId);
                }
            }
        }
        result.addAll(roleMenuIds);
        return result;
    }
@@ -913,9 +909,10 @@
            }
        }
        final Set<Long> allowedIds = allowed;
        Set<Long> visibleIds = new LinkedHashSet<>(expandWithAncestors(new ArrayList<>(allowed)));
        List<SysMenu> navigable = listMenus().stream()
                .filter(menu -> menu.getMenuType() == null || menu.getMenuType() != 2)
                .filter(menu -> allowedIds.contains(menu.getId()))
                .filter(menu -> visibleIds.contains(menu.getId()))
                .collect(Collectors.toList());
        return buildTree(navigable);
    }
@@ -1064,6 +1061,47 @@
        return operationLogMapper.selectPage(page, wrapper);
    }
    public Page<SysErrorLog> errorLogPage(int current, int size, String keyword, String module, String exceptionType, String startDate, String endDate) {
        Page<SysErrorLog> page = new Page<>(current <= 0 ? 1 : current, size <= 0 ? 20 : size);
        LambdaQueryWrapper<SysErrorLog> wrapper = Wrappers.lambdaQuery();
        wrapper.select(SysErrorLog::getId, SysErrorLog::getUserId, SysErrorLog::getUserName,
                SysErrorLog::getNickName, SysErrorLog::getModule, SysErrorLog::getRequestMethod,
                SysErrorLog::getRequestUri, SysErrorLog::getExceptionType, SysErrorLog::getMessage,
                SysErrorLog::getErrorTime);
        if (StringUtils.hasText(keyword)) {
            String value = keyword.trim();
            wrapper.and(item -> item.like(SysErrorLog::getUserName, value)
                    .or().like(SysErrorLog::getNickName, value)
                    .or().like(SysErrorLog::getRequestUri, value)
                    .or().like(SysErrorLog::getExceptionType, value)
                    .or().like(SysErrorLog::getMessage, value));
        }
        if (StringUtils.hasText(module)) {
            wrapper.eq(SysErrorLog::getModule, module.trim());
        }
        if (StringUtils.hasText(exceptionType)) {
            wrapper.like(SysErrorLog::getExceptionType, exceptionType.trim());
        }
        LocalDateTime start = parseStart(startDate);
        LocalDateTime end = parseEnd(endDate);
        if (start != null) {
            wrapper.ge(SysErrorLog::getErrorTime, start);
        }
        if (end != null) {
            wrapper.le(SysErrorLog::getErrorTime, end);
        }
        wrapper.orderByDesc(SysErrorLog::getErrorTime);
        return errorLogMapper.selectPage(page, wrapper);
    }
    public SysErrorLog errorLog(Long id) {
        SysErrorLog log = errorLogMapper.selectById(id);
        if (log == null) {
            throw new BusinessException("错误日志不存在");
        }
        return log;
    }
    public Long resolveUserId(String username) {
        if (!StringUtils.hasText(username)) {
            return null;