<?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.schoolinout.system.mapper.ClazzMapper">

    <resultMap type="Clazz" id="ClazzResult">
        <result property="id" column="id"/>
        <result property="schoolId" column="school_id"/>
        <result property="className" column="class_name"/>
        <result property="classCount" column="class_count"/>
        <result property="phone" column="phone"/>
        <result property="teacherName" column="teacher_name"/>
        <result property="teacherMain" column="teacher_main"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectClazzVo">
        select id,
               school_id,
               class_name,
               class_count,
               phone,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from tb_school_class
    </sql>

    <select id="selectClazzList" parameterType="Clazz" resultMap="ClazzResult">
        select tb_school_class.id,
        tb_school_class.school_id,
        tb_school_class.class_name,
        tb_school_class.class_count,
        tb_school_class.phone,
        teacher.teacher_name,
        relation.teacher_main,
        tb_school_class.del_flag,
        tb_school_class.create_by,
        tb_school_class.create_time,
        tb_school_class.update_by,
        tb_school_class.update_time
        from tb_school_class
        LEFT JOIN tb_school_teacher_class_relation relation on tb_school_class.school_id = relation.school_id
        AND tb_school_class.id = relation.class_id
        AND relation.teacher_main = 'Y'
        LEFT JOIN tb_school_teacher teacher on relation.teacher_id = teacher.id
        <where>
            <if test="schoolId != null ">and school_id = #{schoolId}</if>
            <if test="className != null  and className != ''">and class_name like concat('%', #{className}, '%')</if>
            <if test="classCount != null ">and class_count = #{classCount}</if>
            <if test="phone != null  and phone != ''">and phone = #{phone}</if>
        </where>
        ORDER BY tb_school_class.create_time DESC
    </select>

    <select id="selectClazzById" parameterType="Long" resultMap="ClazzResult">
        <include refid="selectClazzVo"/>
        where id = #{id}
    </select>
    <select id="selectClazzOptionBySchoolId" resultType="com.schoolinout.system.domain.vo.OptionVo">
        SELECT id         as value,
               class_name as label
        FROM tb_school_class
        where school_id = #{schoolId}
    </select>
    <select id="selectClazzByIdAndSchoolId" resultMap="ClazzResult">
        <include refid="selectClazzVo"/>
        where id = #{classId} AND school_id = #{schoolId}
    </select>

    <insert id="insertClazz" parameterType="Clazz" useGeneratedKeys="true" keyProperty="id">
        insert into tb_school_class
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="schoolId != null">school_id,</if>
            <if test="className != null and className != ''">class_name,</if>
            <if test="classCount != null">class_count,</if>
            <if test="phone != null">phone,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="schoolId != null">#{schoolId},</if>
            <if test="className != null and className != ''">#{className},</if>
            <if test="classCount != null">#{classCount},</if>
            <if test="phone != null">#{phone},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateClazz" parameterType="Clazz">
        update tb_school_class
        <trim prefix="SET" suffixOverrides=",">
            <if test="schoolId != null">school_id = #{schoolId},</if>
            <if test="className != null and className != ''">class_name = #{className},</if>
            <if test="classCount != null">class_count = #{classCount},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteClazzById" parameterType="Long">
        delete
        from tb_school_class
        where id = #{id}
    </delete>

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