mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat(product): 支持批量移除分类或分组中的商品
This commit is contained in:
parent
787eda9f3e
commit
cccbce7bc5
@ -425,6 +425,65 @@ ON CONFLICT (product_id, category_id) DO NOTHING
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveProductsFromGroup removes product bindings from the requested category or product group in one transaction.
|
||||||
|
// Missing bindings are ignored so callers can safely retry the same request.
|
||||||
|
func (r *ProductRepository) RemoveProductsFromGroup(ctx context.Context, groupID string, productIDs []string) error {
|
||||||
|
if len(productIDs) == 0 {
|
||||||
|
return errors.New("product_ids is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
parsedGroupID, err := parseID(groupID, "group_id")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
parsedProductIDs := make([]int64, len(productIDs))
|
||||||
|
for i := range productIDs {
|
||||||
|
parsedProductIDs[i], err = parseID(productIDs[i], "product_id")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tx, err := r.db.BeginTx(ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("begin remove products from group: %w", err)
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
err = tx.QueryRowContext(ctx, `
|
||||||
|
SELECT id
|
||||||
|
FROM product.product_category
|
||||||
|
WHERE id = $1
|
||||||
|
`, parsedGroupID).Scan(&parsedGroupID)
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return fmt.Errorf("%w: %s", ErrCategoryNodeNotFound, groupID)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("query category or group node %s: %w", groupID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
placeholders := make([]string, len(parsedProductIDs))
|
||||||
|
args := make([]any, 0, len(parsedProductIDs)+1)
|
||||||
|
args = append(args, parsedGroupID)
|
||||||
|
for i, productID := range parsedProductIDs {
|
||||||
|
placeholders[i] = fmt.Sprintf("$%d", i+2)
|
||||||
|
args = append(args, productID)
|
||||||
|
}
|
||||||
|
_, err = tx.ExecContext(ctx, fmt.Sprintf(`
|
||||||
|
DELETE FROM product.product_category_bindings
|
||||||
|
WHERE category_id = $1
|
||||||
|
AND product_id IN (%s)
|
||||||
|
`, strings.Join(placeholders, ", ")), args...)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("delete product category or group bindings: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Commit(); err != nil {
|
||||||
|
return fmt.Errorf("commit remove products from group: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateProduct updates mutable SPU fields.
|
// UpdateProduct updates mutable SPU fields.
|
||||||
func (r *ProductRepository) UpdateProduct(ctx context.Context, product ProductRecord) error {
|
func (r *ProductRepository) UpdateProduct(ctx context.Context, product ProductRecord) error {
|
||||||
attrs, err := json.Marshal(product.Attrs)
|
attrs, err := json.Marshal(product.Attrs)
|
||||||
|
|||||||
@ -379,6 +379,40 @@ func (s *ProductServer) AddProductsToGroup(ctx context.Context, req *productv1.A
|
|||||||
return &productv1.AddProductsToGroupResponse{Success: true}, nil
|
return &productv1.AddProductsToGroupResponse{Success: true}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveProductsFromGroup removes one or more products from a category or product group.
|
||||||
|
func (s *ProductServer) RemoveProductsFromGroup(ctx context.Context, req *productv1.RemoveProductsFromGroupRequest) (*productv1.RemoveProductsFromGroupResponse, error) {
|
||||||
|
if req == nil {
|
||||||
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
groupID := strings.TrimSpace(req.GetGroupId())
|
||||||
|
if err := validatePositiveSnowflakeID(groupID, "group_id"); err != nil {
|
||||||
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||||
|
}
|
||||||
|
productIDs, err := normalizeGroupProductIDs(req.GetProductIds())
|
||||||
|
if err != nil {
|
||||||
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
dbCtx, dbSpan := s.observer.Tracer().Start(ctx, "product.group.remove_products", trace.WithAttributes(
|
||||||
|
attribute.String("group.id", groupID),
|
||||||
|
attribute.Int("group.product_count", len(productIDs)),
|
||||||
|
))
|
||||||
|
defer dbSpan.End()
|
||||||
|
|
||||||
|
if err := s.repo.RemoveProductsFromGroup(dbCtx, groupID, productIDs); err != nil {
|
||||||
|
dbSpan.RecordError(err)
|
||||||
|
dbSpan.SetStatus(otelcodes.Error, err.Error())
|
||||||
|
s.observer.Logger(ctx).Error("remove products from group failed", zap.String("group_id", groupID), zap.Strings("product_ids", productIDs), zap.Error(err))
|
||||||
|
if errors.Is(err, repository.ErrCategoryNodeNotFound) {
|
||||||
|
return nil, status.Error(codes.NotFound, err.Error())
|
||||||
|
}
|
||||||
|
return nil, status.Errorf(codes.Internal, "remove products from group: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &productv1.RemoveProductsFromGroupResponse{Success: true}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// LockInventory reserves sellable stock for a pending order.
|
// LockInventory reserves sellable stock for a pending order.
|
||||||
func (s *ProductServer) LockInventory(ctx context.Context, req *productv1.LockInventoryRequest) (*productv1.LockInventoryResponse, error) {
|
func (s *ProductServer) LockInventory(ctx context.Context, req *productv1.LockInventoryRequest) (*productv1.LockInventoryResponse, error) {
|
||||||
if req == nil || req.GetSkuId() == "" {
|
if req == nil || req.GetSkuId() == "" {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user