feat: enhance product_category table with global taxonomy structure and additional fields

This commit is contained in:
BXY 2026-10-09 09:36:28 +08:00
parent 339558075e
commit 86bdac8b72

View File

@ -152,37 +152,55 @@ COMMENT ON COLUMN product.product_catalog_bindings.category_id IS '目录分类
CREATE INDEX IF NOT EXISTS idx_product_catalog_bindings_category_id
ON product.product_catalog_bindings (category_id);
-- Store-managed product taxonomy tables. This taxonomy is distinct from catalog_category.
-- Global standard product taxonomy. This taxonomy is distinct from catalog_category.
CREATE TABLE IF NOT EXISTS product.product_category (
id BIGINT PRIMARY KEY,
tenant_id BIGINT NOT NULL,
store_id BIGINT NOT NULL,
category_code VARCHAR(64) NOT NULL,
name VARCHAR(100) NOT NULL,
parent_id BIGINT REFERENCES product.product_category(id) ON DELETE RESTRICT,
level SMALLINT NOT NULL CHECK (level BETWEEN 1 AND 4),
is_leaf BOOLEAN NOT NULL DEFAULT TRUE,
sort_num INTEGER NOT NULL DEFAULT 0,
level SMALLINT NOT NULL CHECK (level >= 1),
name VARCHAR(100) NOT NULL,
priority INTEGER NOT NULL DEFAULT 0,
rule_id VARCHAR(64) NOT NULL,
status SMALLINT NOT NULL DEFAULT 1 CHECK (status IN (0, 1)),
remark VARCHAR(500) NOT NULL DEFAULT '',
category_type SMALLINT NOT NULL,
channel_list INTEGER[] NOT NULL DEFAULT '{}'::INTEGER[],
has_children BOOLEAN NOT NULL DEFAULT FALSE,
has_prop_required BOOLEAN NOT NULL DEFAULT FALSE,
extra TEXT NOT NULL DEFAULT '',
cert_status_code INTEGER NOT NULL,
cert_status_pass BOOLEAN NOT NULL DEFAULT FALSE,
cert_status_msg VARCHAR(500) NOT NULL DEFAULT '',
platform_category_id BIGINT NOT NULL CHECK (platform_category_id > 0),
platform_parent_category_id BIGINT NOT NULL CHECK (platform_parent_category_id >= 0),
version INTEGER NOT NULL DEFAULT 0 CHECK (version >= 0),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ
deleted_at TIMESTAMPTZ,
CONSTRAINT chk_product_category_hierarchy CHECK (
(parent_id IS NULL AND level = 1)
OR (parent_id IS NOT NULL AND level > 1)
)
);
COMMENT ON TABLE product.product_category IS '店铺商品分类表:最多四级的业务商品分类树,与目录分类/分组表独立';
COMMENT ON COLUMN product.product_category.category_code IS '店铺内商品分类编码';
COMMENT ON COLUMN product.product_category.is_leaf IS '是否叶子分类;仅启用的叶子分类允许绑定商品';
COMMENT ON COLUMN product.product_category.status IS '分类状态:0 停用,1 启用';
COMMENT ON TABLE product.product_category IS '全局标准商品类目树,与目录分类/分组表独立';
COMMENT ON COLUMN product.product_category.platform_category_id IS '外部平台分类节点 ID;当前映射有赞响应节点的 id';
COMMENT ON COLUMN product.product_category.platform_parent_category_id IS '外部平台父分类 ID;当前映射有赞响应节点的 parentId,一级节点为 0';
COMMENT ON COLUMN product.product_category.has_children IS '上游声明的是否存在下级类目,不以本地直接子节点数量推断';
COMMENT ON COLUMN product.product_category.deleted_at IS '软删除时间;NULL 表示未删除';
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_category_store_code_active
ON product.product_category (store_id, category_code) WHERE deleted_at IS NULL;
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_category_store_parent_name_active
ON product.product_category (store_id, COALESCE(parent_id, 0), lower(btrim(name))) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_product_category_store_parent_sort
ON product.product_category (store_id, parent_id, sort_num, id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_product_category_active_parent_name
ON product.product_category (COALESCE(parent_id, 0), lower(btrim(name))) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_product_category_active_parent_priority
ON product.product_category (parent_id, priority, id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_product_category_active_status_parent_priority
ON product.product_category (status, parent_id, priority, id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_product_category_rule_id
ON product.product_category (rule_id) WHERE deleted_at IS NULL;
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_category_active_platform_category
ON product.product_category (platform_category_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_product_category_active_platform_parent_category
ON product.product_category (platform_parent_category_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_product_category_channel_list
ON product.product_category USING GIN (channel_list);
CREATE TABLE IF NOT EXISTS product.product_category_bindings (
id BIGINT PRIMARY KEY,
@ -195,7 +213,7 @@ CREATE TABLE IF NOT EXISTS product.product_category_bindings (
deleted_at TIMESTAMPTZ
);
COMMENT ON TABLE product.product_category_bindings IS '店铺商品分类绑定表:存储 SPU 与叶子商品分类的多对多关系';
COMMENT ON TABLE product.product_category_bindings IS '商品标准类目绑定表:存储 SPU 与全局标准商品类目的多对多关系';
COMMENT ON COLUMN product.product_category_bindings.deleted_at IS '软删除时间,便于恢复历史绑定';
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_category_bindings_active