diff --git a/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/SysRoleServiceImpl.java b/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/SysRoleServiceImpl.java index f84d86ebe..55ace19a7 100644 --- a/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/SysRoleServiceImpl.java +++ b/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/SysRoleServiceImpl.java @@ -129,77 +129,70 @@ public class SysRoleServiceImpl extends ServiceImpl imp } public boolean addMenu(Long roleId, List menuIds, boolean isAdmin) { - List> tasks = new ArrayList<>(); - List apiPathList = Collections.synchronizedList(new ArrayList<>()); - List rolesMenus = Collections.synchronizedList(new ArrayList<>()); + List apiPathList = new ArrayList<>(); + List rolesMenus = new ArrayList<>(); // Step 1: 获取菜单列表并校验 List menuList; if (isAdmin) { List sysMenuList = sysMenuService.queryChain().in(SysMenu::getMenuId, menuIds).list(); - if (sysMenuList.size() != menuIds.size()) throw new ApiNotPrintException("菜单id包含错误id"); + if (sysMenuList.size() != menuIds.size()) { + throw new ApiNotPrintException("菜单id包含错误id"); + } menuList = sysMenuList; } else { List cashMenuList = cashMenuService.queryChain().in(CashMenu::getMenuId, menuIds).list(); - if (cashMenuList.size() != menuIds.size()) throw new ApiNotPrintException("菜单id包含错误id"); + if (cashMenuList.size() != menuIds.size()) { + throw new ApiNotPrintException("菜单id包含错误id"); + } menuList = cashMenuList; } int type = isAdmin ? 0 : 1; - // Step 2: 并发处理 apiInfo 解析和绑定判断 + // Step 2: 解析 apiInfo 并判断菜单是否已绑定 for (BaseMenu menu : menuList) { - tasks.add(CompletableFuture.runAsync(() -> { - // 解析 apiInfo - if (StrUtil.isNotBlank(menu.getApiInfo())) { - List itemDTOS = JSONArray.parseArray(menu.getApiInfo()).toJavaList(MenuApiInfoItemDTO.class); - if (!itemDTOS.isEmpty()) { - apiPathList.addAll(itemDTOS); - } - } + // 解析 apiInfo + if (StrUtil.isNotBlank(menu.getApiInfo())) { + List itemDTOS = JSONArray.parseArray(menu.getApiInfo()) + .toJavaList(MenuApiInfoItemDTO.class); + apiPathList.addAll(itemDTOS); + } - // 检查是否已绑定 - long count = sysRolesMenusService.count(new QueryWrapper() - .eq("menu_id", menu.getMenuId()) - .eq("role_id", roleId) - .eq("type", type)); - if (count == 0) { - rolesMenus.add(new SysRolesMenus(menu.getMenuId(), roleId, type)); - } - })); + // 判断是否已绑定 + long count = sysRolesMenusService.count(new QueryWrapper() + .eq("menu_id", menu.getMenuId()) + .eq("role_id", roleId) + .eq("type", type)); + if (count == 0) { + rolesMenus.add(new SysRolesMenus(menu.getMenuId(), roleId, type)); + } } - // 等待上述任务完成 - CompletableFuture.allOf(tasks.toArray(new CompletableFuture[0])).join(); - - // Step 3: 并发处理 apiPath 匹配菜单 + // Step 3: 匹配接口路径补充菜单(如 API 匹配到其他菜单) if (!apiPathList.isEmpty()) { String likeSql = buildLikeSql(apiPathList, "url"); QueryWrapper wrapper = new QueryWrapper(); wrapper.where(likeSql); List matchedMenus = sysMenuService.list(wrapper); - List> matchTasks = new ArrayList<>(); - for (SysMenu matched : matchedMenus) { - matchTasks.add(CompletableFuture.runAsync(() -> { - long count = sysRolesMenusService.count(new QueryWrapper() - .eq("menu_id", matched.getMenuId()) - .eq("role_id", roleId) - .eq("type", type)); - if (count == 0) { - rolesMenus.add(new SysRolesMenus(matched.getMenuId(), roleId, type)); - } - })); + long count = sysRolesMenusService.count(new QueryWrapper() + .eq("menu_id", matched.getMenuId()) + .eq("role_id", roleId) + .eq("type", type)); + if (count == 0) { + rolesMenus.add(new SysRolesMenus(matched.getMenuId(), roleId, type)); + } } - - CompletableFuture.allOf(matchTasks.toArray(new CompletableFuture[0])).join(); } + // Step 4: 批量保存(如有数据) return rolesMenus.isEmpty() || sysRolesMenusService.saveBatch(rolesMenus); } + @Override @Transactional(rollbackFor = Exception.class) public Boolean editPermission(long userId, RolePermissionDTO rolePermissionDTO) {