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

    <resultMap id="VideoCollectionResultMap" type="com.dora.entity.VideoCollection">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="description" column="description"/>
        <result property="ownerId" column="owner_id"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="isPublic" column="is_public"/>
        <result property="auditStatus" column="audit_status"/>
        <result property="videoCount" column="video_count"/>
        <result property="totalDuration" column="total_duration"/>
        <result property="viewCount" column="view_count"/>
        <result property="likeCount" column="like_count"/>
        <result property="category" column="category"/>
        <result property="tags" column="tags"/>
        <result property="sortOrder" column="sort_order"/>
        <result property="rejectReason" column="reject_reason"/>
        <result property="reviewTime" column="review_time"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="isDeleted" column="is_deleted"/>
    </resultMap>

    <select id="selectById" resultMap="VideoCollectionResultMap">
        SELECT * FROM video_collection 
        WHERE id = #{id} AND is_deleted = 0
    </select>

    <select id="selectByOwnerId" resultMap="VideoCollectionResultMap">
        SELECT * FROM video_collection 
        WHERE owner_id = #{ownerId} AND is_deleted = 0
        ORDER BY create_time DESC
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO video_collection (
            name, description, owner_id, cover_url, is_public,
            audit_status, category, tags, sort_order
        ) VALUES (
            #{name}, #{description}, #{ownerId}, #{coverUrl}, #{isPublic},
            #{auditStatus}, #{category}, #{tags}, #{sortOrder}
        )
    </insert>

    <update id="updateById">
        UPDATE video_collection
        SET name = #{name},
            description = #{description},
            cover_url = #{coverUrl},
            is_public = #{isPublic},
            category = #{category},
            tags = #{tags},
            sort_order = #{sortOrder},
            update_time = NOW()
        WHERE id = #{id} AND is_deleted = 0
    </update>

    <update id="submitForAudit">
        UPDATE video_collection
        SET 
            name = COALESCE(#{name}, name),
            description = COALESCE(#{description}, description),
            audit_status = 0,
            reject_reason = NULL,
            review_time = NULL,
            update_time = NOW()
        WHERE id = #{id} AND owner_id = #{ownerId} AND is_deleted = 0
    </update>

</mapper>
