diff --git a/src/worker/sql.rs b/src/worker/sql.rs index 1165c83..5f60daa 100644 --- a/src/worker/sql.rs +++ b/src/worker/sql.rs @@ -73,24 +73,41 @@ JOIN falukant_data.branch b ON b.region_id = c.region_id AND b.falukant_user_id WHERE current_time BETWEEN '08:00:00' AND '17:00:00'; "#; -/// Bester Produktstart: Mit **mindestens einem** `falukant_data.vehicle` pro User — bester `worth_percent` -/// je Produkt über alle Filialregionen; **ohne** Fahrzeug nur noch **lokaler** Markt (Region der Direktor-Filiale), -/// sonst wäre der beste Fern-Preis nicht erreichbar. +/// Bester Produktstart: **Gewinn pro Minute** (wie UI „Gewinn/Minute“) ≈ `(Erlös/Stück − Stückkosten) / Produktionszeit`. +/// Stückkosten = gleiche Formel wie `Director::piece_production_cost` (Basis 6 + Kategorie, Headroom-Rabatt). +/// Marktpreis: mit Fahrzeug `MAX(worth_percent)` über Filialregionen, sonst nur Direktor-Region. **Ohne** Steuer im Ranking. pub const QUERY_GET_BEST_PRODUCTION: &str = r#" SELECT fdu.id falukant_user_id, CAST(fdu.money AS text) AS money, fdu.certificate, ftp.id product_id, ftp.label_tr, COALESCE(ftp.category, 1)::int AS product_category, fdb.region_id, (SELECT SUM(quantity) FROM falukant_data.stock fds WHERE fds.branch_id = fdb.id) AS stock_size, COALESCE((SELECT SUM(COALESCE(fdi.quantity, 0)) FROM falukant_data.stock fds JOIN falukant_data.inventory fdi ON fdi.stock_id = fds.id WHERE fds.branch_id = fdb.id), 0) AS used_in_stock, -( (ftp.sell_cost * ( - CASE - WHEN EXISTS ( - SELECT 1 FROM falukant_data.vehicle v - WHERE v.falukant_user_id = fdu.id +( + ( + ftp.sell_cost * ( + CASE + WHEN EXISTS ( + SELECT 1 FROM falukant_data.vehicle v + WHERE v.falukant_user_id = fdu.id + ) + THEN bw.max_worth_pct + ELSE COALESCE(fdtpw_local.worth_percent::float8, 0.0) + END / 100.0 ) - THEN bw.max_worth_pct - ELSE COALESCE(fdtpw_local.worth_percent::float8, 0.0) - END / 100.0 - )) / NULLIF(ftp.production_time::float8, 0.0) ) AS worth, + - ( + (6.0 + (GREATEST(COALESCE(ftp.category, 1), 1))::float8) + * ( + 1.0 - LEAST( + GREATEST( + (GREATEST(COALESCE(fdu.certificate, 1), 1))::float8 + - (GREATEST(COALESCE(ftp.category, 1), 1))::float8, + 0.0 + ) * 0.035, + 0.14 + ) + ) + ) + ) / NULLIF(ftp.production_time::float8, 0.0) +) AS worth, fdb.id AS branch_id, (SELECT COUNT(id) FROM falukant_data.production WHERE branch_id = fdb.id) AS running_productions, COALESCE((SELECT SUM(COALESCE(fdp.quantity, 0)) quantity FROM falukant_data.production fdp WHERE fdp.branch_id = fdb.id), 0) AS running_productions_quantity FROM falukant_data.director fdd