mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: enhance GroupListFilter and CategoryRecord with GroupID and timestamps
This commit is contained in:
parent
16b2703cba
commit
ac32394959
@ -379,18 +379,21 @@ type ProductListFilter struct {
|
||||
|
||||
// CategoryRecord is the persistence model for product.catalog_category.
|
||||
type CategoryRecord struct {
|
||||
ID string
|
||||
NodeType int32
|
||||
Name string
|
||||
ParentID string
|
||||
Level int32
|
||||
ImageIDs []string
|
||||
SortNum int32
|
||||
Status int32
|
||||
CampaignID string
|
||||
Version int32
|
||||
ID string
|
||||
NodeType int32
|
||||
Name string
|
||||
ParentID string
|
||||
Level int32
|
||||
ImageIDs []string
|
||||
SortNum int32
|
||||
Status int32
|
||||
CampaignID string
|
||||
Version int32
|
||||
UpdatedAt string
|
||||
LatestUpdatedAt string
|
||||
// ProductCount is populated when listing product groups.
|
||||
ProductCount int64
|
||||
ProductCount int64
|
||||
TotalProductCount int64
|
||||
// IsLeaf and BrandProducts are populated when listing product groups.
|
||||
IsLeaf bool
|
||||
BrandProducts []GroupBrandProductRecord
|
||||
@ -477,6 +480,7 @@ type GroupListFilter struct {
|
||||
FirstLevelName string
|
||||
SecondLevelName string
|
||||
ExactMatch bool
|
||||
GroupID string
|
||||
}
|
||||
|
||||
// CategoryFieldUpdate selects mutable fields of one category node.
|
||||
@ -4411,33 +4415,50 @@ func (r *ProductRepository) ListGroupNodes(ctx context.Context, filter GroupList
|
||||
if pageSize < 1 {
|
||||
pageSize = 20
|
||||
}
|
||||
var parentID int64
|
||||
if filter.GroupID != "" {
|
||||
var err error
|
||||
parentID, err = parseID(filter.GroupID, "group_id")
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var nodeType, level int32
|
||||
err = r.db.QueryRowContext(ctx, `SELECT node_type, level FROM product.catalog_category WHERE id = $1 AND store_id = $2 AND deleted_at IS NULL`, parentID, scope.StoreID).Scan(&nodeType, &level)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, 0, ErrCategoryNodeNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("check group parent: %w", err)
|
||||
}
|
||||
if nodeType != 2 || level != 1 {
|
||||
return nil, 0, ErrInvalidProductGroup
|
||||
}
|
||||
}
|
||||
firstLevelName := strings.TrimSpace(filter.FirstLevelName)
|
||||
secondLevelName := strings.TrimSpace(filter.SecondLevelName)
|
||||
if !filter.ExactMatch {
|
||||
firstLevelName = escapeLikeLiteral(firstLevelName)
|
||||
secondLevelName = escapeLikeLiteral(secondLevelName)
|
||||
}
|
||||
var total int64
|
||||
if err := r.db.QueryRowContext(ctx, `
|
||||
SELECT COUNT(*)
|
||||
const fromWhere = `
|
||||
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
|
||||
AND parent.node_type = 2
|
||||
AND parent.deleted_at IS NULL
|
||||
WHERE c.node_type = 2
|
||||
AND c.store_id = $1
|
||||
AND c.deleted_at IS NULL
|
||||
WHERE c.node_type = 2 AND c.store_id = $1 AND c.deleted_at IS NULL
|
||||
AND ($2 = false OR c.status = 1)
|
||||
AND ($3 = '' OR CASE
|
||||
WHEN $5 THEN LOWER(CASE WHEN c.level = 1 THEN c.name ELSE COALESCE(parent.name, '') END) = LOWER($3)
|
||||
ELSE CASE WHEN c.level = 1 THEN c.name ELSE COALESCE(parent.name, '') END ILIKE ('%' || $3 || '%') ESCAPE '\'
|
||||
END)
|
||||
AND ($4 = '' OR (c.level = 2 AND CASE
|
||||
WHEN $5 THEN LOWER(c.name) = LOWER($4)
|
||||
ELSE c.name ILIKE ('%' || $4 || '%') ESCAPE '\'
|
||||
END))
|
||||
`, scope.StoreID, filter.OnlyVisible, firstLevelName, secondLevelName, filter.ExactMatch).Scan(&total); err != nil {
|
||||
AND (($6 = 0 AND c.level = 1 AND c.parent_id IS NULL
|
||||
AND ($3 = '' OR CASE WHEN $5 THEN LOWER(c.name) = LOWER($3)
|
||||
ELSE c.name ILIKE ('%' || $3 || '%') ESCAPE '\' END)
|
||||
AND ($4 = '' OR EXISTS (
|
||||
SELECT 1 FROM product.catalog_category AS match_child
|
||||
WHERE match_child.parent_id = c.id AND match_child.store_id = c.store_id
|
||||
AND match_child.node_type = 2 AND match_child.level = 2
|
||||
AND match_child.deleted_at IS NULL AND ($2 = false OR match_child.status = 1)
|
||||
AND (CASE WHEN $5 THEN LOWER(match_child.name) = LOWER($4)
|
||||
ELSE match_child.name ILIKE ('%' || $4 || '%') ESCAPE '\' END)
|
||||
)))
|
||||
OR ($6 <> 0 AND c.parent_id = $6 AND c.level = 2))`
|
||||
args := []any{scope.StoreID, filter.OnlyVisible, firstLevelName, secondLevelName, filter.ExactMatch, parentID}
|
||||
var total int64
|
||||
if err := r.db.QueryRowContext(ctx, `SELECT COUNT(*) `+fromWhere, args...).Scan(&total); err != nil {
|
||||
return nil, 0, fmt.Errorf("count group nodes: %w", err)
|
||||
}
|
||||
if total == 0 {
|
||||
@ -4445,7 +4466,7 @@ WHERE c.node_type = 2
|
||||
}
|
||||
|
||||
offset := int64(page-1) * int64(pageSize)
|
||||
const query = `
|
||||
query := `
|
||||
SELECT c.id,
|
||||
c.node_type,
|
||||
c.name,
|
||||
@ -4464,33 +4485,56 @@ SELECT c.id,
|
||||
AND child.store_id = c.store_id
|
||||
AND child.node_type = 2
|
||||
AND child.deleted_at IS NULL
|
||||
) AS is_leaf
|
||||
AND ($2 = false OR child.status = 1)
|
||||
) AS is_leaf,
|
||||
to_char(c.updated_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS updated_at,
|
||||
to_char(GREATEST(c.updated_at, COALESCE((
|
||||
SELECT MAX(child.updated_at) FROM product.catalog_category AS child
|
||||
WHERE child.parent_id = c.id AND child.store_id = c.store_id
|
||||
AND child.node_type = 2 AND child.level = 2 AND child.deleted_at IS NULL
|
||||
AND ($2 = false OR child.status = 1)
|
||||
), c.updated_at)) AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS latest_updated_at,
|
||||
COUNT(p.id) + CASE WHEN c.level = 1 THEN (
|
||||
SELECT COUNT(child_product.id)
|
||||
FROM product.catalog_category AS child
|
||||
JOIN product.product_catalog_bindings AS child_binding ON child_binding.category_id = child.id AND child_binding.deleted_at IS NULL
|
||||
JOIN product.products AS child_product ON child_product.id = child_binding.product_id
|
||||
AND child_product.store_id = c.store_id AND child_product.deleted_at IS NULL
|
||||
WHERE child.parent_id = c.id AND child.store_id = c.store_id
|
||||
AND child.node_type = 2 AND child.level = 2 AND child.deleted_at IS NULL
|
||||
AND ($2 = false OR child.status = 1)
|
||||
) ELSE 0 END AS total_product_count
|
||||
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
|
||||
AND parent.node_type = 2
|
||||
AND parent.deleted_at IS NULL
|
||||
LEFT JOIN product.product_catalog_bindings AS b ON b.category_id = c.id AND b.deleted_at IS NULL
|
||||
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 = $1
|
||||
AND c.deleted_at IS NULL
|
||||
WHERE c.node_type = 2 AND c.store_id = $1 AND c.deleted_at IS NULL
|
||||
AND ($2 = false OR c.status = 1)
|
||||
AND ($3 = '' OR CASE
|
||||
WHEN $5 THEN LOWER(CASE WHEN c.level = 1 THEN c.name ELSE COALESCE(parent.name, '') END) = LOWER($3)
|
||||
ELSE CASE WHEN c.level = 1 THEN c.name ELSE COALESCE(parent.name, '') END ILIKE ('%' || $3 || '%') ESCAPE '\'
|
||||
END)
|
||||
AND ($4 = '' OR (c.level = 2 AND CASE
|
||||
WHEN $5 THEN LOWER(c.name) = LOWER($4)
|
||||
ELSE c.name ILIKE ('%' || $4 || '%') ESCAPE '\'
|
||||
END))
|
||||
AND (($6 = 0 AND c.level = 1 AND c.parent_id IS NULL
|
||||
AND ($3 = '' OR CASE WHEN $5 THEN LOWER(c.name) = LOWER($3)
|
||||
ELSE c.name ILIKE ('%' || $3 || '%') ESCAPE '\' END)
|
||||
AND ($4 = '' OR EXISTS (
|
||||
SELECT 1 FROM product.catalog_category AS match_child
|
||||
WHERE match_child.parent_id = c.id AND match_child.store_id = c.store_id
|
||||
AND match_child.node_type = 2 AND match_child.level = 2
|
||||
AND match_child.deleted_at IS NULL AND ($2 = false OR match_child.status = 1)
|
||||
AND (CASE WHEN $5 THEN LOWER(match_child.name) = LOWER($4)
|
||||
ELSE match_child.name ILIKE ('%' || $4 || '%') ESCAPE '\' END)
|
||||
)))
|
||||
OR ($6 <> 0 AND c.parent_id = $6 AND c.level = 2))
|
||||
GROUP BY c.id
|
||||
ORDER BY c.created_at DESC, c.id DESC
|
||||
LIMIT $6 OFFSET $7
|
||||
ORDER BY CASE WHEN $6 = 0 THEN GREATEST(c.updated_at, COALESCE((
|
||||
SELECT MAX(child.updated_at) FROM product.catalog_category AS child
|
||||
WHERE child.parent_id = c.id AND child.store_id = c.store_id
|
||||
AND child.node_type = 2 AND child.level = 2 AND child.deleted_at IS NULL
|
||||
AND ($2 = false OR child.status = 1)
|
||||
), c.updated_at)) ELSE c.updated_at END DESC, c.id DESC
|
||||
`
|
||||
args := []any{scope.StoreID, filter.OnlyVisible, firstLevelName, secondLevelName, filter.ExactMatch, pageSize, offset}
|
||||
if parentID == 0 {
|
||||
query += "LIMIT $7 OFFSET $8"
|
||||
args = append(args, pageSize, offset)
|
||||
}
|
||||
rows, err := r.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, 0, &GroupListQueryError{Query: query, Args: args, Err: err}
|
||||
@ -4561,14 +4605,6 @@ WHERE c.id IN (`+placeholders+`)
|
||||
AND c.node_type = 2
|
||||
AND c.store_id = $2
|
||||
AND c.deleted_at IS NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM product.catalog_category child
|
||||
WHERE child.parent_id = c.id
|
||||
AND child.store_id = $2
|
||||
AND child.node_type = 2
|
||||
AND child.deleted_at IS NULL
|
||||
)
|
||||
ORDER BY c.id ASC,
|
||||
CASE WHEN p.brand_id IS NULL THEN 1 ELSE 0 END ASC,
|
||||
LOWER(CASE
|
||||
@ -5492,7 +5528,7 @@ func scanGroup(scanner rowScanner) (*CategoryRecord, error) {
|
||||
isLeaf bool
|
||||
)
|
||||
if err := scanner.Scan(
|
||||
&id, &nodeType, &rec.Name, &parentID, &level, &imageIDsJSON, &rec.SortNum, &status, &campaignID, &rec.Version, &rec.ProductCount, &isLeaf,
|
||||
&id, &nodeType, &rec.Name, &parentID, &level, &imageIDsJSON, &rec.SortNum, &status, &campaignID, &rec.Version, &rec.ProductCount, &isLeaf, &rec.UpdatedAt, &rec.LatestUpdatedAt, &rec.TotalProductCount,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("scan group: %w", err)
|
||||
}
|
||||
|
||||
@ -1487,6 +1487,7 @@ func (s *ProductServer) ListGroups(ctx context.Context, req *productv1.ListGroup
|
||||
filter.FirstLevelName = strings.TrimSpace(req.GetFirstLevelName())
|
||||
filter.SecondLevelName = strings.TrimSpace(req.GetSecondLevelName())
|
||||
filter.ExactMatch = req.GetExactMatch()
|
||||
filter.GroupID = strings.TrimSpace(req.GetGroupId())
|
||||
}
|
||||
filter.OnlyVisible = onlyVisible
|
||||
|
||||
@ -1496,8 +1497,19 @@ func (s *ProductServer) ListGroups(ctx context.Context, req *productv1.ListGroup
|
||||
defer dbSpan.End()
|
||||
|
||||
page, pageSize := normalizedPage(req.GetPage(), req.GetPageSize())
|
||||
if filter.GroupID != "" {
|
||||
if _, err := normalizeCategoryID(filter.GroupID, "group_id"); err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
}
|
||||
groups, total, err := s.repo.ListGroupNodes(dbCtx, filter, page, pageSize, scope)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrCategoryNodeNotFound) {
|
||||
return nil, status.Error(codes.NotFound, "group_id not found")
|
||||
}
|
||||
if errors.Is(err, repository.ErrInvalidProductGroup) {
|
||||
return nil, status.Error(codes.InvalidArgument, "group_id must identify a first-level group")
|
||||
}
|
||||
dbSpan.RecordError(err)
|
||||
dbSpan.SetStatus(otelcodes.Error, err.Error())
|
||||
fields := []zap.Field{
|
||||
@ -1541,6 +1553,10 @@ func (s *ProductServer) ListGroups(ctx context.Context, req *productv1.ListGroup
|
||||
protoGroups = append(protoGroups, toProtoGroupNode(group))
|
||||
}
|
||||
|
||||
if filter.GroupID != "" {
|
||||
page = 1
|
||||
pageSize = int32(total)
|
||||
}
|
||||
return &productv1.ListGroupsResponse{
|
||||
Groups: protoGroups,
|
||||
Page: page,
|
||||
@ -3273,17 +3289,20 @@ func toProtoGroupNode(group *repository.CategoryRecord) *productv1.GroupNode {
|
||||
return nil
|
||||
}
|
||||
node := &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,
|
||||
ProductCount: group.ProductCount,
|
||||
IsLeaf: group.IsLeaf,
|
||||
Version: group.Version,
|
||||
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,
|
||||
IsLeaf: group.IsLeaf,
|
||||
Version: group.Version,
|
||||
UpdatedAt: group.UpdatedAt,
|
||||
LatestUpdatedAt: group.LatestUpdatedAt,
|
||||
TotalProductCount: group.TotalProductCount,
|
||||
}
|
||||
if group.IsLeaf {
|
||||
node.Brands = make([]*productv1.GroupBrandProductSummary, 0, len(group.BrandProducts))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user