mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: add BatchGetStorefrontProductSKUs method for fetching sellable SKU prices
This commit is contained in:
parent
a86d9a7513
commit
d35d54be80
@ -201,6 +201,14 @@ type CartItemProductRecord struct {
|
||||
SKU *SKURecord
|
||||
}
|
||||
|
||||
// StorefrontProductSKURecord is the minimal current-SKU projection used when
|
||||
// a storefront needs to evaluate display-only promotions for product cards.
|
||||
type StorefrontProductSKURecord struct {
|
||||
ProductID string
|
||||
SKUID string
|
||||
Price int64
|
||||
}
|
||||
|
||||
// SKUChangeMode selects how SKU input is applied during a product update.
|
||||
type SKUChangeMode int32
|
||||
|
||||
@ -3216,6 +3224,77 @@ func (r *ProductRepository) BatchGetCartItemProducts(ctx context.Context, lookup
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// BatchGetStorefrontProductSKUs returns only sellable SKU prices for visible
|
||||
// storefront products. Archived, disabled and soft-deleted specifications are
|
||||
// intentionally absent so promotion displays cannot advertise an unavailable SKU.
|
||||
func (r *ProductRepository) BatchGetStorefrontProductSKUs(ctx context.Context, productIDs []string, scope StoreScope) ([]StorefrontProductSKURecord, error) {
|
||||
if err := requireWriteScope(scope); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(productIDs) == 0 {
|
||||
return []StorefrontProductSKURecord{}, nil
|
||||
}
|
||||
|
||||
placeholders := make([]string, 0, len(productIDs))
|
||||
args := make([]any, 0, len(productIDs)+3)
|
||||
seen := make(map[string]struct{}, len(productIDs))
|
||||
for _, productID := range productIDs {
|
||||
if _, exists := seen[productID]; exists {
|
||||
continue
|
||||
}
|
||||
parsedID, err := parseID(productID, "product id")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
seen[productID] = struct{}{}
|
||||
args = append(args, parsedID)
|
||||
placeholders = append(placeholders, fmt.Sprintf("$%d", len(args)))
|
||||
}
|
||||
args = append(args, scope.StoreID,
|
||||
statusToDB(productv1.ProductStatus_PRODUCT_STATUS_ON_SALE),
|
||||
statusToDB(productv1.ProductStatus_PRODUCT_STATUS_OFF_SALE),
|
||||
)
|
||||
storeArg := len(args) - 2
|
||||
onSaleArg := len(args) - 1
|
||||
offSaleArg := len(args)
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, fmt.Sprintf(`
|
||||
SELECT pi.product_id, pi.id, pi.price
|
||||
FROM product.product_items pi
|
||||
JOIN product.products p ON p.id = pi.product_id
|
||||
WHERE pi.product_id IN (%s)
|
||||
AND p.deleted_at IS NULL
|
||||
AND p.store_id = $%d
|
||||
AND p.status IN ($%d, $%d)
|
||||
AND pi.deleted_at IS NULL
|
||||
AND pi.is_archived = FALSE
|
||||
AND pi.status = 1
|
||||
ORDER BY pi.product_id ASC, pi.price ASC, pi.id ASC
|
||||
`, strings.Join(placeholders, ", "), storeArg, onSaleArg, offSaleArg), args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query storefront product skus: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
result := make([]StorefrontProductSKURecord, 0)
|
||||
for rows.Next() {
|
||||
var productID, skuID int64
|
||||
var price int64
|
||||
if err := rows.Scan(&productID, &skuID, &price); err != nil {
|
||||
return nil, fmt.Errorf("scan storefront product sku: %w", err)
|
||||
}
|
||||
result = append(result, StorefrontProductSKURecord{
|
||||
ProductID: formatID(productID),
|
||||
SKUID: formatID(skuID),
|
||||
Price: price,
|
||||
})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate storefront product skus: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ValidateProductGroup verifies that a group belongs to the current store.
|
||||
func (r *ProductRepository) ValidateProductGroup(ctx context.Context, groupID string, scope StoreScope) error {
|
||||
if err := requireWriteScope(scope); err != nil {
|
||||
|
||||
@ -37,12 +37,13 @@ const (
|
||||
categoryNodeType = int32(1)
|
||||
groupNodeType = int32(2)
|
||||
|
||||
defaultNodeStatus = int32(1)
|
||||
maxNodeNameLength = 100
|
||||
maxProductsPerGroupRequest = 100
|
||||
maxCartItemProductBatchSize = 100
|
||||
maxSearchKeywordLength = 50
|
||||
maxProductTitleLength = 200
|
||||
defaultNodeStatus = int32(1)
|
||||
maxNodeNameLength = 100
|
||||
maxProductsPerGroupRequest = 100
|
||||
maxCartItemProductBatchSize = 100
|
||||
maxStorefrontProductBatchSize = 100
|
||||
maxSearchKeywordLength = 50
|
||||
maxProductTitleLength = 200
|
||||
)
|
||||
|
||||
// ProductServer implements productv1.ProductServiceServer.
|
||||
@ -347,6 +348,63 @@ func (s *ProductServer) BatchGetCartItemProduct(ctx context.Context, req *produc
|
||||
return &productv1.BatchGetCartItemProductResponse{Items: items}, nil
|
||||
}
|
||||
|
||||
// BatchGetStorefrontProductSKUs returns the current sellable SKU prices needed
|
||||
// to calculate product-card promotion display data in one downstream call.
|
||||
func (s *ProductServer) BatchGetStorefrontProductSKUs(ctx context.Context, req *productv1.BatchGetStorefrontProductSKUsRequest) (*productv1.BatchGetStorefrontProductSKUsResponse, error) {
|
||||
scope, err := requireStoreScope(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(req.GetProductIds()) == 0 {
|
||||
return nil, status.Error(codes.InvalidArgument, "product_ids is required")
|
||||
}
|
||||
if len(req.GetProductIds()) > maxStorefrontProductBatchSize {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "product_ids must contain at most %d entries", maxStorefrontProductBatchSize)
|
||||
}
|
||||
|
||||
productIDs := make([]string, 0, len(req.GetProductIds()))
|
||||
seen := make(map[string]struct{}, len(req.GetProductIds()))
|
||||
for index, productID := range req.GetProductIds() {
|
||||
if err := validatePositiveID(productID, "product_id"); err != nil {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "product_ids[%d]: %v", index, err)
|
||||
}
|
||||
if _, exists := seen[productID]; exists {
|
||||
continue
|
||||
}
|
||||
seen[productID] = struct{}{}
|
||||
productIDs = append(productIDs, productID)
|
||||
}
|
||||
|
||||
dbCtx, dbSpan := s.observer.Tracer().Start(ctx, "product.storefront_skus.batch_get", trace.WithAttributes(
|
||||
attribute.Int("product.count", len(productIDs)),
|
||||
))
|
||||
defer dbSpan.End()
|
||||
records, err := s.repo.BatchGetStorefrontProductSKUs(dbCtx, productIDs, scope)
|
||||
if err != nil {
|
||||
dbSpan.RecordError(err)
|
||||
dbSpan.SetStatus(otelcodes.Error, err.Error())
|
||||
s.observer.Logger(ctx).Error("batch get storefront product skus failed", zap.Error(err))
|
||||
return nil, status.Errorf(codes.Internal, "batch get storefront product skus: %v", err)
|
||||
}
|
||||
|
||||
byProduct := make(map[string][]*productv1.StorefrontProductSKU, len(productIDs))
|
||||
for _, record := range records {
|
||||
byProduct[record.ProductID] = append(byProduct[record.ProductID], &productv1.StorefrontProductSKU{
|
||||
SkuId: record.SKUID,
|
||||
OriginalPrice: record.Price,
|
||||
})
|
||||
}
|
||||
products := make([]*productv1.StorefrontProductSKUs, 0, len(byProduct))
|
||||
for _, productID := range productIDs {
|
||||
skus := byProduct[productID]
|
||||
if len(skus) == 0 {
|
||||
continue
|
||||
}
|
||||
products = append(products, &productv1.StorefrontProductSKUs{ProductId: productID, Skus: skus})
|
||||
}
|
||||
return &productv1.BatchGetStorefrontProductSKUsResponse{Products: products}, nil
|
||||
}
|
||||
|
||||
// UpdateProduct updates mutable SPU fields and optionally patches or replaces its SKU set.
|
||||
func (s *ProductServer) UpdateProduct(ctx context.Context, req *productv1.UpdateProductRequest) (*productv1.UpdateProductResponse, error) {
|
||||
scope, err := requireStoreScope(req)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user