mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: add brands handling to supplier records and update proto conversion
This commit is contained in:
parent
099d6eeab0
commit
c48f722137
@ -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`
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user