mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: implement ListStorefrontProducts method for fetching buyer-visible products
This commit is contained in:
parent
f46b1a9b22
commit
dfa843b413
@ -4078,6 +4078,73 @@ func productListWhere(filter ProductListFilter, scope StoreScope) (string, []any
|
|||||||
return strings.Join(predicates, " AND "), args
|
return strings.Join(predicates, " AND "), args
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListStorefrontProducts returns only buyer-visible products and computes all
|
||||||
|
// page metrics from enabled, non-archived SKUs.
|
||||||
|
func (r *ProductRepository) ListStorefrontProducts(ctx context.Context, page, pageSize int32, scope StoreScope) ([]*CategoryProductRecord, int64, error) {
|
||||||
|
if err := requireWriteScope(scope); err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
if page < 1 {
|
||||||
|
page = 1
|
||||||
|
}
|
||||||
|
if pageSize < 1 {
|
||||||
|
pageSize = 20
|
||||||
|
}
|
||||||
|
where := `p.deleted_at IS NULL AND p.store_id = $1 AND p.status IN ($2, $3)
|
||||||
|
AND EXISTS (SELECT 1 FROM product.product_items pi
|
||||||
|
WHERE pi.product_id = p.id AND pi.deleted_at IS NULL
|
||||||
|
AND pi.is_archived = FALSE AND pi.status = 1)`
|
||||||
|
args := []any{scope.StoreID, statusToDB(productv1.ProductStatus_PRODUCT_STATUS_ON_SALE), statusToDB(productv1.ProductStatus_PRODUCT_STATUS_SOLD_OUT)}
|
||||||
|
var total int64
|
||||||
|
if err := r.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM product.products p WHERE `+where, args...).Scan(&total); err != nil {
|
||||||
|
return nil, 0, fmt.Errorf("count storefront products: %w", err)
|
||||||
|
}
|
||||||
|
if total == 0 {
|
||||||
|
return []*CategoryProductRecord{}, 0, nil
|
||||||
|
}
|
||||||
|
offset := int64(page-1) * int64(pageSize)
|
||||||
|
rows, err := r.db.QueryContext(ctx, `
|
||||||
|
SELECT p.updated_at, p.created_at, p.title, p.id, p.spu_no, p.status,
|
||||||
|
COALESCE(to_json(p.image_ids)::text, '[]'),
|
||||||
|
COALESCE(p.category_properties, '{}'::jsonb)::text,
|
||||||
|
p.is_distribution_product, p.presale_type,
|
||||||
|
COALESCE(sku_summary.price, 0), COALESCE(sku_summary.sold_quantity, 0),
|
||||||
|
COALESCE(sku_summary.stock_quantity, 0), COALESCE(sku_summary.locked_quantity, 0)
|
||||||
|
FROM product.products p
|
||||||
|
LEFT JOIN LATERAL (
|
||||||
|
SELECT MIN(pi.price) AS price,
|
||||||
|
SUM(COALESCE(inv.sold_quantity, 0)) AS sold_quantity,
|
||||||
|
SUM(COALESCE(inv.stock_quantity, 0)) AS stock_quantity,
|
||||||
|
SUM(COALESCE(inv.locked_quantity, 0)) AS locked_quantity
|
||||||
|
FROM product.product_items pi
|
||||||
|
LEFT JOIN product.product_item_inventory inv ON inv.sku_id = pi.id
|
||||||
|
WHERE pi.product_id = p.id AND pi.deleted_at IS NULL
|
||||||
|
AND pi.is_archived = FALSE AND pi.status = 1
|
||||||
|
) sku_summary ON TRUE
|
||||||
|
WHERE `+where+`
|
||||||
|
ORDER BY p.created_at DESC, p.id DESC
|
||||||
|
LIMIT $4 OFFSET $5`, append(args, pageSize, offset)...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, fmt.Errorf("query storefront products: %w", err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
products := make([]*CategoryProductRecord, 0, pageSize)
|
||||||
|
for rows.Next() {
|
||||||
|
product, err := scanCategoryProduct(rows)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
products = append(products, product)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, 0, fmt.Errorf("iterate storefront products: %w", err)
|
||||||
|
}
|
||||||
|
if err := r.populateProductListMetadata(ctx, products, scope); err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
return products, total, nil
|
||||||
|
}
|
||||||
|
|
||||||
// SearchProducts loads storefront-visible product summaries whose titles contain
|
// SearchProducts loads storefront-visible product summaries whose titles contain
|
||||||
// keyword. Callers cannot opt out of the visibility predicate: soft-deleted and
|
// keyword. Callers cannot opt out of the visibility predicate: soft-deleted and
|
||||||
// inventory-only products must never be exposed by keyword search.
|
// inventory-only products must never be exposed by keyword search.
|
||||||
|
|||||||
@ -1018,6 +1018,31 @@ func productListFilterFromRequest(req *productv1.ListProductsRequest) (repositor
|
|||||||
return filter, nil
|
return filter, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListStorefrontProducts returns buyer-visible products in the admin list shape.
|
||||||
|
func (s *ProductServer) ListStorefrontProducts(ctx context.Context, req *productv1.ListStorefrontProductsRequest) (*productv1.ListProductsResponse, error) {
|
||||||
|
if req == nil {
|
||||||
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
||||||
|
}
|
||||||
|
scope, err := requireStoreScope(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
page, pageSize := normalizedPage(req.GetPage(), req.GetPageSize())
|
||||||
|
if pageSize > 100 {
|
||||||
|
return nil, status.Error(codes.InvalidArgument, "page_size must be between 1 and 100")
|
||||||
|
}
|
||||||
|
products, total, err := s.repo.ListStorefrontProducts(ctx, page, pageSize, scope)
|
||||||
|
if err != nil {
|
||||||
|
s.observer.Logger(ctx).Error("list storefront products failed", zap.Error(err))
|
||||||
|
return nil, status.Errorf(codes.Internal, "list storefront products: %v", err)
|
||||||
|
}
|
||||||
|
items := make([]*productv1.CategoryProduct, 0, len(products))
|
||||||
|
for _, product := range products {
|
||||||
|
items = append(items, toProtoCategoryProduct(product))
|
||||||
|
}
|
||||||
|
return &productv1.ListProductsResponse{Products: items, Page: page, PageSize: pageSize, Total: total, TotalPages: totalPages(total, pageSize)}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// SearchProducts returns storefront-visible products whose titles contain the
|
// SearchProducts returns storefront-visible products whose titles contain the
|
||||||
// supplied keyword. Visibility is enforced inside the repository and cannot be
|
// supplied keyword. Visibility is enforced inside the repository and cannot be
|
||||||
// relaxed by callers.
|
// relaxed by callers.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user