<?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.LessonPlansDOMapper">
    <resultMap id="BaseResultMap" type="com.yinlihupo.enlish.service.domain.dataobject.LessonPlansDO">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="title" jdbcType="VARCHAR" property="title" />
        <result column="grade_id" jdbcType="VARCHAR" property="gradeId" />
        <result column="unit_id" jdbcType="INTEGER" property="unitId" />
        <result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
    </resultMap>
    <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.yinlihupo.enlish.service.domain.dataobject.LessonPlansDO">
        <result column="content_details" jdbcType="LONGVARCHAR" property="contentDetails" />
    </resultMap>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into lesson_plans (title, grade_id, unit_id, content_details, created_at)
        values (#{title}, #{gradeId}, #{unitId}, #{contentDetails}, now())
    </insert>
    <select id="selectById" resultMap="ResultMapWithBLOBs">
        select *
        from lesson_plans
        where id = #{id}
    </select>

    <select id="findLessonPlansByStudentId" resultMap="BaseResultMap">
        select *
        from lesson_plans
        <if test="ids != null">
            where id in
            <foreach item="id" collection="ids" separator="," open="(" close=")" index="">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="selectByLessonId" resultMap="ResultMapWithBLOBs">
        select *
        from lesson_plans
        where id = #{lessonId}
    </select>

    <select id="selectByStudentId" resultMap="BaseResultMap">
        select *
        from lesson_plans
        where id in (
            select student_lesson_plans.plan_id
            from student_lesson_plans
            where student_id = #{studentId}
                and is_finished = 0
            )
    </select>

</mapper>