<?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.StudentDOMapper">
    <resultMap id="BaseResultMap" type="com.yinlihupo.enlish.service.domain.dataobject.StudentDO">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="class_id" jdbcType="INTEGER" property="classId" />
        <result column="grade_id" jdbcType="INTEGER" property="gradeId" />
        <result column="initial_vocabulary_size" jdbcType="INTEGER" property="initialVocabularySize" />
        <result column="current_vocabulary_size" jdbcType="INTEGER" property="currentVocabularySize" />
        <result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
        <result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
        <result column="actual_grade_id" jdbcType="INTEGER" property="actualGradeId" />
    </resultMap>

    <select id="selectStudentDOListByClassIdAndGradeId" resultMap="BaseResultMap">
        select *
        from student
        where 1 = 1
        <if test="classId != null">
            AND class_id = #{classId}
        </if>
        <if test="gradeId != null">
            AND grade_id = #{gradeId}
        </if>
        <if test="name != null">
            AND name like concat('%', #{name}, '%')
        </if>
            and is_deleted = 0
        order by start_time desc
        LIMIT #{startIndex}, #{pageSize}
    </select>

    <select id="selectStudentCount">
        select count(1)
        from student
    </select>

    <select id="selectStudentById" resultMap="BaseResultMap">
        select *
        from student
        where id = #{id}
    </select>

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

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into student
        (name, class_id, grade_id, is_deleted, start_time, actual_grade_id)
        values (#{name}, #{classId}, #{gradeId}, 0, #{startTime}, #{actualGradeId})
    </insert>

    <update id="deleteById">
        update student
        set is_deleted = 1
        where id = #{id}
    </update>

    <update id="updateStudentActualGradeId">
        update student
        set actual_grade_id = #{gradeId}
        where id = #{studentId}
    </update>

    <select id="selectStudentCountByClassId" resultType="java.lang.Integer">
        select count(1)
        from student
        where class_id = #{classId}
            and is_deleted = 0
    </select>

    <select id="selectStudentDOListByClassId" resultMap="BaseResultMap">
        select *
        from student
        where class_id = #{classId}
            and is_deleted = 0
    </select>

    <select id="selectStudentDOListByGradeId" resultMap="BaseResultMap">
        select *
        from student
        where grade_id = #{gradeId}
            and is_deleted = 0
    </select>
    <select id="selectStudentDOListByName" resultMap="BaseResultMap">
        select *
        from student
        where name like concat('%', #{name}, '%')
            and is_deleted = 0
    </select>
</mapper>