package com.ahies.stm.app.synthesizes.sys.service.impl;
import com.ahies.stm.app.synthesizes.sys.entity.Menu;
import com.ahies.stm.app.synthesizes.sys.mapper.MenuMapper;
import com.ahies.stm.app.synthesizes.sys.service.MenuService;
import com.ahies.stm.app.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* <p>
* 资源菜单表 服务实现类
* </p>
*
* @author zhu
* @since 2019-08-30
*/
@Service
@Transactional
public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, Menu> implements MenuService {
/**
* 根据所给菜单id获取其子节点
* @param id
* @param menuList 数据库中所有菜单
* @return
*/
@Override
public List<Integer> listChildById(Integer id,List<Menu> menuList) {
// 子菜单
List<Integer> childList = new ArrayList<>();
for (Menu menu : menuList) {
// 遍历所有节点,将父菜单id与传过来的id比较
if (Objects.equals(menu.getParentId(), id)) {
childList.add(menu.getId());
}
}
return childList;
}
/**
* 根据所给菜单id获取其子孙节点 不包括它本身
* @param id
* @param menuList 数据库中所有菜单
* @return result 是返回的数据 子孙节点id集合 初始为一个空的list
*/
@Override
public List<Integer> listAllChildsById(Integer id, List<Menu> menuList, List<Integer> result) {
List<Integer> childTreeNodes = listChildById(id,menuList);
result.addAll(childTreeNodes);
for(Integer child : childTreeNodes){
listAllChildsById(child,menuList,result);
}
return result;
}
}
使用如下
@RequestMapping("/delete")
public Map<String, Object> delete(Integer id) {
//查询数据库中所有菜单
List<Menu> menuList = menuService.list();
List<Integer> result = new ArrayList<>();
List<Integer> child = menuService.listAllChildsById(id,menuList,result);
child.add(id);
boolean flag = menuService.removeByIds(child);
if (flag) {
return ResponseMap.messageSucceedMap(SysConstant.DELETE_SUCCESS);
}
return ResponseMap.messageFailedMap(SysConstant.DELETE_FAIL);
}