java树结构递归编码(innercode)

1、数据表

如下:

有一个设备类别表,分类具有树结构的层级关系

id为本身id,parent_id为父节点id,type_name 类别名称, type_level 类别层级, type_code 类别编码。

把type_code根据层级编码为innercode的形式,使type_code也具有层级关系

2.实体类

2.1entity

java 复制代码
package org.jeecg.modules.equipment.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.jeecg.common.system.base.entity.BaseSearchDTO;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.data.annotation.Transient;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 设备分类
 * @Author: jeecg-boot
 * @Date: 2022-08-21
 * @Version: V1.0
 */
@Data
@TableName("tb_equipment_type")
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@ApiModel(value = "tb_equipment_type对象", description = "设备分类")
public class TbEquipmentType extends BaseSearchDTO {

    /**
     * 主键
     */
    @TableId(type = IdType.ASSIGN_ID)
    @ApiModelProperty(value = "主键")
    private String id;
    /**
     * 父ID
     */
    @Excel(name = "父ID", width = 15)
    @ApiModelProperty(value = "父ID")
    private String parentId;
    /**
     * 分类名称
     */
    @Excel(name = "分类名称", width = 15)
    @ApiModelProperty(value = "分类名称")
    private String typeName;
    /**
     * 分类层级
     */
    @Excel(name = "分类层级", width = 15)
    @ApiModelProperty(value = "分类层级")
    private String typeLevel;
    /**
     * 分类编码
     */
    @Excel(name = "分类编码", width = 15)
    @ApiModelProperty(value = "分类编码")
    private String typeCode;
    /**
     * 创建人
     */
    @ApiModelProperty(value = "创建人")
    private String createBy;
    /**
     * 创建日期
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "创建日期")
    private Date createTime;
    /**
     * 更新人
     */
    @ApiModelProperty(value = "更新人")
    private String updateBy;
    /**
     * 更新日期
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "更新日期")
    private Date updateTime;

    /**
     * 子集
     */
    @Transient
    @ApiModelProperty(value = "子集")
    @TableField(exist = false)
    private List<TbEquipmentType> children = new ArrayList<>();
}

2.2dto

java 复制代码
package org.jeecg.modules.equipment.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecg.common.system.base.entity.BaseSearchDTO;
import org.jeecg.common.system.base.support.Condition;
import org.jeecg.common.system.base.support.Match;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.data.annotation.Transient;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 设备分类DTO
 * @Author: jeecg-boot
 * @Date: 2022-08-21
 * @Version: V1.0
 */
@Data
public class EquipmentTypeDTO extends BaseSearchDTO {

    /**
     * 分类id
     */
    @ApiModelProperty(value = "主键")
    private String id;

    /**
     * 分类名称
     */
    @ApiModelProperty(value = "分类名称")
    @Condition(match = Match.LIKE)
    private String typeName;

    /**
     * 创建日期
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "创建日期")
    private Date createTime;

    /**
     * 父ID
     */
    @Excel(name = "父ID", width = 15)
    @ApiModelProperty(value = "父ID")
    private String parentId;
    /**
     * 分类父级名称
     */
    @ApiModelProperty(value = "分类父级名称")
    private String parentName;

    /**
     * 分类层级
     */
    @ApiModelProperty(value = "分类层级")
    private String typeLevel;

    /**
     * 子集
     */
    @Transient
    @ApiModelProperty(value = "子集")
    private List<EquipmentTypeDTO> children = new ArrayList<>();

}

3、获取树结构 及 处理编码

3.1controller

java 复制代码
package org.jeecg.modules.equipment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.equipment.dto.EquipmentTypeDTO;
import org.jeecg.modules.equipment.entity.TbEquipmentType;
import org.jeecg.modules.equipment.service.ITbEquipmentTypeService;
import org.jeecg.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 设备分类
 * @Author: jeecg-boot
 * @Date: 2022-08-21
 * @Version: V1.0
 */
@Api(tags = "6.1.0设备分类")
@RestController
@RequestMapping("/equipment/tbEquipmentType")
@Slf4j
public class TbEquipmentTypeController extends JeecgController<TbEquipmentType, ITbEquipmentTypeService> {
    @Autowired
    private ITbEquipmentTypeService tbEquipmentTypeService;

/**
     * 获取树结构
     *
     * @param dto
     * @return
     */
    @PostMapping("/tree")
    @ApiOperation(value = "设备分类树")
    public Result<EquipmentTypeDTO> tree(@ApiParam(value = "查询条件")
                                         @RequestBody EquipmentTypeDTO dto) {
        List<EquipmentTypeDTO> tree = tbEquipmentTypeService.getTree(dto);
        return getSuccess(tree);
    }

    /**
     * 设置innercode码
     *
     */
    @ApiOperation(value = "设置innercode码", notes = "设置innercode码")
    @GetMapping(value = "/initCode")
    public Result<?> initCode() {
        tbEquipmentTypeService.initCode();
        return Result.OK("处理完成");
    }



}

3.2service

java 复制代码
package org.jeecg.modules.equipment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.system.base.service.BaseService;
import org.jeecg.common.util.StringUtil;
import org.jeecg.modules.equipment.dto.EquipmentTypeDTO;
import org.jeecg.modules.equipment.entity.TbEquipmentType;
import org.jeecg.modules.equipment.mapper.TbEquipmentTypeMapper;
import org.jeecg.modules.equipment.service.ITbEquipmentTypeService;
import org.jeecg.utils.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 设备分类
 * @Author: jeecg-boot
 * @Date: 2022-08-21
 * @Version: V1.0
 */
