-----------张斌2025-06-16 15:00:00--------------

--------------------pgsql创建assets_category表语句---------------------------

-- 分类主表
CREATE TABLE IF NOT EXISTS assets_category (
    -- 基础字段
    id BIGSERIAL PRIMARY KEY,
    creator VARCHAR(64) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleter VARCHAR(64) NOT NULL,
    deleted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    is_deleted BOOLEAN NOT NULL DEFAULT FALSE,

    -- 分类核心字段
    bid VARCHAR(64) NOT NULL,
    name VARCHAR(128) NOT NULL,
    parent_id VARCHAR(64) DEFAULT '',
    path VARCHAR(512) DEFAULT '',
    level INT NOT NULL DEFAULT 0,
    is_leaf_node BOOLEAN NOT NULL DEFAULT FALSE,
    sort INT NOT NULL DEFAULT 0,
    image VARCHAR(256) DEFAULT '',
    attrs JSONB DEFAULT '[]'::JSONB,
    status SMALLINT NOT NULL DEFAULT 1,

    -- 唯一索引 & 普通索引
    CONSTRAINT uk_category_id UNIQUE (bid)
);
-- 为分类表添加索引（PostgreSQL 索引单独创建）
CREATE INDEX idx_category_parent_id ON assets_category(parent_id);
CREATE INDEX idx_category_level ON assets_category(level);
CREATE INDEX idx_category_status ON assets_category(status);
CREATE INDEX idx_category_is_leaf_node ON assets_category(is_leaf_node);

-- 分类表字段注释
COMMENT ON TABLE assets_category IS '商品/服务分类表';
COMMENT ON COLUMN assets_category.id IS '主键ID';
COMMENT ON COLUMN assets_category.creator IS '创建人';
COMMENT ON COLUMN assets_category.created_at IS '创建时间';
COMMENT ON COLUMN assets_category.updater IS '更新人';
COMMENT ON COLUMN assets_category.updated_at IS '更新时间';
COMMENT ON COLUMN assets_category.deleter IS '删除人（软删）';
COMMENT ON COLUMN assets_category.deleted_at IS '删除时间（软删）';
COMMENT ON COLUMN assets_category.is_deleted IS '是否删除（软删）';
COMMENT ON COLUMN assets_category.bid IS '业务ID';
COMMENT ON COLUMN assets_category.name IS '分类名称';
COMMENT ON COLUMN assets_category.parent_id IS '父分类ID，为空表示根分类';
COMMENT ON COLUMN assets_category.path IS '分类路径，如：/root/parent/child';
COMMENT ON COLUMN assets_category.level IS '分类层级';
COMMENT ON COLUMN assets_category.is_leaf_node IS '是否叶子节点';
COMMENT ON COLUMN assets_category.sort IS '排序';
COMMENT ON COLUMN assets_category.image IS '分类图片';
COMMENT ON COLUMN assets_category.attrs IS '分类属性列表，结构参考 CategoryAttr 实体';
COMMENT ON COLUMN assets_category.status IS '状态：1启用/0禁用';

--------------------pgsql创建assets_category表语句---------------------------


--------------------pgsql创建assets_asset表语句---------------------------
-- 资产表（asset）
CREATE TABLE IF NOT EXISTS assets_asset (
    -- 嵌入基础字段（复用 SQLBaseDO 结构）
    id BIGSERIAL PRIMARY KEY,
    creator VARCHAR(64) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleter VARCHAR(64) NOT NULL,
    deleted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    is_deleted BOOLEAN NOT NULL DEFAULT FALSE,

    -- 基础信息字段
    bid VARCHAR(64) NOT NULL,
    name VARCHAR(128) NOT NULL,
    description TEXT DEFAULT '',
    type VARCHAR(32) NOT NULL, -- 资产类型：physical实物/virtual虚拟/service服务
    category_id BIGINT NOT NULL, -- 分类ID（对应分类表category_id，实体中定义为uint64，PG用BIGINT适配）
    category_path VARCHAR(512) DEFAULT '',
    image_url VARCHAR(512) DEFAULT '',
    images JSONB DEFAULT '[]'::JSONB, -- 图片列表（字符串数组，JSONB存储）
    status SMALLINT NOT NULL DEFAULT 1, -- 资产状态：1启用/0停用
    base_price INT NOT NULL DEFAULT 0, -- 基础价格（分为单位）
    currency VARCHAR(16) DEFAULT 'CNY', -- 货币单位，默认CNY
    unlimited_stock BOOLEAN NOT NULL DEFAULT FALSE, -- 是否无库存限制
    stock_mode SMALLINT NOT NULL DEFAULT 1, -- 库存管理模式：1-明细模式 2-批次模式
    online_time TIMESTAMP, -- 上线时间（可为空）
    offline_time TIMESTAMP, -- 下线时间（可为空）

-- 类型专用配置（JSONB存储）
    physical_asset_config JSONB, -- 实物资产配置
    service_asset_config JSONB, -- 服务资产配置
    virtual_asset_config JSONB, -- 虚拟资产配置
    metadata JSONB, -- 扩展字段（动态元数据）

-- 租户相关
    tenant_module_type VARCHAR(64) DEFAULT '',

    -- 唯一索引
    CONSTRAINT uk_asset_id UNIQUE (bid)
);

