<?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.qmth.teachcloud.common.mapper.SysOrgMapper">

    <select id="findByConnectByParentId" resultType="com.qmth.teachcloud.common.entity.SysOrg">
        SELECT su.* FROM (SELECT @a AS _id, (SELECT @a := so.parent_id FROM sys_org so WHERE so.id = _id) AS parent_id FROM (SELECT @a := #{orgId}) vars, sys_org h) temp JOIN sys_org su ON temp._id = su.id
        <where>
            <if test="school != null and school != '' and school == 1">
                and su.type is null
            </if>
            <!--<if test="college != null and college != '' and college == 1">
                and su.type = 'COLLEGE'
            </if>-->
        </where>
    </select>

    <select id="findByConnectByRootOrgId" resultType="com.qmth.teachcloud.common.entity.SysOrg">
        SELECT so.* FROM sys_org so WHERE FIND_IN_SET(id, getRootChildId(#{orgId}))
    </select>

    <select id="findByConnectByRootOrgIdAndFilter" resultType="com.qmth.teachcloud.common.entity.SysOrg">
        SELECT so.* FROM sys_org so WHERE FIND_IN_SET(id, getRootChildId(#{orgId}))
        <if test="filter != null and filter != '' and filter == 1">
            and so.`type` is null
        </if>
    </select>

    <select id="findByConnectByRootOrgIdAndType" resultType="com.qmth.teachcloud.common.entity.SysOrg">
        SELECT so.* FROM sys_org so WHERE FIND_IN_SET(id, getRootChildId(#{orgId}))
        <if test="orgType != null and orgType != ''">
            and so.`type` = #{orgType}
        </if>
    </select>

    <select id="listOrgAll" resultType="com.qmth.teachcloud.common.bean.dto.OrgDto">
        select
            so.id,
            so.school_id as school_id,
            so.code,
            so.name,
            so.`type` type,
            so.parent_id as parentId,
            so.enable,
            bc.id as campusId,
            bc.campus_name as campusName,
            bc.campus_code as campusCode
        from
            sys_org so
        left join basic_campus bc on
            bc.print_house_id = so.id
            and so.`type` = 'PRINTING_HOUSE'
        <where>
            <if test="schoolId != null and schoolId != ''">
                and so.school_id = #{schoolId}
            </if>
        </where>
            order by so.create_time asc
    </select>
</mapper>
