<?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.StudentLessonPlansDOMapper">
    <resultMap id="BaseResultMap" type="com.yinlihupo.enlish.service.domain.dataobject.StudentLessonPlansDO">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="student_id" jdbcType="INTEGER" property="studentId" />
        <result column="plan_id" jdbcType="INTEGER" property="planId" />
        <result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
        <result column="mastery_rat" jdbcType="DECIMAL" property="masteryRat" />
        <result column="total_count" jdbcType="INTEGER" property="totalCount" />
        <result column="is_finished" jdbcType="INTEGER" property="isFinished" />
    </resultMap>

    <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.yinlihupo.enlish.service.domain.dataobject.StudentLessonPlansDO">
        <result column="memorized_words_json" jdbcType="LONGVARCHAR" property="memorizedWordsJson" />
        <result column="unmemorized_words_json" jdbcType="LONGVARCHAR" property="unmemorizedWordsJson" />
    </resultMap>

    <insert id="insert">
        insert into student_lesson_plans (student_id, plan_id, start_time, mastery_rat, total_count)
        values (#{studentId,jdbcType=INTEGER}, #{planId,jdbcType=INTEGER},
        now(), 0.0, 0
        )
    </insert>

    <select id="selectStudentLessonPlanList" resultMap="BaseResultMap">
        SELECT slp.*
        FROM student_lesson_plans slp
        LEFT JOIN student s ON slp.student_id = s.id
        JOIN (
            SELECT DISTINCT slp_in.student_id
            FROM student_lesson_plans slp_in
            LEFT JOIN student s_in ON slp_in.student_id = s_in.id
            <if test="name != null and name != ''">
                WHERE s_in.name LIKE CONCAT('%', #{name}, '%')
            </if>
            ORDER BY slp_in.student_id
            LIMIT #{startIndex}, #{pageSize}
        ) AS temp ON slp.student_id = temp.student_id
        where slp.is_finished = 0
        ORDER BY slp.id
    </select>

    <select id="selectStudentPlanTotal">
        SELECT count(DISTINCT student_id) AS total
        FROM student_lesson_plans;
    </select>
    <select id="selectStudentLessonPlanListByStudentId" resultMap="BaseResultMap">
        SELECT *
        FROM student_lesson_plans
        WHERE student_id = #{studentId}
        limit 500
    </select>

    <update id="finfishStudentPlan">
        update student_lesson_plans
        set is_finished = 1,
            total_count = #{count},
            mastery_rat = #{mastery}
        where student_id = #{studentId}
            and plan_id = #{planId}
    </update>
</mapper>