<?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.learn.mapper.LearnTestStudentMapper">
    
    <resultMap type="LearnTestStudent" id="LearnTestStudentResult">
        <result property="id"    column="id"    />
        <result property="batchId"    column="batch_id"    />
        <result property="studentId"    column="student_id"    />
        <result property="directKey"    column="direct_key"    />
        <result property="paperId"    column="paper_id"    />
        <result property="classId"    column="class_id"    />
        <result property="examineeId"    column="examinee_id"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectLearnTestStudentVo">
        select id, batch_id, student_id, direct_key, paper_id, class_id, examinee_id, status, create_time from learn_test_student
    </sql>

    <select id="selectDirectedStudentList" parameterType="LearnTestStudent" resultMap="LearnTestStudentResult">
        <include refid="selectLearnTestStudentVo"/>
        <where>
            <if test="batchId != null "> and batch_id = #{batchId}</if>
            <if test="studentId != null "> and student_id = #{studentId}</if>
            <if test="directKey != null  and directKey != ''"> and direct_key = #{directKey}</if>
            <if test="paperId != null "> and paper_id = #{paperId}</if>
            <if test="classId != null "> and class_id = #{classId}</if>
            <if test="examineeId != null "> and examinee_id = #{examineeId}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </select>


    <select id="selectStatsForStudent" parameterType="Long" resultMap="LearnTestStudentResult">
        SELECT ts.status, COUNT(*) count FROM learn_test_student ts WHERE ts.`student_id` = #{studentId}
    </select>

    <select id="selectLearnTestStudentList" parameterType="LearnTestStudent" resultMap="LearnTestStudentResult">
        <include refid="selectLearnTestStudentVo"/>
        <where>  
            <if test="batchId != null "> and batch_id = #{batchId}</if>
            <if test="studentId != null "> and student_id = #{studentId}</if>
            <if test="directKey != null  and directKey != ''"> and direct_key = #{directKey}</if>
            <if test="paperId != null "> and paper_id = #{paperId}</if>
            <if test="classId != null "> and class_id = #{classId}</if>
            <if test="examineeId != null "> and examinee_id = #{examineeId}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectLearnTestStudentById" parameterType="String" resultMap="LearnTestStudentResult">
        <include refid="selectLearnTestStudentVo"/>
        where id = #{id}
    </select>

    <insert id="insertLearnTestStudent" parameterType="LearnTestStudent" useGeneratedKeys="true" keyProperty="id">
        insert into learn_test_student
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="batchId != null">batch_id,</if>
            <if test="studentId != null">student_id,</if>
            <if test="directKey != null and directKey != ''">direct_key,</if>
            <if test="paperId != null">paper_id,</if>
            <if test="classId != null">class_id,</if>
            <if test="examineeId != null">examinee_id,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="batchId != null">#{batchId},</if>
            <if test="studentId != null">#{studentId},</if>
            <if test="directKey != null and directKey != ''">#{directKey},</if>
            <if test="paperId != null">#{paperId},</if>
            <if test="classId != null">#{classId},</if>
            <if test="examineeId != null">#{examineeId},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateLearnTestStudent" parameterType="LearnTestStudent">
        update learn_test_student
        <trim prefix="SET" suffixOverrides=",">
            <if test="batchId != null">batch_id = #{batchId},</if>
            <if test="studentId != null">student_id = #{studentId},</if>
            <if test="directKey != null and directKey != ''">direct_key = #{directKey},</if>
            <if test="paperId != null">paper_id = #{paperId},</if>
            <if test="classId != null">class_id = #{classId},</if>
            <if test="examineeId != null">examinee_id = #{examineeId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLearnTestStudentById" parameterType="String">
        delete from learn_test_student where id = #{id}
    </delete>

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