<?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.UserExamWrongRecordMapper">

    <resultMap type="com.zhongzheng.modules.user.domain.UserExamWrongRecord" id="UserExamWrongRecordResult">
        <result property="wrongId" column="wrong_id"/>
        <result property="recordId" column="record_id"/>
        <result property="questionId" column="question_id"/>
        <result property="userId" column="user_id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="examId" column="exam_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.user.vo.UserExamWrongRecordVo" id="UserExamWrongRecordVoResult">
        <result property="wrongId" column="wrong_id"/>
        <result property="recordId" column="record_id"/>
        <result property="questionId" column="question_id"/>
        <result property="userId" column="user_id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="examId" column="exam_id"/>


        <result property="examName" column="exam_name"/>
        <result property="wrongQuestionNum" column="wrong_question_num"/>
        <result property="type" column="type"/>
        <result property="num" column="num"/>

        <result property="content" column="content"/>
        <result property="type" column="type"/>
        <result property="answerQuestion" column="answer_question"/>
        <result property="status" column="status"/>
        <result property="analysisContent" column="analysis_content"/>
        <result property="imgUrl" column="img_url"/>
        <result property="jsonStr" column="json_str"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="knowledgeIds" column="knowledge_ids"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="code" column="code"/>
    </resultMap>



    <select id="selectList" parameterType="com.zhongzheng.modules.user.bo.UserExamWrongRecordQueryBo" resultMap="UserExamWrongRecordVoResult">
        SELECT
        e.exam_name,
        e.exam_id,
        COUNT( DISTINCT uew.question_id ) AS wrong_question_num
        FROM
        user_exam_wrong_record uew
        LEFT JOIN exam e ON uew.exam_id = e.exam_id
        LEFT JOIN exam_paper ep ON ep.paper_id = e.exam_paper_id
        WHERE
        uew.user_id = #{userId}
        <if test="goodsId != null and goodsId != ''">
            AND uew.goods_id = #{goodsId}
        </if>
        <if test="paperId != null and paperId != ''">
            AND e.exam_paper_id = #{paperId}
        </if>
        GROUP BY
        e.exam_id
    </select>

    <select id="selectQuestionList" parameterType="com.zhongzheng.modules.user.bo.UserExamWrongRecordQueryBo" resultMap="UserExamWrongRecordVoResult">
        SELECT DISTINCT
        q.*,
        uew.goods_id,
        uew.exam_id
        FROM
        user_exam_wrong_record uew
        LEFT JOIN question q ON q.question_id = uew.question_id
        WHERE
        uew.user_id = #{userId}
        <if test="type != null and type != ''">
            AND q.type = #{type}
        </if>
        <if test="goodsId != null and goodsId != ''">
            AND uew.goods_id = #{goodsId}
        </if>
        <if test="examId != null and examId != ''">
            AND uew.exam_id = #{examId}
        </if>
        <if test="recordId != null and recordId != ''">
            AND uew.record_id = #{recordId}
        </if>
    </select>

    <select id="selectTypeNum" parameterType="com.zhongzheng.modules.user.bo.UserExamWrongRecordQueryBo"  resultMap="UserExamWrongRecordVoResult">
        SELECT
            q.type,
            count(DISTINCT q.question_id ) AS num
        FROM
                ( SELECT ue.* FROM user_exam_wrong_record ue
        LEFT JOIN exam e ON ue.exam_id = e.exam_id
        LEFT JOIN exam_paper ep ON ep.paper_id = e.exam_paper_id
                WHERE user_id = #{userId}
        <if test="paperId != null and paperId != ''">
            AND e.exam_paper_id = #{paperId}
        </if>) uew
                    LEFT JOIN question q ON uew.question_id = q.question_id
        WHERE
        1 = 1
        <if test="goodsId != null and goodsId != ''">
            AND uew.goods_id = #{goodsId}
        </if>
        GROUP BY
            q.type
    </select>

    <select id="selectTypeQuestionList" parameterType="com.zhongzheng.modules.user.bo.UserExamWrongRecordQueryBo"  resultMap="UserExamWrongRecordVoResult">
        SELECT DISTINCT
        q.*,
        uew.goods_id,
        uew.exam_id
        FROM
        user_exam_wrong_record uew
        LEFT JOIN question q ON uew.question_id = q.question_id
        WHERE
        1 = 1
        AND uew.user_id =  #{userId}
        <if test="type != null and type != ''">
            AND q.type = #{type}
        </if>
        <if test="goodsId != null and goodsId != ''">
            AND uew.goods_id = #{goodsId}
        </if>
    </select>

    <select id="recordNum" parameterType="com.zhongzheng.modules.user.bo.UserExamWrongRecordQueryBo"  resultType="Long">
        SELECT
        count( DISTINCT question_id )
        FROM
        user_exam_wrong_record
        WHERE 1=1
        <if test="goodsId != null and goodsId != ''">
        AND goods_id = #{goodsId}
        </if>
        AND user_id = #{userId}
        <if test="recordId != null and recordId != ''">
            AND record_id = #{recordId}
        </if>
    </select>
</mapper>
