<?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.ytpm.dao.qnjz.QNQuestionMapper">

    <resultMap id="questionListMap" type="com.ytpm.app.view.QuestionListView">
        <id column="question_id" property="questionId" />
        <result column="question_content" property="questionContent" />
        <result column="question_reward" property="questionReward" />
        <result column="correct_item" property="correctItem" />
        <collection property="itemList" ofType="com.ytpm.app.view.QuestionItemListView">
            <id column="item_id" property="itemId" />
            <result column="item_header" property="itemHeader" />
            <result column="item_content" property="itemContent" />
            <result column="question_id" property="questionId" />
        </collection>
    </resultMap>
    <insert id="saveAnswerRecord">
        insert into yt_dyz_answer_record (record_id, question_id, item_id, duration, answer_time, user_id)
        values (#{recordId},#{questionId},#{itemId},#{duration},#{answerTime},#{userId});
    </insert>
    <select id="questionList" resultMap="questionListMap">
        SELECT
            ydq.question_id,
            ydq.question_content,
            ydq.question_reward,
            ydq.correct_item,
            ydqi.item_id,
            ydqi.item_header,
            ydqi.item_content,
            ydqi.question_id
        FROM yt_dyz_question ydq
        LEFT JOIN yt_dyz_question_item ydqi
        ON ydq.question_id = ydqi.question_id
        where app_id = #{appId}
    </select>
    <select id="getLastQuestionId" resultType="java.lang.String">
        select
            question_id
        from yt_dyz_answer_record
        where user_id = #{userId}
        order by answer_time desc limit 1
    </select>
    <select id="getAnswerCount" resultType="java.lang.Integer">
        select
            count(*)
        from yt_dyz_answer_record
        where user_id = #{userId}
        <if test="type != null and type == 1">
            and DATE_FORMAT(answer_time, '%Y-%m-%d') = current_date()
        </if>
    </select>
    <select id="getAnswerRecords" resultType="com.ytpm.app.model.YtDyzAnswerRecord">
        select
            record_id, question_id, item_id, duration, answer_time, user_id
        from yt_dyz_answer_record
        where user_id = #{userId}
        order by answer_time desc
    </select>
</mapper>