Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -11,9 +11,13 @@ import (
|
||||
// GetProductPricingFilterOptions returns distinct option values for ProductPricing filters.
|
||||
// This is used to render filter dropdowns without loading the full dataset.
|
||||
func GetProductPricingFilterOptions(ctx context.Context, field string, q string, limit int, scopeUrunIlkGrubu []string) ([]string, error) {
|
||||
pg := db.PgDB
|
||||
mssql := db.MssqlDB
|
||||
if mssql == nil {
|
||||
return nil, fmt.Errorf("mssql db is nil")
|
||||
// Some option fields can still be served from PG cache table.
|
||||
if pg == nil {
|
||||
return nil, fmt.Errorf("mssql db is nil")
|
||||
}
|
||||
}
|
||||
field = strings.TrimSpace(field)
|
||||
q = strings.TrimSpace(q)
|
||||
@@ -24,6 +28,83 @@ func GetProductPricingFilterOptions(ctx context.Context, field string, q string,
|
||||
scopeUrunIlkGrubu = scopeUrunIlkGrubu[:3]
|
||||
}
|
||||
|
||||
// Fast path: use PG-derived pricing parameter cache for most fields.
|
||||
// This avoids scanning ProductFilterWithDescription('TR') on MSSQL, which can be slow and cause 504s.
|
||||
// productCode is not available in mk_urunpricingprmtr, keep MSSQL for that.
|
||||
if pg != nil && field != "productCode" {
|
||||
pgCol := ""
|
||||
switch field {
|
||||
case "brandGroupSelection":
|
||||
pgCol = "brand_group_sec"
|
||||
case "marka":
|
||||
pgCol = "marka"
|
||||
case "askiliYan":
|
||||
pgCol = "askili_yan"
|
||||
case "kategori":
|
||||
pgCol = "kategori"
|
||||
case "urunIlkGrubu":
|
||||
pgCol = "urun_ilk_grubu"
|
||||
case "urunAnaGrubu":
|
||||
pgCol = "urun_ana_grubu"
|
||||
case "urunAltGrubu":
|
||||
pgCol = "urun_alt_grubu"
|
||||
case "icerik":
|
||||
pgCol = "icerik"
|
||||
case "karisim":
|
||||
// "karisim" is intentionally deprecated in mk_urunpricingprmtr, keep MSSQL fallback.
|
||||
pgCol = ""
|
||||
default:
|
||||
pgCol = ""
|
||||
}
|
||||
|
||||
if pgCol != "" {
|
||||
args := make([]any, 0, 8)
|
||||
where := []string{
|
||||
"is_active = TRUE",
|
||||
fmt.Sprintf("NULLIF(BTRIM(%s), '') IS NOT NULL", pgCol),
|
||||
}
|
||||
if len(scopeUrunIlkGrubu) > 0 && field != "urunIlkGrubu" {
|
||||
args = append(args, scopeUrunIlkGrubu)
|
||||
where = append(where, fmt.Sprintf("urun_ilk_grubu = ANY($%d::text[])", len(args)))
|
||||
}
|
||||
if q != "" {
|
||||
like := q + "%"
|
||||
args = append(args, like)
|
||||
where = append(where, fmt.Sprintf("%s ILIKE $%d", pgCol, len(args)))
|
||||
}
|
||||
whereSQL := strings.Join(where, " AND ")
|
||||
|
||||
// Note: DISTINCT+ORDER+LIMIT is fine here due to small mk_urunpricingprmtr cardinality (~1000 rows).
|
||||
sqlText := fmt.Sprintf(`
|
||||
SELECT DISTINCT %s AS val
|
||||
FROM mk_urunpricingprmtr
|
||||
WHERE %s
|
||||
ORDER BY val ASC
|
||||
LIMIT %d
|
||||
`, pgCol, whereSQL, limit)
|
||||
|
||||
rows, err := pg.QueryContext(ctx, sqlText, args...)
|
||||
if err == nil {
|
||||
defer rows.Close()
|
||||
out := make([]string, 0, limit)
|
||||
for rows.Next() {
|
||||
var v sql.NullString
|
||||
if err := rows.Scan(&v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s := strings.TrimSpace(v.String); s != "" {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
// If PG path fails, fall back to MSSQL below.
|
||||
}
|
||||
}
|
||||
|
||||
// Map UI filter fields -> MSSQL expression in ProductFilterWithDescription('TR')
|
||||
var expr string
|
||||
switch field {
|
||||
|
||||
Reference in New Issue
Block a user