feat: add spu_no and sku_no fields to product_item_inventory table for enhanced inventory tracking

This commit is contained in:
robinj0726 2026-07-10 12:29:46 +08:00
parent 114c9578bb
commit da065d097f
2 changed files with 13 additions and 2 deletions

View File

@ -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 {

View File

@ -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 '支付成功后的累计销量';