<?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="banner_id" property="bannerID" 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, banner_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="bannerID != null and bannerID != ''">
                AND banner_id = #{bannerID}
            </if>
            <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>
    <sql id="Filter_Clause">
        <where>
            deleted = 0
            <if test="filter.bannerID != null and filter.bannerID != ''">
                AND banner_id = #{filter.bannerID}
            </if>
            <if test="filter.title != null and filter.title != ''">
                AND title LIKE CONCAT('%', #{filter.title}, '%')
            </if>
            <if test="filter.linkType != null">
                AND link_type = #{filter.linkType}
            </if>
            <if test="filter.linkID != null and filter.linkID != ''">
                AND link_id = #{filter.linkID}
            </if>
            <if test="filter.status != null">
                AND status = #{filter.status}
            </if>
        </where>
    </sql>

    <!-- 权限过滤条件（基于dept_path的高效继承） -->
    <sql id="Permission_Filter">
        INNER JOIN tb_resource_permission rp ON b.banner_id = rp.resource_id 
            AND rp.resource_type = 8 
            AND rp.deleted = 0
            AND rp.can_read = 1
            AND (
                (rp.dept_id IS NULL AND rp.role_id IS NULL)
                <if test="userDeptRoles != null and userDeptRoles.size() > 0">
                    OR EXISTS (
                        SELECT 1 
                        FROM (
                            <foreach collection="userDeptRoles" item="udr" separator=" UNION ALL ">
                                SELECT #{udr.deptID} AS dept_id, #{udr.deptPath} AS dept_path, #{udr.roleID} AS role_id
                            </foreach>
                        ) user_roles
                        LEFT JOIN tb_sys_dept perm_dept ON perm_dept.dept_id = rp.dept_id AND perm_dept.deleted = 0
                        WHERE 
                           (rp.role_id IS NULL AND rp.dept_id IS NOT NULL 
                               AND user_roles.dept_path LIKE CONCAT(perm_dept.dept_path, '%'))
                           OR (rp.dept_id IS NULL AND rp.role_id = user_roles.role_id)
                           OR (rp.dept_id = user_roles.dept_id AND rp.role_id = user_roles.role_id)
                    )
                </if>
            )
    </sql>

    <!-- selectBanners - 添加权限过滤 -->
    <select id="selectBanners" resultMap="BaseResultMap">
        SELECT DISTINCT b.*
        FROM tb_banner b
        <include refid="Permission_Filter"/>
        WHERE b.deleted = 0
            <if test="filter.bannerID != null and filter.bannerID != ''">
                AND b.banner_id = #{filter.bannerID}
            </if>
            <if test="filter.title != null and filter.title != ''">
                AND b.title LIKE CONCAT('%', #{filter.title}, '%')
            </if>
            <if test="filter.linkType != null">
                AND b.link_type = #{filter.linkType}
            </if>
            <if test="filter.linkID != null and filter.linkID != ''">
                AND b.link_id = #{filter.linkID}
            </if>
            <if test="filter.status != null">
                AND b.status = #{filter.status}
            </if>
        ORDER BY b.order_num ASC, b.create_time DESC
    </select>

    <!-- selectBannersLimit - 添加权限过滤 -->
    <select id="selectBannersLimit" resultMap="BaseResultMap">
        SELECT DISTINCT b.*
        FROM tb_banner b
        <include refid="Permission_Filter"/>
        WHERE b.deleted = 0
            <if test="filter.bannerID != null and filter.bannerID != ''">
                AND b.banner_id = #{filter.bannerID}
            </if>
            <if test="filter.title != null and filter.title != ''">
                AND b.title LIKE CONCAT('%', #{filter.title}, '%')
            </if>
            <if test="filter.linkType != null">
                AND b.link_type = #{filter.linkType}
            </if>
            <if test="filter.linkID != null and filter.linkID != ''">
                AND b.link_id = #{filter.linkID}
            </if>
            <if test="filter.status != null">
                AND b.status = #{filter.status}
            </if>
        ORDER BY b.order_num ASC, b.create_time DESC
        LIMIT #{limit}
    </select>

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

    <!-- 根据状态查询Banner列表 - 添加权限过滤 -->
    <select id="selectByStatus" resultMap="BaseResultMap">
        SELECT DISTINCT b.*
        FROM tb_banner b
        <include refid="Permission_Filter"/>
        WHERE b.status = #{status} AND b.deleted = 0
        ORDER BY b.order_num ASC, b.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 DISTINCT b.*
        FROM tb_banner b
        <include refid="Permission_Filter"/>
        WHERE b.status = 1 AND b.deleted = 0
        ORDER BY b.order_num ASC, b.create_time DESC
    </select>

    <!-- 检查Banner标题是否存在 - 添加权限过滤 -->
    <select id="countByTitle" resultType="int">
        SELECT COUNT(DISTINCT b.id)
        FROM tb_banner b
        <include refid="Permission_Filter"/>
        WHERE b.title = #{title} AND b.deleted = 0
        <if test="excludeId != null and excludeId != ''">
            AND b.banner_id != #{excludeId}
        </if>
    </select>

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

    <!-- 更新Banner -->
    <update id="updateBanner" parameterType="org.xyzh.common.dto.resource.TbBanner">
        UPDATE tb_banner
        <set>
            <if test="bannerID != null and bannerID != ''">
                banner_id = #{bannerID},
            </if>
            <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 banner_id = #{bannerID}
    </update>

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

    <!-- 批量插入Banner -->
    <insert id="batchInsertBanners" parameterType="java.util.List">
        INSERT INTO tb_banner (
            id, 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.bannerID}, #{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 banner_id IN
        <foreach collection="ids" item="bannerID" open="(" separator="," close=")">
            #{bannerID}
        </foreach>
    </delete>

    <!-- 分页查询Banner -->
    <!-- selectBannersPage - 添加权限过滤 -->
    <select id="selectBannersPage" resultMap="BaseResultMap">
        SELECT DISTINCT b.*
        FROM tb_banner b
        <include refid="Permission_Filter"/>
        WHERE b.deleted = 0
            <if test="filter != null">
                <if test="filter.bannerID != null and filter.bannerID != ''">
                    AND b.banner_id = #{filter.bannerID}
                </if>
                <if test="filter.title != null and filter.title != ''">
                    AND b.title LIKE CONCAT('%', #{filter.title}, '%')
                </if>
                <if test="filter.linkType != null">
                    AND b.link_type = #{filter.linkType}
                </if>
                <if test="filter.linkID != null and filter.linkID != ''">
                    AND b.link_id = #{filter.linkID}
                </if>
                <if test="filter.status != null">
                    AND b.status = #{filter.status}
                </if>
            </if>
        ORDER BY b.order_num ASC, b.create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计Banner总数 - 添加权限过滤 -->
    <select id="countBanners" resultType="long">
        SELECT COUNT(DISTINCT b.id)
        FROM tb_banner b
        <include refid="Permission_Filter"/>
        WHERE b.deleted = 0
            <if test="filter != null">
                <if test="filter.bannerID != null and filter.bannerID != ''">
                    AND b.banner_id = #{filter.bannerID}
                </if>
                <if test="filter.title != null and filter.title != ''">
                    AND b.title LIKE CONCAT('%', #{filter.title}, '%')
                </if>
                <if test="filter.linkType != null">
                    AND b.link_type = #{filter.linkType}
                </if>
                <if test="filter.linkID != null and filter.linkID != ''">
                    AND b.link_id = #{filter.linkID}
                </if>
                <if test="filter.status != null">
                    AND b.status = #{filter.status}
                </if>
            </if>
    </select>

</mapper>
