<?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="com.elephant.framework.galaxy.aroad.module.system.mapper.DeptMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.elephant.framework.galaxy.aroad.module.system.entity.Dept">
        <id column="dept_id" property="deptId" />
        <result column="name" property="name" />
        <result column="sort" property="sort" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="del_flag" property="delFlag" />
        <result column="parent_id" property="parentId" />
        <result column="tenant_id" property="tenantId" />
    </resultMap>

    <!--关联查询部门列表-->
    <select id="listDepts" resultType="com.elephant.framework.galaxy.aroad.module.system.entity.Dept">
        SELECT
            t.*
        FROM
            sys_dept t
                LEFT JOIN sys_dept_relation dr ON t.dept_id = dr.descendant
        WHERE dr.ancestor = 0
    </select>

    <select id="getDeptById" resultMap="BaseResultMap">
        select dept_id,name,parent_id,type from sys_dept
        where del_flag = 0 and dept_id = #{deptId}
    </select>

    <select id="getDeptByParentId" resultMap="BaseResultMap">
        select dept_id,name,parent_id from sys_dept
        where del_flag = 0 and parent_id = #{parentId}
    </select>

    <select id="getDeptByParentIds" resultMap="BaseResultMap">
        select dept_id,name,parent_id from sys_dept
        where del_flag = 0
        and
        parent_id in
        <foreach collection="parentIds" item="parentId" index="index" open="(" close=")" separator=",">
            #{parentId}
        </foreach>
    </select>

</mapper>
