权限修改

This commit is contained in:
张松
2025-05-26 16:03:05 +08:00
parent 45a704d4f7
commit 4a9e2aa9a9

View File

@@ -129,77 +129,70 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> imp
}
public boolean addMenu(Long roleId, List<Long> menuIds, boolean isAdmin) {
List<CompletableFuture<Void>> tasks = new ArrayList<>();
List<MenuApiInfoItemDTO> apiPathList = Collections.synchronizedList(new ArrayList<>());
List<SysRolesMenus> rolesMenus = Collections.synchronizedList(new ArrayList<>());
List<MenuApiInfoItemDTO> apiPathList = new ArrayList<>();
List<SysRolesMenus> rolesMenus = new ArrayList<>();
// Step 1: 获取菜单列表并校验
List<? extends BaseMenu> menuList;
if (isAdmin) {
List<SysMenu> 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<CashMenu> 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<MenuApiInfoItemDTO> itemDTOS = JSONArray.parseArray(menu.getApiInfo()).toJavaList(MenuApiInfoItemDTO.class);
if (!itemDTOS.isEmpty()) {
apiPathList.addAll(itemDTOS);
}
}
// 解析 apiInfo
if (StrUtil.isNotBlank(menu.getApiInfo())) {
List<MenuApiInfoItemDTO> 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<SysMenu> matchedMenus = sysMenuService.list(wrapper);
List<CompletableFuture<Void>> 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) {