<?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="shop.alien.mapper.LifeImgMapper">

    <!-- 通用结果映射 -->
    <resultMap id="BaseResultMap" type="shop.alien.entity.store.LifeImg">
        <id column="id" property="id" />
        <result column="feedback_id" property="feedbackId" />
        <result column="img_url" property="imgUrl" />
        <result column="thumbnail_url" property="thumbnailUrl" />
        <result column="file_type" property="fileType" />
        <result column="upload_time" property="uploadTime" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="created_user_id" property="createdUserId" />
        <result column="updated_user_id" property="updatedUserId" />
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, feedback_id, img_url, thumbnail_url, file_type, upload_time,
        create_time, update_time, created_user_id, updated_user_id
    </sql>

    <!-- 根据反馈ID查询图片列表 -->
    <select id="selectByFeedbackId" resultMap="BaseResultMap">
        SELECT 
            <include refid="Base_Column_List" />
        FROM life_img
        WHERE feedback_id = #{feedbackId}
    </select>

    <!-- 批量插入附件（图片和视频） -->
    <insert id="batchInsert" parameterType="java.util.List">
        INSERT INTO life_img (
            feedback_id, img_url, thumbnail_url, file_type, upload_time,
            create_time, update_time, created_user_id, updated_user_id
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
                #{item.feedbackId},
                #{item.imgUrl},
                #{item.thumbnailUrl},
                #{item.fileType},
                #{item.uploadTime},
                NOW(),
                NOW(),
                #{item.createdUserId},
                #{item.updatedUserId}
            )
        </foreach>
    </insert>

    <!-- 根据反馈ID删除图片 -->
    <delete id="deleteByFeedbackId">
        DELETE FROM life_img
        WHERE feedback_id = #{feedbackId}
    </delete>

    <!-- 查询反馈的图片URL列表 -->
    <select id="selectImgUrlsByFeedbackId" resultType="java.lang.String">
        SELECT img_url
        FROM life_img
        WHERE feedback_id = #{feedbackId}
        AND file_type = 1
        ORDER BY upload_time ASC
    </select>

    <!-- 查询反馈的视频URL列表 -->
    <select id="selectVideoUrlsByFeedbackId" resultType="java.lang.String">
        SELECT img_url
        FROM life_img
        WHERE feedback_id = #{feedbackId}
        AND file_type = 2
        ORDER BY upload_time ASC
    </select>

    <!-- 查询反馈的视频信息（包含视频URL和缩略图URL） -->
    <select id="selectVideoInfoByFeedbackId" resultMap="BaseResultMap">
        SELECT
            <include refid="Base_Column_List" />
        FROM life_img
        WHERE feedback_id = #{feedbackId}
        AND file_type = 2
        ORDER BY upload_time ASC
    </select>

</mapper>
