feat: add ProductCount to BrandRecord and update brand queries

This commit is contained in:
BXY 2026-10-27 18:08:15 +08:00
parent 075f72c2d9
commit 6f49fd3fc5
2 changed files with 9 additions and 4 deletions

View File

@ -51,6 +51,7 @@ type BrandRecord struct {
ID string ID string
Name, Alias, BrandCode, SupplierID string Name, Alias, BrandCode, SupplierID string
Status, Version int32 Status, Version int32
ProductCount int64
Scope StoreScope Scope StoreScope
CreatedAt, UpdatedAt time.Time CreatedAt, UpdatedAt time.Time
DeletedAt *time.Time DeletedAt *time.Time
@ -356,7 +357,9 @@ func (r *ProductRepository) DeleteBrand(ctx context.Context, id string, expected
} }
const supplierSelect = `SELECT id,name,alias,supplier_code,attachments,contact_name,contact_phone,remark,status,tenant_id,store_id,version,created_at,updated_at,deleted_at FROM product.suppliers` const supplierSelect = `SELECT id,name,alias,supplier_code,attachments,contact_name,contact_phone,remark,status,tenant_id,store_id,version,created_at,updated_at,deleted_at FROM product.suppliers`
const brandSelect = `SELECT id,name,alias,brand_code,supplier_id,status,tenant_id,store_id,version,created_at,updated_at,deleted_at FROM product.brands` const brandSelect = `SELECT b.id,b.name,b.alias,b.brand_code,b.supplier_id,b.status,b.tenant_id,b.store_id,b.version,b.created_at,b.updated_at,b.deleted_at,
COALESCE((SELECT COUNT(*) FROM product.products p WHERE p.brand_id=b.id AND p.tenant_id=b.tenant_id AND p.store_id=b.store_id AND p.deleted_at IS NULL), 0)
FROM product.brands b`
type scanner interface{ Scan(...any) error } type scanner interface{ Scan(...any) error }
@ -379,7 +382,7 @@ func scanSupplier(row scanner) (*SupplierRecord, error) {
func scanBrand(row scanner) (*BrandRecord, error) { func scanBrand(row scanner) (*BrandRecord, error) {
var value BrandRecord var value BrandRecord
var deleted sql.NullTime var deleted sql.NullTime
err := row.Scan(&value.ID, &value.Name, &value.Alias, &value.BrandCode, &value.SupplierID, &value.Status, &value.Scope.TenantID, &value.Scope.StoreID, &value.Version, &value.CreatedAt, &value.UpdatedAt, &deleted) err := row.Scan(&value.ID, &value.Name, &value.Alias, &value.BrandCode, &value.SupplierID, &value.Status, &value.Scope.TenantID, &value.Scope.StoreID, &value.Version, &value.CreatedAt, &value.UpdatedAt, &deleted, &value.ProductCount)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -419,7 +422,9 @@ func (r *ProductRepository) populateSupplierBrands(ctx context.Context, supplier
position := len(args) position := len(args)
where += fmt.Sprintf(" AND (b.name ILIKE $%d OR b.alias ILIKE $%d OR b.brand_code ILIKE $%d)", position, position, position) where += fmt.Sprintf(" AND (b.name ILIKE $%d OR b.alias ILIKE $%d OR b.brand_code ILIKE $%d)", position, position, position)
} }
rows, err := r.db.QueryContext(ctx, `SELECT b.id,b.name,b.alias,b.brand_code,b.supplier_id,b.status,b.tenant_id,b.store_id,b.version,b.created_at,b.updated_at,b.deleted_at FROM product.brands b`+where+` ORDER BY b.created_at DESC, b.id DESC`, args...) rows, err := r.db.QueryContext(ctx, `SELECT b.id,b.name,b.alias,b.brand_code,b.supplier_id,b.status,b.tenant_id,b.store_id,b.version,b.created_at,b.updated_at,b.deleted_at,
COALESCE((SELECT COUNT(*) FROM product.products p WHERE p.brand_id=b.id AND p.tenant_id=b.tenant_id AND p.store_id=b.store_id AND p.deleted_at IS NULL), 0)
FROM product.brands b`+where+` ORDER BY b.created_at DESC, b.id DESC`, args...)
if err != nil { if err != nil {
return fmt.Errorf("list supplier brands: %w", err) return fmt.Errorf("list supplier brands: %w", err)
} }

View File

@ -484,7 +484,7 @@ func toProtoBrand(value *repository.BrandRecord) *productv1.Brand {
if value == nil { if value == nil {
return nil return nil
} }
return &productv1.Brand{BrandId: value.ID, Name: value.Name, Alias: value.Alias, BrandCode: value.BrandCode, SupplierId: value.SupplierID, Status: value.Status, TenantId: fmt.Sprint(value.Scope.TenantID), StoreId: fmt.Sprint(value.Scope.StoreID), CreatedAt: timeToMillisString(value.CreatedAt), UpdatedAt: timeToMillisString(value.UpdatedAt), DeletedAt: timePtrToMillisString(value.DeletedAt), Version: value.Version} return &productv1.Brand{BrandId: value.ID, Name: value.Name, Alias: value.Alias, BrandCode: value.BrandCode, SupplierId: value.SupplierID, Status: value.Status, TenantId: fmt.Sprint(value.Scope.TenantID), StoreId: fmt.Sprint(value.Scope.StoreID), CreatedAt: timeToMillisString(value.CreatedAt), UpdatedAt: timeToMillisString(value.UpdatedAt), DeletedAt: timePtrToMillisString(value.DeletedAt), Version: value.Version, ProductCount: value.ProductCount}
} }
func catalogGRPCError(err error) error { func catalogGRPCError(err error) error {
switch { switch {