<?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.testor.module.notice.dao.TSysOrgDao">
    <resultMap id="treeVoMap" type="com.testor.module.notice.model.domain.TreeVO">
        <id column="org_id" property="id" />
        <result column="org_name" property="name" />
        <result column="parent_id" property="pid"  />
        <collection property="children" ofType="com.testor.module.notice.model.domain.TreeVO"
                    column="org_id" select="findTreeByPid"/>
    </resultMap>

    <!--级联查询父菜单-->
    <select id="selectAllTree" resultMap="treeVoMap" >
        select org_id , org_name , parent_id from t_sys_org where parent_id = #{id}
    </select>

    <!--级联查询子菜单-->
    <select id="findTreeByPid" resultMap="treeVoMap" >
        select org_id , org_name , parent_id from t_sys_org where parent_id = #{id}
    </select>

    <!--查询父id下所有的子id-->
    <select id="selectAllSubId" resultType="string">
        SELECT org_id FROM t_sys_org
        WHERE status = '0' and is_dept = '0'
        START WITH parent_id in
        <foreach item="id" collection="orgIds" open="(" separator="," close=")">
            #{id}
        </foreach>
        CONNECT BY PRIOR org_id = parent_id

        union all
        SELECT org_id FROM t_sys_org
        WHERE status = '0' and is_dept = '0' and org_id in
        <foreach item="id" collection="orgIds" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectOrgIdAllSubId" resultType="string">
        SELECT org_id FROM t_sys_org
        WHERE status = '0' and is_dept = '0'
        START WITH parent_id =#{orgId}
        CONNECT BY PRIOR org_id = parent_id

        union all
        SELECT org_id FROM t_sys_org
        WHERE status = '0' and is_dept = '0' and org_id=#{orgId}
    </select>

    <select id="selectIsRent" resultType="com.testor.module.notice.model.domain.TSysOrg">
        select * from t_sys_org
        where is_rent = '1' and status = '0' AND org_name LIKE '%租赁库%'
    </select>

    <select id="selectIsRentDept" resultType="com.testor.module.notice.model.domain.TSysOrg">
        select * from t_sys_org
        where parent_id in(
            select org_id from t_sys_org
            where is_rent = '1' and status = '0' AND org_name LIKE '%租赁库%'
        )
    </select>

    <select id="selectOrgIdByParentId" resultType="string">
        select org_id from t_sys_org
        where status = '0' and parent_ids like concat('%',#{orgId},'%') or org_id =#{orgId}
    </select>

</mapper>