<?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="org.xyzh.news.mapper.BannerMapper">

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.resource.TbBanner">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="image_url" property="imageUrl" jdbcType="VARCHAR"/>
        <result column="link_type" property="linkType" jdbcType="INTEGER"/>
        <result column="link_id" property="linkID" jdbcType="VARCHAR"/>
        <result column="link_url" property="linkUrl" jdbcType="VARCHAR"/>
        <result column="order_num" property="orderNum" jdbcType="INTEGER"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, title, image_url, link_type, link_id, link_url, order_num, status, 
        creator, updater, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            deleted = 0
            <if test="title != null and title != ''">
                AND title LIKE CONCAT('%', #{title}, '%')
            </if>
            <if test="linkType != null">
                AND link_type = #{linkType}
            </if>
            <if test="linkID != null and linkID != ''">
                AND link_id = #{linkID}
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
        </where>
    </sql>

    <!-- selectBanners -->
    <select id="selectBanners" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_banner
        <include refid="Where_Clause"/>
        ORDER BY order_num ASC, create_time DESC
    </select>

    <!-- 根据Banner ID查询Banner信息 -->
    <select id="selectByBannerId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_banner
        WHERE id = #{bannerId} AND deleted = 0
    </select>

    <!-- 根据状态查询Banner列表 -->
    <select id="selectByStatus" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_banner
        WHERE status = #{status} AND deleted = 0
        ORDER BY order_num ASC, create_time DESC
    </select>

    <!-- 根据位置查询Banner列表 -->
    <select id="selectByPosition" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_banner
        WHERE link_type = #{position} AND deleted = 0
        ORDER BY order_num ASC, create_time DESC
    </select>

    <!-- 查询有效的Banner列表（按排序） -->
    <select id="selectActiveBanners" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_banner
        WHERE status = 1 AND deleted = 0
        ORDER BY order_num ASC, create_time DESC
    </select>

    <!-- 检查Banner标题是否存在 -->
    <select id="countByTitle" resultType="int">
        SELECT COUNT(1)
        FROM tb_banner
        WHERE title = #{title} AND deleted = 0
        <if test="excludeId != null and excludeId != ''">
            AND id != #{excludeId}
        </if>
    </select>

    <!-- 插入Banner -->
    <insert id="insertBanner" parameterType="org.xyzh.common.dto.resource.TbBanner">
        INSERT INTO tb_banner (
            id, title, image_url, link_type, link_id, link_url, order_num, status,
            creator, updater, create_time, update_time, delete_time, deleted
        ) VALUES (
            #{id}, #{title}, #{imageUrl}, #{linkType}, #{linkID}, #{linkUrl}, #{orderNum}, #{status},
            #{creator}, #{updater}, #{createTime}, #{updateTime}, #{deleteTime}, #{deleted}
        )
    </insert>

    <!-- 更新Banner -->
    <update id="updateBanner" parameterType="org.xyzh.common.dto.resource.TbBanner">
        UPDATE tb_banner
        <set>
            <if test="title != null and title != ''">
                title = #{title},
            </if>
            <if test="imageUrl != null and imageUrl != ''">
                image_url = #{imageUrl},
            </if>
            <if test="linkType != null">
                link_type = #{linkType},
            </if>
            <if test="linkID != null and linkID != ''">
                link_id = #{linkID},
            </if>
            <if test="linkUrl != null and linkUrl != ''">
                link_url = #{linkUrl},
            </if>
            <if test="orderNum != null">
                order_num = #{orderNum},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="updater != null and updater != ''">
                updater = #{updater},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="deleteTime != null">
                delete_time = #{deleteTime},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- 删除Banner -->
    <delete id="deleteBanner" parameterType="org.xyzh.common.dto.resource.TbBanner">
        DELETE FROM tb_banner
        WHERE id = #{id}
    </delete>

    <!-- 批量插入Banner -->
    <insert id="batchInsertBanners" parameterType="java.util.List">
        INSERT INTO tb_banner (
            id, title, image_url, link_type, link_id, link_url, order_num, status,
            creator, updater, create_time, update_time, delete_time, deleted
        ) VALUES
        <foreach collection="bannerList" item="item" separator=",">
            (
                #{item.id}, #{item.title}, #{item.imageUrl}, #{item.linkType}, #{item.linkID}, 
                #{item.linkUrl}, #{item.orderNum}, #{item.status}, #{item.creator}, #{item.updater}, 
                #{item.createTime}, #{item.updateTime}, #{item.deleteTime}, #{item.deleted}
            )
        </foreach>
    </insert>

    <!-- 批量删除Banner -->
    <delete id="batchDeleteBanners">
        DELETE FROM tb_banner
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 分页查询Banner -->
    <select id="selectBannersPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_banner
        <include refid="Where_Clause" />
        ORDER BY order_num ASC, create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计Banner总数 -->
    <select id="countBanners" resultType="long">
        SELECT COUNT(1)
        FROM tb_banner
        <include refid="Where_Clause" />
    </select>

</mapper>
