<?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="DOUBLE"/>
        <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="DOUBLE"/>
        <result column="total_score" property="totalScore" jdbcType="DOUBLE"/>
        <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="DOUBLE"/>
        <result column="objective_score" property="objectiveScore" jdbcType="DOUBLE"/>
        <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"/>
        <result column="realname" property="realName" jdbcType="VARCHAR"/>
        <result column="exam_title" property="examTitle" 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="DOUBLE"/>
            <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="DOUBLE"/>
            <result column="can_miss_option" property="canMissOption" jdbcType="BOOLEAN"/>
            <result column="can_blank_option" property="canBlankOption" jdbcType="BOOLEAN"/>
            <result column="child" property="child" jdbcType="BOOLEAN"/>
            <result column="parent_question_id" property="parentQuestionId" jdbcType="VARCHAR"/>
        </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.title as exam_title,
               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,
               ueq.child,
               ueq.parent_question_id,
               user.realname as realname
        FROM (SELECT ue.id,
                     ue.user_id,
                     ue.exam_id,
                     uep.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
              from user_exam ue
                       LEFT JOIN user_exam_papers uep on ue.id=uep.user_exam_id
              where uep.state='0') ue
                 LEFT JOIN exam e ON ue.exam_id = e.id
                 LEFT JOIN user_exam_question ueq ON ue.ID = ueq.user_exam_id and ue.paper_id=ueq.paper_id
                 LEFT JOIN sys_user user ON user.ID = ue.user_id
        WHERE ue.id = #{id}
        ORDER BY ueq.question_type,
                 ueq.sort
    </select>
    <select id="listUserExamYueJuan" 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.title as exam_title,
               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,
               ueq.child,
               ueq.parent_question_id,
               user.realname as realname
        FROM (SELECT ue.id,
                     ue.user_id,
                     ue.exam_id,
                     uep.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
              from user_exam ue
                       LEFT JOIN user_exam_papers uep on ue.id=uep.user_exam_id
              where uep.paper_id=#{paperId}) ue
                 LEFT JOIN exam e ON ue.exam_id = e.id
                 LEFT JOIN user_exam_question ueq ON ue.ID = ueq.user_exam_id and ue.paper_id=ueq.paper_id
                 LEFT JOIN sys_user user ON user.ID = ue.user_id
        WHERE ue.id = #{id}
        ORDER BY ueq.question_type,
            ueq.sort
    </select>
    <select id="listUserExamYueJuans" 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.title as exam_title,
               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,
               ueq.child,
               ueq.parent_question_id,
               user.realname as realname
        FROM (SELECT ue.id,
                     ue.user_id,
                     ue.exam_id,
                     uep.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
              from user_exam ue
                       LEFT JOIN user_exam_papers uep on ue.id=uep.user_exam_id
              where uep.state='1' and uep.paper_id=#{paperId}) ue
                 LEFT JOIN exam e ON ue.exam_id = e.id
                 LEFT JOIN user_exam_question ueq ON ue.ID = ueq.user_exam_id and ue.paper_id=ueq.paper_id
                 LEFT JOIN sys_user user ON user.ID = ue.user_id

        ORDER BY ueq.question_type,
            ueq.sort
    </select>
<!-- <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.title as exam_title,-->
<!--               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,-->
<!--               ueq.child,-->
<!--               ueq.parent_question_id,-->
<!--               user.realname as realname-->
<!--        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-->
<!--                 LEFT JOIN sys_user user ON user.ID = ue.user_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 (SELECT
        ue.user_id,ue.exam_id,p.title,
        uep.user_exam_id id,
        uep.paper_id,
        uep.user_time,
        uep.user_score,
        uep.commit_time,
        uep.state,
        uep.create_time,
        uep.update_by,
        uep.update_time,
        uep.create_by,
        uep.limit_time,
        uep.qualify_score,
        uep.total_score,
        uep.passed,
        uep.has_subjective,
        uep.subjective_score,
        uep.objective_score,
        uep.preview_user,
        uep.preview_time
        FROM user_exam ue
        LEFT JOIN user_exam_papers uep on ue.id = uep.user_exam_id
        LEFT JOIN paper p on uep.paper_id = p.id) 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.paperId != null and userExam.paperId != ''">
                AND ue.paper_id = #{userExam.paperId}
            </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>
<!--    <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>