Jelajahi Sumber

菜单昵称逻辑调整

hanzhengyi 14 jam lalu
induk
melakukan
36d0c7774e

+ 78 - 64
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/service/impl/SysPlatformMenuServiceImpl.java

@@ -15,10 +15,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -205,70 +203,86 @@ public class SysPlatformMenuServiceImpl extends AbstractCrudService<SysPlatformM
     @Override
     @Transactional
     public void updateMenu(TenantMenuVo tenantMenuVo) {
-        List arr3 = new ArrayList();
-        Long[] arr = tenantMenuVo.getMenuIds();
-        //菜单ID
-        List<SysMenu> menuIdList = baseMapper.getMenuIdList(arr, "F");
-        Long arr1[] = new Long[menuIdList.size()];
-        for (int i = 0; i < menuIdList.size(); i++) {
-            arr1[i] = menuIdList.get(i).getParentId();
+        Long tenantId = tenantMenuVo.getTenantId();
+        Long[] selectedMenuIds = tenantMenuVo.getMenuIds();
+
+        // 基础参数校验,避免无效数据库操作
+        if (tenantId == null || selectedMenuIds == null || selectedMenuIds.length == 0) {
+            // 若需清空该租户全部菜单,可放开下方注释;默认无选中则不做变更
+            // sysTenantMenuService.remove(Wrappers.lambdaQuery(SysTenantMenu.class).eq(SysTenantMenu::getTenantId, tenantId));
+            return;
         }
-        //目录ID
-        if(arr1.length > 0){
-            List<SysMenu> menuIdListOne = baseMapper.getMenuIdList(arr1, "C");
-            Long arr2[] = new Long[menuIdListOne.size()];
-            for (int i = 0; i < menuIdListOne.size(); i++) {
-                arr2[i] = menuIdListOne.get(i).getParentId();
-            }
-            List<SysMenu> menuIdListTwo = baseMapper.getMenuIdList(arr, "C");
-            Long arr4[] = new Long[menuIdListTwo.size()];
-            for (int i = 0; i < menuIdListTwo.size(); i++) {
-                arr4[i] = menuIdListTwo.get(i).getParentId();
-            }
-            Long[] arr5 = arrayMerge(arr1, arr2);
-            Long[] arr6 = arrayMerge(arr5, arr4);
-            Long[] arr7 = arrayMerge(arr6, arr);
-            for (int i = 0; i < arr7.length; i++) {
-                if (!arr3.contains(arr7[i])) {
-                    arr3.add(arr7[i]);
-                }
-            }
-        }else{
-            List<SysMenu> menuIdListTwo = baseMapper.getMenuIdList(arr, "C");
-            Long arr4[] = new Long[menuIdListTwo.size()];
-            for (int i = 0; i < menuIdListTwo.size(); i++) {
-                arr4[i] = menuIdListTwo.get(i).getParentId();
-            }
-            Long[] arr7 = arrayMerge(arr4, arr);
-            for (int i = 0; i < arr7.length; i++) {
-                if (!arr3.contains(arr7[i])) {
-                    arr3.add(arr7[i]);
-                }
-            }
+
+        // ========== 1. 收集全量授权菜单ID(按钮+各级父目录,Set天然去重) ==========
+        Set<Long> allMenuIdSet = new HashSet<>();
+        // 加入原始选中的菜单ID
+        allMenuIdSet.addAll(Arrays.asList(selectedMenuIds));
+
+        // 查询选中ID中类型为F的菜单,提取一级父目录ID
+        List<SysMenu> fTypeMenuList = baseMapper.getMenuIdList(selectedMenuIds, "F");
+        List<Long> firstLevelParentIds = fTypeMenuList.stream()
+                .map(SysMenu::getParentId)
+                .collect(Collectors.toList());
+
+        // 存在一级父目录时,继续向上查询二级父目录
+        if (!firstLevelParentIds.isEmpty()) {
+            allMenuIdSet.addAll(firstLevelParentIds);
+            // 查询一级父目录(C类型)的上级ID
+            List<SysMenu> cTypeMenuList = baseMapper.getMenuIdList(firstLevelParentIds.toArray(new Long[0]), "C");
+            List<Long> secondLevelParentIds = cTypeMenuList.stream()
+                    .map(SysMenu::getParentId)
+                    .collect(Collectors.toList());
+            allMenuIdSet.addAll(secondLevelParentIds);
         }
 
-//        Long[] arr3 =ifRepeat2(arr6);
-//        Long arr3[] = new Long[arr1.length+arr2.length+arr.length];
-//        for (int i=0;i<arr1.length;i++){
-//            arr3[i] = arr1[i];
-//        }
-//        for (int i=0;i<arr2.length;i++){
-//            arr3[i+arr1.length] = arr2[i];
-//        }
-//        for (int i=0;i<arr.length;i++){
-//            arr3[i+arr1.length+arr2.length] = arr[i];
-//        }
+        // 查询选中ID中类型为C的菜单,提取其父目录ID并加入集合
+        List<SysMenu> selectedCTypeMenuList = baseMapper.getMenuIdList(selectedMenuIds, "C");
+        List<Long> selectedCParentIds = selectedCTypeMenuList.stream()
+                .map(SysMenu::getParentId)
+                .collect(Collectors.toList());
+        allMenuIdSet.addAll(selectedCParentIds);
 
-        // 删除租户菜单关联
-        baseMapper.deleteTenantMenuBy(tenantMenuVo.getTenantId());
-        //新增菜单
-        if (arr3.size() > 0) {
-            for (int i = 0; i < arr3.size(); i++) {
-                SysTenantMenu sysTenantMenu = new SysTenantMenu();
-                sysTenantMenu.setMenuId((Long) arr3.get(i));
-                sysTenantMenu.setTenantId(tenantMenuVo.getTenantId());
-                sysTenantMenuService.save(sysTenantMenu);
-            }
+        // ========== 2. 计算数据差异 ==========
+        // 查询当前租户已有的菜单关联
+        List<SysTenantMenu> existMenuList = sysTenantMenuService.list(
+                Wrappers.lambdaQuery(SysTenantMenu.class)
+                        .eq(SysTenantMenu::getTenantId, tenantId)
+                        .select(SysTenantMenu::getMenuId)
+        );
+        Set<Long> existMenuIds = existMenuList.stream()
+                .map(SysTenantMenu::getMenuId)
+                .collect(Collectors.toSet());
+
+        // 需删除:数据库已存在、新授权列表中没有的冗余关联
+        List<Long> deleteMenuIds = existMenuList.stream()
+                .map(SysTenantMenu::getMenuId)
+                .filter(menuId -> !allMenuIdSet.contains(menuId))
+                .collect(Collectors.toList());
+
+        // 需新增:新授权列表中有、数据库不存在的关联
+        List<SysTenantMenu> saveMenuList = allMenuIdSet.stream()
+                .filter(menuId -> !existMenuIds.contains(menuId))
+                .map(menuId -> {
+                    SysTenantMenu sysTenantMenu = new SysTenantMenu();
+                    sysTenantMenu.setTenantId(tenantId);
+                    sysTenantMenu.setMenuId(menuId);
+                    return sysTenantMenu;
+                })
+                .collect(Collectors.toList());
+
+        // ========== 3. 执行数据库操作 ==========
+        // 精准删除冗余关联
+        if (!deleteMenuIds.isEmpty()) {
+            sysTenantMenuService.remove(
+                    Wrappers.lambdaQuery(SysTenantMenu.class)
+                            .eq(SysTenantMenu::getTenantId, tenantId)
+                            .in(SysTenantMenu::getMenuId, deleteMenuIds)
+            );
+        }
+
+        // 批量新增缺失关联
+        if (!saveMenuList.isEmpty()) {
+            sysTenantMenuService.saveBatch(saveMenuList);
         }
     }