公共下拉选/树
This commit is contained in:
parent
b2ccb03e33
commit
996251e171
|
|
@ -131,6 +131,10 @@
|
||||||
<artifactId>hutool-all</artifactId>
|
<artifactId>hutool-all</artifactId>
|
||||||
<version>5.3.2</version>
|
<version>5.3.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.securitycontrol</groupId>
|
||||||
|
<artifactId>securitycontrol-commons-entity</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author:cwchen
|
||||||
|
* @date:2024-02-22-9:46
|
||||||
|
* @version:1.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.securitycontrol.entity.system.dto;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author:cwchen
|
||||||
|
* @date:2024-02-22-9:42
|
||||||
|
* @version:1.0
|
||||||
|
* @description:下拉选-dto
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class SelectDto {
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "参数")
|
||||||
|
private String param;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.securitycontrol.entity.system.vo;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author:cwchen
|
||||||
|
* @date:2024-02-22-9:41
|
||||||
|
* @version:1.0
|
||||||
|
* @description:下拉选-vo
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class SelectVo {
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "id")
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "名称")
|
||||||
|
private String name;
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author:cwchen
|
||||||
|
* @date:2024-02-22-9:31
|
||||||
|
* @version:1.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<>();
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author:cwchen
|
||||||
|
* @date:2024-02-21-18:25
|
||||||
|
* @version:1.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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author:cwchen
|
||||||
|
* @date:2024-02-22-9:37
|
||||||
|
* @version:1.0
|
||||||
|
* @description:公共下拉选/树-数据库访问层
|
||||||
|
*/
|
||||||
|
@Repository(value = "ISelectMapper")
|
||||||
|
public interface ISelectMapper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 组织机构树
|
||||||
|
* @return List<TreeNode>
|
||||||
|
* @description 组织机构树
|
||||||
|
* @author cwchen
|
||||||
|
* @date 2024/2/22 9:51
|
||||||
|
*/
|
||||||
|
List<TreeNode> getOrgTree();
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.securitycontrol.system.base.service;
|
||||||
|
|
||||||
|
import com.securitycontrol.common.core.web.domain.AjaxResult;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author:cwchen
|
||||||
|
* @date:2024-02-22-9:35
|
||||||
|
* @version:1.0
|
||||||
|
* @description:公共下拉选/树-业务层
|
||||||
|
*/
|
||||||
|
public interface ISelectService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 组织机构树
|
||||||
|
* @return AjaxResult
|
||||||
|
* @description 组织机构树
|
||||||
|
* @author cwchen
|
||||||
|
* @date 2024/2/22 9:44
|
||||||
|
*/
|
||||||
|
AjaxResult getOrgTree();
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author:cwchen
|
||||||
|
* @date:2024-02-22-9:37
|
||||||
|
* @version:1.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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>
|
||||||
Loading…
Reference in New Issue