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
|
SortNum int32
|
||||||
Status int32
|
Status int32
|
||||||
Version int32
|
Version int32
|
||||||
|
// ProductCount is populated when listing product groups.
|
||||||
|
ProductCount int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// CategoryFieldUpdate selects mutable fields of one category node.
|
// CategoryFieldUpdate selects mutable fields of one category node.
|
||||||
@ -2674,20 +2676,26 @@ func (r *ProductRepository) ListGroupNodes(ctx context.Context, onlyVisible bool
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
rows, err := r.db.QueryContext(ctx, `
|
rows, err := r.db.QueryContext(ctx, `
|
||||||
SELECT id,
|
SELECT c.id,
|
||||||
node_type,
|
c.node_type,
|
||||||
name,
|
c.name,
|
||||||
parent_id,
|
c.parent_id,
|
||||||
level,
|
c.level,
|
||||||
COALESCE(to_json(image_ids)::text, '[]'),
|
COALESCE(to_json(c.image_ids)::text, '[]'),
|
||||||
sort_num,
|
c.sort_num,
|
||||||
status,
|
c.status,
|
||||||
version
|
c.version,
|
||||||
FROM product.product_category
|
COUNT(p.id) AS product_count
|
||||||
WHERE node_type = 2
|
FROM product.product_category AS c
|
||||||
AND store_id = $2
|
LEFT JOIN product.product_category_bindings AS b ON b.category_id = c.id
|
||||||
AND ($1 = false OR status = 1)
|
LEFT JOIN product.products AS p ON p.id = b.product_id
|
||||||
ORDER BY sort_num ASC, id ASC
|
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)
|
`, onlyVisible, scope.StoreID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("query group nodes: %w", err)
|
return nil, fmt.Errorf("query group nodes: %w", err)
|
||||||
@ -2696,7 +2704,7 @@ ORDER BY sort_num ASC, id ASC
|
|||||||
|
|
||||||
groups := make([]*CategoryRecord, 0)
|
groups := make([]*CategoryRecord, 0)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
group, err := scanCategory(rows)
|
group, err := scanGroup(rows)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -3450,6 +3458,37 @@ func scanInventory(scanner rowScanner) (*InventoryRecord, error) {
|
|||||||
return &rec, nil
|
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) {
|
func scanCategory(scanner rowScanner) (*CategoryRecord, error) {
|
||||||
var (
|
var (
|
||||||
rec CategoryRecord
|
rec CategoryRecord
|
||||||
|
|||||||
@ -2163,14 +2163,15 @@ func toProtoGroupNode(group *repository.CategoryRecord) *productv1.GroupNode {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &productv1.GroupNode{
|
return &productv1.GroupNode{
|
||||||
GroupId: group.ID,
|
GroupId: group.ID,
|
||||||
NodeType: group.NodeType,
|
NodeType: group.NodeType,
|
||||||
Name: group.Name,
|
Name: group.Name,
|
||||||
ParentId: group.ParentID,
|
ParentId: group.ParentID,
|
||||||
Level: group.Level,
|
Level: group.Level,
|
||||||
ImageJson: categoryImageIDsJSON(group.ImageIDs),
|
ImageJson: categoryImageIDsJSON(group.ImageIDs),
|
||||||
SortNum: group.SortNum,
|
SortNum: group.SortNum,
|
||||||
Status: group.Status,
|
Status: group.Status,
|
||||||
|
ProductCount: group.ProductCount,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user