<?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="org.xyzh.study.mapper.CourseChapterMapper">

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.study.TbCourseChapter">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="chapter_id" property="chapterID" jdbcType="VARCHAR"/>
        <result column="course_id" property="courseID" jdbcType="VARCHAR"/>
        <result column="parent_id" property="parentID" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="content" property="content" jdbcType="LONGVARCHAR"/>
        <result column="chapter_type" property="chapterType" jdbcType="INTEGER"/>
        <result column="resource_id" property="resourceID" jdbcType="VARCHAR"/>
        <result column="video_url" property="videoUrl" jdbcType="VARCHAR"/>
        <result column="duration" property="duration" jdbcType="INTEGER"/>
        <result column="order_num" property="orderNum" jdbcType="INTEGER"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, chapter_id, course_id, parent_id, name, content, chapter_type, resource_id, video_url, duration, order_num, 
        creator, updater, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            deleted = 0
            <if test="chapterID != null and chapterID != ''">
                AND chapter_id = #{chapterID}
            </if>
            <if test="courseID != null and courseID != ''">
                AND course_id = #{courseID}
            </if>
            <if test="parentID != null and parentID != ''">
                AND parent_id = #{parentID}
            </if>
            <if test="name != null and name != ''">
                AND name LIKE CONCAT('%', #{name}, '%')
            </if>
            <if test="chapterType != null">
                AND chapter_type = #{chapterType}
            </if>
        </where>
    </sql>

    <!-- selectCourseChapters -->
    <select id="selectCourseChapters" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_course_chapter
        <include refid="Where_Clause"/>
        ORDER BY order_num ASC, create_time ASC
    </select>

    <!-- 根据章节ID查询章节信息 -->
    <select id="selectByChapterId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_course_chapter
        WHERE id = #{chapterId} AND deleted = 0
    </select>

    <!-- 根据课程ID查询章节列表 -->
    <select id="selectByCourseId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_course_chapter
        WHERE course_id = #{courseId} AND deleted = 0
        ORDER BY order_num ASC, create_time ASC
    </select>

    <!-- 根据章节名称查询章节 -->
    <select id="selectByName" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_course_chapter
        WHERE name = #{name} AND deleted = 0
    </select>

    <!-- 根据状态查询章节列表 -->
    <select id="selectByStatus" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_course_chapter
        WHERE deleted = 0
        ORDER BY order_num ASC, create_time ASC
    </select>

    <!-- 根据课程ID和排序查询章节列表 -->
    <select id="selectByCourseIdOrderBySort" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_course_chapter
        WHERE course_id = #{courseId} AND deleted = 0
        ORDER BY order_num ASC, create_time ASC
    </select>

    <!-- 检查章节名称是否存在 -->
    <select id="countByName" resultType="int">
        SELECT COUNT(1)
        FROM tb_course_chapter
        WHERE name = #{name} AND deleted = 0
        <if test="excludeId != null and excludeId != ''">
            AND id != #{excludeId}
        </if>
    </select>

    <!-- 插入课程章节 -->
    <insert id="insertCourseChapter" parameterType="org.xyzh.common.dto.study.TbCourseChapter">
        INSERT INTO tb_course_chapter (
            id, chapter_id, course_id, parent_id, name, content, chapter_type, resource_id, video_url, duration, order_num,
            creator, updater, create_time, update_time, delete_time, deleted
        ) VALUES (
            #{id}, #{chapterID}, #{courseID}, #{parentID}, #{name}, #{content}, #{chapterType}, #{resourceID}, #{videoUrl}, #{duration}, #{orderNum},
            #{creator}, #{updater}, #{createTime}, #{updateTime}, #{deleteTime}, #{deleted}
        )
    </insert>

    <!-- 更新课程章节 -->
    <update id="updateCourseChapter" parameterType="org.xyzh.common.dto.study.TbCourseChapter">
        UPDATE tb_course_chapter
        <set>
            <if test="chapterID != null and chapterID != ''">
                chapter_id = #{chapterID},
            </if>
            <if test="courseID != null and courseID != ''">
                course_id = #{courseID},
            </if>
            <if test="parentID != null">
                parent_id = #{parentID},
            </if>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="content != null and content != ''">
                content = #{content},
            </if>
            <if test="chapterType != null">
                chapter_type = #{chapterType},
            </if>
            <if test="resourceID != null">
                resource_id = #{resourceID},
            </if>
            <if test="videoUrl != null and videoUrl != ''">
                video_url = #{videoUrl},
            </if>
            <if test="duration != null">
                duration = #{duration},
            </if>
            <if test="orderNum != null">
                order_num = #{orderNum},
            </if>
            <if test="updater != null and updater != ''">
                updater = #{updater},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="deleteTime != null">
                delete_time = #{deleteTime},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
        </set>
        WHERE chapter_id = #{chapterID}
    </update>

    <!-- 删除课程章节 -->
    <delete id="deleteCourseChapter" parameterType="org.xyzh.common.dto.study.TbCourseChapter">
        DELETE FROM tb_course_chapter
        WHERE chapter_id = #{chapterID}
    </delete>

    <!-- 批量插入课程章节 -->
    <insert id="batchInsertCourseChapters" parameterType="java.util.List">
        INSERT INTO tb_course_chapter (
            id, chapter_id, course_id, parent_id, name, content, chapter_type, resource_id, video_url, duration, order_num,
            creator, create_time
        ) VALUES
        <foreach collection="courseChapterList" item="item" separator=",">
            (
                #{item.id}, #{item.chapterID}, #{item.courseID}, #{item.parentID}, #{item.name}, #{item.content}, #{item.chapterType}, 
                #{item.resourceID}, #{item.videoUrl}, #{item.duration}, #{item.orderNum}, #{item.creator}, #{item.createTime}
            )
        </foreach>
    </insert>

    <!-- 批量删除课程章节 -->
    <delete id="batchDeleteCourseChapters">
        DELETE FROM tb_course_chapter
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 根据课程ID批量删除章节 -->
    <delete id="deleteByCourseId">
        DELETE FROM tb_course_chapter
        WHERE course_id = #{courseId}
    </delete>

    <!-- 分页查询课程章节 -->
    <select id="selectCourseChaptersPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_course_chapter
        <include refid="Where_Clause" />
        ORDER BY order_num ASC, create_time ASC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计课程章节总数 -->
    <select id="countCourseChapters" resultType="long">
        SELECT COUNT(1)
        FROM tb_course_chapter
        <include refid="Where_Clause" />
    </select>

</mapper>
