From c48f7221377b3f74e871d141a289377cf2908468 Mon Sep 17 00:00:00 2001 From: BXY <103352887+BXY0414@users.noreply.github.com> Date: Mon, 26 Oct 2026 18:59:50 +0800 Subject: [PATCH] feat: add brands handling to supplier records and update proto conversion --- .../repository/catalog_master_repository.go | 72 ++++++++++++++++++- internal/server/catalog_master_server.go | 3 + 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/internal/repository/catalog_master_repository.go b/internal/repository/catalog_master_repository.go index e6af56e..f2640e7 100644 --- a/internal/repository/catalog_master_repository.go +++ b/internal/repository/catalog_master_repository.go @@ -39,6 +39,7 @@ type SupplierRecord struct { ID string Name, Alias, SupplierCode string Attachments []SupplierAttachmentRecord + Brands []BrandRecord ContactName, ContactPhone, Remark string Status, Version int32 Scope StoreScope @@ -103,7 +104,14 @@ func (r *ProductRepository) GetSupplier(ctx context.Context, id string, scope St if errors.Is(err, sql.ErrNoRows) { return nil, ErrSupplierNotFound } - return supplier, err + if err != nil { + return nil, err + } + values := []SupplierRecord{*supplier} + if err := r.populateSupplierBrands(ctx, values, "", scope); err != nil { + return nil, err + } + return &values[0], nil } func (r *ProductRepository) ListSuppliers(ctx context.Context, filter SupplierListFilter, scope StoreScope) ([]SupplierRecord, int64, error) { @@ -120,7 +128,6 @@ func (r *ProductRepository) ListSuppliers(ctx context.Context, filter SupplierLi if err != nil { return nil, 0, fmt.Errorf("list suppliers: %w", err) } - defer rows.Close() out := make([]SupplierRecord, 0) for rows.Next() { value, err := scanSupplier(rows) @@ -129,7 +136,16 @@ func (r *ProductRepository) ListSuppliers(ctx context.Context, filter SupplierLi } out = append(out, *value) } - return out, total, rows.Err() + if err := rows.Err(); err != nil { + return nil, 0, err + } + if err := rows.Close(); err != nil { + return nil, 0, fmt.Errorf("close supplier rows: %w", err) + } + if err := r.populateSupplierBrands(ctx, out, filter.Query, scope); err != nil { + return nil, 0, err + } + return out, total, nil } func (r *ProductRepository) UpdateSupplier(ctx context.Context, id string, patch SupplierPatch, scope StoreScope) (*SupplierRecord, error) { @@ -372,6 +388,56 @@ func scanBrand(row scanner) (*BrandRecord, error) { } return &value, nil } + +// populateSupplierBrands batch-loads brands for the supplied suppliers. It is +// deliberately separate from the paginated supplier query: joining brands +// before pagination would duplicate suppliers and corrupt total/page results. +// A non-empty query narrows each nested brand list to matching brand fields. +func (r *ProductRepository) populateSupplierBrands(ctx context.Context, suppliers []SupplierRecord, query string, scope StoreScope) error { + if len(suppliers) == 0 { + return nil + } + + ids := make([]int64, 0, len(suppliers)) + byID := make(map[string]*SupplierRecord, len(suppliers)) + for i := range suppliers { + suppliers[i].Brands = []BrandRecord{} + id, err := parseID(suppliers[i].ID, "supplier_id") + if err != nil { + return err + } + ids = append(ids, id) + byID[suppliers[i].ID] = &suppliers[i] + } + + placeholders, idArgs := productIDPlaceholders(ids, 3) + args := append([]any{scope.TenantID, scope.StoreID}, idArgs...) + where := ` WHERE b.tenant_id=$1 AND b.store_id=$2 AND b.deleted_at IS NULL + AND b.supplier_id IN (` + placeholders + `)` + if keyword := strings.TrimSpace(query); keyword != "" { + args = append(args, "%"+keyword+"%") + 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) + } + 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...) + if err != nil { + return fmt.Errorf("list supplier brands: %w", err) + } + defer rows.Close() + for rows.Next() { + brand, err := scanBrand(rows) + if err != nil { + return fmt.Errorf("scan supplier brand: %w", err) + } + if supplier := byID[brand.SupplierID]; supplier != nil { + supplier.Brands = append(supplier.Brands, *brand) + } + } + if err := rows.Err(); err != nil { + return fmt.Errorf("iterate supplier brands: %w", err) + } + return nil +} func catalogWhere(scope StoreScope, query string, statusValue *int32, supplierID string, isBrand bool) (string, []any) { args := []any{scope.TenantID, scope.StoreID} where := ` WHERE tenant_id=$1 AND store_id=$2 AND deleted_at IS NULL` diff --git a/internal/server/catalog_master_server.go b/internal/server/catalog_master_server.go index d55b080..6ee85ec 100644 --- a/internal/server/catalog_master_server.go +++ b/internal/server/catalog_master_server.go @@ -474,6 +474,9 @@ func toProtoSupplier(value *repository.SupplierRecord) *productv1.Supplier { for _, item := range value.Attachments { out.Attachments = append(out.Attachments, &productv1.SupplierAttachment{FileId: item.FileID, FileName: item.FileName, FileType: item.FileType}) } + for i := range value.Brands { + out.Brands = append(out.Brands, toProtoBrand(&value.Brands[i])) + } out.DeletedAt = timePtrToMillisString(value.DeletedAt) return out }