<?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.ruoyi.dz.mapper.DzSchoolMapper">

    <resultMap type="DzSchool" id="DzSchoolResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="deptId"    column="dept_id"    />
        <result property="examTypes"    column="exam_types"    />
        <result property="location"    column="location"    />
        <result property="remark"    column="remark"    />
        <result property="pro"    column="pro"    />
        <result property="city"    column="city"    />
        <result property="area"    column="area"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <association property="dept"     javaType="SysDept"         resultMap="deptResult" />
    </resultMap>

    <resultMap id="deptResult" type="SysDept">
        <id     property="deptId"    column="dept_id"     />
        <result property="parentId"  column="parent_id"   />
        <result property="deptName"  column="dept_name"   />
        <result property="ancestors" column="ancestors"   />
        <result property="orderNum"  column="order_num"   />
        <result property="leader"    column="leader"      />
        <result property="status"    column="dept_status" />
    </resultMap>

    <sql id="selectDzSchoolVo">
        select t1.id, t1.name, t1.dept_id, t1.location, t1.remark, t1.pro, t1.city, t1.area, t1.status, t1.create_time, t1.update_time,t1.exam_types,
        d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.status as dept_status
         from dz_school t1
        left join sys_dept d on t1.dept_id = d.dept_id
    </sql>

    <select id="selectDzSchoolList" parameterType="DzSchool" resultMap="DzSchoolResult">
        <include refid="selectDzSchoolVo"/>
        <where>
            <if test="id != null "> and t1.id = #{id}</if>
            <if test="name != null  and name != ''"> and t1.name like concat('%', #{name}, '%')</if>
<!--            <if test="deptId != null "> and t1.dept_id = #{deptId}</if>-->
            <if test="deptId != null ">
                AND (t1.dept_id = #{deptId} OR t1.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, ancestors) ))
            </if>
            <if test="examTypes != null  and examTypes != ''"> and (t1.exam_types is null or find_in_set(#{examTypes}, t1.exam_types))</if>
            <if test="location != null  and location != ''"> and t1.location = #{location}</if>
            <if test="pro != null "> and t1.pro = #{pro}</if>
            <if test="city != null "> and t1.city = #{city}</if>
            <if test="area != null "> and t1.area = #{area}</if>
            <if test="status != null "> and t1.status = #{status}</if>
            <if test="campus != null and campus"> and t1.dept_id &gt; 0</if>
            <if test="campus != null and not campus"> and (t1.dept_id is null or t1.dept_id = 0)</if>
        </where>
        order by t1.id desc
    </select>

    <select id="selectDzSchoolListByIds" resultMap="DzSchoolResult">
        <include refid="selectDzSchoolVo"/>
        WHERE 1=1
        AND t1.id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        ORDER BY t1.id ASC
    </select>

    <select id="selectDzSchoolById" parameterType="Long" resultMap="DzSchoolResult">
        <include refid="selectDzSchoolVo"/>
        where t1.id = #{id}
    </select>

    <insert id="insertDzSchool" parameterType="DzSchool" useGeneratedKeys="true" keyProperty="id">
        insert into dz_school
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="examTypes != null">exam_types,</if>
            <if test="location != null">location,</if>
            <if test="remark != null">remark,</if>
            <if test="pro != null">pro,</if>
            <if test="city != null">city,</if>
            <if test="area != null">area,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="examTypes != null">#{examTypes},</if>
            <if test="location != null">#{location},</if>
            <if test="remark != null">#{remark},</if>
            <if test="pro != null">#{pro},</if>
            <if test="city != null">#{city},</if>
            <if test="area != null">#{area},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateDzSchool" parameterType="DzSchool">
        update dz_school
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="examTypes != null">exam_types = #{examTypes},</if>
            <if test="location != null">location = #{location},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="pro != null">pro = #{pro},</if>
            <if test="city != null">city = #{city},</if>
            <if test="area != null">area = #{area},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDzSchoolById" parameterType="Long">
        delete from dz_school where id = #{id}
    </delete>

    <delete id="deleteDzSchoolByIds" parameterType="String">
        delete from dz_school where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="checkSchoolName" resultType="int">
        select count(1) from dz_school where name = #{name}
        <if test="id != null">
            and id != #{id}
        </if>
    </select>

    <!-- 根据班级ID列表查询学校信息 -->
    <select id="getDzSchoolByClassIds" resultMap="DzSchoolResult">
        select distinct t3.id, t3.name, t3.dept_id, t3.location, t3.remark, t3.pro, t3.city, t3.area, t3.status, t3.create_time, t3.update_time, t3.exam_types
        from dz_classes t2
        join dz_school t3 on t2.school_id = t3.id
        where t2.class_id in
        <foreach collection="classIds" item="classId" open="(" separator="," close=")">
            #{classId}
        </foreach>
    </select>
</mapper>
