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

    <resultMap type="Teacher" id="TeacherResult">
        <result property="id" column="id"/>
        <result property="schoolId" column="school_id"/>
        <result property="teacherName" column="teacher_name"/>
        <result property="gender" column="gender"/>
        <result property="phone" column="phone"/>
        <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="selectTeacherVo">
        select id,
               school_id,
               teacher_name,
               gender,
               phone,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from tb_school_teacher
    </sql>

    <select id="selectTeacherList" parameterType="Teacher" resultMap="TeacherResult">
        <include refid="selectTeacherVo"/>
        <where>
            <if test="schoolId != null ">and school_id = #{schoolId}</if>
            <if test="teacherName != null  and teacherName != ''">and teacher_name like concat('%', #{teacherName},
                '%')
            </if>
            <if test="gender != null  and gender != ''">and gender = #{gender}</if>
            <if test="phone != null  and phone != ''">and phone like concat('%', #{phone}, '%')</if>
        </where>
    </select>

    <select id="selectTeacherById" parameterType="Long" resultMap="TeacherResult">
        <include refid="selectTeacherVo"/>
        where id = #{id}
    </select>
    <select id="selectTeacherOption" resultType="com.schoolinout.system.domain.vo.OptionVo">
        SELECT id           as value,
               teacher_name as label
        FROM tb_school_teacher
        where school_id = #{schoolId}
    </select>
    <select id="selectTeacherByIdAndSchoolId" resultType="com.schoolinout.system.domain.Teacher">
        <include refid="selectTeacherVo"/>
        where
        id = #{teacherId}
        AND
        school_id = #{schoolId}
    </select>

    <insert id="insertTeacher" parameterType="Teacher" useGeneratedKeys="true" keyProperty="id">
        insert into tb_school_teacher
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="schoolId != null">school_id,</if>
            <if test="teacherName != null and teacherName != ''">teacher_name,</if>
            <if test="gender != null and gender != ''">gender,</if>
            <if test="phone != null and phone != ''">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="teacherName != null and teacherName != ''">#{teacherName},</if>
            <if test="gender != null and gender != ''">#{gender},</if>
            <if test="phone != null and phone != ''">#{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="updateTeacher" parameterType="Teacher">
        update tb_school_teacher
        <trim prefix="SET" suffixOverrides=",">
            <if test="schoolId != null">school_id = #{schoolId},</if>
            <if test="teacherName != null and teacherName != ''">teacher_name = #{teacherName},</if>
            <if test="gender != null and gender != ''">gender = #{gender},</if>
            <if test="phone != null and phone != ''">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="deleteTeacherById" parameterType="Long">
        delete
        from tb_school_teacher
        where id = #{id}
    </delete>

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