<?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.school.paper.mapper.DbTestPaperRecordDetailMapper">

    <resultMap type="com.ruoyi.school.paper.domain.DbTestPaperRecordDetail" id="DbTestPaperRecordDetailResult">
        <result property="id" column="id"/>
        <result property="recordId" column="record_id"/>
        <result property="questionBankId" column="question_bank_id"/>
        <result property="answer" column="answer"/>
        <result property="score" column="score"/>
        <result property="good" column="good"/>
        <result property="remark" column="remark"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>

    <resultMap type="com.ruoyi.school.paper.domain.vo.MyErrorTopicListVo" id="TestPaperRecordDetailResult">
        <result property="recordId" column="record_id"/>
        <result property="topic" column="topic"/>
        <result property="questionBankId" column="question_bank_id"/>
        <result property="answer" column="answer"/>
        <result property="answerPic" column="answers_pic"/>
        <result property="score" column="score"/>
        <result property="topicScore" column="topicScore"/>
        <result property="correctAnswer" column="correctAnswer"/>
        <result property="correctAnswerPic" column="correctAnswerPic"/>
        <result property="testName" column="test_name"/>
        <result property="studentName" column="nick_name"/>
        <result property="good" column="good"/>
        <collection property="options" javaType="java.util.List"  column="question_bank_id" select="getOptionList" ofType="com.ruoyi.school.paper.domain.vo.DbQuestionBankAnswerTestPaperVo">
            <result property="optionTag" column="option_tag"/>
            <result property="optionContent" column="option_content"/>
        </collection>
        <collection property="goodsAnswerCount" column="{questionBankId = question_bank_id, recordId = record_id}" select="getHasGoodsAnserList" ofType="long">
        </collection>
    </resultMap>

    <select id="queryPageList" resultMap="TestPaperRecordDetailResult">
        SELECT
            dtprd.id,
            dqbatp.topic,
            dtprd.record_id,
            dtprd.question_bank_id,
            dqbatp.right_answers AS correctAnswer,
            dqbatp.right_answers_pic as correctAnswerPic,
            dtprd.answer,
            dtprd.answers_pic,
            dtprd.score,
            dqbatp.species,
            dqbatp.score as topicScore,
            dtprd.good,
            dtp.test_name,
            su.nick_name
        FROM
            db_test_paper_record_detail dtprd
            LEFT JOIN db_question_bank_test_paper dqbatp ON dtprd.question_bank_id = dqbatp.id
            LEFT JOIN db_test_paper_record dtpr ON dtprd.record_id = dtpr.id
            left join db_test_paper dtp on dtpr.test_paper_id = dtp.id
            left join sys_user su on dtpr.user_id = su.user_id
        WHERE
            dtprd.record_id = #{bo.recordId}
        AND
        IF
        (
        dtpr.STATUS = 3,
        1 = 1,
        dqbatp.species IN ( 1, 3 ))
    </select>

    <select id="getOptionList" resultType="com.ruoyi.school.paper.domain.vo.DbQuestionBankAnswerTestPaperVo">
        select option_tag, option_content from db_question_bank_answer_test_paper where question_bank_id = #{question_bank_id}
    </select>

    <select id="getHasGoodsAnserList" resultType="long" >
        SELECT
            count(1) as count
        FROM
            db_test_paper_record_detail a
                LEFT JOIN db_test_paper_record dtpr ON a.record_id = dtpr.id
                LEFT JOIN sys_user su ON dtpr.user_id = su.user_id
        WHERE
            a.question_bank_id = #{questionBankId}
          AND good = 0 and a.record_id = #{recordId}
    </select>

    <select id="getGoodsAnserList" resultType="com.ruoyi.school.paper.domain.vo.GoodsAnswersVo">
        SELECT
        a.answer as goods_answer,
        a.answers_pic as goodsAnswerPic,
        su.nick_name
        FROM
        db_test_paper_record_detail a
        LEFT JOIN db_test_paper_record dtpr ON a.record_id = dtpr.id
        LEFT JOIN sys_user su ON dtpr.user_id = su.user_id
        WHERE
        a.question_bank_id = #{bo.questionBankId}
        AND good = 0
    </select>

    <select id="getTotalPoints" resultType="com.ruoyi.school.paper.domain.vo.DbTestPaperRecordDetailVo">
        select detail.*,
        dqbtp.species
        from db_test_paper_record_detail detail
        left join db_question_bank_test_paper dqbtp on detail.question_bank_id = dqbtp.id
        where record_id = #{bo.id}
        and detail.`status` = 0
    </select>
</mapper>
