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

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.resource.TbResourceCategory">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="category_id" property="categoryID" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="parent_id" property="parentID" jdbcType="VARCHAR"/>
        <result column="description" property="description" jdbcType="VARCHAR"/>
        <result column="icon" property="icon" jdbcType="VARCHAR"/>
        <result column="order_num" property="orderNum" 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, category_id, name, parent_id, description, icon, order_num, 
        creator, updater, create_time, update_time, delete_time, deleted
    </sql>

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

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

    <!-- 根据分类ID查询分类信息 -->
    <select id="selectByCategoryId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource_category
        WHERE category_id = #{categoryId} AND deleted = 0
    </select>

    <!-- 根据父分类ID查询子分类列表 -->
    <select id="selectByParentId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource_category
        WHERE parent_id = #{parentId} AND deleted = 0
        ORDER BY order_num ASC, create_time ASC
    </select>

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

    <!-- 查询分类树结构 -->
    <select id="selectCategoryTree" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_resource_category
        WHERE deleted = 0
        ORDER BY order_num ASC, create_time ASC
    </select>

    <!-- 检查分类名称是否存在 -->
    <select id="countByName" resultType="int">
        SELECT COUNT(1)
        FROM tb_resource_category
        WHERE name = #{name} AND deleted = 0
        <if test="excludeId != null and excludeId != ''">
            AND id != #{excludeId}
        </if>
    </select>

    <!-- 插入资源分类 -->
    <insert id="insertResourceCategory" parameterType="org.xyzh.common.dto.resource.TbResourceCategory">
        INSERT INTO tb_resource_category (
            id, category_id, name, parent_id, description, icon, order_num,
            creator, updater, create_time, update_time, delete_time, deleted
        ) VALUES (
            #{id}, #{categoryID}, #{name}, #{parentID}, #{description}, #{icon}, #{orderNum},
            #{creator}, #{updater}, #{createTime}, #{updateTime}, #{deleteTime}, #{deleted}
        )
    </insert>

    <!-- 更新资源分类 -->
    <update id="updateResourceCategory" parameterType="org.xyzh.common.dto.resource.TbResourceCategory">
        UPDATE tb_resource_category
        <set>
            <if test="categoryID != null and categoryID != ''">
                category_id = #{categoryID},
            </if>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="parentID != null and parentID != ''">
                parent_id = #{parentID},
            </if>
            <if test="description != null and description != ''">
                description = #{description},
            </if>
            <if test="icon != null and icon != ''">
                icon = #{icon},
            </if>
            <if test="orderNum != null">
                order_num = #{orderNum},
            </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="deleteResourceCategory" parameterType="org.xyzh.common.dto.resource.TbResourceCategory">
        DELETE FROM tb_resource_category
        WHERE id = #{id}
    </delete>

    <!-- 批量插入资源分类 -->
    <insert id="batchInsertResourceCategories" parameterType="java.util.List">
        INSERT INTO tb_resource_category (
            id, category_id, name, parent_id, description, icon, order_num,
            creator, updater, create_time, update_time, delete_time, deleted
        ) VALUES
        <foreach collection="resourceCategoryList" item="item" separator=",">
            (
                #{item.id}, #{item.categoryID}, #{item.name}, #{item.parentID}, #{item.description}, 
                #{item.icon}, #{item.orderNum}, #{item.creator}, #{item.updater}, 
                #{item.createTime}, #{item.updateTime}, #{item.deleteTime}, #{item.deleted}
            )
        </foreach>
    </insert>

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

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

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

</mapper>
