递归查询机构

递归查询机构及其子机构数据

一、工具类代码

package com.ruoyi.school.utils;

import com.ruoyi.school.mapper.AntiForgetWordRecMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @Description 递归查询机构id工具类
 * @ClassName RecursionUtil
 * @Author gzy
 * @Date 2025/5/16 9:59
 * @Version 1.0
 **/
@Component
public class RecursionUtil {

    @Autowired
    private AntiForgetWordRecMapper antiForgetWordRecMapper;

    //###################################【递归获取机构以及子机构id集合】#########################################################

    /**
     * 递归获取机构以及子机构id集合
     * @param institutionId
     * @return
     */
    //递归1
    public List<Long> queryAllSubInstitutionIds(Long institutionId) {
        List<Long> subInstitutionIds = new ArrayList<>();
        querySubInstitutionIds(institutionId, subInstitutionIds);
        return subInstitutionIds;
    }

    //递归2
    private void querySubInstitutionIds(Long institutionId, List<Long> subInstitutionIds) {
        // 添加当前机构ID
        subInstitutionIds.add(institutionId);
        // 获取当前机构的子机构
        List<Long> childrenIds = deptList(institutionId);
        if (childrenIds!=null && childrenIds.size()>0){
            // 递归查询子机构的子机构
            for (Long deptId : childrenIds) {
                querySubInstitutionIds(deptId, subInstitutionIds);
            }
        }
    }
    //递归3
    private  List<Long> deptList(Long deptId){
            List<Long> list=antiForgetWordRecMapper.getDeptList(deptId);
        return  list;
    }


    /**
     * 获取机构所有上级机构id集合
     * @param deptId
     * @return
     */
    public List<Long> getParentDeptIds(Long deptId){
        String ancestors=antiForgetWordRecMapper.getAncestors(deptId);
        String[] parentIds = ancestors.split(","); // 结果: [0, 1, 2]
        // 完整上级ID列表(包括自身)
        List<Long> deptIds = new ArrayList<>();
        for (String id : parentIds) {
            deptIds.add(Long.parseLong(id));
        }
        deptIds.add(deptId); // 添加自身ID
        return deptIds;
    }
}

二、查询机构下子机构sql

/**
     * 查询机构下子机构
     * @param deptId
     * @return
     */
    List<Long> getDeptList(Long deptId);

<!--查询机构下子机构-->
    <select id="getDeptList" resultType="java.lang.Long">
        select dept_id
        from sys_dept
        where del_flag = 0
          and parent_id = #{deptId}
    </select>

三、根据机构id查询机构上级机构集合

/**
     * 根据机构id查询机构上级机构集合
     * @param deptId
     * @return
     */
    String getAncestors(Long deptId);

<!--根据机构id查询机构上级机构集合-->
<select id="getAncestors" resultType="java.lang.String">
        select ancestors
        from sys_dept
        where del_flag = 0
          and dept_id = #{deptId}
    </select>

四、数据库表结构

1.建表语句

CREATE TABLE `sys_dept` (
  `dept_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '部门id',
  `parent_id` bigint(20) DEFAULT '0' COMMENT '父部门id',
  `ancestors` varchar(50) DEFAULT '' COMMENT '祖级列表',
  `dept_name` varchar(30) DEFAULT '' COMMENT '部门名称',
  `order_num` int(11) DEFAULT '0' COMMENT '显示顺序',
  `leader` varchar(20) DEFAULT NULL COMMENT '负责人',
  `phone` varchar(11) DEFAULT NULL COMMENT '联系电话',
  `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
  `status` char(1) DEFAULT '0' COMMENT '部门状态(0正常 1停用)',
  `del_flag` char(1) DEFAULT '0' COMMENT '删除标志(0代表存在 2代表删除)',
  `create_by` varchar(64) DEFAULT '' COMMENT '创建者',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_by` varchar(64) DEFAULT '' COMMENT '更新者',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `audit_status` tinyint(1) DEFAULT '0' COMMENT '审核状态(0、审核中,1、已通过,2、未通过)',
  `fail_reason` varchar(255) DEFAULT NULL COMMENT '失败原因',
  `license_path` varchar(255) DEFAULT NULL COMMENT '营业执照地址',
  `license_code` varchar(255) DEFAULT NULL COMMENT '信用代码',
  `address` varchar(255) DEFAULT NULL COMMENT '地址',
  `owner_identity` varchar(255) DEFAULT NULL COMMENT '法人身份证号码',
  `legal_card_front` varchar(255) DEFAULT NULL COMMENT '法人身份证正面(人像面)',
  `legal_card_back` varchar(255) DEFAULT NULL COMMENT '法人身份证背面(国徽面)',
  `paid` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '支付状态。(0、未支付,1、已支付)',
  `school_type` varchar(255) DEFAULT NULL COMMENT '机构类型(1、学员服务中心;2、平台;3、交付中心)',
  `school_no` varchar(255) DEFAULT NULL COMMENT '机构编码',
  `dept_name_real` varchar(255) DEFAULT NULL COMMENT '机构真实名称',
  `lessons` int(11) DEFAULT '0' COMMENT '剩余60分钟课程(节)',
  `gifts` int(11) DEFAULT '0' COMMENT '剩余60分钟赠送课程(节)',
  `lessons30` int(11) DEFAULT '0' COMMENT '剩余30分钟课程(节)',
  `gifts30` int(11) DEFAULT '0' COMMENT '剩余30分钟赠送课程(节)',
  `sort` int(11) DEFAULT '0' COMMENT '排序',
  `expiration` varchar(255) DEFAULT NULL COMMENT '到期日期',
  `account_price` decimal(10,2) DEFAULT NULL COMMENT '充值金额(元)',
  `account_lessons60` int(11) DEFAULT '0' COMMENT '累计购买60分钟课程(节)',
  `account_lessons30` int(11) DEFAULT '0' COMMENT '累计购买30分钟课程(节)',
  `account_gifts60` int(11) DEFAULT '0' COMMENT '累计赠送60分钟课程',
  `account_gifts30` int(11) DEFAULT '0' COMMENT '累计赠送30分钟课程',
  `referrer` varchar(255) DEFAULT NULL COMMENT '推荐人',
  `referrer_phone` varchar(255) DEFAULT NULL COMMENT '推荐人手机号',
  `review_opinion` varchar(255) DEFAULT NULL COMMENT '评审意见',
  `identity` varchar(255) DEFAULT NULL COMMENT '身份证号',
  `personal_photo` varchar(255) DEFAULT NULL COMMENT '个人照片',
  `group_photo` varchar(255) DEFAULT NULL COMMENT '集体照片',
  `contract` varchar(255) DEFAULT NULL COMMENT '合同',
  `coach_fee` decimal(10,2) DEFAULT NULL COMMENT '教练费',
  `dept_url` varchar(255) DEFAULT NULL COMMENT '部门url',
  `h5_dept_url` varchar(255) DEFAULT NULL COMMENT 'h5域名',
  `actor` varchar(255) DEFAULT NULL COMMENT 'logo',
  PRIMARY KEY (`dept_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=294 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='部门表';

2.表结构:

image

posted @ 2025-05-23 11:16  青喺半掩眉砂  阅读(32)  评论(0)    收藏  举报