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

    <!-- 结果映射 -->
    <resultMap id="PromotionPosterResultMap" type="com.dora.entity.PromotionPoster">
        <id column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="description" property="description"/>
        <result column="image_url" property="imageUrl"/>
        <result column="sort_order" property="sortOrder"/>
        <result column="is_enabled" property="isEnabled"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>

    <!-- 根据ID查询推广海报 -->
    <select id="selectById" resultMap="PromotionPosterResultMap">
        SELECT 
            id, title, description, image_url,
            sort_order, is_enabled, create_time, update_time, is_deleted
        FROM promotion_poster 
        WHERE id = #{id} AND is_deleted = 0
    </select>

    <!-- 查询所有启用的推广海报列表（按排序） -->
    <select id="selectAllEnabled" resultMap="PromotionPosterResultMap">
        SELECT 
            id, title, description, image_url,
            sort_order, is_enabled, create_time, update_time, is_deleted
        FROM promotion_poster 
        WHERE is_enabled = 1 
          AND is_deleted = 0
        ORDER BY sort_order ASC, id ASC
    </select>

    <!-- 分页查询推广海报列表（管理员用） -->
    <select id="selectByPage" resultMap="PromotionPosterResultMap">
        SELECT 
            id, title, description, image_url,
            sort_order, is_enabled, create_time, update_time, is_deleted
        FROM promotion_poster 
        WHERE is_deleted = 0
        ORDER BY sort_order ASC, id ASC
        LIMIT #{offset}, #{limit}
    </select>

    <!-- 统计推广海报总数 -->
    <select id="countAll" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM promotion_poster 
        WHERE is_deleted = 0
    </select>

    <!-- 插入推广海报 -->
    <insert id="insert" parameterType="com.dora.entity.PromotionPoster" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO promotion_poster (
            title, description, image_url,
            sort_order, is_enabled, create_time, update_time, is_deleted
        ) VALUES (
            #{title}, #{description}, #{imageUrl},
            #{sortOrder}, #{isEnabled}, #{createTime}, #{updateTime}, #{isDeleted}
        )
    </insert>

    <!-- 更新推广海报 -->
    <update id="updateById" parameterType="com.dora.entity.PromotionPoster">
        UPDATE promotion_poster SET 
            title = #{title},
            description = #{description},
            image_url = #{imageUrl},
            sort_order = #{sortOrder},
            is_enabled = #{isEnabled},
            update_time = #{updateTime}
        WHERE id = #{id} AND is_deleted = 0
    </update>

    <!-- 逻辑删除推广海报 -->
    <update id="deleteById">
        UPDATE promotion_poster SET 
            is_deleted = 1,
            update_time = NOW()
        WHERE id = #{id}
    </update>

    <!-- 批量更新排序 -->
    <update id="batchUpdateSortOrder">
        UPDATE promotion_poster 
        SET sort_order = CASE 
            <foreach collection="promotionPosters" item="promotionPoster">
                WHEN id = #{promotionPoster.id} THEN #{promotionPoster.sortOrder}
            </foreach>
            ELSE sort_order
        END,
        update_time = NOW()
        WHERE id IN 
        <foreach collection="promotionPosters" item="promotionPoster" open="(" separator="," close=")">
            #{promotionPoster.id}
        </foreach>
    </update>

    <!-- 更新推广海报状态 -->
    <update id="updateStatus">
        UPDATE promotion_poster SET 
            is_enabled = #{isEnabled},
            update_time = #{updateTime}
        WHERE id = #{id} AND is_deleted = 0
    </update>

</mapper>
