From 99e9b200d9932080ac1e8cecd85f8e73e475acd3 Mon Sep 17 00:00:00 2001 From: BXY <103352887+BXY0414@users.noreply.github.com> Date: Wed, 28 Oct 2026 10:31:23 +0800 Subject: [PATCH] feat: add ProductIDs to CategoryRecord and implement handling for second-level groups --- internal/repository/product_repository.go | 33 +++++++++++++++------- internal/server/category_image_ids_test.go | 25 ++++++++++++++++ internal/server/product_server.go | 6 +++- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/internal/repository/product_repository.go b/internal/repository/product_repository.go index 07a0f25..a609f95 100644 --- a/internal/repository/product_repository.go +++ b/internal/repository/product_repository.go @@ -388,6 +388,8 @@ type CategoryRecord struct { Version int32 // ProductCount is populated when listing product groups. ProductCount int64 + // ProductIDs is populated for second-level product groups when listing groups. + ProductIDs []string // Children is populated for catalog-category tree operations. Children []CategoryRecord } @@ -4295,7 +4297,12 @@ SELECT c.id, c.status, c.campaign_id, c.version, - COUNT(p.id) AS product_count + COUNT(p.id) AS product_count, + COALESCE( + json_agg(p.id::text ORDER BY p.created_at DESC, p.id DESC) + FILTER (WHERE c.level = 2 AND p.id IS NOT NULL), + '[]'::json + )::text AS product_ids FROM product.catalog_category AS c LEFT JOIN product.catalog_category AS parent ON parent.id = c.parent_id AND parent.store_id = c.store_id @@ -5188,17 +5195,18 @@ func scanInventory(scanner rowScanner) (*InventoryRecord, error) { func scanGroup(scanner rowScanner) (*CategoryRecord, error) { var ( - rec CategoryRecord - id int64 - parentID sql.NullInt64 - imageIDsJSON []byte - nodeType int16 - level int16 - status int16 - campaignID sql.NullInt64 + rec CategoryRecord + id int64 + parentID sql.NullInt64 + imageIDsJSON []byte + nodeType int16 + level int16 + status int16 + campaignID sql.NullInt64 + productIDsJSON []byte ) if err := scanner.Scan( - &id, &nodeType, &rec.Name, &parentID, &level, &imageIDsJSON, &rec.SortNum, &status, &campaignID, &rec.Version, &rec.ProductCount, + &id, &nodeType, &rec.Name, &parentID, &level, &imageIDsJSON, &rec.SortNum, &status, &campaignID, &rec.Version, &rec.ProductCount, &productIDsJSON, ); err != nil { return nil, fmt.Errorf("scan group: %w", err) } @@ -5214,6 +5222,11 @@ func scanGroup(scanner rowScanner) (*CategoryRecord, error) { return nil, fmt.Errorf("unmarshal group image_ids: %w", err) } rec.ImageIDs = imageIDs + productIDs, err := parseStringArrayJSON(string(productIDsJSON)) + if err != nil { + return nil, fmt.Errorf("unmarshal group product_ids: %w", err) + } + rec.ProductIDs = productIDs rec.Status = int32(status) if campaignID.Valid { rec.CampaignID = formatID(campaignID.Int64) diff --git a/internal/server/category_image_ids_test.go b/internal/server/category_image_ids_test.go index e148ee9..b742132 100644 --- a/internal/server/category_image_ids_test.go +++ b/internal/server/category_image_ids_test.go @@ -29,3 +29,28 @@ func TestCategoryImageIDsRoundTripThroughExistingProtoField(t *testing.T) { t.Fatal("expected non-array image_json to be rejected") } } + +func TestGroupNodeIncludesProductIDsOnlyForSecondLevelGroups(t *testing.T) { + parent := toProtoGroupNode(&repository.CategoryRecord{ + ID: "1001", + Level: 1, + ProductIDs: []string{"legacy-binding"}, + }) + if parent.GetProductIds() != nil { + t.Fatalf("parent product_ids = %#v, want omitted", parent.GetProductIds()) + } + + leaf := toProtoGroupNode(&repository.CategoryRecord{ + ID: "1002", + Level: 2, + ProductIDs: []string{"2002", "2001"}, + }) + if got := leaf.GetProductIds(); len(got) != 2 || got[0] != "2002" || got[1] != "2001" { + t.Fatalf("leaf product_ids = %#v", got) + } + + emptyLeaf := toProtoGroupNode(&repository.CategoryRecord{ID: "1003", Level: 2}) + if got := emptyLeaf.GetProductIds(); got == nil || len(got) != 0 { + t.Fatalf("empty leaf product_ids = %#v, want []", got) + } +} diff --git a/internal/server/product_server.go b/internal/server/product_server.go index 6c3f889..4eacd9e 100644 --- a/internal/server/product_server.go +++ b/internal/server/product_server.go @@ -3207,7 +3207,7 @@ func toProtoGroupNode(group *repository.CategoryRecord) *productv1.GroupNode { if group == nil { return nil } - return &productv1.GroupNode{ + node := &productv1.GroupNode{ GroupId: group.ID, NodeType: group.NodeType, Name: group.Name, @@ -3219,6 +3219,10 @@ func toProtoGroupNode(group *repository.CategoryRecord) *productv1.GroupNode { ProductCount: group.ProductCount, Version: group.Version, } + if group.Level == 2 { + node.ProductIds = append([]string{}, group.ProductIDs...) + } + return node } func categoryImageIDsJSON(imageIDs []string) string {