mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: add GetProductTotalSoldQuantity method to track total sold quantity for products
This commit is contained in:
parent
e2e14ae337
commit
d4ef3b5a57
@ -2678,6 +2678,33 @@ func (r *ProductRepository) GetProduct(ctx context.Context, productID string, sc
|
|||||||
return product, skus[productID], nil
|
return product, skus[productID], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetProductTotalSoldQuantity returns the lifetime paid-unit count for one
|
||||||
|
// product in the current store. Retired SKU rows are intentionally included:
|
||||||
|
// removing a SKU from the storefront must not make the product's cumulative
|
||||||
|
// sales count decrease.
|
||||||
|
func (r *ProductRepository) GetProductTotalSoldQuantity(ctx context.Context, productID string, scope StoreScope) (int64, error) {
|
||||||
|
if err := requireWriteScope(scope); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
parsedProductID, err := parseID(productID, "product_id")
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var soldQuantity int64
|
||||||
|
err = r.db.QueryRowContext(ctx, `
|
||||||
|
SELECT COALESCE(SUM(COALESCE(inv.sold_quantity, 0)), 0)
|
||||||
|
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
|
||||||
|
AND pi.store_id = $2
|
||||||
|
`, parsedProductID, scope.StoreID).Scan(&soldQuantity)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("query product total sold quantity: %w", err)
|
||||||
|
}
|
||||||
|
return soldQuantity, nil
|
||||||
|
}
|
||||||
|
|
||||||
// ListProductSKUStocks loads every current SKU and its inventory pool quantity for one product.
|
// ListProductSKUStocks loads every current SKU and its inventory pool quantity for one product.
|
||||||
// Archived and soft-deleted SKU rows are historical records and are deliberately excluded.
|
// Archived and soft-deleted SKU rows are historical records and are deliberately excluded.
|
||||||
func (r *ProductRepository) ListProductSKUStocks(ctx context.Context, productID string, scope StoreScope) ([]ProductSKUStockRecord, error) {
|
func (r *ProductRepository) ListProductSKUStocks(ctx context.Context, productID string, scope StoreScope) ([]ProductSKUStockRecord, error) {
|
||||||
|
|||||||
@ -185,9 +185,17 @@ func (s *ProductServer) GetProduct(ctx context.Context, req *productv1.GetProduc
|
|||||||
s.observer.Logger(ctx).Error("get product failed", zap.String("product_id", req.GetProductId()), zap.Error(err))
|
s.observer.Logger(ctx).Error("get product failed", zap.String("product_id", req.GetProductId()), zap.Error(err))
|
||||||
return nil, status.Errorf(codes.Internal, "get product: %v", err)
|
return nil, status.Errorf(codes.Internal, "get product: %v", err)
|
||||||
}
|
}
|
||||||
|
totalSoldQuantity, err := s.repo.GetProductTotalSoldQuantity(dbCtx, req.GetProductId(), scope)
|
||||||
|
if err != nil {
|
||||||
|
dbSpan.RecordError(err)
|
||||||
|
dbSpan.SetStatus(otelcodes.Error, err.Error())
|
||||||
|
s.observer.Logger(ctx).Error("get product total sold quantity failed", zap.String("product_id", req.GetProductId()), zap.Error(err))
|
||||||
|
return nil, status.Errorf(codes.Internal, "get product total sold quantity: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
return &productv1.GetProductResponse{
|
return &productv1.GetProductResponse{
|
||||||
Product: toProtoProduct(product, skus),
|
Product: toProtoProduct(product, skus),
|
||||||
|
TotalSoldQuantity: totalSoldQuantity,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user