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

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.resource.TbResource">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="resource_id" property="resourceID" jdbcType="VARCHAR"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="content" property="content" jdbcType="LONGVARCHAR"/>
        <result column="summary" property="summary" jdbcType="VARCHAR"/>
        <result column="cover_image" property="coverImage" jdbcType="VARCHAR"/>
        <result column="category_id" property="categoryID" jdbcType="VARCHAR"/>
        <result column="author" property="author" jdbcType="VARCHAR"/>
        <result column="source" property="source" jdbcType="VARCHAR"/>
        <result column="source_url" property="sourceUrl" jdbcType="VARCHAR"/>
        <result column="view_count" property="viewCount" jdbcType="INTEGER"/>
        <result column="like_count" property="likeCount" jdbcType="INTEGER"/>
        <result column="collect_count" property="collectCount" jdbcType="INTEGER"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="is_recommend" property="isRecommend" jdbcType="BOOLEAN"/>
        <result column="is_banner" property="isBanner" jdbcType="BOOLEAN"/>
        <result column="publish_time" property="publishTime" jdbcType="TIMESTAMP"/>
        <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, resource_id, title, content, summary, cover_image, category_id, author, source, 
        source_url, view_count, like_count, collect_count, status, is_recommend, 
        is_banner, publish_time, 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="categoryID != null and categoryID != ''">
                AND category_id = #{categoryID}
            </if>
            <if test="author != null and author != ''">
                AND author LIKE CONCAT('%', #{author}, '%')
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
            <if test="isRecommend != null">
                AND is_recommend = #{isRecommend}
            </if>
            <if test="isBanner != null">
                AND is_banner = #{isBanner}
            </if>
        </where>
    </sql>

    <!-- selectResources -->
    <select id="selectResources" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_resource
        <include refid="Where_Clause"/>
        ORDER BY publish_time DESC, create_time DESC
    </select>

    <!-- 根据资源ID查询资源信息 -->
    <select id="selectByResourceId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource
        WHERE resource_id = #{resourceId} AND deleted = 0
    </select>

    <!-- 根据分类ID查询资源列表 -->
    <select id="selectByCategoryId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource
        WHERE category_id = #{categoryId} AND deleted = 0
        ORDER BY publish_time DESC, create_time DESC
    </select>

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

    <!-- 根据类型查询资源列表 -->
    <select id="selectByType" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource
        WHERE category_id = #{type} AND deleted = 0
        ORDER BY publish_time DESC, create_time DESC
    </select>

    <!-- 查询热门资源列表 -->
    <select id="selectHotResources" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource
        WHERE status = 1 AND deleted = 0
        ORDER BY view_count DESC, like_count DESC, publish_time DESC
        <if test="limit != null and limit > 0">
            LIMIT #{limit}
        </if>
    </select>

    <!-- 查询最新资源列表 -->
    <select id="selectLatestResources" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource
        WHERE status = 1 AND deleted = 0
        ORDER BY publish_time DESC, create_time DESC
        <if test="limit != null and limit > 0">
            LIMIT #{limit}
        </if>
    </select>

    <!-- 根据关键词搜索资源 -->
    <select id="searchByKeyword" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource
        WHERE (title LIKE CONCAT('%', #{keyword}, '%') 
               OR content LIKE CONCAT('%', #{keyword}, '%')
               OR summary LIKE CONCAT('%', #{keyword}, '%'))
        AND status = 1 AND deleted = 0
        ORDER BY publish_time DESC, create_time DESC
    </select>

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

    <!-- 插入资源 -->
    <insert id="insertResource" parameterType="org.xyzh.common.dto.resource.TbResource">
        INSERT INTO tb_resource (
            id, resource_id, title, content, summary, cover_image, category_id, author, source,
            source_url, view_count, like_count, collect_count, status, is_recommend,
            is_banner, publish_time, creator, updater, create_time, update_time,
            delete_time, deleted
        ) VALUES (
            #{id}, #{resourceID}, #{title}, #{content}, #{summary}, #{coverImage}, #{categoryID}, #{author}, #{source},
            #{sourceUrl}, #{viewCount}, #{likeCount}, #{collectCount}, #{status}, #{isRecommend},
            #{isBanner}, #{publishTime}, #{creator}, #{updater}, #{createTime}, #{updateTime},
            #{deleteTime}, #{deleted}
        )
    </insert>

    <!-- 更新资源 -->
    <update id="updateResource" parameterType="org.xyzh.common.dto.resource.TbResource">
        UPDATE tb_resource
        <set>
            <if test="title != null and title != ''">
                title = #{title},
            </if>
            <if test="content != null and content != ''">
                content = #{content},
            </if>
            <if test="summary != null and summary != ''">
                summary = #{summary},
            </if>
            <if test="coverImage != null and coverImage != ''">
                cover_image = #{coverImage},
            </if>
            <if test="categoryID != null and categoryID != ''">
                category_id = #{categoryID},
            </if>
            <if test="author != null and author != ''">
                author = #{author},
            </if>
            <if test="source != null and source != ''">
                source = #{source},
            </if>
            <if test="sourceUrl != null and sourceUrl != ''">
                source_url = #{sourceUrl},
            </if>
            <if test="viewCount != null">
                view_count = #{viewCount},
            </if>
            <if test="likeCount != null">
                like_count = #{likeCount},
            </if>
            <if test="collectCount != null">
                collect_count = #{collectCount},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="isRecommend != null">
                is_recommend = #{isRecommend},
            </if>
            <if test="isBanner != null">
                is_banner = #{isBanner},
            </if>
            <if test="publishTime != null">
                publish_time = #{publishTime},
            </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 resource_id = #{resourceID}
    </update>

    <!-- 删除资源 -->
    <delete id="deleteResource" parameterType="org.xyzh.common.dto.resource.TbResource">
        DELETE FROM tb_resource
        WHERE resource_id = #{resourceID}
    </delete>

    <!-- 批量插入资源 -->
    <insert id="batchInsertResources" parameterType="java.util.List">
        INSERT INTO tb_resource (
            id, resource_id, title, content, summary, cover_image, category_id, author, source,
            source_url, view_count, like_count, collect_count, status, is_recommend,
            is_banner, publish_time, creator, updater, create_time, update_time,
            delete_time, deleted
        ) VALUES
        <foreach collection="resourceList" item="item" separator=",">
            (
                #{item.id}, #{item.resourceID}, #{item.title}, #{item.content}, #{item.summary}, #{item.coverImage}, 
                #{item.categoryID}, #{item.author}, #{item.source}, #{item.sourceUrl}, 
                #{item.viewCount}, #{item.likeCount}, #{item.collectCount}, #{item.status}, 
                #{item.isRecommend}, #{item.isBanner}, #{item.publishTime}, #{item.creator}, 
                #{item.updater}, #{item.createTime}, #{item.updateTime}, #{item.deleteTime}, #{item.deleted}
            )
        </foreach>
    </insert>

    <!-- 批量删除资源 -->
    <delete id="batchDeleteResources">
        DELETE FROM tb_resource
        WHERE resource_id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{resourceID}
        </foreach>
    </delete>

    <!-- 分页查询资源 -->
    <select id="selectResourcesPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource
        <where>
            deleted = 0
            <if test="filter.title != null and filter.title != ''">
                AND title LIKE CONCAT('%', #{filter.title}, '%')
            </if>
            <if test="filter.categoryID != null and filter.categoryID != ''">
                AND category_id = #{filter.categoryID}
            </if>
            <if test="filter.author != null and filter.author != ''">
                AND author LIKE CONCAT('%', #{filter.author}, '%')
            </if>
            <if test="filter.status != null">
                AND status = #{filter.status}
            </if>
            <if test="filter.isRecommend != null">
                AND is_recommend = #{filter.isRecommend}
            </if>
            <if test="filter.isBanner != null">
                AND is_banner = #{filter.isBanner}
            </if>
        </where>
        ORDER BY publish_time DESC, create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计资源总数 -->
    <select id="countResources" resultType="long">
        SELECT COUNT(1)
        FROM tb_resource
        <include refid="Where_Clause" />
    </select>

    <!-- updateResourceCollectCount --> 

    <update id="updateResourceCollectCount">
        UPDATE tb_resource
        SET collect_count = collect_count + #{collectionValue}
        WHERE resource_id = #{resourceID}
    </update>
</mapper>
