mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: enhance product category listing with hierarchical structure and optimized queries
This commit is contained in:
parent
cb81c23830
commit
e5d0075f37
@ -119,30 +119,82 @@ func createProductCategoryNode(ctx context.Context, tx *sql.Tx, input *ProductCa
|
||||
|
||||
func (r *ProductRepository) ListProductCategories(ctx context.Context, id string) ([]*ProductCategoryRecord, error) {
|
||||
if id != "" {
|
||||
node, err := r.GetProductCategory(ctx, id)
|
||||
node, err := r.loadProductCategoryOneLevel(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []*ProductCategoryRecord{node}, nil
|
||||
}
|
||||
rows, err := r.db.QueryContext(ctx, `SELECT id FROM product.product_category WHERE parent_id IS NULL AND deleted_at IS NULL ORDER BY priority,id`)
|
||||
rows, err := r.db.QueryContext(ctx, `SELECT `+productCategoryColumns+` FROM product.product_category c WHERE c.parent_id IS NULL AND c.deleted_at IS NULL ORDER BY c.priority,c.id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []*ProductCategoryRecord
|
||||
out := make([]*ProductCategoryRecord, 0)
|
||||
byID := make(map[string]*ProductCategoryRecord)
|
||||
for rows.Next() {
|
||||
var raw int64
|
||||
if err := rows.Scan(&raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err := r.GetProductCategory(ctx, formatID(raw))
|
||||
node, err := scanProductCategory(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, node)
|
||||
byID[node.ID] = node
|
||||
}
|
||||
return out, rows.Err()
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(byID) == 0 {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
children, err := r.db.QueryContext(ctx, `SELECT `+productCategoryColumns+` FROM product.product_category c JOIN product.product_category p ON p.id=c.parent_id WHERE c.deleted_at IS NULL AND p.parent_id IS NULL AND p.deleted_at IS NULL ORDER BY p.priority,p.id,c.priority,c.id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer children.Close()
|
||||
for children.Next() {
|
||||
child, err := scanProductCategory(children)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if parent := byID[child.ParentID]; parent != nil {
|
||||
parent.Children = append(parent.Children, child)
|
||||
}
|
||||
}
|
||||
if err := children.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (r *ProductRepository) loadProductCategoryOneLevel(ctx context.Context, id string) (*ProductCategoryRecord, error) {
|
||||
parsed, err := parseID(id, "product_category_id")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
root, err := scanProductCategory(r.db.QueryRowContext(ctx, `SELECT `+productCategoryColumns+` FROM product.product_category c WHERE c.id=$1 AND c.deleted_at IS NULL`, parsed))
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrProductCategoryNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := r.db.QueryContext(ctx, `SELECT `+productCategoryColumns+` FROM product.product_category c WHERE c.parent_id=$1 AND c.deleted_at IS NULL ORDER BY c.priority,c.id`, parsed)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
child, err := scanProductCategory(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
root.Children = append(root.Children, child)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return root, nil
|
||||
}
|
||||
|
||||
func (r *ProductRepository) GetProductCategory(ctx context.Context, id string) (*ProductCategoryRecord, error) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user