查询领域树形数据接口。
This commit is contained in:
+4
@@ -516,5 +516,9 @@ public class SysCategoryController {
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/getSysCategoryTree")
|
||||
public Result getSysCategoryTree() {
|
||||
return sysCategoryService.getSysCategoryTree();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.jero.modules.system.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
/**
|
||||
* 树形VO
|
||||
*@Author: wzj
|
||||
*@Date: 2022/3/3 09:00
|
||||
**/
|
||||
@Data
|
||||
public class SysCategoryTreeVO {
|
||||
|
||||
private String id;
|
||||
|
||||
private String parentId;
|
||||
|
||||
private String title;
|
||||
|
||||
private List<SysCategoryTreeVO> children;
|
||||
|
||||
}
|
||||
+7
@@ -1,6 +1,7 @@
|
||||
package com.jero.modules.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.jero.common.api.vo.Result;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import com.jero.modules.system.entity.SysCategory;
|
||||
import com.jero.modules.system.model.TreeSelectModel;
|
||||
@@ -64,4 +65,10 @@ public interface ISysCategoryService extends IService<SysCategory> {
|
||||
* @return
|
||||
*/
|
||||
List<SysCategory> queryBySysDictId(String sysDictId);
|
||||
|
||||
/**
|
||||
* 获取树形结构
|
||||
* @return
|
||||
*/
|
||||
Result getSysCategoryTree();
|
||||
}
|
||||
|
||||
+20
@@ -6,17 +6,22 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.jero.common.api.vo.Result;
|
||||
import com.jero.common.constant.FillRuleConstant;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import com.jero.common.util.FillRuleUtil;
|
||||
import com.jero.common.util.oConvertUtils;
|
||||
import com.jero.modules.system.entity.SysCategory;
|
||||
import com.jero.modules.system.entity.SysCategoryTreeVO;
|
||||
import com.jero.modules.system.mapper.SysCategoryMapper;
|
||||
import com.jero.modules.system.model.TreeSelectModel;
|
||||
import com.jero.modules.system.service.ISysCategoryService;
|
||||
import com.jero.modules.system.util.TreeUtils;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -214,4 +219,19 @@ public class SysCategoryServiceImpl extends ServiceImpl<SysCategoryMapper, SysCa
|
||||
queryWrapper.eq("sys_dict_id",sysDictId);
|
||||
return list(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result getSysCategoryTree() {
|
||||
try {
|
||||
List<SysCategory> list = super.baseMapper.selectList(new QueryWrapper<>());
|
||||
List<SysCategoryTreeVO> tree = new ArrayList<>();
|
||||
if(CollectionUtils.isNotEmpty(list)){
|
||||
tree = TreeUtils.getSysCategoryTree(list);
|
||||
}
|
||||
return Result.OK(tree);
|
||||
}catch (Exception ex){
|
||||
log.error("查询树形结构失败: " + ex.getMessage());
|
||||
throw new JeroBootException("查询树形结构失败!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.jero.modules.system.util;
|
||||
|
||||
import com.jero.modules.system.entity.SysCategory;
|
||||
import com.jero.modules.system.entity.SysCategoryTreeVO;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*生成树形结构工具类
|
||||
*@Author: wzj
|
||||
*@Date: 2022/3/3 09:02
|
||||
**/
|
||||
@Component
|
||||
public class TreeUtils {
|
||||
/**
|
||||
* 根节点CODE
|
||||
*/
|
||||
private static final String TREE_ROOT_CODE = "0";
|
||||
|
||||
public static List<SysCategoryTreeVO> getSysCategoryTree(List<SysCategory> sysCategoryList) {
|
||||
List<SysCategoryTreeVO> sysCategoryTreeVOList = new LinkedList();
|
||||
for (SysCategory sysCategory : sysCategoryList) {
|
||||
if (TREE_ROOT_CODE.equals(sysCategory.getPid())) {
|
||||
SysCategoryTreeVO sysCategoryTreeVO = new SysCategoryTreeVO();
|
||||
sysCategoryTreeVO.setId(sysCategory.getId());
|
||||
sysCategoryTreeVO.setTitle(sysCategory.getName());
|
||||
sysCategoryTreeVO.setParentId(sysCategory.getPid());
|
||||
sysCategoryTreeVO.setChildren(getSysCategoryTreeChild(sysCategory.getId(), sysCategoryList));
|
||||
sysCategoryTreeVOList.add(sysCategoryTreeVO);
|
||||
}
|
||||
}
|
||||
return sysCategoryTreeVOList;
|
||||
}
|
||||
|
||||
private static List<SysCategoryTreeVO> getSysCategoryTreeChild(String id, List<SysCategory> sysCategoryList) {
|
||||
List<SysCategoryTreeVO> childrenList = new LinkedList();
|
||||
for (SysCategory sysCategory : sysCategoryList) {
|
||||
if (id.equals(sysCategory.getPid())) {
|
||||
SysCategoryTreeVO sysCategoryTreeVO = new SysCategoryTreeVO();
|
||||
sysCategoryTreeVO.setId(sysCategory.getId());
|
||||
sysCategoryTreeVO.setTitle(sysCategory.getName());
|
||||
sysCategoryTreeVO.setParentId(sysCategory.getPid());
|
||||
sysCategoryTreeVO.setChildren(getSysCategoryTreeChild(sysCategory.getId(), sysCategoryList));
|
||||
childrenList.add(sysCategoryTreeVO);
|
||||
}
|
||||
}
|
||||
return childrenList;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user