<?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.bank.mapper.QuestionChapterExamMapper">

    <resultMap type="com.zhongzheng.modules.bank.domain.QuestionChapterExam" id="QuestionChapterExamResult">
        <result property="examId" column="exam_id"/>
        <result property="chapterExamId" column="chapter_exam_id"/>
        <result property="sort" column="sort"/>
        <result property="id" column="id"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.bank.vo.ExamVo" id="QuestionChapterExamResultVo">
        <result property="examId" column="exam_id"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="code" column="code"/>
        <result property="examName" column="exam_name"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="year" column="year"/>
        <result property="cityId" column="city_id"/>
        <result property="provinceId" column="province_id"/>
        <result property="examPaperId" column="exam_paper_id"/>
        <result property="answerTime" column="answer_time"/>
        <result property="examPaperId" column="exam_paper_id"/>
        <result property="answerNum" column="answer_num"/>
        <result property="sort" column="exam_sort"/>
        <result property="type" column="type"/>
        <result property="questionNum" column="question_num"/>
        <result property="recordId" column="record_id"/>
        <result property="recordStatus" column="record_status"/>
        <result property="totalScore" column="total_score"/>
        <result property="totalQuestionNum" column="total_question_num"/>
        <result property="rightQuestionNum" column="right_question_num"/>
        <result property="doQuestionNum" column="do_question_num"/>
        <result property="recordStatus" column="record_status"/>
        <result property="recordId" column="record_id"/>
    </resultMap>

    <select id="getList" parameterType="com.zhongzheng.modules.bank.bo.QuestionChapterExamQueryBo" resultMap="QuestionChapterExamResultVo">
        SELECT
            e.*,
            qce.sort AS exam_sort,
               3 as type
        <if test="userId != null ">
                ,
            ifnull( (select `status` from user_exam_record uer where uer.exam_id = e.exam_id and uer.user_id = #{userId} ORDER BY uer.record_id desc limit 1),-1) record_status
            ,
            ifnull( (select record_id from user_exam_record uer where uer.exam_id = e.exam_id and uer.user_id = #{userId} ORDER BY uer.record_id desc limit 1),-1) record_id
        </if>
        FROM
            question_chapter_exam qce
                LEFT JOIN (
                SELECT
                    e.*,
                    count( eq.question_id ) question_num
                FROM
                    exam e
                        LEFT JOIN exam_question eq ON e.exam_id = eq.exam_id
                GROUP BY
                    e.exam_id
            ) e ON qce.exam_id = e.exam_id

        WHERE
            qce.chapter_exam_id =#{chapterExamId}
          AND e.status = 1

    </select>

    <select id="getDoList" parameterType="com.zhongzheng.modules.bank.bo.QuestionChapterExamQueryBo" resultMap="QuestionChapterExamResultVo">
        SELECT
            e.*,
            qce.sort AS exam_sort,
            3 as type,
            uer.record_id,
            uer.record_status,
            uer.total_question_num,
            uer.right_question_num,
            uer.do_question_num
        FROM
            question_chapter_exam qce
                LEFT JOIN (
                SELECT
                    e.*,
                    count( eq.question_id ) question_num
                FROM
                    exam e
                        LEFT JOIN exam_question eq ON e.exam_id = eq.exam_id
                GROUP BY
                    e.exam_id
            ) e ON qce.exam_id = e.exam_id
                LEFT JOIN (SELECT
                               record_id,
                               `status` as record_status,
                               exam_id,
                               total_question_num,
                               right_question_num,
                               do_question_num
                           FROM
                               user_exam_record
                           WHERE
                                   record_id IN (
                                   SELECT
                                       MAX( record_id ) record_id
                                   FROM
                                       user_exam_record
                                   WHERE
                                       user_id = #{userId}
                                     AND goods_id = #{goodsId}
                                   GROUP BY
                                       exam_id)) uer on uer.exam_id = e.exam_id
        WHERE
            qce.chapter_exam_id =#{chapterExamId}
          AND e.status = 1 AND  e.exam_id IN ( SELECT exam_id FROM user_exam_record WHERE user_id = #{userId} AND goods_id = #{goodsId} AND module_exam_id = 0 AND chapter_exam_id = 0 GROUP BY exam_id )
    </select>
</mapper>
