<?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.DzClassesMapper">

    <resultMap type="DzClasses" id="DzClassesResult">
        <result property="classId"    column="class_id"    />
        <result property="schoolId"    column="school_id"    />
        <result property="year"    column="year"    />
        <result property="name"    column="name"    />
        <result property="online"    column="online"    />
        <result property="status"    column="status"    />
        <result property="stats"    column="stats"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDefault"    column="is_default"    />
        <association property="school"     javaType="DzSchool"         resultMap="DzSchoolResult" />
    </resultMap>

    <resultMap type="DzSchool" id="DzSchoolResult">
        <result property="id"    column="id"    />
        <result property="name"    column="school_name"    />
        <result property="deptId"    column="dept_id"    />
        <result property="location"    column="location"    />
        <result property="pro"    column="pro"    />
        <result property="city"    column="city"    />
        <result property="area"    column="area"    />
    </resultMap>

    <sql id="selectDzClassesVo">
        select t1.class_id, t1.school_id, t1.year, t1.name, t1.online, t1.status, t1.stats, t1.create_time, t1.update_time,t1.is_default,
        t2.id, t2.name school_name, t2.dept_id, t2.pro, t2.city, t2.area
        from dz_classes t1
        left join dz_school t2 on t1.school_id=t2.id
    </sql>

    <select id="selectDzClassesList" parameterType="DzClasses" resultMap="DzClassesResult">
        <include refid="selectDzClassesVo"/>
        <where>
            <if test="schoolId != null "> and t1.school_id = #{schoolId}</if>
            <if test="year != null "> and t1.year = #{year}</if>
            <if test="name != null  and name != ''"> and t1.name like concat('%', #{name}, '%')</if>
            <if test="online != null "> and t1.online = #{online}</if>
            <if test="status != null "> and t1.status = #{status}</if>
            <if test="isDefault != null "> and t1.is_default = #{isDefault}</if>
            <if test="stats != null  and stats != ''"> and t1.stats = #{stats}</if>
        </where>
    </select>

    <select id="selectClassesForTeacher" parameterType="Long" resultMap="DzClassesResult">
        SELECT c.* FROM `dz_teacher_class` tc
        JOIN `dz_classes` c ON tc.`class_id` = c.`class_id`
        WHERE tc.`teacher_id` = #{teacherId} AND NOW() &lt; tc.`out_date`
    </select>

    <select id="selectClassesByIds" resultMap="DzClassesResult">
        <include refid="selectDzClassesVo"/>
        where t1.class_id in <foreach collection="ids" item="id" open="(" separator="," close=")">#{id}</foreach>
    </select>

    <select id="selectDzClassesListBySchoolId" resultMap="DzClassesResult">
        select * from dz_classes t1
        where t1.school_id = #{schoolId}
    </select>

    <select id="selectDzClassesByClassId" parameterType="Long" resultMap="DzClassesResult">
        <include refid="selectDzClassesVo"/>
        where t1.class_id = #{classId}
    </select>

    <insert id="insertDzClasses" parameterType="DzClasses" useGeneratedKeys="true" keyProperty="classId">
        insert into dz_classes
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="schoolId != null">school_id,</if>
            <if test="year != null">year,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="online != null">online,</if>
            <if test="status != null">status,</if>
            <if test="stats != null">stats,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDefault != null">is_default,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="schoolId != null">#{schoolId},</if>
            <if test="year != null">#{year},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="online != null">#{online},</if>
            <if test="status != null">#{status},</if>
            <if test="stats != null">#{stats},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDefault != null">#{isDefault},</if>
        </trim>
    </insert>

    <update id="updateDzClasses" parameterType="DzClasses">
        update dz_classes
        <trim prefix="SET" suffixOverrides=",">
            <if test="schoolId != null">school_id = #{schoolId},</if>
            <if test="year != null">year = #{year},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="online != null">online = #{online},</if>
            <if test="status != null">status = #{status},</if>
            <if test="stats != null">stats = #{stats},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDefault != null">is_default = #{isDefault},</if>
        </trim>
        where class_id = #{classId}
    </update>

    <delete id="deleteDzClassesByClassId" parameterType="Long">
        delete from dz_classes where class_id = #{classId}
    </delete>

    <delete id="deleteDzClassesByClassIds" parameterType="String">
        delete from dz_classes where class_id in
        <foreach item="classId" collection="array" open="(" separator="," close=")">
            #{classId}
        </foreach>
    </delete>
</mapper>
