mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: add ProductCount to CategoryRecord and update group node serialization
This commit is contained in:
parent
d1b8aa2996
commit
0b848372f3
@ -264,6 +264,8 @@ type CategoryRecord struct {
|
||||
SortNum int32
|
||||
Status int32
|
||||
Version int32
|
||||
// ProductCount is populated when listing product groups.
|
||||
ProductCount int64
|
||||
}
|
||||
|
||||
// CategoryFieldUpdate selects mutable fields of one category node.
|
||||
@ -2674,20 +2676,26 @@ func (r *ProductRepository) ListGroupNodes(ctx context.Context, onlyVisible bool
|
||||
return nil, err
|
||||
}
|
||||
rows, err := r.db.QueryContext(ctx, `
|
||||
SELECT id,
|
||||
node_type,
|
||||
name,
|
||||
parent_id,
|
||||
level,
|
||||
COALESCE(to_json(image_ids)::text, '[]'),
|
||||
sort_num,
|
||||
status,
|
||||
version
|
||||
FROM product.product_category
|
||||
WHERE node_type = 2
|
||||
AND store_id = $2
|
||||
AND ($1 = false OR status = 1)
|
||||
ORDER BY sort_num ASC, id ASC
|
||||
SELECT c.id,
|
||||
c.node_type,
|
||||
c.name,
|
||||
c.parent_id,
|
||||
c.level,
|
||||
COALESCE(to_json(c.image_ids)::text, '[]'),
|
||||
c.sort_num,
|
||||
c.status,
|
||||
c.version,
|
||||
COUNT(p.id) AS product_count
|
||||
FROM product.product_category AS c
|
||||
LEFT JOIN product.product_category_bindings AS b ON b.category_id = c.id
|
||||
LEFT JOIN product.products AS p ON p.id = b.product_id
|
||||
AND p.store_id = c.store_id
|
||||
AND p.deleted_at IS NULL
|
||||
WHERE c.node_type = 2
|
||||
AND c.store_id = $2
|
||||
AND ($1 = false OR c.status = 1)
|
||||
GROUP BY c.id
|
||||
ORDER BY c.sort_num ASC, c.id ASC
|
||||
`, onlyVisible, scope.StoreID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query group nodes: %w", err)
|
||||
@ -2696,7 +2704,7 @@ ORDER BY sort_num ASC, id ASC
|
||||
|
||||
groups := make([]*CategoryRecord, 0)
|
||||
for rows.Next() {
|
||||
group, err := scanCategory(rows)
|
||||
group, err := scanGroup(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -3450,6 +3458,37 @@ func scanInventory(scanner rowScanner) (*InventoryRecord, error) {
|
||||
return &rec, nil
|
||||
}
|
||||
|
||||
func scanGroup(scanner rowScanner) (*CategoryRecord, error) {
|
||||
var (
|
||||
rec CategoryRecord
|
||||
id int64
|
||||
parentID sql.NullInt64
|
||||
imageIDsJSON []byte
|
||||
nodeType int16
|
||||
level int16
|
||||
status int16
|
||||
)
|
||||
if err := scanner.Scan(
|
||||
&id, &nodeType, &rec.Name, &parentID, &level, &imageIDsJSON, &rec.SortNum, &status, &rec.Version, &rec.ProductCount,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("scan group: %w", err)
|
||||
}
|
||||
|
||||
rec.ID = formatID(id)
|
||||
rec.NodeType = int32(nodeType)
|
||||
if parentID.Valid {
|
||||
rec.ParentID = formatID(parentID.Int64)
|
||||
}
|
||||
rec.Level = int32(level)
|
||||
imageIDs, err := parseStringArrayJSON(string(imageIDsJSON))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unmarshal group image_ids: %w", err)
|
||||
}
|
||||
rec.ImageIDs = imageIDs
|
||||
rec.Status = int32(status)
|
||||
return &rec, nil
|
||||
}
|
||||
|
||||
func scanCategory(scanner rowScanner) (*CategoryRecord, error) {
|
||||
var (
|
||||
rec CategoryRecord
|
||||
|
||||
@ -2163,14 +2163,15 @@ func toProtoGroupNode(group *repository.CategoryRecord) *productv1.GroupNode {
|
||||
return nil
|
||||
}
|
||||
return &productv1.GroupNode{
|
||||
GroupId: group.ID,
|
||||
NodeType: group.NodeType,
|
||||
Name: group.Name,
|
||||
ParentId: group.ParentID,
|
||||
Level: group.Level,
|
||||
ImageJson: categoryImageIDsJSON(group.ImageIDs),
|
||||
SortNum: group.SortNum,
|
||||
Status: group.Status,
|
||||
GroupId: group.ID,
|
||||
NodeType: group.NodeType,
|
||||
Name: group.Name,
|
||||
ParentId: group.ParentID,
|
||||
Level: group.Level,
|
||||
ImageJson: categoryImageIDsJSON(group.ImageIDs),
|
||||
SortNum: group.SortNum,
|
||||
Status: group.Status,
|
||||
ProductCount: group.ProductCount,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user