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

    <resultMap type="TeacherClassRelation" id="TeacherClassRelationResult">
        <result property="id" column="id"/>
        <result property="schoolId" column="school_id"/>
        <result property="teacherId" column="teacher_id"/>
        <result property="classId" column="class_id"/>
        <result property="teacherMain" column="teacher_main"/>
        <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="selectTeacherClassRelationVo">
        select id,
               school_id,
               teacher_id,
               class_id,
               teacher_main,
               create_by,
               create_time,
               update_by,
               update_time
        from tb_school_teacher_class_relation
    </sql>

    <select id="selectTeacherClassRelationList" parameterType="TeacherClassRelation"
            resultMap="TeacherClassRelationResult">
        <include refid="selectTeacherClassRelationVo"/>
        <where>
            <if test="schoolId != null ">and school_id = #{schoolId}</if>
            <if test="teacherId != null ">and teacher_id = #{teacherId}</if>
            <if test="classId != null ">and class_id = #{classId}</if>
            <if test="teacherMain != null  and teacherMain != ''">and teacher_main = #{teacherMain}</if>
        </where>
    </select>

    <select id="selectTeacherClassRelationById" parameterType="Long" resultMap="TeacherClassRelationResult">
        <include refid="selectTeacherClassRelationVo"/>
        where id = #{id}
    </select>
    <select id="selectTeacherClass" resultType="com.schoolinout.system.domain.vo.TeacherClassInfoVo">
        SELECT
        relation.id,
        relation.teacher_id,
        relation.class_id,
        relation.school_id,
        relation.teacher_main,
        class.class_name,
        teacher.teacher_name
        FROM
        tb_school_teacher_class_relation relation
        JOIN
        tb_school_class class ON relation.class_id = class.id AND relation.school_id = class.school_id
        JOIN
        tb_school_teacher teacher ON relation.teacher_id = teacher.id AND relation.school_id = teacher.school_id
        <where>
            <if test="schoolId != null">
                AND relation.school_id = #{schoolId}
            </if>
            <if test="teacherId != null">
                AND relation.teacher_id = #{teacherId}
            </if>
            <if test="schoolId != null">
                AND relation.class_id = #{classId}
            </if>
        </where>
    </select>
    <select id="selectTeacherMain" resultType="com.schoolinout.system.domain.TeacherClassRelation">
        <include refid="selectTeacherClassRelationVo"/>
        <where>
            <if test="schoolId != null">
                AND school_id = #{schoolId}
            </if>
            <if test="teacherId != null">
                AND teacher_id = #{teacherId}
            </if>
            <if test="classId != null">
                AND class_id = #{classId}
            </if>
            AND teacher_main = 'Y'
        </where>
    </select>


    <insert id="insertTeacherClassRelation" parameterType="TeacherClassRelation" useGeneratedKeys="true"
            keyProperty="id">
        insert into tb_school_teacher_class_relation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="schoolId != null">school_id,</if>
            <if test="teacherId != null">teacher_id,</if>
            <if test="classId != null">class_id,</if>
            <if test="teacherMain != null">teacher_main,</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="teacherId != null">#{teacherId},</if>
            <if test="classId != null">#{classId},</if>
            <if test="teacherMain != null">#{teacherMain},</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>
    <insert id="insertTeacherClassRelationBatch">
        INSERT INTO tb_school_teacher_class_relation(school_id, teacher_id, class_id)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.schoolId}, #{item.teacherId}, #{item.classId})
        </foreach>
    </insert>
    <insert id="insertRelationBatch">
        INSERT INTO tb_school_teacher_class_relation(school_id, teacher_id, class_id)
        VALUES
        <if test="list != null">
            <foreach collection="list" item="item" separator=",">
                (#{schoolId}, #{item}, #{classId})
            </foreach>
        </if>
    </insert>
    <insert id="insertRelationBatchForClassId">
        INSERT INTO tb_school_teacher_class_relation(school_id, teacher_id, class_id)
        VALUES
        <if test="list != null">
            <foreach collection="list" item="item" separator=",">
                (#{schoolId}, #{teacherId}, #{item})
            </foreach>
        </if>
    </insert>

    <update id="updateTeacherClassRelation" parameterType="TeacherClassRelation">
        update tb_school_teacher_class_relation
        <trim prefix="SET" suffixOverrides=",">
            <if test="schoolId != null">school_id = #{schoolId},</if>
            <if test="teacherId != null">teacher_id = #{teacherId},</if>
            <if test="classId != null">class_id = #{classId},</if>
            <if test="teacherMain != null">teacher_main = #{teacherMain},</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>
    <update id="updateTeacherMainBindRelation">
        UPDATE tb_school_teacher_class_relation
        SET teacher_main = #{setTeacherMain}
        <where>
            <if test="schoolId != null">
                AND school_id = #{schoolId}
            </if>
            <if test="classId != null">
                AND class_id = #{classId}
            </if>
            <if test="teacherId != null">
                AND teacher_id = #{teacherId}
            </if>
            <if test="teacherMain != null">
                AND teacher_main = #{teacherMain}
            </if>
        </where>
    </update>

    <delete id="deleteTeacherClassRelationById" parameterType="Long">
        delete
        from tb_school_teacher_class_relation
        where id = #{id}
    </delete>

    <delete id="deleteTeacherClassRelationByIds" parameterType="String">
        delete from tb_school_teacher_class_relation where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteRelationBySchoolIdAndTeacherId">
        DELETE
        FROM tb_school_teacher_class_relation
        WHERE school_id = #{schoolId}
          AND teacher_id = #{teacherId}
    </delete>
    <delete id="deleteRelationBatch">
        DELETE FROM tb_school_teacher_class_relation
        <where>
            <if test="schoolId != null">
                AND school_id = #{schoolId}
            </if>
            <if test="classId != null">
                AND class_id = #{classId}
            </if>
            <if test="list != null">
                AND teacher_id IN
                <foreach collection="list" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </delete>
    <delete id="deleteAllRelationsForClass">
        DELETE
        FROM tb_school_teacher_class_relation
        <where>
            <if test="schoolId != null">
                AND school_id = #{schoolId}
            </if>
            <if test="classId != null">
                AND class_id = #{classId}
            </if>
            <if test="teacherId != null">
                AND teacher_id = #{teacherId}
            </if>
        </where>
    </delete>
    <delete id="deleteRelationBatchForClassId">
        DELETE
        FROM tb_school_teacher_class_relation
        <where>
            <if test="schoolId != null">
                AND school_id = #{schoolId}
            </if>
            <if test="teacherId != null">
                AND teacher_id = #{teacherId}
            </if>
            <if test="list != null">
                AND class_id IN
                <foreach collection="list" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>

        </where>
    </delete>
</mapper>