<?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.ynfy.buss.exam.userexam.mapper.UserExamMapper">
    <resultMap id="userExamMap" type="com.ynfy.buss.exam.userexam.entity.UserExam">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="user_id" property="userId" jdbcType="VARCHAR"/>
        <result column="exam_id" property="examId" jdbcType="VARCHAR"/>
        <result column="paper_id" property="paperId" jdbcType="VARCHAR"/>
        <result column="user_time" property="userTime" jdbcType="INTEGER"/>
        <result column="user_score" property="userScore" jdbcType="INTEGER"/>
        <result column="commit_time" property="commitTime" jdbcType="TIMESTAMP"/>
        <result column="state" property="state" jdbcType="INTEGER"/>
        <result column="limit_time" property="limitTime" jdbcType="TIMESTAMP"/>
        <result column="qualify_score" property="qualifyScore" jdbcType="INTEGER"/>
        <result column="total_score" property="totalScore" jdbcType="INTEGER"/>
        <result column="passed" property="passed" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="leave_on" property="leaveOn" jdbcType="BOOLEAN"/>
        <result column="total_leave_times" property="totalLeaveTimes" jdbcType="INTEGER"/>
        <result column="leave_time" property="leaveTime" jdbcType="INTEGER"/>
        <result column="has_subjective" property="hasSubjective" jdbcType="BOOLEAN"/>
        <result column="subjective_score" property="subjectiveScore" jdbcType="INTEGER"/>
        <result column="objective_score" property="objectiveScore" jdbcType="INTEGER"/>
        <result column="exam_result_showtype" property="examResultShowtype" jdbcType="INTEGER"/>
        <result column="show_deadline" property="showDeadline" jdbcType="INTEGER"/>
        <result column="preview_time" property="previewTime" jdbcType="TIMESTAMP"/>
        <result column="preview_user" property="previewUser" jdbcType="VARCHAR"/>


        <collection property="userExamQuestionList" ofType="com.ynfy.buss.exam.userexamquestion.entity.UserExamQuestion">
            <id column="ueq_id" property="id" jdbcType="VARCHAR"/>
            <result column="user_exam_id" property="userExamId" jdbcType="VARCHAR"/>
            <result column="question_id" property="questionId" jdbcType="VARCHAR"/>
            <result column="question_type" property="questionType" jdbcType="INTEGER"/>
            <result column="sort" property="sort" jdbcType="INTEGER"/>
            <result column="question_score" property="questionScore" jdbcType="INTEGER"/>
            <result column="answered" property="answered" jdbcType="BOOLEAN"/>
            <result column="is_right" property="isRight" jdbcType="BOOLEAN"/>
            <result column="answer" property="answer" jdbcType="VARCHAR"/>
            <result column="question_index" property="questionIndex" jdbcType="INTEGER"/>
            <result column="actual_score" property="actualScore" jdbcType="INTEGER"/>
            <result column="can_miss_option" property="canMissOption" jdbcType="BOOLEAN"/>
            <result column="can_blank_option" property="canBlankOption" jdbcType="BOOLEAN"/>
        </collection>
    </resultMap>
    <select id="listUserExam" resultMap="userExamMap">
        SELECT
            ue.id,
            ue.user_id,
            ue.exam_id,
            ue.paper_id,
            ue.user_time,
            ue.user_score,
            ue.commit_time,
            ue.state,
            ue.limit_time,
            ue.qualify_score,
            ue.total_score,
            ue.passed,
            ue.create_time,
            ue.has_subjective,
            ue.subjective_score,
            ue.objective_score,
            ue.preview_time,
            ue.preview_user,
            e.leave_on,
            e.total_leave_times,
            e.leave_time,
            e.exam_result_showtype,
            e.show_deadline,
            ueq.ID as ueq_id,
            ueq.user_exam_id,
            ueq.question_id,
            ueq.question_type,
            ueq.sort,
            ueq.question_score,
            ueq.answered,
            ueq.is_right,
            ueq.answer,
            ueq.question_index,
            ueq.actual_score,
            ueq.can_miss_option,
            ueq.can_blank_option
        FROM
            user_exam ue
        LEFT JOIN exam e ON ue.exam_id = e.id
        LEFT JOIN user_exam_question ueq ON ue.ID = ueq.user_exam_id
        WHERE
            ue.id =  #{id}
        ORDER BY
            ueq.question_type,
            ueq.sort
    </select>

    <select id="selectPageList" resultType="com.ynfy.buss.exam.userexam.entity.UserExam">
        SELECT
            ue.*,
            u.realname as realName
        FROM user_exam ue
        LEFT JOIN sys_user u on ue.user_id = u.id
        <where>
            <if test="userExam.realName != null and userExam.realName != ''">
                AND u.realname like concat('%', #{userExam.realName}, '%')
            </if>
            <if test="userExam.userId != null and userExam.userId != ''">
                AND ue.user_id = #{userExam.userId}
            </if>
            <if test="userExam.examId != null and userExam.examId != ''">
                AND ue.exam_id = #{userExam.examId}
            </if>
            <if test="userExam.state != null">
                AND ue.state = #{userExam.state}
            </if>
            <if test="userExam.passed != null">
                AND ue.passed = #{userExam.passed}
            </if>
        </where>
        ORDER BY ue.commit_time DESC
    </select>
</mapper>