调整 接口返回字段

This commit is contained in:
BXY0414 2026-07-09 15:41:19 +08:00
parent 548ed19954
commit 5e74ff2816
2 changed files with 508 additions and 51 deletions

View File

@ -21,31 +21,82 @@ var ErrSKUNotFound = errors.New("sku not found")
// ProductRecord is the persistence model for product.products (SPU).
type ProductRecord struct {
ID string
SPUNo string
Title string
SellingPoint string
Status productv1.ProductStatus
Attrs map[string]any
CreatedAt time.Time
UpdatedAt time.Time
ID string
SPUNo string
Title string
SellingPoint string
BrandID string
SupplierID string
ImageIDs []string
DetailHTML string
MemberDiscountEnabled bool
GoodsType int32
ServiceGuarantees []string
Status productv1.ProductStatus
Attrs map[string]any
CrossBorderJSON string
FreightChargeType int32
FreightFixedAmount int64
FreightTemplateID string
SaleTimeType int32
ScheduledSaleTime *time.Time
DeletedAt *time.Time
CreatedAt time.Time
UpdatedAt time.Time
}
// SKURecord is the persistence model for product.product_items.
type SKURecord struct {
ID string
ProductID string
SKUNo string
Name string
Price int64
AvailableQuantity int32
LockedQuantity int32
SoldQuantity int32
AttributesJSON string
Status int32
DeletedAt *time.Time
CreatedAt time.Time
UpdatedAt time.Time
Inventory *InventoryRecord
}
// InventoryRecord is the persistence model for product.product_item_inventory.
type InventoryRecord struct {
ID string
SKUID string
AvailableQuantity int32
LockedQuantity int32
SoldQuantity int32
CreatedAt time.Time
UpdatedAt time.Time
}
// CategoryProductRecord is the product summary returned by category product lists.
type CategoryProductRecord struct {
ID string
SPUNo string
Title string
Status productv1.ProductStatus
ImageIDs []string
CreatedAt time.Time
UpdatedAt time.Time
}
// CategoryProductSKURecord is the SKU summary returned by category product lists.
type CategoryProductSKURecord struct {
ID string
ProductID string
AvailableQuantity int32
Price int64
}
// CategoryRecord is the persistence model for product.product_category.
type CategoryRecord struct {
ID string
NodeType int32
Name string
ParentID string
Level int32
@ -265,7 +316,7 @@ VALUES ($1, $2, $3, $4, $5::jsonb)
return fmt.Errorf("insert sku %s: %w", sku.ID, err)
}
if err := upsertSKUInventory(ctx, tx, id, sku.AvailableQuantity); err != nil {
if _, err := upsertSKUInventory(ctx, tx, id, sku.AvailableQuantity); err != nil {
return err
}
@ -309,7 +360,7 @@ WHERE id = $1
return ErrSKUNotFound
}
if err := upsertSKUInventory(ctx, tx, id, sku.AvailableQuantity); err != nil {
if _, err := upsertSKUInventory(ctx, tx, id, sku.AvailableQuantity); err != nil {
return err
}
@ -359,8 +410,8 @@ func (r *ProductRepository) GetProduct(ctx context.Context, productID string) (*
return product, skus[productID], nil
}
// ListCategoryProducts loads a paginated product list for a category ID.
func (r *ProductRepository) ListCategoryProducts(ctx context.Context, categoryID string, page, pageSize int32) ([]*ProductRecord, map[string][]SKURecord, int64, error) {
// ListCategoryProducts loads paginated product summaries for a category ID.
func (r *ProductRepository) ListCategoryProducts(ctx context.Context, categoryID string, page, pageSize int32) ([]*CategoryProductRecord, map[string][]CategoryProductSKURecord, int64, error) {
parsedCategoryID, err := parseID(categoryID, "category id")
if err != nil {
return nil, nil, 0, err
@ -384,12 +435,18 @@ WHERE b.category_id = $1
return nil, nil, 0, fmt.Errorf("count category products: %w", err)
}
if total == 0 {
return []*ProductRecord{}, map[string][]SKURecord{}, 0, nil
return []*CategoryProductRecord{}, map[string][]CategoryProductSKURecord{}, 0, nil
}
offset := int64(page-1) * int64(pageSize)
rows, err := r.db.QueryContext(ctx, `
SELECT p.id, p.spu_no, p.title, COALESCE(p.selling_point, ''), p.status, p.cross_border, p.created_at, p.updated_at
SELECT p.updated_at,
p.created_at,
p.title,
p.id,
p.spu_no,
p.status,
COALESCE(to_json(p.image_ids)::text, '[]')
FROM product.products p
JOIN product.product_category_bindings b ON b.product_id = p.id
WHERE b.category_id = $1
@ -402,9 +459,9 @@ LIMIT $2 OFFSET $3
}
defer rows.Close()
products := make([]*ProductRecord, 0, pageSize)
products := make([]*CategoryProductRecord, 0, pageSize)
for rows.Next() {
product, err := scanProduct(rows)
product, err := scanCategoryProduct(rows)
if err != nil {
return nil, nil, 0, err
}
@ -414,7 +471,7 @@ LIMIT $2 OFFSET $3
return nil, nil, 0, fmt.Errorf("iterate category products: %w", err)
}
if len(products) == 0 {
return products, map[string][]SKURecord{}, total, nil
return products, map[string][]CategoryProductSKURecord{}, total, nil
}
foundIDs := make([]string, 0, len(products))
@ -422,7 +479,7 @@ LIMIT $2 OFFSET $3
foundIDs = append(foundIDs, product.ID)
}
skuMap, err := r.loadSKUsByProductIDs(ctx, foundIDs)
skuMap, err := r.loadCategoryProductSKUsByProductIDs(ctx, foundIDs)
if err != nil {
return nil, nil, 0, err
}
@ -434,6 +491,7 @@ LIMIT $2 OFFSET $3
func (r *ProductRepository) ListCategoryTreeNodes(ctx context.Context, onlyVisible bool) ([]*CategoryRecord, error) {
rows, err := r.db.QueryContext(ctx, `
SELECT id,
node_type,
name,
parent_id,
level,
@ -489,7 +547,27 @@ func (r *ProductRepository) loadProductsByIDs(ctx context.Context, productIDs []
}
query := fmt.Sprintf(`
SELECT id, spu_no, title, COALESCE(selling_point, ''), status, cross_border, created_at, updated_at
SELECT id,
spu_no,
title,
COALESCE(selling_point, ''),
brand_id,
supplier_id,
COALESCE(to_json(image_ids)::text, '[]'),
COALESCE(detail_html, ''),
member_discount_enabled,
goods_type,
COALESCE(to_json(service_guarantees)::text, '[]'),
status,
cross_border,
freight_charge_type,
freight_fixed_amount,
freight_template_id,
sale_time_type,
scheduled_sale_time,
deleted_at,
created_at,
updated_at
FROM product.products
WHERE id IN (%s)
AND deleted_at IS NULL
@ -521,7 +599,27 @@ func insertProduct(ctx context.Context, tx *sql.Tx, id int64, product ProductRec
row := tx.QueryRowContext(ctx, `
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
RETURNING id,
spu_no,
title,
COALESCE(selling_point, ''),
brand_id,
supplier_id,
COALESCE(to_json(image_ids)::text, '[]'),
COALESCE(detail_html, ''),
member_discount_enabled,
goods_type,
COALESCE(to_json(service_guarantees)::text, '[]'),
status,
cross_border,
freight_charge_type,
freight_fixed_amount,
freight_template_id,
sale_time_type,
scheduled_sale_time,
deleted_at,
created_at,
updated_at
`, id, product.SPUNo, product.Title, product.SellingPoint, statusToDB(product.Status), attrs)
createdProduct, err := scanProduct(row)
@ -549,7 +647,23 @@ 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, attributes)
VALUES ($1, $2, $3, $4, $5::jsonb)
RETURNING id, product_id, name, price, 0, attributes, created_at, updated_at
RETURNING id,
product_id,
sku_no,
name,
price,
attributes,
status,
deleted_at,
created_at,
updated_at,
NULL::bigint,
NULL::bigint,
NULL::integer,
NULL::integer,
NULL::integer,
NULL::timestamptz,
NULL::timestamptz
`, id, parsedProductID, sku.Name, sku.Price, attrs)
createdSKU, err := scanSKU(row)
@ -557,25 +671,33 @@ RETURNING id, product_id, name, price, 0, attributes, created_at, updated_at
return nil, fmt.Errorf("insert sku %s: %w", sku.ID, err)
}
if err := upsertSKUInventory(ctx, tx, id, sku.AvailableQuantity); err != nil {
inventory, err := upsertSKUInventory(ctx, tx, id, sku.AvailableQuantity)
if err != nil {
return nil, err
}
createdSKU.AvailableQuantity = sku.AvailableQuantity
createdSKU.Inventory = inventory
if inventory != nil {
createdSKU.AvailableQuantity = inventory.AvailableQuantity
createdSKU.LockedQuantity = inventory.LockedQuantity
createdSKU.SoldQuantity = inventory.SoldQuantity
}
return createdSKU, nil
}
func upsertSKUInventory(ctx context.Context, tx *sql.Tx, skuID int64, availableQuantity int32) error {
_, err := tx.ExecContext(ctx, `
func upsertSKUInventory(ctx context.Context, tx *sql.Tx, skuID int64, availableQuantity int32) (*InventoryRecord, error) {
row := tx.QueryRowContext(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()
RETURNING id, sku_id, available_quantity, locked_quantity, sold_quantity, created_at, updated_at
`, skuID, availableQuantity)
inventory, err := scanInventory(row)
if err != nil {
return fmt.Errorf("upsert sku inventory %s: %w", formatID(skuID), err)
return nil, fmt.Errorf("upsert sku inventory %s: %w", formatID(skuID), err)
}
return nil
return inventory, nil
}
func (r *ProductRepository) loadSKUsByProductIDs(ctx context.Context, productIDs []string) (map[string][]SKURecord, error) {
@ -598,12 +720,21 @@ func (r *ProductRepository) loadSKUsByProductIDs(ctx context.Context, productIDs
query := fmt.Sprintf(`
SELECT pi.id,
pi.product_id,
pi.sku_no,
pi.name,
pi.price,
COALESCE(inv.sold_quantity, 0),
pi.attributes,
pi.status,
pi.deleted_at,
pi.created_at,
pi.updated_at
pi.updated_at,
inv.id,
inv.sku_id,
inv.available_quantity,
inv.locked_quantity,
inv.sold_quantity,
inv.created_at,
inv.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)
@ -631,52 +762,285 @@ ORDER BY pi.created_at ASC
return result, nil
}
func (r *ProductRepository) loadCategoryProductSKUsByProductIDs(ctx context.Context, productIDs []string) (map[string][]CategoryProductSKURecord, error) {
result := make(map[string][]CategoryProductSKURecord, len(productIDs))
if len(productIDs) == 0 {
return result, nil
}
placeholders := make([]string, len(productIDs))
args := make([]any, len(productIDs))
for i, id := range productIDs {
parsed, err := parseID(id, "product id")
if err != nil {
return nil, err
}
placeholders[i] = fmt.Sprintf("$%d", i+1)
args[i] = parsed
}
query := fmt.Sprintf(`
SELECT pi.id,
pi.product_id,
COALESCE(inv.available_quantity, 0),
pi.price
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...)
if err != nil {
return nil, fmt.Errorf("query category product skus: %w", err)
}
defer rows.Close()
for rows.Next() {
sku, err := scanCategoryProductSKU(rows)
if err != nil {
return nil, err
}
result[sku.ProductID] = append(result[sku.ProductID], *sku)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterate category product skus: %w", err)
}
return result, nil
}
type rowScanner interface {
Scan(dest ...any) error
}
func scanProduct(scanner rowScanner) (*ProductRecord, error) {
func scanCategoryProduct(scanner rowScanner) (*CategoryProductRecord, error) {
var (
rec ProductRecord
id int64
status int16
attrsJSON []byte
rec CategoryProductRecord
id int64
status int16
imageIDsJSON string
)
if err := scanner.Scan(
&id, &rec.SPUNo, &rec.Title, &rec.SellingPoint, &status, &attrsJSON, &rec.CreatedAt, &rec.UpdatedAt,
&rec.UpdatedAt,
&rec.CreatedAt,
&rec.Title,
&id,
&rec.SPUNo,
&status,
&imageIDsJSON,
); err != nil {
return nil, fmt.Errorf("scan category product: %w", err)
}
rec.ID = formatID(id)
rec.Status = statusFromDB(status)
imageIDs, err := parseStringArrayJSON(imageIDsJSON)
if err != nil {
return nil, fmt.Errorf("unmarshal category product image_ids: %w", err)
}
rec.ImageIDs = imageIDs
return &rec, nil
}
func scanCategoryProductSKU(scanner rowScanner) (*CategoryProductSKURecord, error) {
var (
rec CategoryProductSKURecord
id int64
productID int64
)
if err := scanner.Scan(
&id,
&productID,
&rec.AvailableQuantity,
&rec.Price,
); err != nil {
return nil, fmt.Errorf("scan category product sku: %w", err)
}
rec.ID = formatID(id)
rec.ProductID = formatID(productID)
return &rec, nil
}
func scanProduct(scanner rowScanner) (*ProductRecord, error) {
var (
rec ProductRecord
id int64
brandID sql.NullInt64
supplierID sql.NullInt64
imageIDsJSON string
serviceGuaranteesJSON string
goodsType int16
status int16
attrsJSON []byte
freightChargeType int16
freightTemplateID sql.NullInt64
saleTimeType int16
scheduledSaleTime sql.NullTime
deletedAt sql.NullTime
)
if err := scanner.Scan(
&id,
&rec.SPUNo,
&rec.Title,
&rec.SellingPoint,
&brandID,
&supplierID,
&imageIDsJSON,
&rec.DetailHTML,
&rec.MemberDiscountEnabled,
&goodsType,
&serviceGuaranteesJSON,
&status,
&attrsJSON,
&freightChargeType,
&rec.FreightFixedAmount,
&freightTemplateID,
&saleTimeType,
&scheduledSaleTime,
&deletedAt,
&rec.CreatedAt,
&rec.UpdatedAt,
); err != nil {
return nil, fmt.Errorf("scan product: %w", err)
}
rec.ID = formatID(id)
if brandID.Valid {
rec.BrandID = formatID(brandID.Int64)
}
if supplierID.Valid {
rec.SupplierID = formatID(supplierID.Int64)
}
rec.GoodsType = int32(goodsType)
imageIDs, err := parseStringArrayJSON(imageIDsJSON)
if err != nil {
return nil, fmt.Errorf("unmarshal product image_ids: %w", err)
}
rec.ImageIDs = imageIDs
serviceGuarantees, err := parseStringArrayJSON(serviceGuaranteesJSON)
if err != nil {
return nil, fmt.Errorf("unmarshal product service_guarantees: %w", err)
}
rec.ServiceGuarantees = serviceGuarantees
rec.Status = statusFromDB(status)
rec.CrossBorderJSON = string(attrsJSON)
if rec.CrossBorderJSON == "" {
rec.CrossBorderJSON = "{}"
}
if len(attrsJSON) > 0 {
if err := json.Unmarshal(attrsJSON, &rec.Attrs); err != nil {
return nil, fmt.Errorf("unmarshal product attrs: %w", err)
}
}
if freightTemplateID.Valid {
rec.FreightTemplateID = formatID(freightTemplateID.Int64)
}
rec.FreightChargeType = int32(freightChargeType)
rec.SaleTimeType = int32(saleTimeType)
if scheduledSaleTime.Valid {
rec.ScheduledSaleTime = timePtr(scheduledSaleTime.Time)
}
if deletedAt.Valid {
rec.DeletedAt = timePtr(deletedAt.Time)
}
return &rec, nil
}
func scanSKU(scanner rowScanner) (*SKURecord, error) {
var (
rec SKURecord
id int64
productID int64
attrsJSON []byte
rec SKURecord
id int64
productID int64
skuNo sql.NullString
attrsJSON []byte
status int16
deletedAt sql.NullTime
inventoryID sql.NullInt64
inventorySKUID sql.NullInt64
availableQuantity sql.NullInt32
lockedQuantity sql.NullInt32
soldQuantity sql.NullInt32
inventoryCreatedAt sql.NullTime
inventoryUpdatedAt sql.NullTime
)
if err := scanner.Scan(
&id, &productID, &rec.Name, &rec.Price, &rec.AvailableQuantity, &attrsJSON, &rec.CreatedAt, &rec.UpdatedAt,
&id,
&productID,
&skuNo,
&rec.Name,
&rec.Price,
&attrsJSON,
&status,
&deletedAt,
&rec.CreatedAt,
&rec.UpdatedAt,
&inventoryID,
&inventorySKUID,
&availableQuantity,
&lockedQuantity,
&soldQuantity,
&inventoryCreatedAt,
&inventoryUpdatedAt,
); err != nil {
return nil, fmt.Errorf("scan sku: %w", err)
}
rec.ID = formatID(id)
rec.ProductID = formatID(productID)
if skuNo.Valid {
rec.SKUNo = skuNo.String
}
rec.AttributesJSON = string(attrsJSON)
if rec.AttributesJSON == "" {
rec.AttributesJSON = "{}"
}
rec.Status = int32(status)
if deletedAt.Valid {
rec.DeletedAt = timePtr(deletedAt.Time)
}
if inventoryID.Valid {
rec.Inventory = &InventoryRecord{
ID: formatID(inventoryID.Int64),
SKUID: formatID(inventorySKUID.Int64),
AvailableQuantity: nullInt32Value(availableQuantity),
LockedQuantity: nullInt32Value(lockedQuantity),
SoldQuantity: nullInt32Value(soldQuantity),
}
if inventoryCreatedAt.Valid {
rec.Inventory.CreatedAt = inventoryCreatedAt.Time
}
if inventoryUpdatedAt.Valid {
rec.Inventory.UpdatedAt = inventoryUpdatedAt.Time
}
rec.AvailableQuantity = rec.Inventory.AvailableQuantity
rec.LockedQuantity = rec.Inventory.LockedQuantity
rec.SoldQuantity = rec.Inventory.SoldQuantity
}
return &rec, nil
}
func scanInventory(scanner rowScanner) (*InventoryRecord, error) {
var (
rec InventoryRecord
id int64
skuID int64
)
if err := scanner.Scan(
&id,
&skuID,
&rec.AvailableQuantity,
&rec.LockedQuantity,
&rec.SoldQuantity,
&rec.CreatedAt,
&rec.UpdatedAt,
); err != nil {
return nil, fmt.Errorf("scan inventory: %w", err)
}
rec.ID = formatID(id)
rec.SKUID = formatID(skuID)
return &rec, nil
}
@ -686,16 +1050,18 @@ func scanCategory(scanner rowScanner) (*CategoryRecord, error) {
id int64
parentID sql.NullInt64
imageJSON []byte
nodeType int16
level int16
status int16
)
if err := scanner.Scan(
&id, &rec.Name, &parentID, &level, &imageJSON, &rec.SortNum, &status,
&id, &nodeType, &rec.Name, &parentID, &level, &imageJSON, &rec.SortNum, &status,
); err != nil {
return nil, fmt.Errorf("scan category: %w", err)
}
rec.ID = formatID(id)
rec.NodeType = int32(nodeType)
if parentID.Valid {
rec.ParentID = formatID(parentID.Int64)
}
@ -718,6 +1084,28 @@ func normalizeAttributesJSON(raw string) ([]byte, error) {
return []byte(raw), nil
}
func parseStringArrayJSON(raw string) ([]string, error) {
if strings.TrimSpace(raw) == "" {
return []string{}, nil
}
var values []string
if err := json.Unmarshal([]byte(raw), &values); err != nil {
return nil, err
}
return values, nil
}
func nullInt32Value(value sql.NullInt32) int32 {
if value.Valid {
return value.Int32
}
return 0
}
func timePtr(value time.Time) *time.Time {
return &value
}
func statusToDB(status productv1.ProductStatus) int16 {
switch status {
case productv1.ProductStatus_PRODUCT_STATUS_DRAFT:

View File

@ -7,6 +7,7 @@ import (
"math"
"strconv"
"strings"
"time"
"go.opentelemetry.io/otel/attribute"
otelcodes "go.opentelemetry.io/otel/codes"
@ -147,9 +148,9 @@ func (s *ProductServer) ListCategoryProducts(ctx context.Context, req *productv1
dbSpan.SetAttributes(attribute.Int64("product.total", total), attribute.Int("product.result_count", len(products)))
observability.IncCategoryProductLists()
protoProducts := make([]*productv1.Product, 0, len(products))
protoProducts := make([]*productv1.CategoryProduct, 0, len(products))
for _, product := range products {
protoProducts = append(protoProducts, toProtoProduct(product, skuMap[product.ID]))
protoProducts = append(protoProducts, toProtoCategoryProduct(product, skuMap[product.ID]))
}
return &productv1.ListCategoryProductsResponse{
@ -298,25 +299,92 @@ func toProtoProduct(product *repository.ProductRecord, skus []repository.SKUReco
protoSKUs = append(protoSKUs, &productv1.SKU{
SkuId: sku.ID,
ProductId: sku.ProductID,
SkuNo: sku.SKUNo,
Name: sku.Name,
Price: sku.Price,
AvailableQuantity: sku.AvailableQuantity,
SoldQuantity: sku.SoldQuantity,
AttributesJson: sku.AttributesJSON,
Status: sku.Status,
DeletedAt: timestampOrNil(sku.DeletedAt),
CreatedAt: timestamppb.New(sku.CreatedAt),
UpdatedAt: timestamppb.New(sku.UpdatedAt),
Inventory: toProtoInventory(sku.Inventory),
})
}
return &productv1.Product{
ProductId: product.ID,
SpuNo: product.SPUNo,
Title: product.Title,
SellingPoint: product.SellingPoint,
Status: product.Status,
Skus: protoSKUs,
CreatedAt: timestamppb.New(product.CreatedAt),
UpdatedAt: timestamppb.New(product.UpdatedAt),
ProductId: product.ID,
SpuNo: product.SPUNo,
Title: product.Title,
SellingPoint: product.SellingPoint,
Status: product.Status,
Skus: protoSKUs,
CreatedAt: timestamppb.New(product.CreatedAt),
UpdatedAt: timestamppb.New(product.UpdatedAt),
ImageIds: product.ImageIDs,
BrandId: product.BrandID,
SupplierId: product.SupplierID,
DetailHtml: product.DetailHTML,
MemberDiscountEnabled: product.MemberDiscountEnabled,
GoodsType: product.GoodsType,
ServiceGuarantees: product.ServiceGuarantees,
CrossBorderJson: product.CrossBorderJSON,
FreightChargeType: product.FreightChargeType,
FreightFixedAmount: product.FreightFixedAmount,
FreightTemplateId: product.FreightTemplateID,
SaleTimeType: product.SaleTimeType,
ScheduledSaleTime: timestampOrNil(product.ScheduledSaleTime),
DeletedAt: timestampOrNil(product.DeletedAt),
}
}
func toProtoCategoryProduct(product *repository.CategoryProductRecord, skus []repository.CategoryProductSKURecord) *productv1.CategoryProduct {
if product == nil {
return nil
}
protoSKUs := make([]*productv1.CategoryProductSKU, 0, len(skus))
for _, sku := range skus {
protoSKUs = append(protoSKUs, &productv1.CategoryProductSKU{
SkuId: sku.ID,
AvailableQuantity: sku.AvailableQuantity,
Price: sku.Price,
})
}
return &productv1.CategoryProduct{
UpdatedAt: timestamppb.New(product.UpdatedAt),
CreatedAt: timestamppb.New(product.CreatedAt),
Title: product.Title,
ProductId: product.ID,
SpuNo: product.SPUNo,
Status: product.Status,
ImageIds: product.ImageIDs,
Skus: protoSKUs,
}
}
func toProtoInventory(inventory *repository.InventoryRecord) *productv1.ProductItemInventory {
if inventory == nil {
return nil
}
return &productv1.ProductItemInventory{
InventoryId: inventory.ID,
SkuId: inventory.SKUID,
AvailableQuantity: inventory.AvailableQuantity,
LockedQuantity: inventory.LockedQuantity,
SoldQuantity: inventory.SoldQuantity,
CreatedAt: timestamppb.New(inventory.CreatedAt),
UpdatedAt: timestamppb.New(inventory.UpdatedAt),
}
}
func timestampOrNil(value *time.Time) *timestamppb.Timestamp {
if value == nil {
return nil
}
return timestamppb.New(*value)
}
func buildCategoryTree(categories []*repository.CategoryRecord) []*productv1.CategoryNode {
nodes := make(map[string]*productv1.CategoryNode, len(categories))
records := make(map[string]*repository.CategoryRecord, len(categories))
@ -359,6 +427,7 @@ func toProtoCategoryNode(category *repository.CategoryRecord) *productv1.Categor
}
return &productv1.CategoryNode{
CategoryId: category.ID,
NodeType: category.NodeType,
Name: category.Name,
ParentId: category.ParentID,
Level: category.Level,