From da065d097f2b4bc10c38ee5738a857b620ceb30d Mon Sep 17 00:00:00 2001 From: robinj0726 <88188467+robinj0726@users.noreply.github.com> Date: Fri, 10 Jul 2026 12:29:46 +0800 Subject: [PATCH] feat: add spu_no and sku_no fields to product_item_inventory table for enhanced inventory tracking --- internal/repository/product_repository.go | 11 +++++++++-- scripts/sql/recreate-product-tables.sql | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/internal/repository/product_repository.go b/internal/repository/product_repository.go index b7e2bde..42061c6 100644 --- a/internal/repository/product_repository.go +++ b/internal/repository/product_repository.go @@ -72,6 +72,8 @@ CREATE TABLE IF NOT EXISTS product.product_items ( CREATE TABLE IF NOT EXISTS product.product_item_inventory ( id BIGINT PRIMARY KEY, sku_id BIGINT NOT NULL REFERENCES product.product_items(id) ON DELETE CASCADE, + spu_no VARCHAR(100), + sku_no VARCHAR(100), available_quantity INTEGER NOT NULL DEFAULT 0 CHECK (available_quantity >= 0), locked_quantity INTEGER NOT NULL DEFAULT 0 CHECK (locked_quantity >= 0), sold_quantity INTEGER NOT NULL DEFAULT 0 CHECK (sold_quantity >= 0), @@ -520,10 +522,15 @@ RETURNING id, product_id, name, price, 0, attributes, created_at, updated_at func upsertSKUInventory(ctx context.Context, tx *sql.Tx, skuID int64, availableQuantity int32) error { _, err := tx.ExecContext(ctx, ` -INSERT INTO product.product_item_inventory (id, sku_id, available_quantity) -VALUES ($1, $1, $2) +INSERT INTO product.product_item_inventory (id, sku_id, spu_no, sku_no, available_quantity) +SELECT pi.id, pi.id, p.spu_no, pi.sku_no, $2 +FROM product.product_items pi +JOIN product.products p ON p.id = pi.product_id +WHERE pi.id = $1 ON CONFLICT (sku_id) DO UPDATE SET available_quantity = EXCLUDED.available_quantity, + spu_no = EXCLUDED.spu_no, + sku_no = EXCLUDED.sku_no, updated_at = NOW() `, skuID, availableQuantity) if err != nil { diff --git a/scripts/sql/recreate-product-tables.sql b/scripts/sql/recreate-product-tables.sql index 34ac606..32f24a0 100644 --- a/scripts/sql/recreate-product-tables.sql +++ b/scripts/sql/recreate-product-tables.sql @@ -189,6 +189,8 @@ CREATE UNIQUE INDEX IF NOT EXISTS uq_product_items_sku_no CREATE TABLE IF NOT EXISTS product.product_item_inventory ( id BIGINT PRIMARY KEY, sku_id BIGINT NOT NULL REFERENCES product.product_items(id) ON DELETE CASCADE, + spu_no VARCHAR(100), + sku_no VARCHAR(100), available_quantity INTEGER NOT NULL DEFAULT 0 CHECK (available_quantity >= 0), locked_quantity INTEGER NOT NULL DEFAULT 0 CHECK (locked_quantity >= 0), sold_quantity INTEGER NOT NULL DEFAULT 0 CHECK (sold_quantity >= 0), @@ -199,6 +201,8 @@ CREATE TABLE IF NOT EXISTS product.product_item_inventory ( ); COMMENT ON TABLE product.product_item_inventory IS '商品 SKU 库存销量表:存储可售库存、锁定库存和已售数量'; +COMMENT ON COLUMN product.product_item_inventory.spu_no IS '商品 SPU 编码冗余字段,用于库存查询和对账'; +COMMENT ON COLUMN product.product_item_inventory.sku_no IS '商品 SKU 编码冗余字段,用于库存查询和对账'; COMMENT ON COLUMN product.product_item_inventory.available_quantity IS 'SKU 当前可售库存'; COMMENT ON COLUMN product.product_item_inventory.locked_quantity IS '待支付订单锁定库存'; COMMENT ON COLUMN product.product_item_inventory.sold_quantity IS '支付成功后的累计销量';