fix: 删除部门附加删除子部门
This commit is contained in:
@@ -9,6 +9,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.system.common.constant.SystemConstants;
|
||||
import com.youlai.system.common.enums.MenuTypeEnum;
|
||||
import com.youlai.system.common.enums.StatusEnum;
|
||||
import com.youlai.system.pojo.entity.SysDept;
|
||||
import com.youlai.system.pojo.form.MenuForm;
|
||||
import com.youlai.system.pojo.vo.Option;
|
||||
import com.youlai.system.converter.MenuConverter;
|
||||
import com.youlai.system.mapper.SysMenuMapper;
|
||||
@@ -16,18 +18,15 @@ import com.youlai.system.pojo.entity.SysMenu;
|
||||
import com.youlai.system.pojo.bo.RouteBO;
|
||||
import com.youlai.system.pojo.query.MenuQuery;
|
||||
import com.youlai.system.pojo.vo.MenuVO;
|
||||
import com.youlai.system.pojo.vo.ResourceVO;
|
||||
import com.youlai.system.pojo.vo.RouteVO;
|
||||
import com.youlai.system.service.SysMenuService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
/**
|
||||
* 菜单业务实现类
|
||||
*
|
||||
@@ -40,7 +39,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
private final MenuConverter menuConverter;
|
||||
|
||||
/**
|
||||
* 菜单表格树形列表
|
||||
* 菜单列表
|
||||
*/
|
||||
@Override
|
||||
public List<MenuVO> listMenus(MenuQuery queryParams) {
|
||||
@@ -49,11 +48,13 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
.orderByAsc(SysMenu::getSort)
|
||||
);
|
||||
|
||||
Set<Long> cacheMenuIds = menus.stream().map(menu -> menu.getId()).collect(Collectors.toSet());
|
||||
Set<Long> cacheMenuIds = menus.stream()
|
||||
.map(menu -> menu.getId())
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
List<MenuVO> list = menus.stream().map(menu -> {
|
||||
Long parentId = menu.getParentId();
|
||||
// parentId不在当前菜单ID的列表,说明为顶级菜单ID,根据此ID作为递归的开始条件节点
|
||||
// parentId不在当前菜单ID的列表,说明为顶级菜单ID,根据此ID作为递归的开始节点
|
||||
if (!cacheMenuIds.contains(parentId)) {
|
||||
cacheMenuIds.add(parentId);
|
||||
return recurMenus(parentId, menus);
|
||||
@@ -67,21 +68,22 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
* 保存菜单
|
||||
*/
|
||||
@Override
|
||||
public boolean saveMenu(SysMenu menu) {
|
||||
String path = menu.getPath();
|
||||
public boolean saveMenu(MenuForm menuForm) {
|
||||
String path = menuForm.getPath();
|
||||
|
||||
MenuTypeEnum menuType = menu.getType(); // 菜单类型
|
||||
MenuTypeEnum menuType = menuForm.getType(); // 菜单类型
|
||||
switch (menuType) {
|
||||
case CATALOG: // 目录
|
||||
Assert.isTrue(path.startsWith("/"), "目录路由路径格式错误,必须以/开始");
|
||||
menu.setComponent("Layout");
|
||||
menuForm.setComponent("Layout");
|
||||
break;
|
||||
case EXTLINK: // 外链
|
||||
menu.setComponent(null);
|
||||
menuForm.setComponent(null);
|
||||
break;
|
||||
}
|
||||
SysMenu entity = menuConverter.form2Entity(menuForm);
|
||||
|
||||
boolean result = this.saveOrUpdate(menu);
|
||||
boolean result = this.saveOrUpdate(entity);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -119,9 +121,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
.filter(menu -> menu.getParentId().equals(parentId))
|
||||
.forEach(menu -> {
|
||||
RouteVO routeVO = new RouteVO();
|
||||
|
||||
MenuTypeEnum menuTypeEnum = menu.getType();
|
||||
|
||||
if (MenuTypeEnum.MENU.equals(menuTypeEnum)) {
|
||||
routeVO.setName(menu.getPath()); // 根据name路由跳转 this.$router.push({name:xxx})
|
||||
}
|
||||
@@ -135,35 +135,16 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
meta.setRoles(menu.getRoles());
|
||||
meta.setHidden(StatusEnum.DISABLE.getValue().equals(menu.getVisible()));
|
||||
meta.setKeepAlive(true);
|
||||
|
||||
routeVO.setMeta(meta);
|
||||
List<RouteVO> children = recurRoutes(menu.getId(), menuList);
|
||||
// 含有子节点的目录设置为可见
|
||||
boolean alwaysShow = CollectionUtil.isNotEmpty(children) && children.stream().anyMatch(item -> item.getMeta().getHidden().equals(false));
|
||||
meta.setAlwaysShow(alwaysShow);
|
||||
routeVO.setChildren(children);
|
||||
|
||||
List<RouteVO> children = recurRoutes(menu.getId(), menuList);
|
||||
routeVO.setChildren(children);
|
||||
list.add(routeVO);
|
||||
}));
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取菜单资源树形列表
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<ResourceVO> listResources() {
|
||||
List<SysMenu> menuList = this.list(new LambdaQueryWrapper<SysMenu>()
|
||||
.orderByAsc(SysMenu::getSort));
|
||||
|
||||
List<ResourceVO> resources = recurResources(SystemConstants.ROOT_NODE_ID, menuList);
|
||||
|
||||
return resources;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改菜单显示状态
|
||||
*
|
||||
@@ -180,12 +161,48 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色权限集合
|
||||
*
|
||||
* @param roles
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Set<String> listRolePerms(Set<String> roles) {
|
||||
Set<String> perms = this.baseMapper.listRolePerms(roles);
|
||||
return perms;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取菜单表单数据
|
||||
*
|
||||
* @param id 菜单ID
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public MenuForm getMenuForm(Long id) {
|
||||
SysMenu entity = this.getById(id);
|
||||
MenuForm menuForm = menuConverter.entity2Form(entity);
|
||||
return menuForm;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除菜单
|
||||
*
|
||||
* @param id 菜单ID
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean deleteMenu(Long id) {
|
||||
if (id != null) {
|
||||
this.remove(new LambdaQueryWrapper<SysMenu>()
|
||||
.eq(SysMenu::getId, id)
|
||||
.or()
|
||||
.apply("CONCAT (',',tree_path,',') LIKE CONCAT('%,',{0},',%')", id));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归生成菜单列表
|
||||
*
|
||||
@@ -201,7 +218,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
List<MenuVO> menus = menuList.stream()
|
||||
.filter(menu -> menu.getParentId().equals(parentId))
|
||||
.map(entity -> {
|
||||
MenuVO menuVO = menuConverter.entity2VO(entity);
|
||||
MenuVO menuVO = menuConverter.entity2Vo(entity);
|
||||
List<MenuVO> children = recurMenus(entity.getId(), menuList);
|
||||
menuVO.setChildren(children);
|
||||
return menuVO;
|
||||
@@ -228,40 +245,5 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
return menus;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归生成资源(菜单+权限)树形列表
|
||||
*
|
||||
* @param parentId 父级ID
|
||||
* @param menuList 菜单列表
|
||||
* @return
|
||||
*/
|
||||
private static List<ResourceVO> recurResources(Long parentId, List<SysMenu> menuList) {
|
||||
if (CollectionUtil.isEmpty(menuList)) {
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
List<ResourceVO> menus = menuList.stream()
|
||||
.filter(menu -> menu.getParentId().equals(parentId))
|
||||
.map(menu -> {
|
||||
ResourceVO resourceVO = new ResourceVO();
|
||||
resourceVO.setValue(menu.getId());
|
||||
resourceVO.setLabel(menu.getName());
|
||||
|
||||
List<ResourceVO> children = recurResources(menu.getId(), menuList);
|
||||
resourceVO.setChildren(children);
|
||||
|
||||
return resourceVO;
|
||||
}).collect(Collectors.toList());
|
||||
return menus;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 清理路由缓存
|
||||
*/
|
||||
@Override
|
||||
@CacheEvict(cacheNames = "system", key = "'routes'")
|
||||
public void cleanCache() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user