feat: 新增店铺联级查询接口
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m3s

- 新增 GET /api/admin/shops/cascade 接口,支持按店铺名模糊查询和上级ID过滤
- Store 层新增 ListForCascade(支持数据权限过滤)和 GetParentIDsWithChildren 方法
- Service 层新增 ListCascade,批量判断 has_children 避免 N+1 查询
- 返回格式:[{id, shop_name, has_children}]

💘 Generated with Crush

Assisted-by: Claude Sonnet 4.6 via Crush <crush@charm.land>
This commit is contained in:
2026-04-09 11:43:37 +08:00
parent 384a54164b
commit 81ba84cf05
6 changed files with 201 additions and 0 deletions

View File

@@ -228,3 +228,40 @@ func (s *ShopStore) GetByIDs(ctx context.Context, ids []uint) ([]*model.Shop, er
}
return shops, nil
}
// ListForCascade 联级查询店铺支持按名称模糊查询和按上级ID过滤
func (s *ShopStore) ListForCascade(ctx context.Context, shopName string, parentID *uint) ([]*model.Shop, error) {
query := s.db.WithContext(ctx).Model(&model.Shop{})
query = middleware.ApplyShopIDFilter(ctx, query)
if parentID != nil {
query = query.Where("parent_id = ?", *parentID)
} else {
query = query.Where("parent_id IS NULL")
}
if shopName != "" {
query = query.Where("shop_name LIKE ?", "%"+shopName+"%")
}
var shops []*model.Shop
if err := query.Order("created_at DESC").Find(&shops).Error; err != nil {
return nil, err
}
return shops, nil
}
// GetParentIDsWithChildren 查询指定 ID 列表中哪些店铺有下级返回作为上级的店铺ID集合
func (s *ShopStore) GetParentIDsWithChildren(ctx context.Context, ids []uint) ([]uint, error) {
if len(ids) == 0 {
return []uint{}, nil
}
var parentIDs []uint
if err := s.db.WithContext(ctx).Model(&model.Shop{}).
Distinct("parent_id").
Where("parent_id IN ?", ids).
Pluck("parent_id", &parentIDs).Error; err != nil {
return nil, err
}
return parentIDs, nil
}