<?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.zhongzheng.modules.user.mapper.UserExamRecordMapper">

    <resultMap type="com.zhongzheng.modules.user.domain.UserExamRecord" id="UserExamRecordResult">
        <result property="recordId" column="record_id"/>
        <result property="examId" column="exam_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="userId" column="user_id"/>
        <result property="status" column="status"/>
        <result property="goodsId" column="goods_id"/>
        <result property="reportStatus" column="report_status"/>
        <result property="performance" column="performance"/>
        <result property="historyExamJson" column="history_exam_json"/>
        <result property="totalQuestionNum" column="total_question_num"/>
        <result property="rightQuestionNum" column="right_question_num"/>
        <result property="doQuestionNum" column="do_question_num"/>
        <result property="moduleExamId" column="module_exam_id"/>
        <result property="chapterExamId" column="chapter_exam_id"/>
        <result property="doTime" column="do_time"/>
        <result property="examTime" column="exam_time"/>
        <result property="totalScore" column="total_score"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.user.vo.UserExamRecordVo" id="UserExamRecordVoResult">
        <result property="recordId" column="record_id"/>
        <result property="examId" column="exam_id"/>
        <result property="updateTime" column="update_time"/>
        <result property="userId" column="user_id"/>
        <result property="status" column="status"/>
        <result property="goodsId" column="goods_id"/>
        <result property="reportStatus" column="report_status"/>
        <result property="performance" column="performance"/>
        <result property="historyExamJson" column="history_exam_json"/>
        <result property="totalQuestionNum" column="total_question_num"/>
        <result property="rightQuestionNum" column="right_question_num"/>
        <result property="doQuestionNum" column="do_question_num"/>
        <result property="moduleExamId" column="module_exam_id"/>
        <result property="chapterExamId" column="chapter_exam_id"/>

        <result property="examName" column="exam_name"/>
        <result property="paperName" column="paper_name"/>
        <result property="doTime" column="do_time"/>
        <result property="examTime" column="exam_time"/>
        <result property="totalScore" column="total_score"/>
    </resultMap>

    <select id="selectList" parameterType="com.zhongzheng.modules.user.bo.UserExamRecordQueryBo" resultMap="UserExamRecordVoResult">
        SELECT
            ue.*,
            e.exam_name,
            ep.paper_name
        FROM
            user_exam_record ue
                LEFT JOIN exam e ON ue.exam_id = e.exam_id
                LEFT JOIN exam_paper ep ON e.exam_paper_id = ep.paper_id
        WHERE
            ue.user_id = #{userId}
        <if test="paperId != null and paperId != ''">
            AND ep.paper_id = #{paperId}
        </if>
        <if test="goodsId != null and goodsId != ''">
            AND ue.goods_id = #{goodsId}
        </if>
        ORDER by ue.record_id DESC
    </select>

    <select id="selectDoNum" parameterType="com.zhongzheng.modules.user.bo.UserExamRecordQueryBo" resultType="Long">
        SELECT
            COALESCE(sum( uer.num ),0) num
        FROM
        ( SELECT MAX( do_question_num ) AS num FROM user_exam_record WHERE user_id = #{userId} AND goods_id = #{goodsId} GROUP BY exam_id ) uer

    </select>

    <select id="selectRightNum" parameterType="com.zhongzheng.modules.user.bo.UserExamRecordQueryBo" resultType="Long">
        SELECT
            COALESCE(sum( uer.num ),0) num
        FROM
            ( SELECT MAX( right_question_num ) AS num FROM user_exam_record WHERE user_id = #{userId} AND goods_id = #{goodsId} GROUP BY exam_id ) uer

    </select>

    <select id="selectDetail" parameterType="com.zhongzheng.modules.user.bo.UserExamRecordQueryBo" resultMap="UserExamRecordVoResult">
        SELECT
            uer.*,
            e.exam_name
        FROM
            user_exam_record uer
                LEFT JOIN exam e ON uer.exam_id = e.exam_id
        WHERE
            uer.record_id = #{recordId}
          AND uer.user_id = #{userId}

    </select>
</mapper>
