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

    <!-- 结果映射 -->
    <resultMap id="BannerResultMap" type="com.dora.entity.Banner">
        <id column="id" property="id"/>
        <result column="image" property="image"/>
        <result column="title" property="title"/>
        <result column="description" property="description"/>
        <result column="button_text" property="buttonText"/>
        <result column="link_type" property="linkType"/>
        <result column="link" property="link"/>
        <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查询Banner -->
    <select id="selectById" resultMap="BannerResultMap">
        SELECT 
            id, image, title, description, button_text, link_type, link,
            sort_order, is_enabled, create_time, update_time, is_deleted
        FROM banner 
        WHERE id = #{id} AND is_deleted = 0
    </select>

    <!-- 查询所有启用的Banner列表（按排序） -->
    <select id="selectAllEnabled" resultMap="BannerResultMap">
        SELECT 
            id, image, title, description, button_text, link_type, link,
            sort_order, is_enabled, create_time, update_time, is_deleted
        FROM banner 
        WHERE is_enabled = 1 
          AND is_deleted = 0
        ORDER BY sort_order ASC, id ASC
    </select>

    <!-- 分页查询Banner列表（管理员用） -->
    <select id="selectByPage" resultMap="BannerResultMap">
        SELECT 
            id, image, title, description, button_text, link_type, link,
            sort_order, is_enabled, create_time, update_time, is_deleted
        FROM banner 
        WHERE is_deleted = 0
        ORDER BY sort_order ASC, id ASC
        LIMIT #{offset}, #{limit}
    </select>

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

    <!-- 插入Banner -->
    <insert id="insert" parameterType="com.dora.entity.Banner" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO banner (
            image, title, description, button_text, link_type, link,
            sort_order, is_enabled, create_time, update_time, is_deleted
        ) VALUES (
            #{image}, #{title}, #{description}, #{buttonText}, #{linkType}, #{link},
            #{sortOrder}, #{isEnabled}, #{createTime}, #{updateTime}, #{isDeleted}
        )
    </insert>

    <!-- 更新Banner -->
    <update id="updateById" parameterType="com.dora.entity.Banner">
        UPDATE banner SET 
            image = #{image},
            title = #{title},
            description = #{description},
            button_text = #{buttonText},
            link_type = #{linkType},
            link = #{link},
            sort_order = #{sortOrder},
            is_enabled = #{isEnabled},
            update_time = #{updateTime}
        WHERE id = #{id} AND is_deleted = 0
    </update>

    <!-- 逻辑删除Banner -->
    <update id="deleteById">
        UPDATE banner SET 
            is_deleted = 1,
            update_time = NOW()
        WHERE id = #{id}
    </update>

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

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

</mapper>
