公共下拉选/树

This commit is contained in:
cwchen 2024-02-22 09:58:31 +08:00
parent b2ccb03e33
commit 996251e171
10 changed files with 295 additions and 0 deletions

View File

@ -131,6 +131,10 @@
<artifactId>hutool-all</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>com.securitycontrol</groupId>
<artifactId>securitycontrol-commons-entity</artifactId>
</dependency>
</dependencies>

View File

@ -0,0 +1,81 @@
package com.securitycontrol.common.core.utils;
import com.securitycontrol.entity.system.vo.TreeNode;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* @authorcwchen
* @date2024-02-22-9:46
* @version1.0
* @description下拉树-工具类
*/
public class TreeBuild {
public List<TreeNode> nodeList = new ArrayList<>();
/**
* 构造方法
* @param nodeList 将数据集合赋值给nodeList即所有数据作为所有节点
*/
public TreeBuild(List<TreeNode> nodeList){
this.nodeList = nodeList;
}
/**
* 获取需构建的所有根节点顶级节点 "0"
* @return 所有根节点List集合
*/
public List<TreeNode> getRootNode(){
// 保存所有根节点所有根节点的数据
List<TreeNode> rootNodeList = new ArrayList<>();
// treeNode查询出的每一条数据节点
for (TreeNode treeNode : nodeList){
// 判断当前节点是否为根节点此处注意若parentId类型是String则要采用equals()方法判断
if (0 == treeNode.getParentId()) {
// 添加
rootNodeList.add(treeNode);
}
}
return rootNodeList;
}
/**
* 根据每一个顶级节点根节点进行构建树形结构
* @return 构建整棵树
*/
public List<TreeNode> buildTree(){
// treeNodes保存一个顶级节点所构建出来的完整树形
List<TreeNode> treeNodes = new ArrayList<TreeNode>();
// getRootNode()获取所有的根节点
for (TreeNode treeRootNode : getRootNode()) {
// 将顶级节点进行构建子树
treeRootNode = buildChildTree(treeRootNode);
// 完成一个顶级节点所构建的树形增加进来
treeNodes.add(treeRootNode);
}
return treeNodes;
}
/**
* 递归-----构建子树形结构
* @param pNode 根节点顶级节点
* @return 整棵树
*/
public TreeNode buildChildTree(TreeNode pNode){
List<TreeNode> childTree = new ArrayList<TreeNode>();
// nodeList所有节点集合所有数据
for (TreeNode treeNode : nodeList) {
// 判断当前节点的父节点ID是否等于根节点的ID即当前节点为其下的子节点
if (Objects.equals(treeNode.getParentId(),pNode.getId())) {
// 再递归进行判断当前节点的情况调用自身方法
childTree.add(buildChildTree(treeNode));
}
}
// for循环结束即节点下没有任何节点树形构建结束设置树结果
pNode.setChildren(childTree);
return pNode;
}
}

View File

@ -0,0 +1,17 @@
package com.securitycontrol.entity.system.dto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @authorcwchen
* @date2024-02-22-9:42
* @version1.0
* @description下拉选-dto
*/
@Data
public class SelectDto {
@ApiModelProperty(value = "参数")
private String param;
}

View File

@ -0,0 +1,20 @@
package com.securitycontrol.entity.system.vo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @authorcwchen
* @date2024-02-22-9:41
* @version1.0
* @description下拉选-vo
*/
@Data
public class SelectVo {
@ApiModelProperty(value = "id")
private Integer id;
@ApiModelProperty(value = "名称")
private String name;
}

View File

@ -0,0 +1,35 @@
package com.securitycontrol.entity.system.vo;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* @authorcwchen
* @date2024-02-22-9:31
* @version1.0
* @description下拉树-vo
*/
@Data
public class TreeNode {
@ApiModelProperty(value = "id")
private Integer id;
@ApiModelProperty(value = "名称")
private String label;
@ApiModelProperty(value = "父ID")
private Integer parentId;
@ApiModelProperty(value = "层级")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String level;
@ApiModelProperty(value = "子节点")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<TreeNode> children = new ArrayList<>();
}

View File

@ -0,0 +1,33 @@
package com.securitycontrol.system.base.controller;
import com.securitycontrol.common.core.web.controller.BaseController;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.system.base.service.ISelectService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @authorcwchen
* @date2024-02-21-18:25
* @version1.0
* @description公共下拉选/
*/
@RestController
@RequestMapping("/sys/select/")
public class SelectController extends BaseController {
@Resource(name = "ISelectService")
private ISelectService service;
@ApiOperation(value = "组织机构树")
@PostMapping("getOrgTree")
public AjaxResult getOrgTree(){
return service.getOrgTree();
}
}

View File

@ -0,0 +1,25 @@
package com.securitycontrol.system.base.mapper;
import com.securitycontrol.entity.system.vo.TreeNode;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @authorcwchen
* @date2024-02-22-9:37
* @version1.0
* @description公共下拉选/-数据库访问层
*/
@Repository(value = "ISelectMapper")
public interface ISelectMapper {
/**
* 组织机构树
* @return List<TreeNode>
* @description 组织机构树
* @author cwchen
* @date 2024/2/22 9:51
*/
List<TreeNode> getOrgTree();
}

View File

@ -0,0 +1,21 @@
package com.securitycontrol.system.base.service;
import com.securitycontrol.common.core.web.domain.AjaxResult;
/**
* @authorcwchen
* @date2024-02-22-9:35
* @version1.0
* @description公共下拉选/-业务层
*/
public interface ISelectService {
/**
* 组织机构树
* @return AjaxResult
* @description 组织机构树
* @author cwchen
* @date 2024/2/22 9:44
*/
AjaxResult getOrgTree();
}

View File

@ -0,0 +1,46 @@
package com.securitycontrol.system.base.service.impl;
import com.securitycontrol.common.core.utils.TreeBuild;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.entity.system.vo.TreeNode;
import com.securitycontrol.system.base.mapper.ISelectMapper;
import com.securitycontrol.system.base.service.ISelectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* @authorcwchen
* @date2024-02-22-9:37
* @version1.0
* @description公共下拉选/-业务层逻辑层
*/
@Service(value = "ISelectService")
@Slf4j
public class ISelectServiceImpl implements ISelectService {
@Resource(name = "ISelectMapper")
private ISelectMapper mapper;
@Override
public AjaxResult getOrgTree() {
List<TreeNode> groupList = new ArrayList<>();
List<TreeNode> list = new ArrayList<>();
try {
list = mapper.getOrgTree();
if (CollectionUtils.isNotEmpty(list)) {
// 创建树形结构数据集合作为参数
TreeBuild treeBuild = new TreeBuild(list);
// 原查询结果转换树形结构
groupList = treeBuild.buildTree();
}
} catch (Exception e) {
log.error("组织机构树-查询失败", e);
}
return AjaxResult.success(groupList);
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.securitycontrol.system.base.mapper.ISelectMapper">
<!--组织机构树-->
<select id="getOrgTree" resultType="com.securitycontrol.entity.system.vo.TreeNode">
SELECT dict_id AS id,
p_code AS parentId,
dict_value AS label
FROM sys_dict
WHERE del_flag = '0'
</select>
</mapper>