<?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.dora.mapper.CourseVideoMapper">

    <resultMap id="CourseVideoResultMap" type="com.dora.entity.CourseVideo">
        <id column="id" property="id"/>
        <result column="chapter_id" property="chapterId"/>
        <result column="title" property="title"/>
        <result column="video_id" property="videoId"/>
        <result column="duration_sec" property="durationSec"/>
        <result column="order_num" property="orderNum"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>

    <insert id="insert" parameterType="com.dora.entity.CourseVideo" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO course_video (chapter_id, title, video_id, duration_sec, order_num, create_time, update_time, is_deleted)
        VALUES (#{chapterId}, #{title}, #{videoId}, #{durationSec}, #{orderNum}, NOW(), NOW(), 0)
    </insert>

    <select id="selectById" resultMap="CourseVideoResultMap">
        SELECT id, chapter_id, title, video_id, duration_sec, order_num, create_time, update_time, is_deleted
        FROM course_video
        WHERE id = #{id} AND is_deleted = 0
    </select>

    <select id="selectNextOrderNum" resultType="int">
        SELECT IFNULL(MAX(order_num), 0) + 1
        FROM course_video
        WHERE chapter_id = #{chapterId} AND is_deleted = 0
    </select>

    <select id="selectByChapterIds" resultMap="CourseVideoResultMap">
        SELECT id, chapter_id, title, video_id, duration_sec, order_num, create_time, update_time, is_deleted
        FROM course_video
        WHERE is_deleted = 0
          AND chapter_id IN
          <foreach collection="chapterIds" item="cid" open="(" separator="," close=")">
              #{cid}
          </foreach>
        ORDER BY chapter_id ASC, order_num ASC, id ASC
    </select>

    <!-- 新增：根据ID更新视频 -->
    <update id="updateById" parameterType="com.dora.entity.CourseVideo">
        UPDATE course_video
        <set>
            <if test="title != null">title = #{title},</if>
            <if test="videoId != null">video_id = #{videoId},</if>
            <if test="durationSec != null">duration_sec = #{durationSec},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            update_time = NOW()
        </set>
        WHERE id = #{id} AND is_deleted = 0
    </update>

    <!-- 新增：软删除视频 -->
    <update id="softDelete">
        UPDATE course_video
        SET is_deleted = 1,
            update_time = NOW()
        WHERE id = #{id} AND chapter_id = #{chapterId} AND is_deleted = 0
    </update>

    <!-- 新增：根据章节ID软删除所有视频 -->
    <update id="softDeleteByChapterId">
        UPDATE course_video
        SET is_deleted = 1,
            update_time = NOW()
        WHERE chapter_id = #{chapterId} AND is_deleted = 0
    </update>

    <!-- 新增：根据章节ID列表软删除所有视频 -->
    <update id="softDeleteByChapterIds">
        UPDATE course_video
        SET is_deleted = 1,
            update_time = NOW()
        WHERE chapter_id IN
        <foreach collection="chapterIds" item="cid" open="(" separator="," close=")">
            #{cid}
        </foreach>
        AND is_deleted = 0
    </update>

    <!-- 计算课程总时长（秒） -->
    <select id="sumDurationByCourseId" resultType="java.lang.Integer">
        SELECT IFNULL(SUM(cv.duration_sec), 0)
        FROM course_video cv
        INNER JOIN course_chapter cc ON cv.chapter_id = cc.id
        WHERE cc.course_id = #{courseId}
          AND cv.is_deleted = 0
          AND cc.is_deleted = 0
    </select>

    <!-- 根据视频ID查询课程视频关联信息 -->
    <select id="selectByVideoId" resultType="com.dora.entity.CourseVideo">
        SELECT cv.id, cv.chapter_id as chapterId, cv.title, cv.video_id as videoId, 
               cv.duration_sec as durationSec, cv.order_num as orderNum,
               cv.create_time as createTime, cv.update_time as updateTime, 
               cv.is_deleted as isDeleted
        FROM course_video cv
        WHERE cv.video_id = #{videoId} AND cv.is_deleted = 0
        LIMIT 1
    </select>

</mapper>


