<?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="com.snack.server.module.category.mapper.CategoryMapper">

    <!-- 通用 ResultMap -->
    <resultMap id="BaseResultMap" type="com.snack.server.module.category.entity.Category">
        <id     property="id"         column="id"/>
        <result property="name"       column="name"/>
        <result property="parentId"   column="parent_id"/>
        <result property="level"      column="level"/>
        <result property="sort"       column="sort"/>
        <result property="icon"       column="icon"/>
        <result property="status"     column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <!-- 递归查询所有子分类 ID（包含自身） -->
    <select id="selectAllChildIds" resultType="java.lang.Long">
        WITH RECURSIVE category_tree AS (
            SELECT id, parent_id
            FROM category
            WHERE id = #{parentId}
            UNION ALL
            SELECT c.id, c.parent_id
            FROM category c
            INNER JOIN category_tree ct ON c.parent_id = ct.id
        )
        SELECT id FROM category_tree
    </select>

</mapper>