-- 为资产表添加索引（优化查询性能）
CREATE INDEX idx_asset_category_id ON assets_asset(category_id);
CREATE INDEX idx_asset_type ON assets_asset(type);
CREATE INDEX idx_asset_status ON assets_asset(status);
CREATE INDEX idx_asset_online_time ON assets_asset(online_time);
CREATE INDEX idx_asset_offline_time ON assets_asset(offline_time);
CREATE INDEX idx_asset_tenant_module_type ON assets_asset(tenant_module_type);

-- 为资产表添加注释（PostgreSQL 专属语法）
COMMENT ON TABLE assets_asset IS '资产主表';
COMMENT ON COLUMN assets_asset.id IS '主键ID';
COMMENT ON COLUMN assets_asset.creator IS '创建人';
COMMENT ON COLUMN assets_asset.created_at IS '创建时间';
COMMENT ON COLUMN assets_asset.updater IS '更新人';
COMMENT ON COLUMN assets_asset.updated_at IS '更新时间';
COMMENT ON COLUMN assets_category.deleter IS '删除人（软删）';
COMMENT ON COLUMN assets_category.deleted_at IS '删除时间（软删）';
COMMENT ON COLUMN assets_asset.is_deleted IS '是否删除（软删）';
COMMENT ON COLUMN assets_asset.bid IS '业务ID';
COMMENT ON COLUMN assets_asset.name IS '资产名称';
COMMENT ON COLUMN assets_asset.description IS '资产描述';
COMMENT ON COLUMN assets_asset.type IS '资产类型：physical实物/virtual虚拟/service服务';
COMMENT ON COLUMN assets_asset.category_id IS '分类ID';
COMMENT ON COLUMN assets_asset.category_path IS '分类路径';
COMMENT ON COLUMN assets_asset.image_url IS '主图URL';
COMMENT ON COLUMN assets_asset.images IS '图片列表(JSONB)';
COMMENT ON COLUMN assets_asset.status IS '资产状态：1启用/0停用';
COMMENT ON COLUMN assets_asset.base_price IS '基础价格（分为单位）';
COMMENT ON COLUMN assets_asset.currency IS '货币单位，默认CNY';
COMMENT ON COLUMN assets_asset.unlimited_stock IS '是否无库存限制';
COMMENT ON COLUMN assets_asset.stock_mode IS '库存管理模式：1-明细模式 2-批次模式';
COMMENT ON COLUMN assets_asset.online_time IS '上线时间';
COMMENT ON COLUMN assets_asset.offline_time IS '下线时间';
COMMENT ON COLUMN assets_asset.physical_asset_config IS '实物资产配置(JSONB)';
COMMENT ON COLUMN assets_asset.service_asset_config IS '服务资产配置(JSONB)';
COMMENT ON COLUMN assets_asset.virtual_asset_config IS '虚拟资产配置(JSONB)';
COMMENT ON COLUMN assets_asset.metadata IS '动态元数据(JSONB)';
COMMENT ON COLUMN assets_asset.tenant_module_type IS '租户模块类型';

--------------------pgsql创建assets_asset表语句---------------------------