@Service
public class TbEquipmentTypeServiceImpl extends ServiceImpl<TbEquipmentTypeMapper, TbEquipmentType> implements ITbEquipmentTypeService {
    @Resource
    TbEquipmentTypeMapper mapper;
    @Autowired
    private BaseService<TbEquipmentType> baseService;

    @Override
    public List<EquipmentTypeDTO> selectTree(EquipmentTypeDTO dto) {
        return mapper.selectTree(dto);
    }

    @Override
    public List<EquipmentTypeDTO> getTree(EquipmentTypeDTO dto) {
        List<EquipmentTypeDTO> returndemo = this.selectTree(dto);
        List<EquipmentTypeDTO> tree = Collections.synchronizedList(new ArrayList<>());
        String id = "1";
        if (StringUtil.isNotEmpty(dto.getId())) {
            id = dto.getId();
        }

        if (StringUtil.isNotEmpty(returndemo)) {
            String finalId = id;
            returndemo.parallelStream().forEach(x -> {
                if (finalId.equals(x.getId())) {
                    //现在做全局递归,之后更改
                    tree.add(findChildren(x, returndemo));
                }
            });
        }

        //根据日期进行升序排序
        for (EquipmentTypeDTO e : tree) {
            e.setChildren(sort(e.getChildren()));
        }
        List<EquipmentTypeDTO> treeAsce = sort(tree);
        return treeAsce;
    }


    private List<EquipmentTypeDTO> sort(List<EquipmentTypeDTO> source) {
        List<EquipmentTypeDTO> treeAsce
                = source.stream().sorted(Comparator.comparing(EquipmentTypeDTO::getCreateTime))
                .collect(Collectors.toList());

        for (EquipmentTypeDTO e : source) {
            if (e.getChildren().size() > 0) {
                e.setChildren(sort(e.getChildren()));
            }
        }
        return treeAsce;
    }

    /**
     * @param typeDTO .
     * @param lists   .
     * @return .
     */
    private static EquipmentTypeDTO findChildren(EquipmentTypeDTO typeDTO, List<EquipmentTypeDTO> lists) {
        lists.parallelStream().forEach(y -> {
            if (typeDTO.getId().equals(y.getParentId())) {
                if (typeDTO.getChildren() == null) {
                    typeDTO.setChildren(new ArrayList<EquipmentTypeDTO>());
                }
                typeDTO.getChildren().add(findChildren(y, lists));
            }
        });
        return typeDTO;
    }

    @Override
    public void initCode() {
        EquipmentTypeDTO dto = new EquipmentTypeDTO();
        List<EquipmentTypeDTO> treeList = getTree(dto);

        dealInnerCode(treeList, true);
    }

    public void dealInnerCode(List<EquipmentTypeDTO> treeList, Boolean isTop) {

        for (int i = 0; i < treeList.size(); i++) {
            String thisId = treeList.get(i).getId();
            String pId = treeList.get(i).getParentId();
            TbEquipmentType tbEquipmentType = this.getById(thisId);
            if (isTop) {
                tbEquipmentType.setTypeCode("0");
            } else {
                tbEquipmentType.setTypeCode(getCodeById(pId) + (i + 1));
            }
            //更新
            this.updateById(tbEquipmentType);
            List<EquipmentTypeDTO> childrenList = treeList.get(i).getChildren();
            if (StringUtil.isNotEmpty(childrenList)) {
                dealInnerCode(childrenList, false);
            }
        }

    }

    public String getCodeById(String id) {
        TbEquipmentType tbEquipmentType = this.getById(id);
        return tbEquipmentType.getTypeCode();
    }

}

3.3mapper

3.3.1mapper

java 复制代码
package org.jeecg.modules.equipment.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.jeecg.modules.equipment.dto.EquipmentTypeDTO;
import org.jeecg.modules.equipment.entity.TbEquipmentType;

import java.util.List;

/**
 * @Description: 设备分类
 * @Author: jeecg-boot
 * @Date: 2022-08-21
 * @Version: V1.0
 */
public interface TbEquipmentTypeMapper extends BaseMapper<TbEquipmentType> {

    List<EquipmentTypeDTO> selectTree(@Param("dto") EquipmentTypeDTO dto);

}

3.3.2xml

XML 复制代码
<?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="org.jeecg.modules.equipment.mapper.TbEquipmentTypeMapper">

    <!-- 树结构-查父节点的名字 -->
    <select id="selectTree"
            parameterType="Object"
            resultType="org.jeecg.modules.equipment.dto.EquipmentTypeDTO">
        SELECT t1.type_name AS parent_name,
        t.*
        FROM TB_EQUIPMENT_TYPE t
        LEFT JOIN TB_EQUIPMENT_TYPE t1 ON t.parent_id = t1.id
        WHERE 1 = 1
        <if test="dto.typeName !=null and dto.typeName != ''">
            AND t.type_name like concat(concat('%',#{dto.typeName}),'%')
        </if>
        ORDER BY
        t1.CREATE_TIME,t.CREATE_TIME
    </select>

</mapper>
相关推荐
代码雕刻家7 分钟前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
一个闪现必杀技10 分钟前
Python入门--函数
开发语言·python·青少年编程·pycharm
Fan_web14 分钟前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
龙图:会赢的17 分钟前
[C语言]--编译和链接
c语言·开发语言
IT学长编程1 小时前
计算机毕业设计 玩具租赁系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·玩具租赁系统
莹雨潇潇1 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
杨哥带你写代码1 小时前
足球青训俱乐部管理:Spring Boot技术驱动
java·spring boot·后端
XKSYA(小巢校长)2 小时前
NatGo我的世界联机篇
开发语言·php
Cons.W2 小时前
Codeforces Round 975 (Div. 1) C. Tree Pruning
c语言·开发语言·剪枝
憧憬成为原神糕手2 小时前
c++_ 多态
开发语言·c++