feat(product): add CountProducts method to retrieve aggregate product counts by lifecycle status

This commit is contained in:
robinj0726 2026-08-06 12:37:38 +08:00
parent bec6d5a69a
commit 6856553440
2 changed files with 57 additions and 0 deletions

View File

@ -74,6 +74,16 @@ type ProductRecord struct {
UpdatedAt time.Time UpdatedAt time.Time
} }
// ProductCountRecord contains aggregate counts for product.products.
type ProductCountRecord struct {
Total int64
Active int64
Draft int64
OnSale int64
OffSale int64
Deleted int64
}
// SKURecord is the persistence model for product.product_items. // SKURecord is the persistence model for product.product_items.
type SKURecord struct { type SKURecord struct {
ID string ID string
@ -1295,6 +1305,30 @@ LIMIT $1 OFFSET $2
return products, total, nil return products, total, nil
} }
// CountProducts returns aggregate product counts grouped by lifecycle status.
func (r *ProductRepository) CountProducts(ctx context.Context) (*ProductCountRecord, error) {
var counts ProductCountRecord
if err := r.db.QueryRowContext(ctx, `
SELECT COUNT(*) AS total,
COUNT(*) FILTER (WHERE deleted_at IS NULL) AS active,
COUNT(*) FILTER (WHERE deleted_at IS NULL AND status = 0) AS draft,
COUNT(*) FILTER (WHERE deleted_at IS NULL AND status = 1) AS on_sale,
COUNT(*) FILTER (WHERE deleted_at IS NULL AND status = 3) AS off_sale,
COUNT(*) FILTER (WHERE deleted_at IS NOT NULL) AS deleted
FROM product.products
`).Scan(
&counts.Total,
&counts.Active,
&counts.Draft,
&counts.OnSale,
&counts.OffSale,
&counts.Deleted,
); err != nil {
return nil, fmt.Errorf("count products: %w", err)
}
return &counts, nil
}
// ListCategoryProducts loads paginated product summaries for a category ID. // ListCategoryProducts loads paginated product summaries for a category ID.
func (r *ProductRepository) ListCategoryProducts(ctx context.Context, categoryID string, page, pageSize int32, onlyVisible bool) ([]*CategoryProductRecord, int64, error) { func (r *ProductRepository) ListCategoryProducts(ctx context.Context, categoryID string, page, pageSize int32, onlyVisible bool) ([]*CategoryProductRecord, int64, error) {
parsedCategoryID, err := parseID(categoryID, "category id") parsedCategoryID, err := parseID(categoryID, "category id")

View File

@ -336,6 +336,29 @@ func normalizedPage(page, pageSize int32) (int32, int32) {
return page, pageSize return page, pageSize
} }
// CountProducts returns product counts grouped by lifecycle status.
func (s *ProductServer) CountProducts(ctx context.Context, req *productv1.CountProductsRequest) (*productv1.CountProductsResponse, error) {
dbCtx, dbSpan := s.observer.Tracer().Start(ctx, "product.count")
defer dbSpan.End()
counts, err := s.repo.CountProducts(dbCtx)
if err != nil {
dbSpan.RecordError(err)
dbSpan.SetStatus(otelcodes.Error, err.Error())
s.observer.Logger(ctx).Error("count products failed", zap.Error(err))
return nil, status.Errorf(codes.Internal, "count products: %v", err)
}
return &productv1.CountProductsResponse{
Total: counts.Total,
Active: counts.Active,
Draft: counts.Draft,
OnSale: counts.OnSale,
OffSale: counts.OffSale,
Deleted: counts.Deleted,
}, nil
}
// ListCategoryTree returns product category nodes as a parent-child hierarchy. // ListCategoryTree returns product category nodes as a parent-child hierarchy.
func (s *ProductServer) ListCategoryTree(ctx context.Context, req *productv1.ListCategoryTreeRequest) (*productv1.ListCategoryTreeResponse, error) { func (s *ProductServer) ListCategoryTree(ctx context.Context, req *productv1.ListCategoryTreeRequest) (*productv1.ListCategoryTreeResponse, error) {
onlyVisible := false onlyVisible := false