<?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.srs.system.mapper.QgzxTeacherMapper">

    <resultMap type="QgzxTeacher" id="QgzxTeacherResult">
        <result property="teacherId"    column="teacher_id"    />
        <result property="name"    column="name"    />
        <result property="gender"    column="gender"    />
        <result property="age"    column="age"    />
        <result property="employeeId"    column="employee_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="phoneNumber"    column="phone_number"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="deptName"    column="dept_name"    />
    </resultMap>

    <sql id="selectQgzxTeacherVo">
        select a.*,b.dept_name from qgzx_teacher a left join sys_dept b on a.dept_id=b.dept_id
    </sql>

    <select id="selectQgzxTeacherList" parameterType="QgzxTeacher" resultMap="QgzxTeacherResult">
        <include refid="selectQgzxTeacherVo"/>
        <where>
            <if test="name != null  and name != ''"> and a.name like concat('%', #{name}, '%')</if>
            <if test="gender != null  and gender != ''"> and a.gender = #{gender}</if>
            <if test="age != null "> and a.age = #{age}</if>
            <if test="employeeId != null  and employeeId != ''"> and a.employee_id = #{employeeId}</if>
            <if test="deptId != null "> and a.dept_id = #{deptId}</if>
            <if test="phoneNumber != null  and phoneNumber != ''"> and a.phone_number = #{phoneNumber}</if>
            <if test="status != null  and status != ''"> and a.status = #{status}</if>
        </where>
    </select>

    <select id="selectQgzxTeacherByTeacherId" parameterType="Long" resultMap="QgzxTeacherResult">
        <include refid="selectQgzxTeacherVo"/>
        where teacher_id = #{teacherId}
    </select>
    <select id="selectTeacherList" resultType="com.srs.system.domain.QgzxTeacher">
        select * from qgzx_teacher
    </select>
    <select id="selectDeptCode" resultType="java.lang.Long">
        select a.dept_id
        from sys_dept as a
        where dept_code = #{ksh}
    </select>
    <select id="selectQgzxTeacherByEmployeeId" parameterType="String" resultMap="QgzxTeacherResult">
        <include refid="selectQgzxTeacherVo"/>
        where employee_id = #{employeeId}
    </select>
    <insert id="insertQgzxTeacher" parameterType="QgzxTeacher" useGeneratedKeys="true" keyProperty="teacherId">
        insert into qgzx_teacher
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="gender != null and gender != ''">gender,</if>
            age,
            <if test="employeeId != null and employeeId != ''">employee_id,</if>
            <if test="deptId != null">dept_id,</if>
            phone_number,
            <if test="status != null and status != ''">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 and name != ''">#{name},</if>
            <if test="gender != null and gender != ''">#{gender},</if>
            #{age},
            <if test="employeeId != null and employeeId != ''">#{employeeId},</if>
            <if test="deptId != null">#{deptId},</if>
            #{phoneNumber},
            <if test="status != null and status != ''">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>
    <insert id="insertBatchSomeColumn" parameterType="java.util.List">
        insert into qgzx_teacher(name,gender,employee_id,dept_id,status,create_time)
        values
        <foreach item="entity" collection="list" separator="," >
            (
            <if test="entity.name != null and entity.name != ''">#{entity.name}</if>,
            <if test="entity.gender != null and entity.gender != ''">#{entity.gender}</if>,
            <if test="entity.employeeId != null and entity.employeeId != ''">#{entity.employeeId}</if>,
            <if test="entity.deptId !=null">#{entity.deptId}</if>,
            <if test="entity.status != null and entity.status != ''">#{entity.status}</if>,
            <if test="entity.createTime !=null">#{entity.createTime}</if>
            )
        </foreach>
    </insert>

    <update id="updateQgzxTeacher" parameterType="QgzxTeacher">
        update qgzx_teacher
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="gender != null and gender != ''">gender = #{gender},</if>
            age = #{age},
            <if test="employeeId != null and employeeId != ''">employee_id = #{employeeId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            phone_number = #{phoneNumber},
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where teacher_id = #{teacherId}
    </update>

    <update id="updateQgzxTeacherNew" parameterType="QgzxTeacher">
        update qgzx_teacher
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="gender != null and gender != ''">gender = #{gender},</if>
            age = #{age},
            <if test="deptId != null">dept_id = #{deptId},</if>
            phone_number = #{phoneNumber},
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where employee_id = #{employeeId}
    </update>
    <update id="updateBatchSomeColumn" parameterType="java.util.List">
        UPDATE qgzx_teacher
        <set>
            <!-- 批量更新姓名字段：根据主键匹配对应值 -->
            name = CASE
            <foreach collection="list" item="item" separator="">
                WHEN employee_id = #{item.employeeId} THEN #{item.name}
            </foreach>
            END,
            <!-- 批量更新性别编码字段：可同时更新多个字段 -->
            gender = CASE
            <foreach collection="list" item="item" separator="">
                WHEN employee_id = #{item.employeeId} THEN #{item.gender}
            </foreach>
            END,
            <!-- 批量更新所属部门编码字段 -->
            dept_id = CASE
            <foreach collection="list" item="item" separator="">
                WHEN employee_id = #{item.employeeId} THEN #{item.deptId}
            </foreach>
            END,
            status = CASE
            <foreach collection="list" item="item" separator="">
                WHEN employee_id = #{item.employeeId} THEN #{item.status}
            </foreach>
            END,
            update_time = CASE
            <foreach collection="list" item="item" separator="">
                WHEN employee_id = #{item.employeeId} THEN #{item.updateTime}
            </foreach>
            END
        </set>
        <!-- 关键：通过主键过滤，只更新传入的数据集 -->
        WHERE employee_id IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.employeeId}
        </foreach>
    </update>

    <delete id="deleteQgzxTeacherByTeacherId" parameterType="Long">
        delete from qgzx_teacher where teacher_id = #{teacherId}
    </delete>

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