mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
Refactor product model and update CreateProduct logic to use new fields. Enhance SKU handling with inventory management and update README for consistency in product attributes.
This commit is contained in:
parent
54bbf73f56
commit
62ec3c43c4
@ -91,7 +91,7 @@ grpcurl -plaintext -d '{"product_id":"<snowflake-id>"}' \
|
||||
localhost:50052 ids.product.v1.ProductService/GetProduct
|
||||
|
||||
# 创建商品
|
||||
grpcurl -plaintext -d '{"product":{"product_id":"<snowflake-id>","spu_code":"<spu-code>","name":"商品名称","description":"商品描述","status":"PRODUCT_STATUS_DRAFT","skus":[]}}' \
|
||||
grpcurl -plaintext -d '{"product_id":"<snowflake-id>","spu_no":"<spu-no>","title":"商品标题","selling_point":"商品卖点","status":"PRODUCT_STATUS_DRAFT","skus":[]}' \
|
||||
localhost:50052 ids.product.v1.ProductService/CreateProduct
|
||||
|
||||
# 批量查询
|
||||
@ -109,8 +109,9 @@ grpcurl -plaintext -d '{"category_id":"<snowflake-category-id>","page":1,"page_s
|
||||
|
||||
| 表 | 职责 |
|
||||
|---|---|
|
||||
| `product.products` | SPU 主表(名称、描述、状态、扩展属性) |
|
||||
| `product.product_items` | SKU 明细(规格、价格、库存) |
|
||||
| `product.products` | SPU 主表(标题、卖点、状态、扩展属性) |
|
||||
| `product.product_items` | SKU 明细(规格、价格、属性) |
|
||||
| `product.product_item_inventory` | SKU 库存销量(可售、锁定、已售) |
|
||||
|
||||
- 价格字段统一为 `BIGINT` / Go `int64`,单位:**分**
|
||||
- 禁止跨 Schema JOIN(`trade`、`customer` 等)
|
||||
|
||||
@ -22,9 +22,9 @@ var ErrSKUNotFound = errors.New("sku not found")
|
||||
// ProductRecord is the persistence model for product.products (SPU).
|
||||
type ProductRecord struct {
|
||||
ID string
|
||||
SPUCode string
|
||||
Name string
|
||||
Description string
|
||||
SPUNo string
|
||||
Title string
|
||||
SellingPoint string
|
||||
Status productv1.ProductStatus
|
||||
Attrs map[string]any
|
||||
CreatedAt time.Time
|
||||
@ -37,8 +37,8 @@ type SKURecord struct {
|
||||
ProductID string
|
||||
Name string
|
||||
Price int64
|
||||
Stock int32
|
||||
AttributesJSONB string
|
||||
AvailableQuantity int32
|
||||
AttributesJSON string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
@ -59,23 +59,30 @@ func (r *ProductRepository) EnsureSchema(ctx context.Context) error {
|
||||
CREATE TABLE IF NOT EXISTS product.product_items (
|
||||
id BIGINT PRIMARY KEY,
|
||||
product_id BIGINT NOT NULL REFERENCES product.products(id) ON DELETE CASCADE,
|
||||
sku_no VARCHAR(100) UNIQUE,
|
||||
sku_no VARCHAR(100),
|
||||
name VARCHAR(256) NOT NULL,
|
||||
price BIGINT NOT NULL CHECK (price >= 0),
|
||||
stock INTEGER NOT NULL DEFAULT 0 CHECK (stock >= 0),
|
||||
attributes JSONB NOT NULL DEFAULT '{}',
|
||||
status SMALLINT NOT NULL DEFAULT 1 CHECK (status IN (0, 1)),
|
||||
version INTEGER NOT NULL DEFAULT 0 CHECK (version >= 0),
|
||||
deleted_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
ALTER TABLE product.product_items
|
||||
ADD COLUMN IF NOT EXISTS sku_no VARCHAR(100),
|
||||
ADD COLUMN IF NOT EXISTS version INTEGER NOT NULL DEFAULT 0,
|
||||
ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
|
||||
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,
|
||||
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),
|
||||
version INTEGER NOT NULL DEFAULT 0 CHECK (version >= 0),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
CONSTRAINT uq_product_item_inventory_sku_id UNIQUE (sku_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_product_items_product_id ON product.product_items(product_id) WHERE deleted_at IS NULL;
|
||||
CREATE INDEX IF NOT EXISTS idx_product_items_price ON product.product_items(price) WHERE deleted_at IS NULL;
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_items_sku_no ON product.product_items(sku_no) WHERE sku_no IS NOT NULL;
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_items_sku_no ON product.product_items(sku_no) WHERE sku_no IS NOT NULL AND deleted_at IS NULL;
|
||||
`
|
||||
if _, err := r.db.ExecContext(ctx, ddl); err != nil {
|
||||
return fmt.Errorf("ensure product tables: %w", err)
|
||||
@ -101,7 +108,7 @@ func (r *ProductRepository) CreateProduct(ctx context.Context, product ProductRe
|
||||
_, err = r.db.ExecContext(ctx, `
|
||||
INSERT INTO product.products (id, spu_no, title, selling_point, status, cross_border)
|
||||
VALUES ($1, $2, $3, $4, $5, $6::jsonb)
|
||||
`, id, product.SPUCode, product.Name, product.Description, statusToDB(product.Status), attrs)
|
||||
`, id, product.SPUNo, product.Title, product.SellingPoint, statusToDB(product.Status), attrs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert product %s: %w", product.ID, err)
|
||||
}
|
||||
@ -169,7 +176,7 @@ func (r *ProductRepository) UpdateProduct(ctx context.Context, product ProductRe
|
||||
UPDATE product.products
|
||||
SET spu_no = $2, title = $3, selling_point = $4, status = $5, cross_border = $6::jsonb, updated_at = NOW()
|
||||
WHERE id = $1
|
||||
`, id, product.SPUCode, product.Name, product.Description, statusToDB(product.Status), attrs)
|
||||
`, id, product.SPUNo, product.Title, product.SellingPoint, statusToDB(product.Status), attrs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update product %s: %w", product.ID, err)
|
||||
}
|
||||
@ -219,7 +226,7 @@ WHERE product_id = $1 AND deleted_at IS NULL
|
||||
|
||||
// CreateSKU inserts a SKU row under an existing SPU.
|
||||
func (r *ProductRepository) CreateSKU(ctx context.Context, sku SKURecord) error {
|
||||
attrs, err := normalizeAttributesJSON(sku.AttributesJSONB)
|
||||
attrs, err := normalizeAttributesJSON(sku.AttributesJSON)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -233,19 +240,33 @@ func (r *ProductRepository) CreateSKU(ctx context.Context, sku SKURecord) error
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = r.db.ExecContext(ctx, `
|
||||
INSERT INTO product.product_items (id, product_id, name, price, stock, attributes)
|
||||
VALUES ($1, $2, $3, $4, $5, $6::jsonb)
|
||||
`, id, productID, sku.Name, sku.Price, sku.Stock, attrs)
|
||||
tx, err := r.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin create sku: %w", err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
_, err = tx.ExecContext(ctx, `
|
||||
INSERT INTO product.product_items (id, product_id, name, price, attributes)
|
||||
VALUES ($1, $2, $3, $4, $5::jsonb)
|
||||
`, id, productID, sku.Name, sku.Price, attrs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert sku %s: %w", sku.ID, err)
|
||||
}
|
||||
|
||||
if err := upsertSKUInventory(ctx, tx, id, sku.AvailableQuantity); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return fmt.Errorf("commit create sku: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateSKU updates mutable SKU fields.
|
||||
func (r *ProductRepository) UpdateSKU(ctx context.Context, sku SKURecord) error {
|
||||
attrs, err := normalizeAttributesJSON(sku.AttributesJSONB)
|
||||
attrs, err := normalizeAttributesJSON(sku.AttributesJSON)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -254,11 +275,17 @@ func (r *ProductRepository) UpdateSKU(ctx context.Context, sku SKURecord) error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res, err := r.db.ExecContext(ctx, `
|
||||
tx, err := r.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin update sku: %w", err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
res, err := tx.ExecContext(ctx, `
|
||||
UPDATE product.product_items
|
||||
SET name = $2, price = $3, stock = $4, attributes = $5::jsonb, updated_at = NOW()
|
||||
SET name = $2, price = $3, attributes = $4::jsonb, updated_at = NOW()
|
||||
WHERE id = $1
|
||||
`, id, sku.Name, sku.Price, sku.Stock, attrs)
|
||||
`, id, sku.Name, sku.Price, attrs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update sku %s: %w", sku.ID, err)
|
||||
}
|
||||
@ -270,6 +297,14 @@ WHERE id = $1
|
||||
if affected == 0 {
|
||||
return ErrSKUNotFound
|
||||
}
|
||||
|
||||
if err := upsertSKUInventory(ctx, tx, id, sku.AvailableQuantity); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return fmt.Errorf("commit update sku: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -358,9 +393,10 @@ func (r *ProductRepository) ListCategoryProducts(ctx context.Context, categoryID
|
||||
var total int64
|
||||
if err := r.db.QueryRowContext(ctx, `
|
||||
SELECT COUNT(*)
|
||||
FROM product.products
|
||||
WHERE category_ids @> ARRAY[$1]::BIGINT[]
|
||||
AND deleted_at IS NULL
|
||||
FROM product.products p
|
||||
JOIN product.product_category_bindings b ON b.product_id = p.id
|
||||
WHERE b.category_id = $1
|
||||
AND p.deleted_at IS NULL
|
||||
`, parsedCategoryID).Scan(&total); err != nil {
|
||||
return nil, nil, 0, fmt.Errorf("count category products: %w", err)
|
||||
}
|
||||
@ -370,11 +406,12 @@ WHERE category_ids @> ARRAY[$1]::BIGINT[]
|
||||
|
||||
offset := int64(page-1) * int64(pageSize)
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
SELECT id, spu_no, title, COALESCE(selling_point, ''), status, cross_border, created_at, updated_at
|
||||
FROM product.products
|
||||
WHERE category_ids @> ARRAY[$1]::BIGINT[]
|
||||
AND deleted_at IS NULL
|
||||
ORDER BY created_at DESC, id DESC
|
||||
SELECT p.id, p.spu_no, p.title, COALESCE(p.selling_point, ''), p.status, p.cross_border, p.created_at, p.updated_at
|
||||
FROM product.products p
|
||||
JOIN product.product_category_bindings b ON b.product_id = p.id
|
||||
WHERE b.category_id = $1
|
||||
AND p.deleted_at IS NULL
|
||||
ORDER BY p.created_at DESC, p.id DESC
|
||||
LIMIT $2 OFFSET $3
|
||||
`, parsedCategoryID, pageSize, offset)
|
||||
if err != nil {
|
||||
@ -467,7 +504,7 @@ func insertProduct(ctx context.Context, tx *sql.Tx, id int64, product ProductRec
|
||||
INSERT INTO product.products (id, spu_no, title, selling_point, status, cross_border)
|
||||
VALUES ($1, $2, $3, $4, $5, $6::jsonb)
|
||||
RETURNING id, spu_no, title, COALESCE(selling_point, ''), status, cross_border, created_at, updated_at
|
||||
`, id, product.SPUCode, product.Name, product.Description, statusToDB(product.Status), attrs)
|
||||
`, id, product.SPUNo, product.Title, product.SellingPoint, statusToDB(product.Status), attrs)
|
||||
|
||||
createdProduct, err := scanProduct(row)
|
||||
if err != nil {
|
||||
@ -477,7 +514,7 @@ RETURNING id, spu_no, title, COALESCE(selling_point, ''), status, cross_border,
|
||||
}
|
||||
|
||||
func insertSKU(ctx context.Context, tx *sql.Tx, productID string, sku SKURecord) (*SKURecord, error) {
|
||||
attrs, err := normalizeAttributesJSON(sku.AttributesJSONB)
|
||||
attrs, err := normalizeAttributesJSON(sku.AttributesJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -492,18 +529,37 @@ func insertSKU(ctx context.Context, tx *sql.Tx, productID string, sku SKURecord)
|
||||
}
|
||||
|
||||
row := tx.QueryRowContext(ctx, `
|
||||
INSERT INTO product.product_items (id, product_id, name, price, stock, attributes)
|
||||
VALUES ($1, $2, $3, $4, $5, $6::jsonb)
|
||||
RETURNING id, product_id, name, price, stock, attributes, created_at, updated_at
|
||||
`, id, parsedProductID, sku.Name, sku.Price, sku.Stock, attrs)
|
||||
INSERT INTO product.product_items (id, product_id, name, price, attributes)
|
||||
VALUES ($1, $2, $3, $4, $5::jsonb)
|
||||
RETURNING id, product_id, name, price, 0, attributes, created_at, updated_at
|
||||
`, id, parsedProductID, sku.Name, sku.Price, attrs)
|
||||
|
||||
createdSKU, err := scanSKU(row)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("insert sku %s: %w", sku.ID, err)
|
||||
}
|
||||
|
||||
if err := upsertSKUInventory(ctx, tx, id, sku.AvailableQuantity); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
createdSKU.AvailableQuantity = sku.AvailableQuantity
|
||||
return createdSKU, nil
|
||||
}
|
||||
|
||||
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)
|
||||
ON CONFLICT (sku_id) DO UPDATE
|
||||
SET available_quantity = EXCLUDED.available_quantity,
|
||||
updated_at = NOW()
|
||||
`, skuID, availableQuantity)
|
||||
if err != nil {
|
||||
return fmt.Errorf("upsert sku inventory %s: %w", formatID(skuID), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ProductRepository) loadSKUsByProductIDs(ctx context.Context, productIDs []string) (map[string][]SKURecord, error) {
|
||||
result := make(map[string][]SKURecord, len(productIDs))
|
||||
if len(productIDs) == 0 {
|
||||
@ -522,11 +578,19 @@ func (r *ProductRepository) loadSKUsByProductIDs(ctx context.Context, productIDs
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
SELECT id, product_id, name, price, stock, attributes, created_at, updated_at
|
||||
FROM product.product_items
|
||||
WHERE product_id IN (%s)
|
||||
AND deleted_at IS NULL
|
||||
ORDER BY created_at ASC
|
||||
SELECT pi.id,
|
||||
pi.product_id,
|
||||
pi.name,
|
||||
pi.price,
|
||||
COALESCE(inv.available_quantity, 0),
|
||||
pi.attributes,
|
||||
pi.created_at,
|
||||
pi.updated_at
|
||||
FROM product.product_items pi
|
||||
LEFT JOIN product.product_item_inventory inv ON inv.sku_id = pi.id
|
||||
WHERE pi.product_id IN (%s)
|
||||
AND pi.deleted_at IS NULL
|
||||
ORDER BY pi.created_at ASC
|
||||
`, strings.Join(placeholders, ", "))
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, query, args...)
|
||||
@ -561,7 +625,7 @@ func scanProduct(scanner rowScanner) (*ProductRecord, error) {
|
||||
attrsJSON []byte
|
||||
)
|
||||
if err := scanner.Scan(
|
||||
&id, &rec.SPUCode, &rec.Name, &rec.Description, &status, &attrsJSON, &rec.CreatedAt, &rec.UpdatedAt,
|
||||
&id, &rec.SPUNo, &rec.Title, &rec.SellingPoint, &status, &attrsJSON, &rec.CreatedAt, &rec.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("scan product: %w", err)
|
||||
}
|
||||
@ -584,16 +648,16 @@ func scanSKU(scanner rowScanner) (*SKURecord, error) {
|
||||
attrsJSON []byte
|
||||
)
|
||||
if err := scanner.Scan(
|
||||
&id, &productID, &rec.Name, &rec.Price, &rec.Stock, &attrsJSON, &rec.CreatedAt, &rec.UpdatedAt,
|
||||
&id, &productID, &rec.Name, &rec.Price, &rec.AvailableQuantity, &attrsJSON, &rec.CreatedAt, &rec.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("scan sku: %w", err)
|
||||
}
|
||||
|
||||
rec.ID = formatID(id)
|
||||
rec.ProductID = formatID(productID)
|
||||
rec.AttributesJSONB = string(attrsJSON)
|
||||
if rec.AttributesJSONB == "" {
|
||||
rec.AttributesJSONB = "{}"
|
||||
rec.AttributesJSON = string(attrsJSON)
|
||||
if rec.AttributesJSON == "" {
|
||||
rec.AttributesJSON = "{}"
|
||||
}
|
||||
return &rec, nil
|
||||
}
|
||||
|
||||
@ -44,11 +44,11 @@ func NewProductServer(repo *repository.ProductRepository, logger *zap.Logger) *P
|
||||
|
||||
// CreateProduct creates a product SPU and optional SKU list.
|
||||
func (s *ProductServer) CreateProduct(ctx context.Context, req *productv1.CreateProductRequest) (*productv1.CreateProductResponse, error) {
|
||||
if req == nil || req.GetProduct() == nil {
|
||||
return nil, status.Error(codes.InvalidArgument, "product is required")
|
||||
if req == nil {
|
||||
return nil, status.Error(codes.InvalidArgument, "request is required")
|
||||
}
|
||||
|
||||
product, skus, err := productFromProto(req.GetProduct())
|
||||
product, skus, err := productFromCreateRequest(req)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
@ -159,39 +159,39 @@ func totalPages(total int64, pageSize int32) int32 {
|
||||
return int32(pages)
|
||||
}
|
||||
|
||||
func productFromProto(product *productv1.Product) (repository.ProductRecord, []repository.SKURecord, error) {
|
||||
productID := strings.TrimSpace(product.GetProductId())
|
||||
func productFromCreateRequest(req *productv1.CreateProductRequest) (repository.ProductRecord, []repository.SKURecord, error) {
|
||||
productID := strings.TrimSpace(req.GetProductId())
|
||||
if err := validatePositiveID(productID, "product_id"); err != nil {
|
||||
return repository.ProductRecord{}, nil, err
|
||||
}
|
||||
|
||||
spuCode := strings.TrimSpace(product.GetSpuCode())
|
||||
if spuCode == "" {
|
||||
return repository.ProductRecord{}, nil, errors.New("spu_code is required")
|
||||
spuNo := strings.TrimSpace(req.GetSpuNo())
|
||||
if spuNo == "" {
|
||||
return repository.ProductRecord{}, nil, errors.New("spu_no is required")
|
||||
}
|
||||
|
||||
name := strings.TrimSpace(product.GetName())
|
||||
if name == "" {
|
||||
return repository.ProductRecord{}, nil, errors.New("name is required")
|
||||
title := strings.TrimSpace(req.GetTitle())
|
||||
if title == "" {
|
||||
return repository.ProductRecord{}, nil, errors.New("title is required")
|
||||
}
|
||||
|
||||
statusValue := product.GetStatus()
|
||||
statusValue := req.GetStatus()
|
||||
if statusValue == productv1.ProductStatus_PRODUCT_STATUS_UNSPECIFIED {
|
||||
statusValue = productv1.ProductStatus_PRODUCT_STATUS_DRAFT
|
||||
}
|
||||
|
||||
record := repository.ProductRecord{
|
||||
ID: productID,
|
||||
SPUCode: spuCode,
|
||||
Name: name,
|
||||
Description: product.GetDescription(),
|
||||
SPUNo: spuNo,
|
||||
Title: title,
|
||||
SellingPoint: req.GetSellingPoint(),
|
||||
Status: statusValue,
|
||||
Attrs: map[string]any{},
|
||||
}
|
||||
|
||||
skus := make([]repository.SKURecord, 0, len(product.GetSkus()))
|
||||
for i, protoSKU := range product.GetSkus() {
|
||||
sku, err := skuFromProto(productID, protoSKU)
|
||||
skus := make([]repository.SKURecord, 0, len(req.GetSkus()))
|
||||
for i, protoSKU := range req.GetSkus() {
|
||||
sku, err := skuFromCreateInput(productID, protoSKU)
|
||||
if err != nil {
|
||||
return repository.ProductRecord{}, nil, errors.New("skus[" + strconv.Itoa(i) + "]: " + err.Error())
|
||||
}
|
||||
@ -201,7 +201,7 @@ func productFromProto(product *productv1.Product) (repository.ProductRecord, []r
|
||||
return record, skus, nil
|
||||
}
|
||||
|
||||
func skuFromProto(productID string, sku *productv1.SKU) (repository.SKURecord, error) {
|
||||
func skuFromCreateInput(productID string, sku *productv1.CreateProductSKU) (repository.SKURecord, error) {
|
||||
if sku == nil {
|
||||
return repository.SKURecord{}, errors.New("sku is required")
|
||||
}
|
||||
@ -211,10 +211,6 @@ func skuFromProto(productID string, sku *productv1.SKU) (repository.SKURecord, e
|
||||
return repository.SKURecord{}, err
|
||||
}
|
||||
|
||||
if sku.GetProductId() != "" && sku.GetProductId() != productID {
|
||||
return repository.SKURecord{}, errors.New("product_id must match product.product_id")
|
||||
}
|
||||
|
||||
name := strings.TrimSpace(sku.GetName())
|
||||
if name == "" {
|
||||
return repository.SKURecord{}, errors.New("name is required")
|
||||
@ -222,13 +218,13 @@ func skuFromProto(productID string, sku *productv1.SKU) (repository.SKURecord, e
|
||||
if sku.GetPrice() < 0 {
|
||||
return repository.SKURecord{}, errors.New("price must be greater than or equal to 0")
|
||||
}
|
||||
if sku.GetStock() < 0 {
|
||||
return repository.SKURecord{}, errors.New("stock must be greater than or equal to 0")
|
||||
if sku.GetAvailableQuantity() < 0 {
|
||||
return repository.SKURecord{}, errors.New("available_quantity must be greater than or equal to 0")
|
||||
}
|
||||
|
||||
attrs := strings.TrimSpace(sku.GetAttributesJsonb())
|
||||
attrs := strings.TrimSpace(sku.GetAttributesJson())
|
||||
if attrs != "" && !json.Valid([]byte(attrs)) {
|
||||
return repository.SKURecord{}, errors.New("attributes_jsonb must be valid json")
|
||||
return repository.SKURecord{}, errors.New("attributes_json must be valid json")
|
||||
}
|
||||
|
||||
return repository.SKURecord{
|
||||
@ -236,8 +232,8 @@ func skuFromProto(productID string, sku *productv1.SKU) (repository.SKURecord, e
|
||||
ProductID: productID,
|
||||
Name: name,
|
||||
Price: sku.GetPrice(),
|
||||
Stock: sku.GetStock(),
|
||||
AttributesJSONB: attrs,
|
||||
AvailableQuantity: sku.GetAvailableQuantity(),
|
||||
AttributesJSON: attrs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -264,16 +260,16 @@ func toProtoProduct(product *repository.ProductRecord, skus []repository.SKUReco
|
||||
ProductId: sku.ProductID,
|
||||
Name: sku.Name,
|
||||
Price: sku.Price,
|
||||
Stock: sku.Stock,
|
||||
AttributesJsonb: sku.AttributesJSONB,
|
||||
AvailableQuantity: sku.AvailableQuantity,
|
||||
AttributesJson: sku.AttributesJSON,
|
||||
})
|
||||
}
|
||||
|
||||
return &productv1.Product{
|
||||
ProductId: product.ID,
|
||||
SpuCode: product.SPUCode,
|
||||
Name: product.Name,
|
||||
Description: product.Description,
|
||||
SpuNo: product.SPUNo,
|
||||
Title: product.Title,
|
||||
SellingPoint: product.SellingPoint,
|
||||
Status: product.Status,
|
||||
Skus: protoSKUs,
|
||||
CreatedAt: timestamppb.New(product.CreatedAt),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user