Merge branch 'develop_3' into 'develop_master'
feat: 标准库 体系树形结构等 See merge request !19
This commit is contained in:
+74
@@ -0,0 +1,74 @@
|
||||
package com.adc.da.sys.sarMenuStandard.controller;
|
||||
|
||||
|
||||
import com.adc.da.base.web.BaseController;
|
||||
import com.adc.da.http.ResponseMessage;
|
||||
import com.adc.da.http.Result;
|
||||
import com.adc.da.sys.sarMenuStandard.entity.SarMenuStandard;
|
||||
import com.adc.da.sys.sarMenuStandard.service.ISarMenuStandardService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 前端控制器
|
||||
* </p>
|
||||
*
|
||||
* @author super_liu
|
||||
* @since 2021-07-07
|
||||
*/
|
||||
@RestController
|
||||
@Api(tags = "福田标准法规--标准法规库-菜单标准体系")
|
||||
@RequestMapping("/${restPath}/lawss/sarMenuStandard")
|
||||
public class SarMenuStandardController extends BaseController<SarMenuStandard> {
|
||||
|
||||
@Autowired
|
||||
private ISarMenuStandardService iSarMenuStandardService;
|
||||
|
||||
@ApiOperation("查询所有菜单")
|
||||
@GetMapping
|
||||
public ResponseMessage<List<SarMenuStandard>> getAll(@RequestParam String dicId){
|
||||
List<SarMenuStandard> sarMenus=iSarMenuStandardService.getAll(dicId);
|
||||
return Result.success(sarMenus);
|
||||
}
|
||||
|
||||
@ApiOperation("根据ID查询菜单")
|
||||
@GetMapping("/getSarMenuStandardById")
|
||||
public ResponseMessage<SarMenuStandard> getSarMenuStandardById(@RequestParam String menuId){
|
||||
SarMenuStandard sarMenus = iSarMenuStandardService.getById(menuId);
|
||||
if(sarMenus != null && sarMenus.getParentIds() != null && StringUtils.isNotBlank(sarMenus.getParentIds())){
|
||||
List<SarMenuStandard> menuStandards = iSarMenuStandardService.getMenuByIds(Arrays.asList(sarMenus.getParentIds().split(",")));
|
||||
if(!menuStandards.isEmpty()){
|
||||
String parentIdsName = menuStandards.stream().map(SarMenuStandard::getMenuName).collect(Collectors.joining(","));
|
||||
sarMenus.setParentIdsName(parentIdsName);
|
||||
}
|
||||
}
|
||||
return Result.success(sarMenus);
|
||||
}
|
||||
|
||||
@ApiOperation("添加菜单")
|
||||
@PostMapping("/add")
|
||||
public ResponseMessage<Object> addMenu(@RequestBody SarMenuStandard sarMenu){
|
||||
return iSarMenuStandardService.addMenu(sarMenu);
|
||||
}
|
||||
|
||||
@ApiOperation("编辑菜单")
|
||||
@PutMapping
|
||||
public ResponseMessage<Object> updateMenu(@Validated(SarMenuStandard.update.class) @RequestBody SarMenuStandard sarMenu){
|
||||
return iSarMenuStandardService.updateMenu(sarMenu);
|
||||
}
|
||||
|
||||
@ApiOperation("删除菜单")
|
||||
@DeleteMapping
|
||||
public ResponseMessage<Object> delMenu(@RequestParam String menuId){
|
||||
return iSarMenuStandardService.delMenu(menuId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.adc.da.sys.sarMenuStandard.dao;
|
||||
|
||||
import com.adc.da.sys.sarMenuStandard.entity.SarMenuStandard;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
* @author super_liu
|
||||
* @since 2021-07-07
|
||||
*/
|
||||
public interface SarMenuStandardDao extends BaseMapper<SarMenuStandard> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.adc.da.sys.sarMenuStandard.entity;
|
||||
|
||||
import com.adc.da.base.entity.BaseEntity;
|
||||
import com.adc.da.sys.utils.treetool.annotation.TreeNodeName;
|
||||
import com.adc.da.sys.utils.treetool.annotation.TreeNodeParentId;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* </p>
|
||||
*
|
||||
* @author super_liu
|
||||
* @since 2021-07-07
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Accessors(chain = true)
|
||||
@ApiModel(value="SarMenuStandard对象", description="")
|
||||
public class SarMenuStandard extends BaseEntity {
|
||||
public interface update{
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@NotNull(message = "id不能为空",groups = {update.class})
|
||||
@ApiModelProperty(value = "主键")
|
||||
@TableId(value = "ID",type = IdType.UUID)
|
||||
private String id;
|
||||
|
||||
@ApiModelProperty(value = "标准法规划分")
|
||||
@TableField("SOR_DIVIDE")
|
||||
private String sorDivide;
|
||||
|
||||
@TreeNodeName
|
||||
@ApiModelProperty(value = "目录名称")
|
||||
@TableField("MENU_NAME")
|
||||
private String menuName;
|
||||
|
||||
@TreeNodeParentId
|
||||
@ApiModelProperty(value = "父级ID")
|
||||
@TableField("PARENT_ID")
|
||||
private String parentId;
|
||||
|
||||
@ApiModelProperty(value = "父级ID集合")
|
||||
@TableField("PARENT_IDS")
|
||||
private String parentIds;
|
||||
|
||||
@ApiModelProperty(value = "排序序号")
|
||||
@TableField("DISPLAY_SEQ")
|
||||
private Integer displaySeq;
|
||||
|
||||
@ApiModelProperty(value = "路径")
|
||||
@TableField("HREF")
|
||||
private String href;
|
||||
|
||||
@ApiModelProperty(value = "是否有效")
|
||||
@TableField("VALID_FLAG")
|
||||
private Integer validFlag;
|
||||
|
||||
@ApiModelProperty(value = "创建时间")
|
||||
@TableField("CREATION_TIME")
|
||||
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
|
||||
private Date creationTime;
|
||||
|
||||
@ApiModelProperty(value = "修改时间")
|
||||
@TableField("MODIFY_TIME")
|
||||
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
|
||||
private Date modifyTime;
|
||||
|
||||
@ApiModelProperty(value = "备注")
|
||||
@TableField("REMARKS")
|
||||
private String remarks;
|
||||
|
||||
@ApiModelProperty(value = "数据字典ID")
|
||||
@TableField("DIC_ID")
|
||||
private String dicId;
|
||||
|
||||
@ApiModelProperty(value = "子菜单")
|
||||
@TableField(exist = false)
|
||||
private List<SarMenuStandard> children;
|
||||
|
||||
@TableField(exist=false)
|
||||
private List<String> roleIds;
|
||||
|
||||
@TableField(exist=false)
|
||||
private List<String> childMenuIds;
|
||||
|
||||
@TableField(exist=false)
|
||||
private String parentIdsName;
|
||||
|
||||
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.adc.da.sys.sarMenuStandard.service;
|
||||
|
||||
import com.adc.da.http.ResponseMessage;
|
||||
import com.adc.da.sys.sarMenuStandard.entity.SarMenuStandard;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 服务类
|
||||
* </p>
|
||||
*
|
||||
* @author super_liu
|
||||
* @since 2021-07-07
|
||||
*/
|
||||
public interface ISarMenuStandardService extends IService<SarMenuStandard> {
|
||||
|
||||
/**
|
||||
* 查询所有菜单
|
||||
*/
|
||||
List<SarMenuStandard> getAll(String dicId);
|
||||
|
||||
/**
|
||||
* 添加菜单
|
||||
* @param sarMenu:菜单信息
|
||||
* @return ResponseMessage<Object>
|
||||
*/
|
||||
ResponseMessage<Object> addMenu(SarMenuStandard sarMenu);
|
||||
|
||||
/**
|
||||
* 编辑菜单
|
||||
* @param sarMenu:菜单信息
|
||||
* @return ResponseMessage<Object>
|
||||
*/
|
||||
ResponseMessage<Object> updateMenu(SarMenuStandard sarMenu);
|
||||
|
||||
/**
|
||||
* 删除菜单
|
||||
* @param menuId :菜单ID
|
||||
* @return ResponseMessage<Object>
|
||||
*/
|
||||
ResponseMessage<Object> delMenu(String menuId);
|
||||
|
||||
/**
|
||||
* 通过Ids查询menu
|
||||
* @param menuIds :菜单Ids
|
||||
* @return List<Menu>
|
||||
*/
|
||||
List<SarMenuStandard> getMenuByIds(List<String> menuIds);
|
||||
}
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
package com.adc.da.sys.sarMenuStandard.service.impl;
|
||||
|
||||
import com.adc.da.http.ResponseMessage;
|
||||
import com.adc.da.http.Result;
|
||||
import com.adc.da.sys.sarMenuStandard.dao.SarMenuStandardDao;
|
||||
import com.adc.da.sys.sarMenuStandard.entity.SarMenuStandard;
|
||||
import com.adc.da.sys.sarMenuStandard.service.ISarMenuStandardService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author super_liu
|
||||
* @since 2021-07-07
|
||||
*/
|
||||
@Service
|
||||
public class SarMenuStandardServiceImpl extends ServiceImpl<SarMenuStandardDao, SarMenuStandard> implements ISarMenuStandardService {
|
||||
|
||||
@Autowired
|
||||
private SarMenuStandardDao dao;
|
||||
|
||||
/**
|
||||
* 查询所有菜单
|
||||
*/
|
||||
@Override
|
||||
public List<SarMenuStandard> getAll(String dicId) {
|
||||
QueryWrapper sarMenuQueryWrapper=new QueryWrapper<>();
|
||||
sarMenuQueryWrapper.eq("DIC_ID",dicId);
|
||||
sarMenuQueryWrapper.orderByAsc("DISPLAY_SEQ");
|
||||
sarMenuQueryWrapper.isNull("PARENT_ID");
|
||||
List<SarMenuStandard> sarMenus=dao.selectList(sarMenuQueryWrapper);
|
||||
for(SarMenuStandard sarMenu:sarMenus){
|
||||
sarMenu.setChildren(recursionGetChildren((sarMenu)));
|
||||
}
|
||||
return sarMenus;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加菜单
|
||||
* @param sarMenu:菜单信息
|
||||
* @return ResponseMessage<Object>
|
||||
*/
|
||||
@Override
|
||||
public ResponseMessage<Object> addMenu(SarMenuStandard sarMenu) {
|
||||
SarMenuStandard save = new SarMenuStandard();
|
||||
if(sarMenu.getParentId() != null && StringUtils.isNotBlank(sarMenu.getParentId())){
|
||||
SarMenuStandard parent=dao.selectById(sarMenu.getParentId());
|
||||
if(parent==null){
|
||||
return Result.error("该父菜单不存在");
|
||||
}
|
||||
}
|
||||
sarMenu.setCreationTime(new Date());
|
||||
sarMenu.setModifyTime(new Date());
|
||||
dao.insert(sarMenu);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑菜单
|
||||
* @param sarMenu:菜单信息
|
||||
* @return ResponseMessage<Object>
|
||||
*/
|
||||
@Override
|
||||
public ResponseMessage<Object> updateMenu(SarMenuStandard sarMenu) {
|
||||
SarMenuStandard oldMenu=dao.selectById(sarMenu.getId());
|
||||
if(oldMenu==null){
|
||||
return Result.error("该菜单不存在");
|
||||
}
|
||||
if(sarMenu.getParentId() != null && StringUtils.isNotBlank(sarMenu.getParentId())){
|
||||
SarMenuStandard parent=dao.selectById(sarMenu.getParentId());
|
||||
if(parent==null){
|
||||
return Result.error("父节点不存在");
|
||||
}
|
||||
}
|
||||
dao.updateById(sarMenu);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除菜单
|
||||
* @param menuId :菜单ID
|
||||
* @return ResponseMessage<Object>
|
||||
*/
|
||||
@Override
|
||||
public ResponseMessage<Object> delMenu(String menuId) {
|
||||
QueryWrapper<SarMenuStandard> sarMenuQueryWrapper=new QueryWrapper<>();
|
||||
sarMenuQueryWrapper.eq("parent_id",menuId);
|
||||
List<SarMenuStandard> children=dao.selectList(sarMenuQueryWrapper);
|
||||
if(children !=null && !children.isEmpty()){
|
||||
return Result.error("该菜单有子节点,无法删除");
|
||||
}
|
||||
dao.deleteById(menuId);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过Ids查询menu
|
||||
* @param menuIds :菜单Ids
|
||||
* @return List<Menu>
|
||||
*/
|
||||
@Override
|
||||
public List<SarMenuStandard> getMenuByIds(List<String> menuIds) {
|
||||
if(menuIds!=null&&menuIds.size()>0){
|
||||
QueryWrapper<SarMenuStandard> sarMenuQueryWrapper=new QueryWrapper<>();
|
||||
sarMenuQueryWrapper.in("id",menuIds);
|
||||
return dao.selectList(sarMenuQueryWrapper);
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归调用获取子节点
|
||||
* @param parent:父节点
|
||||
* @return List<SarMenu>
|
||||
*/
|
||||
private List<SarMenuStandard> recursionGetChildren(SarMenuStandard parent){
|
||||
QueryWrapper<SarMenuStandard> sarMenuQueryWrapper=new QueryWrapper<>();
|
||||
sarMenuQueryWrapper.orderByAsc("DISPLAY_SEQ");
|
||||
sarMenuQueryWrapper.eq("PARENT_ID",parent.getId());
|
||||
List<SarMenuStandard> children=dao.selectList(sarMenuQueryWrapper);
|
||||
for(SarMenuStandard sarMenu:children){
|
||||
sarMenu.setChildren(recursionGetChildren((sarMenu)));
|
||||
}
|
||||
return children;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
package com.adc.da.sys.service.impl;
|
||||
|
||||
import com.adc.da.sys.common.SelectionResult;
|
||||
import com.adc.da.sys.dao.DicEODao;
|
||||
import com.adc.da.sys.dao.DicTypeEODao;
|
||||
import com.adc.da.sys.entity.DicTypeEO;
|
||||
import com.adc.da.sys.entity.DictionaryEO;
|
||||
import com.adc.da.sys.page.DicTypeEOPage;
|
||||
import com.adc.da.sys.sarMenuStandard.entity.SarMenuStandard;
|
||||
import com.adc.da.sys.sarMenuStandard.service.ISarMenuStandardService;
|
||||
import com.adc.da.sys.service.IDicEOService;
|
||||
import com.adc.da.sys.service.IDicTypeEOService;
|
||||
import com.adc.da.util.LoginUserUtil;
|
||||
@@ -33,6 +34,9 @@ public class DicTypeEOServiceImpl extends ServiceImpl<DicTypeEODao,DicTypeEO> im
|
||||
@Autowired
|
||||
private IDicEOService dicEODao;
|
||||
|
||||
@Autowired
|
||||
private ISarMenuStandardService iSarMenuStandardService;
|
||||
|
||||
|
||||
|
||||
public DicTypeEODao getDao() {
|
||||
@@ -168,8 +172,13 @@ public class DicTypeEOServiceImpl extends ServiceImpl<DicTypeEODao,DicTypeEO> im
|
||||
Map<String,Object> resultMap = new HashMap<>();
|
||||
List<DictionaryEO> diclist = dicEODao.getDictionaryEO();
|
||||
for (DictionaryEO dictionaryEO : diclist){
|
||||
List<Map<String,String >> relist = getDicTypeByDicCode(dictionaryEO.getDictionaryCode());
|
||||
resultMap.put(dictionaryEO.getDictionaryCode(),relist);
|
||||
if(dictionaryEO.getDictionType().equals("TREE")){
|
||||
List<SarMenuStandard> treeList = iSarMenuStandardService.getAll(dictionaryEO.getId());
|
||||
resultMap.put(dictionaryEO.getDictionaryCode(),treeList);
|
||||
}else {
|
||||
List<Map<String,String >> relist = getDicTypeByDicCode(dictionaryEO.getDictionaryCode());
|
||||
resultMap.put(dictionaryEO.getDictionaryCode(),relist);
|
||||
}
|
||||
}
|
||||
// 查询细类
|
||||
List<Map<String,String >> relist = getDicTypeByDicCode("BUSSFINECLASSSHOW");
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.adc.da.sys.utils.tree;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 操作树的接口类
|
||||
*
|
||||
*/
|
||||
public interface ITree {
|
||||
/**
|
||||
* 获取构建数的list,因为根节点可能不止一个,所有返回List
|
||||
* 获取的结构与getRoot基本一致,但是后边增加了另外的数据,不建议调用
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<TreeNode> getTree();
|
||||
|
||||
/**
|
||||
* 获取根节点树结构,因为根节点可能不止一个,所有返回List
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<TreeNode> getRoot();
|
||||
|
||||
/**
|
||||
* 获取指定节点数据
|
||||
*
|
||||
* @param nodeId
|
||||
* @return
|
||||
*/
|
||||
TreeNode getTreeNode(String nodeId);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.adc.da.sys.utils.tree;
|
||||
|
||||
/**
|
||||
* 需要实现树的实体类需要实现的接口,获取关键数据
|
||||
*
|
||||
*/
|
||||
public interface ITreeNode<T> {
|
||||
/**
|
||||
* TreeNode获取nodeId
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getNodeId();
|
||||
|
||||
/**
|
||||
* TreeNode获取nodeName
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getNodeName();
|
||||
|
||||
/**
|
||||
* TreeNode获取nodeParentId(父id)
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getNodeParentId();
|
||||
|
||||
/**
|
||||
* TreeNode获取orderNum(排序字段用于排序)
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Integer getOrderNum();
|
||||
|
||||
/**
|
||||
* TreeNode获取nodeLevel(当前属于第几层级)
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Integer getNodeLevel();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package com.adc.da.sys.utils.tree;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 构建树形建构
|
||||
*/
|
||||
public class Tree implements ITree {
|
||||
/**
|
||||
* 用于存放treeNode的Map
|
||||
*/
|
||||
private LinkedHashMap<String, TreeNode> treeNodesMap = new LinkedHashMap<>();
|
||||
/**
|
||||
* 用于存放treeNode的list
|
||||
*/
|
||||
private List<TreeNode> treeNodesList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*
|
||||
* @param list
|
||||
*/
|
||||
public Tree(List<ITreeNode> list) {
|
||||
initTreeNodeMap(list);
|
||||
initTreeNodeList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将List对象数据转为TreeNodeMap
|
||||
*
|
||||
* @param list
|
||||
*/
|
||||
private void initTreeNodeMap(List<ITreeNode> list) {
|
||||
TreeNode treeNode;
|
||||
for (ITreeNode item : list) {
|
||||
treeNode = new TreeNode(item);
|
||||
treeNode.setLastNodeNum(1);
|
||||
treeNodesMap.put(treeNode.getNodeId(), treeNode);
|
||||
}
|
||||
Iterator<TreeNode> iterator = treeNodesMap.values().iterator();
|
||||
TreeNode parentTreeNode;
|
||||
while (iterator.hasNext()) {
|
||||
treeNode = iterator.next();
|
||||
if (StringUtils.isEmpty(treeNode.getParentNodeId())) {
|
||||
continue;
|
||||
}
|
||||
parentTreeNode = treeNodesMap.get(treeNode.getParentNodeId());
|
||||
if (parentTreeNode != null) {
|
||||
treeNode.setParent(parentTreeNode);
|
||||
parentTreeNode.addChild(treeNode);
|
||||
// 按照orderNum排序
|
||||
Collections.sort(parentTreeNode.getChildren(), new OrdNamComparator());
|
||||
// 判断这个节点是否是最子节点
|
||||
if (treeNode.getChildren().size() == 0) {
|
||||
treeNode.setLastNode(true);
|
||||
}
|
||||
// 计算每一个节点的最子节点的数量
|
||||
List<TreeNode> children = parentTreeNode.getChildren();
|
||||
if (children.size() > 0) {
|
||||
int sum = 0;
|
||||
for (TreeNode treeNode2 : children) {
|
||||
sum += treeNode2.getLastNodeNum();
|
||||
}
|
||||
parentTreeNode.setLastNodeNum(sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据treeNodesMap转为treeNodesList
|
||||
*/
|
||||
private void initTreeNodeList() {
|
||||
if (treeNodesList.size() > 0) {
|
||||
return;
|
||||
}
|
||||
if (treeNodesMap.size() == 0) {
|
||||
return;
|
||||
}
|
||||
Iterator<TreeNode> iterator = treeNodesMap.values().iterator();
|
||||
TreeNode treeNode;
|
||||
while (iterator.hasNext()) {
|
||||
treeNode = iterator.next();
|
||||
if (treeNode.getParent() == null) {
|
||||
this.treeNodesList.add(treeNode);
|
||||
this.treeNodesList.addAll(treeNode.getAllChildren());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TreeNode> getTree() {
|
||||
return this.treeNodesList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TreeNode> getRoot() {
|
||||
List<TreeNode> rootList = new ArrayList<>();
|
||||
if (this.treeNodesList.size() > 0) {
|
||||
for (TreeNode node : treeNodesList) {
|
||||
if (node.getParent() == null) {
|
||||
rootList.add(node);
|
||||
Collections.sort(rootList, new OrdNamComparator());
|
||||
}
|
||||
}
|
||||
}
|
||||
return rootList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TreeNode getTreeNode(String nodeId) {
|
||||
return this.treeNodesMap.get(nodeId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义排序,按照orderNum排序
|
||||
*/
|
||||
class OrdNamComparator implements Comparator<TreeNode> {
|
||||
@Override
|
||||
public int compare(TreeNode t1, TreeNode t2) {
|
||||
if (t1.getOrderNum() > t2.getOrderNum()) {
|
||||
return 1;
|
||||
}
|
||||
if (t1.getOrderNum() < t2.getOrderNum()) {
|
||||
return -1;
|
||||
}
|
||||
return t1.getNodeName().compareTo(t2.getNodeName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
package com.adc.da.sys.utils.tree;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TreeNode {
|
||||
|
||||
/**
|
||||
* 树节点ID
|
||||
*/
|
||||
@JSONField(ordinal = 1)
|
||||
private String nodeId;
|
||||
/**
|
||||
* 树节点名称
|
||||
*/
|
||||
@JSONField(ordinal = 2)
|
||||
private String nodeName;
|
||||
/**
|
||||
* 父节点ID
|
||||
*/
|
||||
@JSONField(ordinal = 3)
|
||||
private String parentNodeId;
|
||||
/**
|
||||
* 节点在树中的排序号
|
||||
*/
|
||||
@JSONField(ordinal = 4)
|
||||
private int orderNum;
|
||||
/**
|
||||
* 节点所在的层级
|
||||
*/
|
||||
@JSONField(ordinal = 5)
|
||||
private int level;
|
||||
/**
|
||||
* 最子节点的数量
|
||||
*/
|
||||
@JSONField(ordinal = 6)
|
||||
private int lastNodeNum;
|
||||
|
||||
/**
|
||||
* 是否是最子节点
|
||||
*/
|
||||
@JSONField(ordinal = 7)
|
||||
private boolean lastNode;
|
||||
|
||||
/**
|
||||
* 当前节点的儿子节点
|
||||
*/
|
||||
@JSONField(ordinal = 8)
|
||||
private List<TreeNode> children = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 当前节点的完整路径
|
||||
*/
|
||||
@JSONField(ordinal = 9)
|
||||
private String completeName;
|
||||
|
||||
/**
|
||||
* 当前节点的父级节点
|
||||
* 转json的时候忽略此属性,因为此属性到前端无作用
|
||||
*/
|
||||
@JSONField(serialize = false)
|
||||
private TreeNode parent;
|
||||
|
||||
/**
|
||||
* 当前节点的子孙节点
|
||||
* 转json的时候忽略此属性,因为此属性到前端无作用
|
||||
*/
|
||||
@JSONField(serialize = false)
|
||||
private List<TreeNode> allChildren = new ArrayList<>();
|
||||
|
||||
public TreeNode(ITreeNode obj) {
|
||||
this.nodeId = obj.getNodeId();
|
||||
this.nodeName = obj.getNodeName();
|
||||
this.parentNodeId = obj.getNodeParentId();
|
||||
this.orderNum = obj.getOrderNum();
|
||||
this.level = obj.getNodeLevel();
|
||||
}
|
||||
|
||||
public TreeNode(TreeNode obj) {
|
||||
this.orderNum = obj.getOrderNum();
|
||||
this.level = obj.getLevel();
|
||||
this.lastNodeNum = obj.getLastNodeNum();
|
||||
}
|
||||
|
||||
public TreeNode() {
|
||||
}
|
||||
|
||||
public void addChild(TreeNode treeNode) {
|
||||
this.children.add(treeNode);
|
||||
}
|
||||
|
||||
public void removeChild(TreeNode treeNode) {
|
||||
this.children.remove(treeNode);
|
||||
}
|
||||
|
||||
public String getNodeId() {
|
||||
return nodeId;
|
||||
}
|
||||
|
||||
public void setNodeId(String nodeId) {
|
||||
this.nodeId = nodeId;
|
||||
}
|
||||
|
||||
public String getNodeName() {
|
||||
return nodeName;
|
||||
}
|
||||
|
||||
public void setNodeName(String nodeName) {
|
||||
this.nodeName = nodeName;
|
||||
}
|
||||
|
||||
public String getParentNodeId() {
|
||||
return parentNodeId;
|
||||
}
|
||||
|
||||
public void setParentNodeId(String parentNodeId) {
|
||||
this.parentNodeId = parentNodeId;
|
||||
}
|
||||
|
||||
public int getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public void setLevel(int level) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public TreeNode getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(TreeNode parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public List<TreeNode> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(List<TreeNode> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
public int getOrderNum() {
|
||||
return orderNum;
|
||||
}
|
||||
|
||||
public void setOrderNum(int orderNum) {
|
||||
this.orderNum = orderNum;
|
||||
}
|
||||
|
||||
public int getLastNodeNum() {
|
||||
return lastNodeNum;
|
||||
}
|
||||
|
||||
public void setLastNodeNum(int lastNodeNum) {
|
||||
this.lastNodeNum = lastNodeNum;
|
||||
}
|
||||
|
||||
public boolean isLastNode() {
|
||||
return lastNode;
|
||||
}
|
||||
|
||||
public void setLastNode(boolean lastNode) {
|
||||
this.lastNode = lastNode;
|
||||
}
|
||||
|
||||
public List<TreeNode> getAllChildren() {
|
||||
if (this.allChildren.isEmpty()) {
|
||||
for (TreeNode treeNode : this.children) {
|
||||
this.allChildren.add(treeNode);
|
||||
this.allChildren.addAll(treeNode.getAllChildren());
|
||||
}
|
||||
}
|
||||
return this.allChildren;
|
||||
}
|
||||
|
||||
public String getCompleteName() {
|
||||
return completeName;
|
||||
}
|
||||
|
||||
public void setCompleteName(String completeName) {
|
||||
this.completeName = completeName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.adc.da.sys.utils.treetool;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* List 排序 Comparator
|
||||
* @author david
|
||||
*/
|
||||
public class OrdNamComparator implements Comparator<TreeNode> {
|
||||
|
||||
@Override
|
||||
public int compare(TreeNode t1, TreeNode t2) {
|
||||
if (t1.getOrderNum() > t2.getOrderNum()) {
|
||||
return 1;
|
||||
}
|
||||
if (t1.getOrderNum() < t2.getOrderNum()) {
|
||||
return -1;
|
||||
}
|
||||
return t1.getNodeName().compareTo(t2.getNodeName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package com.adc.da.sys.utils.treetool;
|
||||
|
||||
import com.adc.da.sys.utils.treetool.annotation.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
|
||||
//import group.ipp.tree.util.annotation.TreeNodeLevel;
|
||||
//import group.ipp.tree.util.annotation.TreeNodeName;
|
||||
//import group.ipp.tree.util.annotation.TreeNodeOrder;
|
||||
//import group.ipp.tree.util.annotation.TreeNodeParentId;
|
||||
|
||||
/**
|
||||
* 构建树形建构
|
||||
*
|
||||
* @author David
|
||||
*/
|
||||
public class Tree<T> {
|
||||
/**
|
||||
* 用于存放treeNode的Map
|
||||
*/
|
||||
private LinkedHashMap<String, TreeNode> treeNodesMap = new LinkedHashMap<>();
|
||||
/**
|
||||
* 用于存放treeNode的list
|
||||
*/
|
||||
private List<TreeNode> treeNodesList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*
|
||||
* @param list
|
||||
*/
|
||||
public Tree(List<T> list) {
|
||||
initTreeNodeMap(list);
|
||||
initTreeNodeList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将List对象数据转为TreeNodeMap
|
||||
*
|
||||
* @param list
|
||||
*/
|
||||
private void initTreeNodeMap(List<T> list) {
|
||||
TreeNode treeNode;
|
||||
for (Object item : list) {
|
||||
treeNode = new TreeNode();
|
||||
treeNode.setNodeId(getFieldValue(item, "TreeNodeId"));
|
||||
treeNode.setNodeName(getFieldValue(item, "TreeNodeName"));
|
||||
treeNode.setParentNodeId(getFieldValue(item, "TreeNodeParentId"));
|
||||
if(StringUtils.isEmpty(getFieldValue(item, "TreeNodeLevel"))) {
|
||||
treeNode.setLevel(0);
|
||||
} else {
|
||||
treeNode.setLevel(Integer.parseInt(getFieldValue(item, "TreeNodeLevel")));
|
||||
}
|
||||
if(StringUtils.isEmpty(getFieldValue(item, "TreeNodeOrder"))) {
|
||||
treeNode.setOrderNum(0);
|
||||
} else {
|
||||
treeNode.setOrderNum(Integer.parseInt(getFieldValue(item, "TreeNodeOrder")));
|
||||
}
|
||||
treeNode.setLastNodeNum(1);
|
||||
treeNode.setData(item);
|
||||
treeNodesMap.put(treeNode.getNodeId(), treeNode);
|
||||
}
|
||||
Iterator<TreeNode> iterator = treeNodesMap.values().iterator();
|
||||
TreeNode parentTreeNode;
|
||||
while (iterator.hasNext()) {
|
||||
treeNode = iterator.next();
|
||||
if (StringUtils.isEmpty(treeNode.getParentNodeId())) {
|
||||
continue;
|
||||
}
|
||||
parentTreeNode = treeNodesMap.get(treeNode.getParentNodeId());
|
||||
if (parentTreeNode != null) {
|
||||
treeNode.setParent(parentTreeNode);
|
||||
parentTreeNode.addChild(treeNode);
|
||||
// 按照orderNum排序
|
||||
Collections.sort(parentTreeNode.getChildren(), new OrdNamComparator());
|
||||
// 判断这个节点是否是最子节点
|
||||
if (treeNode.getChildren().size() == 0) {
|
||||
treeNode.setLastNode(true);
|
||||
}
|
||||
// 计算每一个节点的最子节点的数量
|
||||
List<TreeNode> children = parentTreeNode.getChildren();
|
||||
if (children.size() > 0) {
|
||||
int sum = 0;
|
||||
for (TreeNode treeNode2 : children) {
|
||||
sum += treeNode2.getLastNodeNum();
|
||||
}
|
||||
parentTreeNode.setLastNodeNum(sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getFieldValue(Object obj, String type) {
|
||||
Class clz = obj.getClass();
|
||||
Field[] fields = clz.getDeclaredFields();
|
||||
String value = null;
|
||||
try {
|
||||
for(Field field : fields){
|
||||
field.setAccessible(true);
|
||||
switch (type){
|
||||
case "TreeNodeId":
|
||||
if(field.isAnnotationPresent(TreeNodeId.class)) {
|
||||
value = String.valueOf(field.get(obj));
|
||||
}
|
||||
break;
|
||||
case "TreeNodeParentId":
|
||||
if(field.isAnnotationPresent(TreeNodeParentId.class)) {
|
||||
value = String.valueOf(field.get(obj));
|
||||
}
|
||||
break;
|
||||
case "TreeNodeName":
|
||||
if(field.isAnnotationPresent(TreeNodeName.class)) {
|
||||
value = String.valueOf(field.get(obj));
|
||||
}
|
||||
break;
|
||||
case "TreeNodeOrder":
|
||||
if(field.isAnnotationPresent(TreeNodeOrder.class)) {
|
||||
value = String.valueOf(field.get(obj));
|
||||
}
|
||||
break;
|
||||
case "TreeNodeLevel":
|
||||
if(field.isAnnotationPresent(TreeNodeLevel.class)) {
|
||||
value = String.valueOf(field.get(obj));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据treeNodesMap转为treeNodesList
|
||||
*/
|
||||
private void initTreeNodeList() {
|
||||
if (treeNodesList.size() > 0) {
|
||||
return;
|
||||
}
|
||||
if (treeNodesMap.size() == 0) {
|
||||
return;
|
||||
}
|
||||
Iterator<TreeNode> iterator = treeNodesMap.values().iterator();
|
||||
TreeNode treeNode;
|
||||
while (iterator.hasNext()) {
|
||||
treeNode = iterator.next();
|
||||
if (treeNode.getParent() == null) {
|
||||
this.treeNodesList.add(treeNode);
|
||||
this.treeNodesList.addAll(treeNode.getAllChildren());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<TreeNode> getTree() {
|
||||
return this.treeNodesList;
|
||||
}
|
||||
|
||||
public List<TreeNode> getRoot() {
|
||||
List<TreeNode> rootList = new ArrayList<>();
|
||||
if (this.treeNodesList.size() > 0) {
|
||||
for (TreeNode node : treeNodesList) {
|
||||
if (node.getParent() == null) {
|
||||
rootList.add(node);
|
||||
Collections.sort(rootList, new OrdNamComparator());
|
||||
}
|
||||
}
|
||||
}
|
||||
return rootList;
|
||||
}
|
||||
|
||||
public TreeNode getTreeNode(String nodeId) {
|
||||
return this.treeNodesMap.get(nodeId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
package com.adc.da.sys.utils.treetool;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author David
|
||||
*/
|
||||
public class TreeNode {
|
||||
|
||||
/**
|
||||
* 树节点ID
|
||||
*/
|
||||
@JSONField(ordinal = 1)
|
||||
private String nodeId;
|
||||
/**
|
||||
* 树节点名称
|
||||
*/
|
||||
@JSONField(ordinal = 2)
|
||||
private String nodeName;
|
||||
/**
|
||||
* 父节点ID
|
||||
*/
|
||||
@JSONField(ordinal = 3)
|
||||
private String parentNodeId;
|
||||
/**
|
||||
* 节点在树中的排序号
|
||||
*/
|
||||
@JSONField(ordinal = 4)
|
||||
private int orderNum;
|
||||
/**
|
||||
* 节点所在的层级
|
||||
*/
|
||||
@JSONField(ordinal = 5)
|
||||
private int level;
|
||||
/**
|
||||
* 最子节点的数量
|
||||
*/
|
||||
@JSONField(ordinal = 6)
|
||||
private int lastNodeNum;
|
||||
|
||||
/**
|
||||
* 是否是最子节点
|
||||
*/
|
||||
@JSONField(ordinal = 7)
|
||||
private boolean lastNode;
|
||||
|
||||
private Object data;
|
||||
|
||||
/**
|
||||
* 当前节点的儿子节点
|
||||
*/
|
||||
@JSONField(ordinal = 8)
|
||||
private List<TreeNode> children = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 当前节点的完整路径
|
||||
*/
|
||||
@JSONField(ordinal = 9)
|
||||
private String completeName;
|
||||
|
||||
/**
|
||||
* 当前节点的父级节点
|
||||
* 转json的时候忽略此属性,因为此属性到前端无作用
|
||||
*/
|
||||
@JSONField(serialize = false)
|
||||
private TreeNode parent;
|
||||
|
||||
/**
|
||||
* 当前节点的子孙节点
|
||||
* 转json的时候忽略此属性,因为此属性到前端无作用
|
||||
*/
|
||||
@JSONField(serialize = false)
|
||||
private List<TreeNode> allChildren = new ArrayList<>();
|
||||
|
||||
private int standFlag;
|
||||
|
||||
public TreeNode(TreeNode obj) {
|
||||
this.orderNum = obj.getOrderNum();
|
||||
this.level = obj.getLevel();
|
||||
this.lastNodeNum = obj.getLastNodeNum();
|
||||
}
|
||||
|
||||
public TreeNode() {
|
||||
}
|
||||
|
||||
public void addChild(TreeNode treeNode) {
|
||||
this.children.add(treeNode);
|
||||
}
|
||||
|
||||
public void removeChild(TreeNode treeNode) {
|
||||
this.children.remove(treeNode);
|
||||
}
|
||||
|
||||
public String getNodeId() {
|
||||
return nodeId;
|
||||
}
|
||||
|
||||
public void setNodeId(String nodeId) {
|
||||
this.nodeId = nodeId;
|
||||
}
|
||||
|
||||
public String getNodeName() {
|
||||
return nodeName;
|
||||
}
|
||||
|
||||
public void setNodeName(String nodeName) {
|
||||
this.nodeName = nodeName;
|
||||
}
|
||||
|
||||
public String getParentNodeId() {
|
||||
return parentNodeId;
|
||||
}
|
||||
|
||||
public void setParentNodeId(String parentNodeId) {
|
||||
this.parentNodeId = parentNodeId;
|
||||
}
|
||||
|
||||
public int getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public void setLevel(int level) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public TreeNode getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(TreeNode parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public List<TreeNode> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(List<TreeNode> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
public int getOrderNum() {
|
||||
return orderNum;
|
||||
}
|
||||
|
||||
public void setOrderNum(int orderNum) {
|
||||
this.orderNum = orderNum;
|
||||
}
|
||||
|
||||
public int getLastNodeNum() {
|
||||
return lastNodeNum;
|
||||
}
|
||||
|
||||
public void setLastNodeNum(int lastNodeNum) {
|
||||
this.lastNodeNum = lastNodeNum;
|
||||
}
|
||||
|
||||
public boolean isLastNode() {
|
||||
return lastNode;
|
||||
}
|
||||
|
||||
public void setLastNode(boolean lastNode) {
|
||||
this.lastNode = lastNode;
|
||||
}
|
||||
|
||||
public List<TreeNode> getAllChildren() {
|
||||
if (this.allChildren.isEmpty()) {
|
||||
for (TreeNode treeNode : this.children) {
|
||||
this.allChildren.add(treeNode);
|
||||
this.allChildren.addAll(treeNode.getAllChildren());
|
||||
}
|
||||
}
|
||||
return this.allChildren;
|
||||
}
|
||||
|
||||
public String getCompleteName() {
|
||||
return completeName;
|
||||
}
|
||||
|
||||
public void setCompleteName(String completeName) {
|
||||
this.completeName = completeName;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(Object data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public int getStandFlag() {
|
||||
return standFlag;
|
||||
}
|
||||
|
||||
public void setStandFlag(int standFlag) {
|
||||
this.standFlag = standFlag;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.adc.da.sys.utils.treetool.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @author david
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD})
|
||||
@Documented
|
||||
public @interface TreeNodeId {
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.adc.da.sys.utils.treetool.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @author david
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD})
|
||||
@Documented
|
||||
public @interface TreeNodeLevel {
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.adc.da.sys.utils.treetool.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @author david
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD})
|
||||
@Documented
|
||||
public @interface TreeNodeName {
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.adc.da.sys.utils.treetool.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @author david
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD})
|
||||
@Documented
|
||||
public @interface TreeNodeOrder {
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.adc.da.sys.utils.treetool.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @author david
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD})
|
||||
@Documented
|
||||
public @interface TreeNodeParentId {
|
||||
}
|
||||
Reference in New Issue
Block a user