递归--列表树形结构后端逻辑

  1. 查看部门列表的树形结构
/**
    * 部门树形结构
    * @return
    */
   public TreeVo deptTreeVo() {
       public TreeVo deptTreeVo() {
       //获取根节点树形 100000000000000
       String parentDeptNo = SuperConstant.ROOT_DEPT_PARENT_ID;
       //构建查询条件
       DeptDto param = DeptDto.builder()
               .dataState(SuperConstant.DATA_STATE_0)
               .parentDeptNo(NoProcessing.processString(parentDeptNo))
               .build();
       //查询部门列表所有数据
       List<Dept> deptList = deptMapper.getList(param);
       if(EmptyUtil.isNullOrEmpty(deptList)){
           throw new BaseException("部门数据没有定义");
       }
       //找根节点
       Dept rootDept = deptList.stream().filter(d -> SuperConstant.ROOT_DEPT_PARENT_ID.
               equals(d.getParentDeptNo())).collect(Collectors.toList()).get(0);
       //返回的部门数据,需要将每次返回的部门列表treeItemVoList封装到TreeVo
       List<TreeItemVo> treeItemVoList = new ArrayList<>();
       //递归调用
       recursionTreeItem(treeItemVoList,rootDept,deptList);

       return TreeVo.builder().items(treeItemVoList).build();
   }    

/**
    * 递归调用拼装数据
    * @param treeItemVoList  封装返回的数据
    * @param rootDept  当前部门
    * @param deptList  部门列表(全部数据)
    */
   private void recursionTreeItem(List<TreeItemVo> treeItemVoList, Dept rootDept, List<Dept> deptList) {
       //构建item对象
       TreeItemVo treeItemVo = TreeItemVo.builder().id(rootDept.getDeptNo()).label(rootDept.getDeptName()).build();
       //获得当前部门下的子部门
       List<Dept> childrenDept = deptList.stream()
               .filter(n -> n.getParentDeptNo().equals(rootDept.getDeptNo()))
               .collect(Collectors.toList());
       //如果子部门不为空,则继续递归调用
       if(!EmptyUtil.isNullOrEmpty(childrenDept)){
           ArrayList<TreeItemVo> listChildren = Lists.newArrayList();
           //子部门列表
           childrenDept.forEach(dept -> {
               this.recursionTreeItem(listChildren,dept,deptList);
           });
           treeItemVo.setChildren(listChildren);
       }
       treeItemVoList.add(treeItemVo);
   }

//Mapper映射文件
   <select id="getList" resultType="com.zzyl.vo.DeptVo">
       select
       d.id, d.parent_dept_no, d.dept_no, d.dept_name, d.sort_no, d.data_state, d.create_time, d.update_time,
       d.create_by, d.update_by, d.remark, d.leader_id
       , u.real_name as leader_name,DATE_FORMAT(d.create_time,'%Y-%m-%d') as create_day
       from sys_dept d
       left join sys_user u on u.id = leader_id
       <where>
           <if test="deptName!=null and deptName!=''">
               and d.dept_name like concat('%',#{deptName},'%')
           </if>
           <if test="parentDeptNo!=null and parentDeptNo!=''">
           //匹配当前部门的所有子部门编号,如100000001 --> 100000001000000,100000002000000,100000001001000,100000001001001
               and d.parent_dept_no like concat(#{parentDeptNo},'%')
           </if>
           <if test="dataState!=null and dataState!=''">
               and d.data_state=#{dataState}
           </if>
       </where>
       order by d.sort_no asc, d.create_time desc
   </select>

在这里插入图片描述

  1. 新增部门,需要生成新的部门编号
	/**
     * 新增部门
     * @param deptDto
     */
    public void createDept(DeptDto deptDto) {
        //转换对象
        Dept dept = BeanUtil.toBean(deptDto, Dept.class);
        //根据父部门编号,构建部门编号
        String deptNo = createDeptNo(deptDto.getParentDeptNo());
        dept.setDeptNo(deptNo);

        //保存
        int flag = deptMapper.insertDept(dept);

        if(flag != 1){
            throw new BaseException("保存部门失败");
        }
    }
	/**
     * 构建部门编号
     * @param parentDeptNo
     * @return
     */
    private String createDeptNo(String parentDeptNo) {
        //部门层级不能超过4级
        if(NoProcessing.processString(parentDeptNo).length() /3 == 5){
            throw new BaseException("部门创建不能超过4级");
        }
        //构建部门编号,有两种情况,当前父部门编号,有子部门:在已有的子部门的基础上累加  |  没有子部门:新增子部门编号
        DeptDto deptDto = DeptDto.builder().parentDeptNo(parentDeptNo).build();
        List<DeptVo> deptVoList = getList(deptDto);
        //无下属节点则创建下属节点
        if(EmptyUtil.isNullOrEmpty(deptVoList)){
            //没有子部门:新增子部门编号
            return NoProcessing.createNo(parentDeptNo,false);
        }else {
            //有下属节点则累加下属节点
            Long deptNo = deptVoList.stream().map(dept -> {
                //找到最大值加1
                return Long.valueOf(dept.getDeptNo());
            }).max(Comparator.comparing(i -> i)).get();
            return NoProcessing.createNo(String.valueOf(deptNo),true);
        }
    }
  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值