Fix product performance grouped JSON build

This commit is contained in:
M_Kececi
2026-07-12 16:15:41 +03:00
parent cc2930caf2
commit 5eaf8be9e6
2 changed files with 20 additions and 38 deletions
+5 -27
View File
@@ -4677,25 +4677,16 @@ FROM (
CASE
WHEN SUM(CASE
WHEN COALESCE(sales_qty_90d,0) > 0 THEN sales_qty_90d
WHEN COALESCE(sales_usd_90d,0) > 0 THEN sales_usd_90d
WHEN COALESCE(stock_qty,0) > 0 AND COALESCE(cost_price_usd,0) > 0 THEN stock_qty * cost_price_usd
WHEN COALESCE(stock_qty,0) > 0 THEN stock_qty
ELSE 1
ELSE 0
END) > 0
THEN SUM(performance_score * CASE
WHEN COALESCE(sales_qty_90d,0) > 0 THEN sales_qty_90d
WHEN COALESCE(sales_usd_90d,0) > 0 THEN sales_usd_90d
WHEN COALESCE(stock_qty,0) > 0 AND COALESCE(cost_price_usd,0) > 0 THEN stock_qty * cost_price_usd
WHEN COALESCE(stock_qty,0) > 0 THEN stock_qty
ELSE 1
ELSE 0
END) / NULLIF(SUM(CASE
WHEN COALESCE(sales_qty_90d,0) > 0 THEN sales_qty_90d
WHEN COALESCE(sales_usd_90d,0) > 0 THEN sales_usd_90d
WHEN COALESCE(stock_qty,0) > 0 AND COALESCE(cost_price_usd,0) > 0 THEN stock_qty * cost_price_usd
WHEN COALESCE(stock_qty,0) > 0 THEN stock_qty
ELSE 1
ELSE 0
END),0)
ELSE 0
ELSE COALESCE(AVG(performance_score),0)
END AS performance_score,
MODE() WITHIN GROUP (ORDER BY performance_bucket) AS performance_bucket,
COALESCE(MODE() WITHIN GROUP (ORDER BY NULLIF(recommendation,'')), '') AS recommendation,
@@ -6841,20 +6832,7 @@ func productPerformanceScoreWeight(row map[string]any, suffix string) float64 {
if weight := floatFromMap(row, "sales_qty_"+suffix); weight > 0 {
return weight
}
if weight := floatFromMap(row, "sales_usd_"+suffix); weight > 0 {
return weight
}
stockQty := floatFromMap(row, "stock_qty")
if stockQty <= 0 {
return 1
}
if cost := floatFromMap(row, "cost_price_usd_"+suffix); cost > 0 {
return stockQty * cost
}
if cost := floatFromMap(row, "cost_price_usd"); cost > 0 {
return stockQty * cost
}
return stockQty
return 0
}
func productPerformanceCostWeight(row map[string]any) float64 {