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

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.resource.TbResourceRecommend">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="resource_id" property="resourceID" jdbcType="VARCHAR"/>
        <result column="order_num" property="orderNum" jdbcType="INTEGER"/>
        <result column="reason" property="reason" jdbcType="VARCHAR"/>
        <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, order_num, reason, creator, updater, create_time, 
        update_time, delete_time, deleted
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            deleted = 0
            <if test="resourceID != null and resourceID != ''">
                AND resource_id = #{resourceID}
            </if>
            <if test="reason != null and reason != ''">
                AND reason LIKE CONCAT('%', #{reason}, '%')
            </if>
        </where>
    </sql>

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

    <!-- 根据推荐ID查询推荐信息 -->
    <select id="selectByRecommendId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource_recommend
        WHERE id = #{recommendId} AND deleted = 0
    </select>

    <!-- 根据资源ID查询推荐列表 -->
    <select id="selectByResourceId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource_recommend
        WHERE resource_id = #{resourceId} AND deleted = 0
        ORDER BY order_num ASC, create_time DESC
    </select>

    <!-- 根据状态查询推荐列表 -->
    <select id="selectByStatus" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource_recommend
        WHERE deleted = 0
        ORDER BY order_num ASC, create_time DESC
    </select>

    <!-- 根据类型查询推荐列表 -->
    <select id="selectByType" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource_recommend
        WHERE deleted = 0
        ORDER BY order_num ASC, create_time DESC
    </select>

    <!-- 查询热门推荐列表 -->
    <select id="selectHotRecommends" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource_recommend
        WHERE deleted = 0
        ORDER BY order_num ASC, create_time DESC
        <if test="limit != null and limit > 0">
            LIMIT #{limit}
        </if>
    </select>

    <!-- 查询最新推荐列表 -->
    <select id="selectLatestRecommends" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource_recommend
        WHERE deleted = 0
        ORDER BY create_time DESC
        <if test="limit != null and limit > 0">
            LIMIT #{limit}
        </if>
    </select>

    <!-- 插入资源推荐 -->
    <insert id="insertResourceRecommend" parameterType="org.xyzh.common.dto.resource.TbResourceRecommend">
        INSERT INTO tb_resource_recommend (
            id, resource_id, order_num, reason, creator, updater, create_time,
            update_time, delete_time, deleted
        ) VALUES (
            #{id}, #{resourceID}, #{orderNum}, #{reason}, #{creator}, #{updater}, #{createTime},
            #{updateTime}, #{deleteTime}, #{deleted}
        )
    </insert>

    <!-- 更新资源推荐 -->
    <update id="updateResourceRecommend" parameterType="org.xyzh.common.dto.resource.TbResourceRecommend">
        UPDATE tb_resource_recommend
        <set>
            <if test="resourceID != null and resourceID != ''">
                resource_id = #{resourceID},
            </if>
            <if test="orderNum != null">
                order_num = #{orderNum},
            </if>
            <if test="reason != null and reason != ''">
                reason = #{reason},
            </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>

    <!-- 删除资源推荐 -->
    <delete id="deleteResourceRecommend" parameterType="org.xyzh.common.dto.resource.TbResourceRecommend">
        DELETE FROM tb_resource_recommend
        WHERE id = #{id}
    </delete>

    <!-- 批量插入资源推荐 -->
    <insert id="batchInsertResourceRecommends" parameterType="java.util.List">
        INSERT INTO tb_resource_recommend (
            id, resource_id, order_num, reason, creator, updater, create_time,
            update_time, delete_time, deleted
        ) VALUES
        <foreach collection="resourceRecommendList" item="item" separator=",">
            (
                #{item.id}, #{item.resourceID}, #{item.orderNum}, #{item.reason}, #{item.creator}, 
                #{item.updater}, #{item.createTime}, #{item.updateTime}, #{item.deleteTime}, #{item.deleted}
            )
        </foreach>
    </insert>

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

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

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

</mapper>
