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

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.resource.TbResourceTag">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="resource_id" property="resourceID" jdbcType="VARCHAR"/>
        <result column="tag_id" property="tagID" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="TagResultMap" type="org.xyzh.common.vo.TagVO">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="resource_id" property="resourceID" jdbcType="VARCHAR"/>
        <result column="tag_id" property="tagID" jdbcType="VARCHAR"/>
        <result column="tag_name" property="tagName" jdbcType="VARCHAR"/>
        <result column="tag_color" property="tagColor" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, resource_id, tag_id, creator, create_time
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            <if test="resourceID != null and resourceID != ''">
                AND resource_id = #{resourceID}
            </if>
            <if test="tagID != null and tagID != ''">
                AND tag_id = #{tagID}
            </if>
        </where>
    </sql>

    <!-- selectResourceTags -->
    <select id="selectResourceTags" resultMap="TagResultMap">
        SELECT 
        rt.id, rt.resource_id, rt.tag_id, t.name as tag_name, t.color as tag_color, rt.creator, rt.create_time
        FROM tb_resource_tag rt
        LEFT JOIN tb_tag t ON rt.tag_id = t.id
        WHERE 1=1
        <if test="resourceID != null and resourceID != ''">
            AND rt.resource_id = #{resourceID}
        </if>
        <if test="tagID != null and tagID != ''">
            AND rt.tag_id = #{tagID}
        </if>
        <if test="creator != null and creator != ''">
            AND rt.creator = #{creator}
        </if>
        <if test="createTime != null">
            AND rt.create_time = #{createTime}
        </if>
        ORDER BY rt.create_time DESC
    </select>

    <!-- 根据关联ID查询关联信息 -->
    <select id="selectByRelationId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource_tag
        WHERE id = #{relationId}
    </select>

    <!-- 根据资源ID查询标签关联列表 -->
    <select id="selectByResourceId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource_tag
        WHERE resource_id = #{resourceId}
        ORDER BY create_time DESC
    </select>

    <!-- 根据标签ID查询资源关联列表 -->
    <select id="selectByTagId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource_tag
        WHERE tag_id = #{tagId}
        ORDER BY create_time DESC
    </select>

    <!-- 根据资源ID和标签ID查询关联信息 -->
    <select id="selectByResourceIdAndTagId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource_tag
        WHERE resource_id = #{resourceId} AND tag_id = #{tagId}
    </select>

    <!-- 检查资源标签关联是否存在 -->
    <select id="countByResourceIdAndTagId" resultType="int">
        SELECT COUNT(1)
        FROM tb_resource_tag
        WHERE resource_id = #{resourceId} AND tag_id = #{tagId}
        <if test="excludeId != null and excludeId != ''">
            AND id != #{excludeId}
        </if>
    </select>

    <!-- 插入资源标签关联 -->
    <insert id="insertResourceTag" parameterType="org.xyzh.common.dto.resource.TbResourceTag">
        INSERT INTO tb_resource_tag (
            id, resource_id, tag_id, creator, create_time
        ) VALUES (
            #{id}, #{resourceID}, #{tagID}, #{creator}, #{createTime}
        )
    </insert>

    <!-- 更新资源标签关联 -->
    <update id="updateResourceTag" parameterType="org.xyzh.common.dto.resource.TbResourceTag">
        UPDATE tb_resource_tag
        <set>
            <if test="resourceID != null and resourceID != ''">
                resource_id = #{resourceID},
            </if>
            <if test="tagID != null and tagID != ''">
                tag_id = #{tagID},
            </if>
            <if test="creator != null and creator != ''">
                creator = #{creator},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- 删除资源标签关联 -->
    <delete id="deleteResourceTag" parameterType="org.xyzh.common.dto.resource.TbResourceTag">
        DELETE FROM tb_resource_tag
        WHERE id = #{id}
    </delete>

    <!-- 批量插入资源标签关联 -->
    <insert id="batchInsertResourceTags" parameterType="java.util.List">
        INSERT INTO tb_resource_tag (
            id, resource_id, tag_id, creator, create_time
        ) VALUES
        <foreach collection="resourceTagList" item="item" separator=",">
            (
                #{item.id}, #{item.resourceID}, #{item.tagID}, #{item.creator}, #{item.createTime}
            )
        </foreach>
    </insert>

    <!-- 批量删除资源标签关联 -->
    <delete id="batchDeleteResourceTags">
        DELETE FROM tb_resource_tag
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 根据资源ID批量删除标签关联 -->
    <delete id="deleteByResourceId">
        DELETE FROM tb_resource_tag
        WHERE resource_id = #{resourceId}
    </delete>

    <!-- 根据标签ID批量删除资源关联 -->
    <delete id="deleteByTagId">
        DELETE FROM tb_resource_tag
        WHERE tag_id = #{tagId}
    </delete>

    <!-- 分页查询资源标签关联 -->
    <select id="selectResourceTagsPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource_tag
        <include refid="Where_Clause" />
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计资源标签关联总数 -->
    <select id="countResourceTags" resultType="long">
        SELECT COUNT(1)
        FROM tb_resource_tag
        <include refid="Where_Clause" />
    </select>

</mapper>
