<?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.VideoCollectionItemMapper">

    <resultMap id="VideoCollectionItemResultMap" type="com.dora.entity.VideoCollectionItem">
        <id property="id" column="id"/>
        <result property="collectionId" column="collection_id"/>
        <result property="videoId" column="video_id"/>
        <result property="sortOrder" column="sort_order"/>
        <result property="createTime" column="create_time"/>
        <result property="isDeleted" column="is_deleted"/>
    </resultMap>

    <select id="selectByCollectionId" resultMap="VideoCollectionItemResultMap">
        SELECT * FROM video_collection_item 
        WHERE collection_id = #{collectionId} AND is_deleted = 0
        ORDER BY sort_order ASC, create_time ASC
    </select>

    <select id="selectByVideoId" resultMap="VideoCollectionItemResultMap">
        SELECT * FROM video_collection_item 
        WHERE video_id = #{videoId} AND is_deleted = 0
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO video_collection_item (
            collection_id, video_id, sort_order
        ) VALUES (
            #{collectionId}, #{videoId}, #{sortOrder}
        )
    </insert>

    <update id="deleteByCollectionAndVideo">
        UPDATE video_collection_item
        SET is_deleted = 1
        WHERE collection_id = #{collectionId} AND video_id = #{videoId} AND is_deleted = 0
    </update>

    <update id="updateSortOrder">
        UPDATE video_collection_item
        SET sort_order = #{sortOrder}
        WHERE id = #{id} AND is_deleted = 0
    </update>

</mapper>
