<?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.yinlihupo.enlish.service.domain.mapper.ExamWordsJudgeResultDOMapper">

    <resultMap id="BaseResultMap" type="com.yinlihupo.enlish.service.domain.dataobject.ExamWordsJudgeResultDO">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="ans_sheet_path" jdbcType="VARCHAR" property="ansSheetPath" />
        <result column="student_id" jdbcType="INTEGER" property="studentId" />
        <result column="exam_words_id" jdbcType="INTEGER" property="examWordsId" />
        <result column="correct_word_count" jdbcType="INTEGER" property="correctWordCount" />
        <result column="wrong_word_count" jdbcType="INTEGER" property="wrongWordCount" />
        <result column="is_finished" jdbcType="INTEGER" property="isFinished" />
        <result column="start_date" jdbcType="TIMESTAMP" property="startDate" />
        <result column="msg" jdbcType="VARCHAR" property="msg" />
    </resultMap>

    <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.yinlihupo.enlish.service.domain.dataobject.ExamWordsJudgeResultDO">
        <result column="correct_word_ids" jdbcType="LONGVARCHAR" property="correctWordIds" typeHandler="com.yinlihupo.enlish.service.config.ListWordIdTypeHandler" />
        <result column="wrong_word_ids" jdbcType="LONGVARCHAR" property="wrongWordIds" typeHandler="com.yinlihupo.enlish.service.config.ListWordIdTypeHandler" />
    </resultMap>

    <insert id="insert">
        insert into exam_words_judge_result
        (ans_sheet_path, is_finished, start_date)
        values (#{path}, 0, now())
    </insert>

    <select id="selectUnfinishedExamWordsJudgeResultDOList" resultMap="BaseResultMap">
        select * from exam_words_judge_result
        where is_finished = 0
        limit #{count}
    </select>

    <update id="updateMsg">
        update exam_words_judge_result
        set msg = #{msg},
            is_finished = 2
        where id = #{id}
    </update>

    <update id="updateExamWordsJudgeResultDO">
        update exam_words_judge_result
        set student_id = #{examWordsJudgeResultDO.studentId},
            exam_words_id = #{examWordsJudgeResultDO.examWordsId},
            correct_word_ids = #{examWordsJudgeResultDO.correctWordIds, typeHandler=com.yinlihupo.enlish.service.config.ListWordIdTypeHandler},
            wrong_word_ids = #{examWordsJudgeResultDO.wrongWordIds, typeHandler=com.yinlihupo.enlish.service.config.ListWordIdTypeHandler},
            correct_word_count = #{examWordsJudgeResultDO.correctWordCount},
            wrong_word_count = #{examWordsJudgeResultDO.wrongWordCount},
            is_finished = 1,
            msg = #{examWordsJudgeResultDO.msg}
        where id = #{examWordsJudgeResultDO.id}
    </update>

    <select id="selectByPage" resultMap="BaseResultMap">
        select *
        from exam_words_judge_result
        order by start_date
        limit #{startIndex}, #{pageSize}
    </select>

    <select id="selectCount" resultType="java.lang.Integer">
        select count(1)
        from exam_words_judge_result
        where is_finished = 0
    </select>

    <select id="selectDetailById" resultMap="ResultMapWithBLOBs">
        select *
        from exam_words_judge_result
        where id = #{id}
    </select>
    <select id="selectByStudentId" resultMap="BaseResultMap">
        select *
        from exam_words_judge_result
        where student_id = #{studentId}
        order by start_date desc
        limit 500;
    </select>
    <select id="selectByStudentIdAndLimitTime" resultMap="ResultMapWithBLOBs">
        select *
        from exam_words_judge_result
        where student_id = #{studentId}
        and start_date between date_sub(now(), interval 7 day) and now()
    </select>

</mapper>