From 996251e171e98bfcdd0dd0fd26f2d652df524ff3 Mon Sep 17 00:00:00 2001 From: cwchen <1048842385@qq.com> Date: Thu, 22 Feb 2024 09:58:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E5=85=B1=E4=B8=8B=E6=8B=89=E9=80=89/?= =?UTF-8?q?=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../securitycontrol-commons-core/pom.xml | 4 + .../common/core/utils/TreeBuild.java | 81 +++++++++++++++++++ .../entity/system/dto/SelectDto.java | 17 ++++ .../entity/system/vo/SelectVo.java | 20 +++++ .../entity/system/vo/TreeNode.java | 35 ++++++++ .../base/controller/SelectController.java | 33 ++++++++ .../system/base/mapper/ISelectMapper.java | 25 ++++++ .../system/base/service/ISelectService.java | 21 +++++ .../base/service/impl/ISelectServiceImpl.java | 46 +++++++++++ .../resources/mapper/base/SelectMapper.xml | 13 +++ 10 files changed, 295 insertions(+) create mode 100644 securitycontrol-commons/securitycontrol-commons-core/src/main/java/com/securitycontrol/common/core/utils/TreeBuild.java create mode 100644 securitycontrol-commons/securitycontrol-commons-entity/src/main/java/com/securitycontrol/entity/system/dto/SelectDto.java create mode 100644 securitycontrol-commons/securitycontrol-commons-entity/src/main/java/com/securitycontrol/entity/system/vo/SelectVo.java create mode 100644 securitycontrol-commons/securitycontrol-commons-entity/src/main/java/com/securitycontrol/entity/system/vo/TreeNode.java create mode 100644 securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/controller/SelectController.java create mode 100644 securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/mapper/ISelectMapper.java create mode 100644 securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/service/ISelectService.java create mode 100644 securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/service/impl/ISelectServiceImpl.java create mode 100644 securitycontrol-model/securitycontrol-system/src/main/resources/mapper/base/SelectMapper.xml diff --git a/securitycontrol-commons/securitycontrol-commons-core/pom.xml b/securitycontrol-commons/securitycontrol-commons-core/pom.xml index dac9d69..48f4639 100644 --- a/securitycontrol-commons/securitycontrol-commons-core/pom.xml +++ b/securitycontrol-commons/securitycontrol-commons-core/pom.xml @@ -131,6 +131,10 @@ hutool-all 5.3.2 + + com.securitycontrol + securitycontrol-commons-entity + diff --git a/securitycontrol-commons/securitycontrol-commons-core/src/main/java/com/securitycontrol/common/core/utils/TreeBuild.java b/securitycontrol-commons/securitycontrol-commons-core/src/main/java/com/securitycontrol/common/core/utils/TreeBuild.java new file mode 100644 index 0000000..4758c37 --- /dev/null +++ b/securitycontrol-commons/securitycontrol-commons-core/src/main/java/com/securitycontrol/common/core/utils/TreeBuild.java @@ -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 nodeList = new ArrayList<>(); + + /** + * 构造方法 + * @param nodeList 将数据集合赋值给nodeList,即所有数据作为所有节点。 + */ + public TreeBuild(List nodeList){ + this.nodeList = nodeList; + } + + /** + * 获取需构建的所有根节点(顶级节点) "0" + * @return 所有根节点List集合 + */ + public List getRootNode(){ + // 保存所有根节点(所有根节点的数据) + List rootNodeList = new ArrayList<>(); + // treeNode:查询出的每一条数据(节点) + for (TreeNode treeNode : nodeList){ + // 判断当前节点是否为根节点,此处注意:若parentId类型是String,则要采用equals()方法判断。 + if (0 == treeNode.getParentId()) { + // 是,添加 + rootNodeList.add(treeNode); + } + } + return rootNodeList; + } + + /** + * 根据每一个顶级节点(根节点)进行构建树形结构 + * @return 构建整棵树 + */ + public List buildTree(){ + // treeNodes:保存一个顶级节点所构建出来的完整树形 + List treeNodes = new ArrayList(); + // getRootNode():获取所有的根节点 + for (TreeNode treeRootNode : getRootNode()) { + // 将顶级节点进行构建子树 + treeRootNode = buildChildTree(treeRootNode); + // 完成一个顶级节点所构建的树形,增加进来 + treeNodes.add(treeRootNode); + } + return treeNodes; + } + + /** + * 递归-----构建子树形结构 + * @param pNode 根节点(顶级节点) + * @return 整棵树 + */ + public TreeNode buildChildTree(TreeNode pNode){ + List childTree = new ArrayList(); + // nodeList:所有节点集合(所有数据) + for (TreeNode treeNode : nodeList) { + // 判断当前节点的父节点ID是否等于根节点的ID,即当前节点为其下的子节点 + if (Objects.equals(treeNode.getParentId(),pNode.getId())) { + // 再递归进行判断当前节点的情况,调用自身方法 + childTree.add(buildChildTree(treeNode)); + } + } + // for循环结束,即节点下没有任何节点,树形构建结束,设置树结果 + pNode.setChildren(childTree); + return pNode; + } + +} diff --git a/securitycontrol-commons/securitycontrol-commons-entity/src/main/java/com/securitycontrol/entity/system/dto/SelectDto.java b/securitycontrol-commons/securitycontrol-commons-entity/src/main/java/com/securitycontrol/entity/system/dto/SelectDto.java new file mode 100644 index 0000000..f6d6bc9 --- /dev/null +++ b/securitycontrol-commons/securitycontrol-commons-entity/src/main/java/com/securitycontrol/entity/system/dto/SelectDto.java @@ -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; +} diff --git a/securitycontrol-commons/securitycontrol-commons-entity/src/main/java/com/securitycontrol/entity/system/vo/SelectVo.java b/securitycontrol-commons/securitycontrol-commons-entity/src/main/java/com/securitycontrol/entity/system/vo/SelectVo.java new file mode 100644 index 0000000..2d8d3b8 --- /dev/null +++ b/securitycontrol-commons/securitycontrol-commons-entity/src/main/java/com/securitycontrol/entity/system/vo/SelectVo.java @@ -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; +} diff --git a/securitycontrol-commons/securitycontrol-commons-entity/src/main/java/com/securitycontrol/entity/system/vo/TreeNode.java b/securitycontrol-commons/securitycontrol-commons-entity/src/main/java/com/securitycontrol/entity/system/vo/TreeNode.java new file mode 100644 index 0000000..032fd3c --- /dev/null +++ b/securitycontrol-commons/securitycontrol-commons-entity/src/main/java/com/securitycontrol/entity/system/vo/TreeNode.java @@ -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 children = new ArrayList<>(); +} diff --git a/securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/controller/SelectController.java b/securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/controller/SelectController.java new file mode 100644 index 0000000..e28f11a --- /dev/null +++ b/securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/controller/SelectController.java @@ -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(); + } + +} diff --git a/securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/mapper/ISelectMapper.java b/securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/mapper/ISelectMapper.java new file mode 100644 index 0000000..6d31544 --- /dev/null +++ b/securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/mapper/ISelectMapper.java @@ -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 + * @description 组织机构树 + * @author cwchen + * @date 2024/2/22 9:51 + */ + List getOrgTree(); +} diff --git a/securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/service/ISelectService.java b/securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/service/ISelectService.java new file mode 100644 index 0000000..658f32a --- /dev/null +++ b/securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/service/ISelectService.java @@ -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(); +} diff --git a/securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/service/impl/ISelectServiceImpl.java b/securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/service/impl/ISelectServiceImpl.java new file mode 100644 index 0000000..54aabea --- /dev/null +++ b/securitycontrol-model/securitycontrol-system/src/main/java/com/securitycontrol/system/base/service/impl/ISelectServiceImpl.java @@ -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 groupList = new ArrayList<>(); + List 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); + } +} diff --git a/securitycontrol-model/securitycontrol-system/src/main/resources/mapper/base/SelectMapper.xml b/securitycontrol-model/securitycontrol-system/src/main/resources/mapper/base/SelectMapper.xml new file mode 100644 index 0000000..9a823f0 --- /dev/null +++ b/securitycontrol-model/securitycontrol-system/src/main/resources/mapper/base/SelectMapper.xml @@ -0,0 +1,13 @@ + + + + + + + \ No newline at end of file