<?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.VocabularyBankDOMapper">
    <resultMap id="BaseResultMap" type="com.yinlihupo.enlish.service.domain.dataobject.VocabularyBankDO">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="word" jdbcType="VARCHAR" property="word" />
        <result column="definition" jdbcType="VARCHAR" property="definition" />
        <result column="pronunciation" jdbcType="VARCHAR" property="pronunciation" />
        <result column="unit_id" jdbcType="INTEGER" property="unitId" />
        <result column="pos" jdbcType="VARCHAR" property="pos" />
    </resultMap>


    <insert id="insertSelective" parameterType="com.yinlihupo.enlish.service.domain.dataobject.VocabularyBankDO">
        insert into vocabulary_bank
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="word != null">
                word,
            </if>
            <if test="definition != null">
                `definition`,
            </if>
            <if test="pronunciation != null">
                pronunciation,
            </if>
            <if test="unitId != null">
                unit_id,
            </if>
            <if test="pos != null">
                pos,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="word != null">
                #{word,jdbcType=VARCHAR},
            </if>
            <if test="definition != null">
                #{definition,jdbcType=VARCHAR},
            </if>
            <if test="pronunciation != null">
                #{pronunciation,jdbcType=VARCHAR},
            </if>
            <if test="unitId != null">
                #{unitId,jdbcType=INTEGER},
            </if>
            <if test="pos != null">
                #{pos,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>

    <select id="selectVocabularyBankDOListByUnitId" resultMap="BaseResultMap">
        select *
        from vocabulary_bank
        where unit_id = #{unitId}
        limit #{wordCount}
    </select>

    <select id="selectVocabularyBankDOListByIds" resultMap="BaseResultMap">
        select *
        from vocabulary_bank
        where id in
        <foreach item="id" collection="ids" separator="," open="(" close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectAllIds" resultType="java.lang.Integer">
        select id
        from vocabulary_bank
    </select>

    <select id="selectVocabularyBankDOAllByUnitId" resultMap="BaseResultMap">
        select *
        from vocabulary_bank
        where unit_id = #{unitId}
    </select>

    <select id="selectVocabularyBankListStudentNotMaster" resultMap="BaseResultMap">
    <![CDATA[
        select *
        from vocabulary_bank
        where unit_id in (
            select unit_id
            from grade_unit
            where grade_id = #{gradeId}
        )
            and id in (
                select word_id
                from word_mastery_log
                where memory_strength <= 0
                    and student_id = #{studentId}
            )
        ]]>
    </select>

    <select id="selectVocabularyBankListSelfCheck" resultMap="BaseResultMap">
        select *
        from vocabulary_bank vb
        inner join grade_unit gu on vb.unit_id = gu.unit_id
        inner join word_mastery_log wml on vb.id = wml.word_id
        where gu.grade_id = #{gradeId}
        and wml.student_id = #{studentId}
        <!-- 修复not in的语法错误 + 空集合防护 -->
        <if test="ids != null and ids.size() > 0">
            and vb.id not in (
            <foreach item="id" collection="ids" separator=",">
                #{id}
            </foreach>
            )
        </if>
        limit #{wordCount}
    </select>

    <select id="selectWordTotal" resultType="java.lang.Integer">
        select count(*)
        from vocabulary_bank
    </select>

    <select id="selectVocabularyBankListByGradeIdRandom" resultMap="BaseResultMap">
        select *
        from vocabulary_bank
        where unit_id in (
            select unit_id
            from grade_unit
            where grade_id = #{gradeId}
        )
        order by rand()
        limit #{wordCount}
    </select>

    <select id="selectByUnitIds" resultMap="BaseResultMap">
        select *
        from vocabulary_bank
        where unit_id in
        <foreach item="unitId" collection="unitIds" separator="," open="(" close=")">
            #{unitId}
        </foreach>
        order by rand()
        limit 100
    </select>

</mapper>