<?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.dromara.system.mapper.SysDeptMapper">

    <resultMap type="org.dromara.system.domain.vo.SysDeptVo" id="SysDeptResult">
    </resultMap>

    <sql id="selectDeptVo">
        select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, d.del_flag, d.create_by, d.create_time
        from sys_dept d
    </sql>

    <select id="selectDeptList" resultMap="SysDeptResult">
        select * from sys_dept ${ew.getCustomSqlSegment}
    </select>

    <select id="selectDeptById" resultMap="SysDeptResult">
        select * from sys_dept where del_flag = '0' and dept_id = #{deptId}
    </select>

    <select id="selectDeptListByRoleId" resultType="Long">
        select d.dept_id
        from sys_dept d
            left join sys_role_dept rd on d.dept_id = rd.dept_id
        where rd.role_id = #{roleId}
            <if test="deptCheckStrictly">
                and d.dept_id not in (select d.parent_id from sys_dept d inner join sys_role_dept rd on d.dept_id = rd.dept_id and rd.role_id = #{roleId})
            </if>
        order by d.parent_id, d.order_num
    </select>



    <select id="selectDeptByParentId" parameterType="Long" resultMap="SysDeptResult">
        <include refid="selectDeptVo"/>
        where parent_id = #{parentId} order by create_time desc limit 1
    </select>
    <select id="selectDeptListChildren" resultMap="SysDeptResult" parameterType="SysDept">
        select * from sys_dept
        where del_flag = '0'
        AND parent_id = #{parentId}
        AND status = '0'
    </select>
    <select id="selectDeptOldList" resultMap="SysDeptResult">
        select * from sys_dept
        where del_flag = '0'
        <if test="parentId != null and parentId != 0">
            AND parent_id = #{parentId}
        </if>
        <if test="deptName != null and deptName != ''">
            AND dept_name like concat('%', #{deptName}, '%')
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        <if test="tenantId != null and tenantId != ''">
            and tenant_id = #{tenantId}
        </if>
        order by parent_id, order_num
    </select>
    <select id="selectDeptListMapper" resultMap="SysDeptResult">
        <include refid="selectDeptVo"/>
        where d.del_flag = '0'
        <if test="parentId != null and parentId != 0">
            AND (dept_id = #{deptId} or parent_id = #{parentId})
        </if>
        <!--<if test="parentId != null and parentId != 0">
			AND parent_id = #{parentId}
		</if>-->
        <if test="deptName != null and deptName != ''">
            AND dept_name like concat('%', #{deptName}, '%')
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        order by d.parent_id, d.order_num
    </select>

    <select id="selectDeptByIdNoTenantId" resultMap="SysDeptResult">
        select * from sys_dept where del_flag = '0' and dept_id = #{deptId}
    </select>

    <insert id="insertDeptList">
        insert into sys_dept(parent_id,dept_name,ancestors,order_num,status,create_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.parentId},#{item.deptName},#{item.ancestors},#{item.orderNum},#{item.status},sysdate())
        </foreach>
    </insert>

    <update id="updateDeptIsStatus" parameterType="java.lang.Long">
        update sys_dept set status = '1' where parent_id = #{parentId}
    </update>

</mapper>
