From bb55f4a88aed5f287d5270fe472a5c0bf870f876 Mon Sep 17 00:00:00 2001 From: xiayang Date: Tue, 26 Nov 2024 16:32:53 +0800 Subject: [PATCH] [Improve] Optimize the logic for constructing role menus. (#3952) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 夏洋 --- .../dinky/service/impl/UserServiceImpl.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/dinky-admin/src/main/java/org/dinky/service/impl/UserServiceImpl.java b/dinky-admin/src/main/java/org/dinky/service/impl/UserServiceImpl.java index e280b9f510..e6dc26842f 100644 --- a/dinky-admin/src/main/java/org/dinky/service/impl/UserServiceImpl.java +++ b/dinky-admin/src/main/java/org/dinky/service/impl/UserServiceImpl.java @@ -65,6 +65,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; @@ -73,7 +74,6 @@ import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.RandomUtil; -import cn.hutool.core.util.StrUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -558,12 +558,17 @@ public UserDTO buildUserInfo(Integer userId) { // query role menu List roleMenus = roleMenuService.list(new LambdaQueryWrapper().eq(RoleMenu::getRoleId, role.getId())); - roleMenus.forEach(roleMenu -> { - Menu menu = menuService.getById(roleMenu.getMenuId()); - if (Asserts.isNotNull(menu) && !StrUtil.equals("M", menu.getType())) { - menuList.add(menu); - } - }); + List collect = + roleMenus.stream().map(RoleMenu::getMenuId).collect(Collectors.toList()); + if (CollectionUtils.isEmpty(collect)) { + return; + } + List list = menuService.list(new LambdaQueryWrapper() + .in( + Menu::getId, + roleMenus.stream().map(RoleMenu::getMenuId).collect(Collectors.toList())) + .ne(Menu::getType, "M")); + menuList.addAll(list); } });