Sfoglia il codice sorgente

保存roleMenu问题

shiqian 4 anni fa
parent
commit
d52a0e7178

+ 6 - 0
boman-common/boman-common-core/src/main/java/com/boman/common/core/utils/collection/CollectionUtils.java

@@ -4,6 +4,7 @@ import com.boman.common.core.utils.obj.ObjectUtils;
 
 import java.util.Collection;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * @author shiqian
@@ -28,4 +29,9 @@ public class CollectionUtils {
 
         return buffer.toString();
     }
+
+    public static <T> List<T> distinct(List<T> one, List<T> another) {
+        one.addAll(another);
+        return one.stream().distinct().collect(Collectors.toList());
+    }
 }

+ 16 - 0
boman-common/boman-common-core/src/main/java/com/boman/common/core/utils/obj/ObjectUtils.java

@@ -262,6 +262,22 @@ public class ObjectUtils {
                 .stream().map(function).distinct().collect(Collectors.toList());
     }
 
+    /**
+     * 功能描述: 根据规则获取
+     *
+     * @param input    原数据
+     * @param function FunctionalInterface
+     * @return java.util.List<T>
+     */
+    public static <T, R> List<R> mapFilter(List<T> input, Function<T, R> function, Predicate<T> predicate) {
+        return requireNonNull(input, "list is null")
+                .stream()
+                .filter(predicate)
+                .map(function)
+                .distinct()
+                .collect(Collectors.toList());
+    }
+
 
     /**
      * 功能描述: 根据规则获取单个

+ 2 - 1
boman-modules/boman-system/src/main/java/com/boman/system/mapper/SysRoleMenuMapper.java

@@ -32,9 +32,10 @@ public interface SysRoleMenuMapper
      * 功能描述: 通过角色ID批量删除角色和菜单关联
      *
      * @param roleIdList roleIdList
+     * @param menuIdList menuIdList
      * @return int
      */
-    public int deleteByRoleIdList(@Param("roleIdList") List<Long> roleIdList);
+    public int deleteByRoleIdList(@Param("roleIdList") List<Long> roleIdList, @Param("menuIdList") List<Long> menuIdList);
 
     /**
      * 批量删除角色菜单关联信息

+ 2 - 1
boman-modules/boman-system/src/main/java/com/boman/system/service/impl/ISysRoleMenuService.java

@@ -24,9 +24,10 @@ public interface ISysRoleMenuService {
      * 功能描述: 根据roleIdList删除
      *
      * @param roleIdList roleIdList
+     * @param menuIdList menuIdList
      * @return int
      */
-    int deleteByRoleIdList(List<Long> roleIdList);
+    int deleteByRoleIdList(List<Long> roleIdList, List<Long> menuIdList);
 
     /**
      * 功能描述: 批量保存roleMenuList

+ 6 - 1
boman-modules/boman-system/src/main/java/com/boman/system/service/impl/SysMenuServiceImpl.java

@@ -666,7 +666,12 @@ public class SysMenuServiceImpl implements ISysMenuService {
         List<String> btnList = Lists.newArrayList();
         for (SysMenu childMenu : childList) {
             if (roleMenuIdList.contains(childMenu.getId())) {
-                String btn = RoleMenuDto.getBtnFromPerms(childMenu.getPerms());
+                String perms = childMenu.getPerms();
+                if (isEmpty(perms)) {
+                    throw new IllegalArgumentException("[" + childMenu.getMenuName() + "]菜单未配置权限标识");
+                }
+
+                String btn = RoleMenuDto.getBtnFromPerms(perms);
                 btnList.add(btn);
             }
         }

+ 12 - 6
boman-modules/boman-system/src/main/java/com/boman/system/service/impl/SysRoleMenuServiceImpl.java

@@ -1,5 +1,6 @@
 package com.boman.system.service.impl;
 
+import com.boman.common.core.utils.collection.CollectionUtils;
 import com.boman.domain.constant.TableNameConst;
 import com.boman.domain.dto.RoleMenuDto;
 import com.boman.system.api.domain.SysMenu;
@@ -66,14 +67,18 @@ public class SysRoleMenuServiceImpl implements ISysRoleMenuService{
                 return 0;
             }
 
+            // 父类下所有的子类
+            List<SysMenu> originChildList = Lists.newArrayList(childList);
             // 把parentList放进去
             childList.addAll(parentList);
             // 根据子菜单和前台传过来的head来判断包含AMD...中的哪些
             childList = buildBtnByHead(head, childList);
-            List<SysRoleMenu> roleMenuList = buildRoleMenu(roleId, map(childList, SysMenu::getId));
+            List<Long> childIdList = map(childList, SysMenu::getId);
+            List<SysRoleMenu> roleMenuList = buildRoleMenu(roleId, childIdList);
+            List<Long> distinct = CollectionUtils.distinct(childIdList, map(originChildList, SysMenu::getId));
             try {
                 // 先删除,再添加,防止联合索引报错
-                deleteByRoleIdList(Collections.singletonList(roleId));
+                deleteByRoleIdList(Collections.singletonList(roleId), distinct);
                 effective += batchRoleMenu(roleMenuList);
             } catch (Exception e) {
                 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
@@ -101,7 +106,7 @@ public class SysRoleMenuServiceImpl implements ISysRoleMenuService{
 
             String perms = menu.getPerms();
             if (isEmpty(perms)) {
-                continue;
+                throw new IllegalArgumentException(menu.getMenuName() + " 未配置权限标识, 请配置以后再次保存");
             }
 
             if (head.contains(RoleMenuDto.getBtnFromPerms(menu.getPerms()))) {
@@ -116,14 +121,15 @@ public class SysRoleMenuServiceImpl implements ISysRoleMenuService{
      * 功能描述: 根据roleIdList删除
      *
      * @param roleIdList roleIdList
+     * @param menuIdList menuIdList
      * @return int
      */
     @Override
-    public int deleteByRoleIdList(List<Long> roleIdList) {
-        if (isEmpty(roleIdList)) {
+    public int deleteByRoleIdList(List<Long> roleIdList, List<Long> menuIdList) {
+        if (isEmpty(roleIdList) ||isEmpty(menuIdList) ) {
             return 0;
         }
-        return mapper.deleteByRoleIdList(roleIdList);
+        return mapper.deleteByRoleIdList(roleIdList, menuIdList);
     }
 
     /**

+ 5 - 0
boman-modules/boman-system/src/main/resources/mapper/system/SysRoleMenuMapper.xml

@@ -42,6 +42,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 		<foreach collection="roleIdList" item="roleId" open="(" separator="," close=")">
 			#{roleId}
 		</foreach>
+		and menu_id in
+		<foreach collection="menuIdList" item="menuId" open="(" separator="," close=")">
+			#{menuId}
+		</foreach>
+
 	</delete>
 	
 	<delete id="deleteRoleMenu" parameterType="Long">