<?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.yida.data.system.mapper.DeptSchoolInformationMapper">

    <select id="findSchoolListByDeptId" resultType="com.yida.data.system.dto.SchoolInformationDTO">
        SELECT distinct
        a.id,
        a.school_id,
        a.school_name,
        a.school_type,
        a.school_profit_nature,
        a.school_ownership,
        a.logo_url,
        a.phone,
        a.introduction,
        a.shunt_situation,
        a.address,
        a.zip_code,
        a.email,
        a.student_range,
        GROUP_CONCAT(DISTINCT e.nature) as school_nature
        FROM
        `t_dept_school_information` a
        LEFT JOIN t_dept_school_relate b ON a.school_id = b.school_id
        LEFT JOIN t_dept_school_student_range d ON a.id = d.dept_school_id
        LEFT JOIN t_dept_school_label c on c.dept_school_id = a.id
        LEFT JOIN t_dept_school_information_nature e on a.id = e.information_id
        WHERE
        b.del_flag = 0
        <if test="tag !=null and tag !=''">
            and c.label = #{tag}
        </if>
        <if test="deptId != null">
            AND b.dept_id = #{deptId}
        </if>

        <if test="address != null and address != ''">
            and a.address like concat ('%',#{address},'%')
        </if>
        <if test="schoolType != null">
            AND a.school_type = #{schoolType}
        </if>
        <if test="schoolType == 1 and natureType != null">
            and e.nature = #{natureType}
        </if>
        <if test="schoolOwnership != null">
            and a.school_ownership =#{schoolOwnership}
        </if>
        <if test="schoolName != null and schoolName != ''">
            AND a.school_name LIKE CONCAT('%',#{schoolName},'%')
        </if>
        GROUP BY a.id
        order by a.id

    </select>
    <select id="findSchoolPageList" resultType="com.yida.data.system.dto.SchoolInformationDTO">
        SELECT distinct
        a.id,
        a.school_id,
        a.school_name,
        a.school_type,
        a.school_profit_nature,
        a.school_ownership,
        a.phone,
        a.introduction,
        a.lng,
        a.lat,
        a.logo_url,
        a.shunt_situation,
        a.address,
        f.icon,
        GROUP_CONCAT(DISTINCT d.address) as student_range,
        GROUP_CONCAT(DISTINCT e.nature) as school_nature
        FROM
        `t_dept_school_information` a
        LEFT JOIN t_dept_school_relate b ON a.school_id = b.school_id
        LEFT JOIN t_dept_school_label c ON a.id = c.dept_school_id
        LEFT JOIN t_dept_school_student_range d ON a.id = d.dept_school_id
        LEFT JOIN t_dept_school_information_nature e on a.id = e.information_id
        LEFT JOIN t_dept f on a.school_id = f.DEPT_ID
        WHERE
        b.del_flag = 0
        <if test="deptId !=null">
            and b.dept_id = #{deptId}
        </if>
        <if test="deptSchoolId != null and deptSchoolId.size>0">
            and c.dept_school_id in
            <foreach collection="deptSchoolId" separator="," close=")" open="(" item="item">
                #{item}
            </foreach>
        </if>
        <if test="schoolType != null and schoolType.size>0">
            AND a.school_type in
            <foreach collection="schoolType" separator="," close=")" open="(" item="item">
                #{item}
            </foreach>
        </if>
        <if test="natureType !=null and natureType.size > 0">
            AND e.nature in
            <foreach collection="natureType" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="schoolOwnership !=null and schoolOwnership.size>0">
            and a.school_ownership in
            <foreach collection="schoolOwnership" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>

        <if test="schoolName != null">
            AND a.school_name LIKE CONCAT('%',#{schoolName},'%')
        </if>
        GROUP BY a.id

<!--        union-->

<!--        SELECT distinct-->
<!--        a.id,-->
<!--        a.school_id,-->
<!--        a.school_name,-->
<!--        a.school_type,-->
<!--        a.school_education_nature,-->
<!--        a.school_ownership,-->
<!--        a.phone,-->
<!--        a.introduction,-->
<!--        a.lng,-->
<!--        a.lat,-->
<!--        a.logo_url,-->
<!--        a.shunt_situation,-->
<!--        a.address,-->
<!--        GROUP_CONCAT(DISTINCT d.address) as student_range-->
<!--        FROM-->
<!--        `t_dept_school_information` a-->
<!--        LEFT JOIN t_dept_school_relate b ON a.school_id = b.school_id-->
<!--        LEFT JOIN t_dept_school_label c ON a.id = c.dept_school_id-->
<!--        LEFT JOIN t_dept_school_student_range d ON a.id = d.dept_school_id-->
<!--        WHERE-->
<!--        b.del_flag = 0-->
<!--        <if test="deptId !=null">-->
<!--            and b.dept_id = #{deptId}-->
<!--        </if>-->
<!--        <if test="deptSchoolId != null and deptSchoolId.size>0">-->
<!--            and c.dept_school_id in-->
<!--            <foreach collection="deptSchoolId" separator="," close=")" open="(" item="item">-->
<!--                #{item}-->
<!--            </foreach>-->
<!--        </if>-->
<!--        <if test="schoolType != null and schoolType.size>0">-->
<!--            AND a.school_type in-->
<!--            <foreach collection="schoolType" separator="," close=")" open="(" item="item">-->
<!--                #{item}-->
<!--            </foreach>-->
<!--        </if>-->

<!--        <if test="natureType !=null and natureType.size > 0">-->
<!--            AND a.school_education_nature in-->
<!--            <foreach collection="natureType" item="item" open="(" close=")" separator=",">-->
<!--                #{item}-->
<!--            </foreach>-->
<!--        </if>-->

<!--        <if test="schoolOwnership !=null and schoolOwnership.size>0">-->
<!--            and a.school_ownership in-->
<!--            <foreach collection="schoolOwnership" item="item" open="(" close=")" separator=",">-->
<!--                #{item}-->
<!--            </foreach>-->
<!--        </if>-->
<!--        <if test="schoolName != null">-->
<!--            AND a.school_name LIKE CONCAT('%',#{schoolName},'%')-->
<!--        </if>-->
<!--        GROUP BY a.id-->
    </select>
    <select id="findSchoolByAddress" resultType="com.yida.data.system.dto.SchoolInformationDTO">
        SELECT distinct
        a.id,
        a.school_name,
        a.school_type,
        GROUP_CONCAT(DISTINCT d.nature) as school_nature,
        a.school_ownership,
        a.phone,
        a.logo_url,
        a.introduction,
        a.shunt_situation,
        a.address,
        a.school_id
        FROM
        `t_dept_school_information` a
        LEFT JOIN t_dept_school_student_range b ON a.id = b.dept_school_id
        LEFT JOIN t_dept_school_relate c ON a.school_id = c.school_id
        left join t_dept_school_information_nature d on a.id = d.information_id
        WHERE
        1= 1
        <if test="deptId != null">
            and c.dept_id = #{deptId}
        </if>

        <if test="address != null and address != ''">
            AND b.address LIKE CONCAT( '%', #{address}, '%' )
        </if>
    </select>
    <select id="findDeptSchoolId" resultType="java.lang.Long">
        SELECT
        dept_school_id
        FROM
        t_dept_school_label
        GROUP BY
        dept_school_id

        <if test="tags != null and tags.length>0">
            HAVING
            <foreach collection="tags" item="item" open="(" close=")" separator=" " index="index">
                <if test="index != 0">
                    and
                </if>
                FIND_IN_SET(
                #{item},
                GROUP_CONCAT( label )
                )
            </foreach>
        </if>
    </select>
    
    <!-- 根据名称和类型查询教育局学校列表 -->
    <select id="listSchoolPage" resultType="com.yida.data.system.dto.SchoolInformationDTO">
        SELECT distinct
            a.id,
            a.school_id,
            a.school_name,
            a.school_type,
            a.school_profit_nature,
            a.school_ownership,
            a.phone,
            a.introduction,
            a.lng,
            a.lat,
            a.logo_url,
            a.shunt_situation,
            a.address,
            b.create_date,
            f.icon
        FROM
            `t_dept_school_information` a
        LEFT JOIN t_dept_school_relate b ON a.school_id = b.school_id
        LEFT JOIN t_dept_school_information_nature e on a.id = e.information_id
        LEFT JOIN t_dept f on a.school_id = f.DEPT_ID
        WHERE
            b.del_flag = 0
        and b.dept_id = #{dto.deptId}
        <if test="dto.natureType != null">
            AND e.nature = #{dto.natureType}
        </if>
        <if test="dto.schoolName != null">
            AND a.school_name LIKE CONCAT('%', #{dto.schoolName}, '%')
        </if>
        order by
            b.create_date desc
    </select>
    
</mapper>
