forked from zly666/zhr-2108-server
系统菜单优化
parent
1939a8e120
commit
b888607768
|
@ -20,6 +20,8 @@ public interface SysMenuMapper extends BaseMapper<SysMenu> {
|
|||
* @return 菜单列表
|
||||
*/
|
||||
public List<SysMenu> selectMenuList (SysMenu menu);
|
||||
public List<SysMenu> findMenusWithParents(@Param("longs") List<Long> longs);
|
||||
List<SysMenu> findMenusChParents(@Param("longs") List<Long> longs);
|
||||
|
||||
/**
|
||||
* 根据用户所有权限
|
||||
|
@ -135,4 +137,6 @@ public interface SysMenuMapper extends BaseMapper<SysMenu> {
|
|||
* @return 结果
|
||||
*/
|
||||
public SysMenu checkMenuNameUnique (@Param("menuName") String menuName, @Param("parentId") Long parentId);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
|||
import com.muyu.system.domain.SysMenu;
|
||||
import com.muyu.system.domain.vo.RouterVo;
|
||||
import com.muyu.system.domain.vo.TreeSelect;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
@ -158,4 +159,5 @@ public interface SysMenuService extends IService<SysMenu> {
|
|||
* @return 结果
|
||||
*/
|
||||
public boolean checkMenuNameUnique (SysMenu menu);
|
||||
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import com.muyu.system.mapper.SysMenuMapper;
|
|||
import com.muyu.system.mapper.SysRoleMapper;
|
||||
import com.muyu.system.mapper.SysRoleMenuMapper;
|
||||
import com.muyu.system.service.SysMenuService;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
@ -58,19 +59,81 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||
*
|
||||
* @return 菜单列表
|
||||
*/
|
||||
|
||||
@Override
|
||||
public List<SysMenu> selectMenuList (SysMenu menu, Long userId) {
|
||||
List<SysMenu> menuList = null;
|
||||
// 管理员显示所有菜单信息
|
||||
// 判断是否为管理员,管理员显示所有菜单
|
||||
if (SysUser.isAdmin(userId)) {
|
||||
// 查询所有菜单
|
||||
menuList = menuMapper.selectMenuList(menu);
|
||||
// 添加具有父菜单的菜单项
|
||||
// menuList.addAll(findMenusWithParents(menuList));
|
||||
List<SysMenu> menusWithParents = findMenusWithParents(menuList);
|
||||
System.out.println("=------------------------->"+menusWithParents);
|
||||
if (menusWithParents!=null && menusWithParents.size()!=0){
|
||||
menuList.addAll(menusWithParents);
|
||||
}
|
||||
// 添加具有祖父级菜单的菜单项
|
||||
List<SysMenu> menusChParents = findMenusChParents(menuMapper.selectMenuList(menu));
|
||||
if (menusChParents!=null && menusChParents.size()!=0){
|
||||
menuList.addAll(menusChParents);
|
||||
}
|
||||
} else {
|
||||
// 非管理员,根据用户ID查询有权限的菜单
|
||||
menu.getParams().put("userId", userId);
|
||||
menuList = menuMapper.selectMenuListByUserId(menu);
|
||||
}
|
||||
// 去重,确保菜单列表中没有重复的菜单项
|
||||
menuList = menuList.stream().distinct().toList();
|
||||
return menuList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询具有父菜单的菜单列表
|
||||
* @param menuList 原始菜单列表
|
||||
* @return 返回具有父菜单的菜单列表
|
||||
*/
|
||||
public List<SysMenu> findMenusWithParents( List<SysMenu> menuList) {
|
||||
// 获取所有非根菜单的父菜单ID
|
||||
List<Long> longs = menuList.stream()
|
||||
.map(menu -> menu.getParentId())
|
||||
.filter(parentId -> parentId != 0)
|
||||
.distinct().toList();
|
||||
if (longs.size()==0){
|
||||
return null;
|
||||
}
|
||||
// 查询这些父菜单及其子菜单
|
||||
List<SysMenu> menus = menuMapper.findMenusWithParents(longs);
|
||||
List<SysMenu> menusWithParents = findMenusWithParents(menus);
|
||||
if (menusWithParents!=null){
|
||||
menus.addAll(menusWithParents);
|
||||
|
||||
}
|
||||
return menus;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询具有祖父级菜单的菜单列表
|
||||
* @param menuList 原始菜单列表
|
||||
* @return 返回具有祖父级菜单的菜单列表
|
||||
*/
|
||||
public List<SysMenu> findMenusChParents(List<SysMenu> menuList){
|
||||
// 获取所有菜单的父菜单ID
|
||||
List<Long> longs = menuList.stream().map(menu -> menu.getParentId()).toList();
|
||||
List<SysMenu> sysMenus = menuMapper.findMenusChParents(longs);
|
||||
if (sysMenus!=null && sysMenus.size()!=0){
|
||||
// 查询这些祖父级菜单的父菜单及其子菜单
|
||||
List<SysMenu> menus = findMenusWithParents(sysMenus);
|
||||
if (menus!=null){
|
||||
sysMenus.addAll(menus);
|
||||
}
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
return sysMenus;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID查询权限
|
||||
*
|
||||
|
@ -321,6 +384,8 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||
return UserConstants.UNIQUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取路由名称
|
||||
*
|
||||
|
|
|
@ -185,6 +185,31 @@
|
|||
<include refid="selectMenuVo"/>
|
||||
where menu_name=#{menuName} and parent_id = #{parentId} limit 1
|
||||
</select>
|
||||
<select id="findMenusWithParents" resultType="com.muyu.system.domain.SysMenu">
|
||||
<include refid="selectMenuVo"/>
|
||||
<where>
|
||||
menu_id in (
|
||||
<foreach collection="longs" separator="," item="id">
|
||||
#{id}
|
||||
</foreach>
|
||||
)
|
||||
</where>
|
||||
order by parent_id, order_num
|
||||
|
||||
</select>
|
||||
<select id="findMenusChParents" resultType="com.muyu.system.domain.SysMenu">
|
||||
<include refid="selectMenuVo"/>
|
||||
<where>
|
||||
menu_id in (
|
||||
<foreach collection="longs" separator="," item="id">
|
||||
#{id}
|
||||
</foreach>
|
||||
)
|
||||
</where>
|
||||
order by parent_id, order_num
|
||||
|
||||
</select>
|
||||
|
||||
|
||||
<update id="updateMenu" parameterType="com.muyu.system.domain.SysMenu">
|
||||
update sys_menu
|
||||
|
|
Loading…
Reference in New Issue