<?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.WordMasteryLogDOMapper">
    <resultMap id="BaseResultMap" type="com.yinlihupo.enlish.service.domain.dataobject.WordMasteryLogDO">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="student_id" jdbcType="INTEGER" property="studentId" />
        <result column="word_id" jdbcType="INTEGER" property="wordId" />
        <result column="review_count" jdbcType="INTEGER" property="reviewCount" />
        <result column="memory_strength" jdbcType="DECIMAL" property="memoryStrength" />
        <result column="update_time" jdbcType="TIMESTAMP" property="update_time" />
    </resultMap>

    <select id="selectByStudentIdAndUnitIdOrderByMemoryStrength" resultMap="BaseResultMap">
        select *
        from word_mastery_log
        where student_id = #{studentId}
          and word_id in (
            select id
            from vocabulary_bank
            where unit_id = #{unitId}
        )
        order by memory_strength desc
        limit #{limit}
    </select>

    <select id="selectStudentStrengthCount" resultType="java.lang.Integer">
        select count(*)
        from word_mastery_log
        where student_id = #{studentId}
            and memory_strength > 0
    </select>

    <insert id="batchInsertInitialization">
        insert into word_mastery_log (student_id, word_id, update_time)
        VALUES
        <!-- 增加if判断，防止wordIds为空时生成错误SQL -->
        <if test="wordIds != null and wordIds.size() > 0">
            <foreach collection="wordIds" item="id" separator=",">
                (#{studentId}, #{id}, now())
            </foreach>
        </if>
    </insert>

    <update id="batchUpdateStudentMastery">
        <foreach collection="wordMasteryLogDOs" item="wordMasteryLog" separator=";">
            update word_mastery_log
            set
            memory_strength = memory_strength + #{wordMasteryLog.memoryStrength},
            review_count = review_count + #{wordMasteryLog.reviewCount},
            update_time = now()
            where student_id = #{wordMasteryLog.studentId}
            and word_id = #{wordMasteryLog.wordId}
        </foreach>
    </update>

    <select id="selectByStudentIdAndLimitTime" resultMap="BaseResultMap">
        select *
        from word_mastery_log
        where student_id = #{studentId}
          and update_time between date_sub(now(), interval 7 day) and now()
    </select>

    <select id="selectAllByStudentId" resultMap="BaseResultMap">
        select *
        from word_mastery_log
        where student_id = #{studentId}
    </select>

    <select id="selectMasteryCount" resultType="java.lang.Integer">
        select count(*)
        from word_mastery_log
        where student_id = #{studentId}
          and memory_strength >= 1
    </select>
    <select id="selectNotMasteryCount" resultType="java.lang.Integer">
        <![CDATA[
        select count(*)
        from word_mastery_log
        where student_id = #{studentId}
            and memory_strength < 0
        ]]>
    </select>


</mapper>