<?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.springblade.modules.baseinfo.org.mapper.OrgMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="orgResultMap" type="org.springblade.modules.baseinfo.org.entity.OrgEntity">
        <result column="tenant_id" property="tenantId"/>
        <result column="parent_id" property="parentId"/>
        <result column="ancestors" property="ancestors"/>
        <result column="dept_name" property="deptName"/>
        <result column="full_name" property="fullName"/>
        <result column="remark" property="remark"/>

    </resultMap>

    <resultMap id="orgVoResultMap" type="org.springblade.modules.baseinfo.org.vo.OrgInfoVO">
        <result column="tenant_id" property="tenantId"/>
        <result column="parent_id" property="parentId"/>
        <result column="ancestors" property="ancestors"/>
        <result column="dept_name" property="deptName"/>
        <result column="full_name" property="fullName"/>
        <result column="remark" property="remark"/>
        <result column="reg_type" property="regType"/>
    </resultMap>

    <select id="selectPage" resultMap="orgVoResultMap">
        SELECT
        d.*,a.reg_type
        FROM
        blade_dept d
        LEFT JOIN blade_dept_att a ON a.dept_id = d.id and a.is_deleted = 0
        WHERE
        d.is_deleted = 0
        <if test="dto.tenantId!=null">
            and d.tenant_id = #{dto.tenantId}
        </if>
        <if test="dto.deptName!=null">
            and d.full_name like concat(concat('%', #{dto.deptName}), '%')
        </if>
        <if test="dto.parentId!=null">
            and (d.parent_id = #{dto.parentId} or FIND_IN_SET(#{dto.parentId},ancestors) > 0)
        </if>
        ORDER BY
        d.full_name
    </select>

    <select id="selectList" resultMap="orgVoResultMap">
        SELECT
        d.*,a.reg_type
        FROM
        blade_dept d
        LEFT JOIN blade_dept_att a ON a.dept_id = d.id and a.is_deleted = 0
        WHERE
        d.is_deleted = 0
        <if test="dto.parentId!=null">
            and d.parent_id = #{dto.parentId}
        </if>
        <if test="dto.regType!=null">
            and d.reg_type = #{dto.regType}
        </if>
        <if test="dto.deptName!=null">
            and d.full_name like concat(concat('%', #{dto.deptName}), '%')
        </if>
        ORDER BY
        d.full_name
    </select>

    <select id="orgCount" resultType="java.lang.Long">
        SELECT
        count(*)
        FROM
        blade_dept d
        LEFT JOIN blade_dept_att a ON a.dept_id = d.id and a.is_deleted = 0
        WHERE
        d.is_deleted = 0
        <if test="dto.parentId!=null">
            and d.parent_id = #{dto.parentId}
        </if>
        <if test="dto.regType!=null">
            and d.reg_type = #{dto.regType}
        </if>
    </select>
</mapper>
