<?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">
        <id     property="id"         column="id"    />
        <result property="batchId"    column="batch_id"    />
        <result property="studentId"    column="student_id"    />
        <result property="buildType"    column="build_type"    />
        <result property="subjectId"    column="subject_id"    />
        <result property="directKey"    column="direct_key"    />
        <result property="paperId"    column="paper_id"    />
        <result property="classId"    column="class_id"    />
        <result property="schoolId"    column="school_id"    />
        <result property="campusId"    column="campus_id"    />
        <result property="planId"    column="plan_id"    />
        <result property="creatorId"    column="creator_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, build_type, subject_id, direct_key, paper_id, class_id, school_id, campus_id, plan_id, creator_id, examinee_id, status, create_time from learn_test_student
    </sql>

    <select id="selectExamineeTestList" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT ts.id, p.`paperName` `name`, ts.`subject_id` subjectId, e.`begin_time` beginTime, e.`duration`, p.`fenshu` total, e.`end_time` endTime, IF(e.`state` IS NULL, 2, e.`state`) `state`, e.`score`
        , ts.create_time publishTime, u.`nick_name` publishUser,  p.`direct_key` directKey, us.`name` universityName, sr.`name` schoolName, sc.`name` campusName, mp.`majorName`, ts.`class_id` classId
        FROM `learn_test_student` ts
        JOIN `learn_paper` p ON ts.`paper_id` = p.`id`
        LEFT JOIN `learn_examinee` e ON ts.`examinee_id` = e.`examinee_id` AND ts.`student_id` = #{studentId}
        LEFT JOIN sys_user u ON ts.`creator_id` = u.`user_id`
        LEFT JOIN `dz_school` sr ON ts.`school_id` = sr.`id`
        LEFT JOIN `dz_school` sc ON ts.`campus_id` = sc.`id`
        LEFT JOIN `a_marjor_plan` mp ON ts.`plan_id` = mp.`id`
        LEFT JOIN `b_busi_wish_universities` us ON SUBSTRING_INDEX(p.`direct_key`, '_', 1) = us.`id`
        <where>
            <if test="studentId != null "> and ts.`student_id` = #{studentId}</if>
            <if test="state != null and state != 4"> and IF(e.`state` IS NULL, 2, e.`state`) &lt; 4</if>
            <if test="state != null and state == 4"> and e.`state` &gt;= 4</if>
        </where>
        ORDER BY ts.`create_time` DESC
    </select>

    <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="buildType != null "> and build_type = #{buildType}</if>
            <if test="subjectId != null "> and subject_id = #{subjectId}</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="schoolId != null "> and school_id = #{schoolId}</if>
            <if test="campusId != null "> and campus_id = #{campusId}</if>
            <if test="planId != null "> and plan_id = #{planId}</if>
            <if test="creatorId != null "> and creator_id = #{creatorId}</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="buildType != null "> and build_type = #{buildType}</if>
            <if test="subjectId != null "> and subject_id = #{subjectId}</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="schoolId != null "> and school_id = #{schoolId}</if>
            <if test="campusId != null "> and campus_id = #{campusId}</if>
            <if test="planId != null "> and plan_id = #{planId}</if>
            <if test="creatorId != null "> and creator_id = #{creatorId}</if>
            <if test="examineeId != null "> and examinee_id = #{examineeId}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectLearnTestStudentById" parameterType="Long" 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="buildType != null">build_type,</if>
            <if test="subjectId != null">subject_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="schoolId != null">school_id,</if>
            <if test="campusId != null">campus_id,</if>
            <if test="planId != null">plan_id,</if>
            <if test="creatorId != null">creator_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="buildType != null">#{buildType},</if>
            <if test="subjectId != null">#{subjectId},</if>
            <if test="directKey != null and directKey != ''">#{directKey},</if>
            <if test="paperId != null">#{paperId},</if>
            <if test="classId != null">#{classId},</if>
            <if test="schoolId != null">#{schoolId},</if>
            <if test="campusId != null">#{campusId},</if>
            <if test="planId != null">#{planId},</if>
            <if test="creatorId != null">#{creatorId},</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="buildType != null">build_type = #{buildType},</if>
            <if test="subjectId != null">subject_id = #{subjectId},</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>

    <update id="updateExamineeStatus" parameterType="LearnTestStudent">
        update learn_test_student set status = #{status} where examinee_id = #{examineeId}
    </update>

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

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