feat: refactor product group handling to use structured product items with SPU numbers

This commit is contained in:
BXY 2026-10-28 16:28:39 +08:00
parent d1a4207d3e
commit deaf35e55c
3 changed files with 42 additions and 19 deletions

View File

@ -399,7 +399,13 @@ type CategoryRecord struct {
type GroupBrandProductRecord struct {
BrandID string
BrandName string
ProductIDs []string
Products []GroupBrandProductItemRecord
}
// GroupBrandProductItemRecord is one product and its SPU number in a brand group.
type GroupBrandProductItemRecord struct {
ProductID string
SPUNo string
}
// CreateCatalogCategoryTree inserts a catalog-category subtree of arbitrary
@ -4389,7 +4395,7 @@ WHERE c.node_type = 2
ELSE c.name ILIKE ('%' || $4 || '%') ESCAPE '\'
END))
GROUP BY c.id
ORDER BY c.sort_num ASC, c.id ASC
ORDER BY c.created_at DESC, c.id DESC
LIMIT $6 OFFSET $7
`, scope.StoreID, filter.OnlyVisible, firstLevelName, secondLevelName, filter.ExactMatch, pageSize, offset)
if err != nil {
@ -4443,7 +4449,8 @@ SELECT c.id,
WHEN b.id IS NULL THEN '品牌已删除'
ELSE b.name
END AS brand_name,
p.id
p.id,
COALESCE(p.spu_no, '') AS spu_no
FROM product.catalog_category AS c
JOIN product.product_catalog_bindings AS binding ON binding.category_id = c.id
JOIN product.products AS p ON p.id = binding.product_id
@ -4482,8 +4489,8 @@ ORDER BY c.id ASC,
lastBrandKey := make(map[string]string, len(groupsByID))
for rows.Next() {
var groupID, brandID, productID int64
var name string
if err := rows.Scan(&groupID, &brandID, &name, &productID); err != nil {
var name, spuNo string
if err := rows.Scan(&groupID, &brandID, &name, &productID, &spuNo); err != nil {
return fmt.Errorf("scan group brand product: %w", err)
}
groupKey := formatID(groupID)
@ -4500,12 +4507,15 @@ ORDER BY c.id ASC,
group.BrandProducts = append(group.BrandProducts, GroupBrandProductRecord{
BrandID: brandIDValue,
BrandName: name,
ProductIDs: []string{},
Products: []GroupBrandProductItemRecord{},
})
lastBrandKey[groupKey] = brandKey
}
brand := &group.BrandProducts[len(group.BrandProducts)-1]
brand.ProductIDs = append(brand.ProductIDs, formatID(productID))
brand.Products = append(brand.Products, GroupBrandProductItemRecord{
ProductID: formatID(productID),
SPUNo: spuNo,
})
}
if err := rows.Err(); err != nil {
return fmt.Errorf("iterate group brand products: %w", err)

View File

@ -47,14 +47,20 @@ func TestGroupNodeIncludesBrandProductsOnlyForLeafGroups(t *testing.T) {
BrandProducts: []repository.GroupBrandProductRecord{{
BrandID: "3001",
BrandName: "Brand",
ProductIDs: []string{"2002", "2001"},
Products: []repository.GroupBrandProductItemRecord{{
ProductID: "2002",
SPUNo: "SPU-2",
}, {
ProductID: "2001",
SPUNo: "SPU-1",
}},
}},
})
if !leaf.GetIsLeaf() || len(leaf.GetBrands()) != 1 {
t.Fatalf("leaf = %#v, want one brand", leaf)
}
if got := leaf.GetBrands()[0].GetProductIds(); len(got) != 2 || got[0] != "2002" || got[1] != "2001" {
t.Fatalf("brand product_ids = %#v", got)
if got := leaf.GetBrands()[0].GetProducts(); len(got) != 2 || got[0].GetProductId() != "2002" || got[0].GetSpuNo() != "SPU-2" || got[1].GetProductId() != "2001" || got[1].GetSpuNo() != "SPU-1" {
t.Fatalf("brand products = %#v", got)
}
emptyLeaf := toProtoGroupNode(&repository.CategoryRecord{ID: "1003", Level: 2, IsLeaf: true})

View File

@ -3236,8 +3236,15 @@ func toProtoGroupNode(group *repository.CategoryRecord) *productv1.GroupNode {
node.Brands = append(node.Brands, &productv1.GroupBrandProductSummary{
BrandId: brand.BrandID,
BrandName: brand.BrandName,
ProductIds: append([]string{}, brand.ProductIDs...),
})
protoBrand := node.Brands[len(node.Brands)-1]
protoBrand.Products = make([]*productv1.GroupBrandProduct, 0, len(brand.Products))
for _, product := range brand.Products {
protoBrand.Products = append(protoBrand.Products, &productv1.GroupBrandProduct{
ProductId: product.ProductID,
SpuNo: product.SPUNo,
})
}
}
}
return node