feat: add ProductIDs to CategoryRecord and implement handling for second-level groups

This commit is contained in:
BXY 2026-10-28 10:31:23 +08:00
parent 9e1dbdf583
commit 99e9b200d9
3 changed files with 53 additions and 11 deletions

View File

@ -388,6 +388,8 @@ type CategoryRecord struct {
Version int32 Version int32
// ProductCount is populated when listing product groups. // ProductCount is populated when listing product groups.
ProductCount int64 ProductCount int64
// ProductIDs is populated for second-level product groups when listing groups.
ProductIDs []string
// Children is populated for catalog-category tree operations. // Children is populated for catalog-category tree operations.
Children []CategoryRecord Children []CategoryRecord
} }
@ -4295,7 +4297,12 @@ SELECT c.id,
c.status, c.status,
c.campaign_id, c.campaign_id,
c.version, 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 FROM product.catalog_category AS c
LEFT JOIN product.catalog_category AS parent ON parent.id = c.parent_id LEFT JOIN product.catalog_category AS parent ON parent.id = c.parent_id
AND parent.store_id = c.store_id AND parent.store_id = c.store_id
@ -5188,17 +5195,18 @@ func scanInventory(scanner rowScanner) (*InventoryRecord, error) {
func scanGroup(scanner rowScanner) (*CategoryRecord, error) { func scanGroup(scanner rowScanner) (*CategoryRecord, error) {
var ( var (
rec CategoryRecord rec CategoryRecord
id int64 id int64
parentID sql.NullInt64 parentID sql.NullInt64
imageIDsJSON []byte imageIDsJSON []byte
nodeType int16 nodeType int16
level int16 level int16
status int16 status int16
campaignID sql.NullInt64 campaignID sql.NullInt64
productIDsJSON []byte
) )
if err := scanner.Scan( 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 { ); err != nil {
return nil, fmt.Errorf("scan group: %w", err) 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) return nil, fmt.Errorf("unmarshal group image_ids: %w", err)
} }
rec.ImageIDs = imageIDs 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) rec.Status = int32(status)
if campaignID.Valid { if campaignID.Valid {
rec.CampaignID = formatID(campaignID.Int64) rec.CampaignID = formatID(campaignID.Int64)

View File

@ -29,3 +29,28 @@ func TestCategoryImageIDsRoundTripThroughExistingProtoField(t *testing.T) {
t.Fatal("expected non-array image_json to be rejected") 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)
}
}

View File

@ -3207,7 +3207,7 @@ func toProtoGroupNode(group *repository.CategoryRecord) *productv1.GroupNode {
if group == nil { if group == nil {
return nil return nil
} }
return &productv1.GroupNode{ node := &productv1.GroupNode{
GroupId: group.ID, GroupId: group.ID,
NodeType: group.NodeType, NodeType: group.NodeType,
Name: group.Name, Name: group.Name,
@ -3219,6 +3219,10 @@ func toProtoGroupNode(group *repository.CategoryRecord) *productv1.GroupNode {
ProductCount: group.ProductCount, ProductCount: group.ProductCount,
Version: group.Version, Version: group.Version,
} }
if group.Level == 2 {
node.ProductIds = append([]string{}, group.ProductIDs...)
}
return node
} }
func categoryImageIDsJSON(imageIDs []string) string { func categoryImageIDsJSON(imageIDs []string) string {