From 504c88fa4073674b2deb2c6932fd2fbb7dd8237c Mon Sep 17 00:00:00 2001 From: BXY <103352887+BXY0414@users.noreply.github.com> Date: Thu, 24 Sep 2026 15:27:21 +0800 Subject: [PATCH] feat: add supply price to SKU records and update related database schema --- internal/repository/product_repository.go | 55 ++++++++++++++++--- internal/server/product_server.go | 9 +++ scripts/sql/add-product-item-supply-price.sql | 14 +++++ scripts/sql/recreate-product-tables.sql | 2 + 4 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 scripts/sql/add-product-item-supply-price.sql diff --git a/internal/repository/product_repository.go b/internal/repository/product_repository.go index 575782e..d162176 100644 --- a/internal/repository/product_repository.go +++ b/internal/repository/product_repository.go @@ -116,6 +116,7 @@ type SKURecord struct { SKUNo string Name string Price int64 + SupplyPrice *int64 ImageIDs []string StockQuantity int32 LockedQuantity int32 @@ -142,6 +143,7 @@ type ProductSKUStockRecord struct { ID string Name string StockQuantity int32 + SupplyPrice *int64 } // InventoryRecord is the persistence model for product.product_item_inventory. @@ -193,6 +195,8 @@ type SKUUpdate struct { ExpectedInventoryVersion int32 Name *string Price *int64 + SupplyPrice *int64 + SupplyPriceSet bool StockQuantity *int32 Status *int32 ImageIDs *[]string @@ -381,6 +385,7 @@ CREATE TABLE IF NOT EXISTS product.product_items ( sku_no VARCHAR(100), name VARCHAR(256) NOT NULL, price BIGINT NOT NULL CHECK (price >= 0), + supply_price BIGINT CONSTRAINT chk_product_items_supply_price CHECK (supply_price >= 0), image_ids VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[], attributes JSONB NOT NULL DEFAULT '{}', status SMALLINT NOT NULL DEFAULT 1 CHECK (status IN (0, 1)), @@ -419,6 +424,8 @@ ALTER TABLE product.product_items ALTER COLUMN id DROP DEFAULT; ALTER TABLE product.product_items ADD COLUMN IF NOT EXISTS image_ids VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[]; +ALTER TABLE product.product_items + ADD COLUMN IF NOT EXISTS supply_price BIGINT; ALTER TABLE product.product_items ADD COLUMN IF NOT EXISTS is_archived BOOLEAN NOT NULL DEFAULT FALSE; ALTER TABLE product.product_items @@ -1638,7 +1645,7 @@ FOR UPDATE return ErrSKUVersionConflict } - if input.Name != nil || input.Price != nil || input.Status != nil || input.ImageIDs != nil { + if input.Name != nil || input.Price != nil || input.SupplyPriceSet || input.Status != nil || input.ImageIDs != nil { sets := []string{"updated_at = NOW()", "version = version + 1"} args := []any{skuID, productID} argIndex := 3 @@ -1658,6 +1665,14 @@ FOR UPDATE args = append(args, *input.Price) argIndex++ } + if input.SupplyPriceSet { + if input.SupplyPrice != nil && *input.SupplyPrice < 0 { + return fmt.Errorf("sku supply_price must be greater than or equal to 0") + } + sets = append(sets, fmt.Sprintf("supply_price = $%d", argIndex)) + args = append(args, nullableInt64(input.SupplyPrice)) + argIndex++ + } if input.Status != nil { if *input.Status != 0 && *input.Status != 1 { return fmt.Errorf("sku status must be 0 or 1") @@ -1851,12 +1866,12 @@ func (r *ProductRepository) CreateSKU(ctx context.Context, sku SKURecord) error defer tx.Rollback() err = tx.QueryRowContext(ctx, ` -INSERT INTO product.product_items (id, product_id, sku_no, name, price, image_ids, attributes, status, tenant_id, store_id) -SELECT $1, $2, $3, $4, $5, $6, $7::jsonb, $8, p.tenant_id, p.store_id +INSERT INTO product.product_items (id, product_id, sku_no, name, price, supply_price, image_ids, attributes, status, tenant_id, store_id) +SELECT $1, $2, $3, $4, $5, $6, $7, $8::jsonb, $9, p.tenant_id, p.store_id FROM product.products p WHERE p.id = $2 RETURNING id -`, skuID, productID, nullString(sku.SKUNo), sku.Name, sku.Price, stringSliceOrEmpty(sku.ImageIDs), attrs, int16(sku.Status)).Scan(&skuID) +`, skuID, productID, nullString(sku.SKUNo), sku.Name, sku.Price, nullableInt64(sku.SupplyPrice), stringSliceOrEmpty(sku.ImageIDs), attrs, int16(sku.Status)).Scan(&skuID) if err != nil { return fmt.Errorf("insert sku: %w", err) } @@ -2321,7 +2336,8 @@ SELECT EXISTS( rows, err := r.db.QueryContext(ctx, ` SELECT pi.id, pi.name, - COALESCE(inv.stock_quantity, 0) + COALESCE(inv.stock_quantity, 0), + pi.supply_price FROM product.product_items AS pi LEFT JOIN product.product_item_inventory AS inv ON inv.sku_id = pi.id WHERE pi.product_id = $1 @@ -2341,10 +2357,15 @@ ORDER BY pi.created_at ASC, pi.id ASC sku ProductSKUStockRecord id int64 ) - if err := rows.Scan(&id, &sku.Name, &sku.StockQuantity); err != nil { + var supplyPrice sql.NullInt64 + if err := rows.Scan(&id, &sku.Name, &sku.StockQuantity, &supplyPrice); err != nil { return nil, fmt.Errorf("scan product sku stock: %w", err) } sku.ID = formatID(id) + if supplyPrice.Valid { + value := supplyPrice.Int64 + sku.SupplyPrice = &value + } skus = append(skus, sku) } if err := rows.Err(); err != nil { @@ -3189,8 +3210,8 @@ func insertSKU(ctx context.Context, tx *sql.Tx, productID string, sku SKURecord) } row := tx.QueryRowContext(ctx, ` -INSERT INTO product.product_items (id, product_id, sku_no, name, price, image_ids, attributes, status, tenant_id, store_id) -SELECT $1, $2, $3, $4, $5, $6, $7::jsonb, $8, p.tenant_id, p.store_id +INSERT INTO product.product_items (id, product_id, sku_no, name, price, supply_price, image_ids, attributes, status, tenant_id, store_id) +SELECT $1, $2, $3, $4, $5, $6, $7, $8::jsonb, $9, p.tenant_id, p.store_id FROM product.products p WHERE p.id = $2 RETURNING id, @@ -3198,6 +3219,7 @@ RETURNING id, sku_no, name, price, + supply_price, COALESCE(to_json(image_ids)::text, '[]'), attributes, status, @@ -3215,7 +3237,7 @@ RETURNING id, NULL::integer, NULL::timestamptz, NULL::timestamptz -`, id, parsedProductID, nullString(sku.SKUNo), sku.Name, sku.Price, stringSliceOrEmpty(sku.ImageIDs), attrs, int16(sku.Status)) +`, id, parsedProductID, nullString(sku.SKUNo), sku.Name, sku.Price, nullableInt64(sku.SupplyPrice), stringSliceOrEmpty(sku.ImageIDs), attrs, int16(sku.Status)) createdSKU, err := scanSKU(row) if err != nil { @@ -3392,6 +3414,7 @@ SELECT pi.id, pi.sku_no, pi.name, pi.price, + pi.supply_price, COALESCE(to_json(pi.image_ids)::text, '[]'), pi.attributes, pi.status, @@ -3461,6 +3484,7 @@ SELECT pi.id, pi.sku_no, pi.name, pi.price, + pi.supply_price, COALESCE(to_json(pi.image_ids)::text, '[]'), pi.attributes, pi.status, @@ -3636,6 +3660,7 @@ func scanSKU(scanner rowScanner) (*SKURecord, error) { id int64 productID int64 skuNo sql.NullString + supplyPrice sql.NullInt64 imageIDsJSON string attrsJSON []byte status int16 @@ -3658,6 +3683,7 @@ func scanSKU(scanner rowScanner) (*SKURecord, error) { &skuNo, &rec.Name, &rec.Price, + &supplyPrice, &imageIDsJSON, &attrsJSON, &status, @@ -3684,6 +3710,10 @@ func scanSKU(scanner rowScanner) (*SKURecord, error) { if skuNo.Valid { rec.SKUNo = skuNo.String } + if supplyPrice.Valid { + value := supplyPrice.Int64 + rec.SupplyPrice = &value + } imageIDs, err := parseStringArrayJSON(imageIDsJSON) if err != nil { return nil, fmt.Errorf("unmarshal sku image_ids: %w", err) @@ -3945,6 +3975,13 @@ func nullString(raw string) sql.NullString { return sql.NullString{String: value, Valid: true} } +func nullableInt64(value *int64) any { + if value == nil { + return nil + } + return *value +} + func stringSliceOrEmpty(values []string) []string { if values == nil { return []string{} diff --git a/internal/server/product_server.go b/internal/server/product_server.go index ae8d3e9..fb1d39c 100644 --- a/internal/server/product_server.go +++ b/internal/server/product_server.go @@ -207,6 +207,7 @@ func (s *ProductServer) ListProductSKUs(ctx context.Context, req *productv1.List Id: sku.ID, Name: sku.Name, StockQuantity: sku.StockQuantity, + SupplyPrice: sku.SupplyPrice, }) } return &productv1.ListProductSKUsResponse{ @@ -1851,6 +1852,9 @@ func skuUpdateFromProto(input *productv1.PatchExistingSKU) (repository.SKUUpdate case "price": value := input.GetPrice() update.Price = &value + case "supply_price": + update.SupplyPriceSet = true + update.SupplyPrice = input.SupplyPrice case "stock_quantity": value := input.GetStockQuantity() update.StockQuantity = &value @@ -1903,6 +1907,9 @@ func skuFromCreateInput(sku *productv1.CreateProductSKU) (repository.SKURecord, if sku.GetPrice() < 0 { return repository.SKURecord{}, errors.New("price must be greater than or equal to 0") } + if sku.SupplyPrice != nil && *sku.SupplyPrice < 0 { + return repository.SKURecord{}, errors.New("supply_price must be greater than or equal to 0") + } if sku.GetStockQuantity() < 0 { return repository.SKURecord{}, errors.New("stock_quantity must be greater than or equal to 0") } @@ -1924,6 +1931,7 @@ func skuFromCreateInput(sku *productv1.CreateProductSKU) (repository.SKURecord, SKUNo: strings.TrimSpace(sku.GetSkuNo()), Name: name, Price: sku.GetPrice(), + SupplyPrice: sku.SupplyPrice, ImageIDs: normalizeStringList(sku.GetImageIds()), StockQuantity: sku.GetStockQuantity(), AttributesJSON: attrs, @@ -2159,6 +2167,7 @@ func toProtoProduct(product *repository.ProductRecord, skus []repository.SKUReco SkuNo: sku.SKUNo, Name: sku.Name, Price: sku.Price, + SupplyPrice: sku.SupplyPrice, StockQuantity: sku.StockQuantity, SoldQuantity: sku.SoldQuantity, AttributesJson: sku.AttributesJSON, diff --git a/scripts/sql/add-product-item-supply-price.sql b/scripts/sql/add-product-item-supply-price.sql new file mode 100644 index 0000000..3aead86 --- /dev/null +++ b/scripts/sql/add-product-item-supply-price.sql @@ -0,0 +1,14 @@ +-- Adds the nullable SKU supply price without modifying historical values. +-- Amounts are stored in cents; NULL means the supply price has not been maintained. +ALTER TABLE product.product_items + ADD COLUMN IF NOT EXISTS supply_price BIGINT; + +ALTER TABLE product.product_items + DROP CONSTRAINT IF EXISTS chk_product_items_supply_price; + +ALTER TABLE product.product_items + ADD CONSTRAINT chk_product_items_supply_price + CHECK (supply_price IS NULL OR supply_price >= 0); + +COMMENT ON COLUMN product.product_items.supply_price + IS 'SKU 供货价,单位:分;NULL 表示未维护'; diff --git a/scripts/sql/recreate-product-tables.sql b/scripts/sql/recreate-product-tables.sql index cecc4ab..4047d3b 100644 --- a/scripts/sql/recreate-product-tables.sql +++ b/scripts/sql/recreate-product-tables.sql @@ -167,6 +167,7 @@ CREATE TABLE IF NOT EXISTS product.product_items ( sku_no VARCHAR(100), name VARCHAR(256) NOT NULL, price BIGINT NOT NULL CHECK (price >= 0), + supply_price BIGINT CONSTRAINT chk_product_items_supply_price CHECK (supply_price >= 0), image_ids VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[], attributes JSONB NOT NULL DEFAULT '{}', status SMALLINT NOT NULL DEFAULT 1 CHECK (status IN (0, 1)), @@ -182,6 +183,7 @@ CREATE TABLE IF NOT EXISTS product.product_items ( COMMENT ON TABLE product.product_items IS '商品 SKU 明细表:存储具体可售规格、价格和规格属性;库存统一存储在 product.product_item_inventory'; COMMENT ON COLUMN product.product_items.price IS 'SKU 售价,单位:分'; +COMMENT ON COLUMN product.product_items.supply_price IS 'SKU 供货价,单位:分;NULL 表示未维护'; COMMENT ON COLUMN product.product_items.image_ids IS 'SKU 图片 ID 列表'; COMMENT ON COLUMN product.product_items.attributes IS 'SKU 规格属性 JSONB,如颜色、尺寸等'; COMMENT ON COLUMN product.product_items.status IS 'SKU 状态:0 停用,1 启用';