菜單樹形結(jié)構(gòu)
package com.yun.entity.vo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Data
public class MenuVO implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "主鍵")
private Integer id;
@ApiModelProperty(value = "菜單名稱")
private String name;
@ApiModelProperty(value = "菜單圖標(biāo)")
private String icon;
@ApiModelProperty(value = "菜單路由")
private String link;
@ApiModelProperty(value = "菜單索引")
private String indexTag;
@ApiModelProperty(value = "菜單類型斥杜,目錄:12801001窖贤,菜單:12801002,按鈕:12801003")
private Integer type;
@ApiModelProperty(value = "排序")
private Integer sort;
@ApiModelProperty(value = "狀態(tài)钝诚,啟用:12781001棘劣,停用:12781002")
private Integer status;
@ApiModelProperty(value = "權(quán)限標(biāo)識")
private String permission;
@ApiModelProperty(value = "父節(jié)點主鍵椰拒,-1為根節(jié)點")
private Integer parentId;
@ApiModelProperty(value = "用于前端顯示開關(guān)嫉称,status=12781001為true")
private boolean enable;
@ApiModelProperty(value = "菜單子表")
private List<MenuVO> children = new ArrayList<>();
}
package com.yun.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.constant.DictConstant;
import com.yun.entity.dto.UserDTO;
import com.yun.entity.po.MenuPO;
import com.yun.entity.vo.MenuVO;
import com.yun.mapper.MenuMapper;
import com.yun.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* 菜單表 服務(wù)實現(xiàn)類
* </p>
*
* @author CNSTT
* @since 2023-01-21
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuPO> implements MenuService {
/**
* 菜單樹形查詢
*
* @param dto 傳參
* @return List<MenuVO>
*/
@Override
public List<MenuVO> searchDefault(MenuDTO dto) {
LambdaQueryWrapper<MenuPO> wrapper = new QueryWrapper<MenuPO>().lambda();
// 查詢菜單或目錄
wrapper.in(MenuPO::getType, DictConstant.DICT_MENU_TYPE_CATALOG, DictConstant.DICT_MENU_TYPE_MENU);
wrapper.eq(MenuPO::getDeleteFlag, DictConstant.DICT_NO);
List<MenuPO> menuList = baseMapper.selectList(wrapper);
if (!CollectionUtils.isEmpty(menuList)) {
// 樹形返回
return packageListFunc(menuList);
}
return new ArrayList<>();
}
/**
* 組裝菜單樹形結(jié)構(gòu)主方法
*
* @param menuList 全量數(shù)據(jù)
* @return List<MenuVO>
*/
private List<MenuVO> packageListFunc(List<MenuPO> menuList) {
List<MenuVO> result = new ArrayList<>();
List<MenuVO> list = convertResult(menuList);
for (MenuVO node : list) {
// parentId = -1為頂層目錄或菜單
if (node.getParentId() == -1) {
result.add(findChildren(node, list));
}
}
return result;
}
/**
* 遞歸查詢 - 根據(jù)父節(jié)點獲取子節(jié)點
*
* @param node 父節(jié)點
* @param result 全量數(shù)據(jù)
* @return MenuVO 填充完父節(jié)點
*/
private MenuVO findChildren(MenuVO node, List<MenuVO> result) {
for (MenuVO dto : result) {
// 父節(jié)點的ID等于子節(jié)點的父節(jié)點ID
if (node.getId().equals(dto.getParentId())) {
if (node.getChildren() == null) {
node.setChildren(new ArrayList<>());
}
node.getChildren().add(findChildren(dto, result));
}
}
return node;
}
/**
* 公共方法 - 實體轉(zhuǎn)化VO出參集合
*
* @param menuList PO結(jié)果集
* @return List<MenuVO>
*/
private List<MenuVO> convertResult(List<MenuPO> menuList) {
List<MenuVO> result = new ArrayList<>();
if (!CollectionUtils.isEmpty(menuList)) {
menuList.forEach(menuPO -> {
MenuVO vo = new MenuVO();
BeanUtils.copyProperties(menuPO, vo);
vo.setEnable(vo.getStatus() != null && vo.getStatus() == DictConstant.DICT_YES);
result.add(vo);
});
}
return result;
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者