diff --git a/internal/repository/product_repository.go b/internal/repository/product_repository.go index 8d7c8e9..69a6ebe 100644 --- a/internal/repository/product_repository.go +++ b/internal/repository/product_repository.go @@ -397,9 +397,15 @@ type CategoryRecord struct { // GroupBrandProductRecord is one leaf group's direct product bindings for a brand. type GroupBrandProductRecord struct { - BrandID string - BrandName string - ProductIDs []string + BrandID string + BrandName 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) @@ -4498,14 +4505,17 @@ ORDER BY c.id ASC, brandIDValue = formatID(brandID) } group.BrandProducts = append(group.BrandProducts, GroupBrandProductRecord{ - BrandID: brandIDValue, - BrandName: name, - ProductIDs: []string{}, + BrandID: brandIDValue, + BrandName: name, + 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) diff --git a/internal/server/category_image_ids_test.go b/internal/server/category_image_ids_test.go index ffad9a1..d00ee87 100644 --- a/internal/server/category_image_ids_test.go +++ b/internal/server/category_image_ids_test.go @@ -45,16 +45,22 @@ func TestGroupNodeIncludesBrandProductsOnlyForLeafGroups(t *testing.T) { Level: 1, IsLeaf: true, BrandProducts: []repository.GroupBrandProductRecord{{ - BrandID: "3001", - BrandName: "Brand", - ProductIDs: []string{"2002", "2001"}, + BrandID: "3001", + BrandName: "Brand", + 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}) diff --git a/internal/server/product_server.go b/internal/server/product_server.go index b3a383f..42bcfec 100644 --- a/internal/server/product_server.go +++ b/internal/server/product_server.go @@ -3234,10 +3234,17 @@ func toProtoGroupNode(group *repository.CategoryRecord) *productv1.GroupNode { node.Brands = make([]*productv1.GroupBrandProductSummary, 0, len(group.BrandProducts)) for _, brand := range group.BrandProducts { node.Brands = append(node.Brands, &productv1.GroupBrandProductSummary{ - BrandId: brand.BrandID, - BrandName: brand.BrandName, - ProductIds: append([]string{}, brand.ProductIDs...), + BrandId: brand.BrandID, + BrandName: brand.BrandName, }) + 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