Refactor SQL query for best production calculation: Updated the QUERY_GET_BEST_PRODUCTION to enhance the worth calculation by incorporating vehicle presence checks and adjusting the logic for market pricing based on regional data. Improved documentation to clarify the new formula for profit per minute and its implications for product evaluation across different regions.
All checks were successful
Deploy yourpart (blue-green) / deploy (push) Successful in 2m46s

This commit is contained in:
Torsten Schulz (local)
2026-04-09 08:57:27 +02:00
parent 7d56f51ec9
commit dda559cbe7

View File

@@ -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'; WHERE current_time BETWEEN '08:00:00' AND '17:00:00';
"#; "#;
/// Bester Produktstart: Mit **mindestens einem** `falukant_data.vehicle` pro User — bester `worth_percent` /// Bester Produktstart: **Gewinn pro Minute** (wie UI „Gewinn/Minute“) ≈ `(Erlös/Stück Stückkosten) / Produktionszeit`.
/// je Produkt über alle Filialregionen; **ohne** Fahrzeug nur noch **lokaler** Markt (Region der Direktor-Filiale), /// Stückkosten = gleiche Formel wie `Director::piece_production_cost` (Basis 6 + Kategorie, Headroom-Rabatt).
/// sonst wäre der beste Fern-Preis nicht erreichbar. /// Marktpreis: mit Fahrzeug `MAX(worth_percent)` über Filialregionen, sonst nur Direktor-Region. **Ohne** Steuer im Ranking.
pub const QUERY_GET_BEST_PRODUCTION: &str = r#" 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, 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, 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, (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, 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 ( ftp.sell_cost * (
SELECT 1 FROM falukant_data.vehicle v CASE
WHERE v.falukant_user_id = fdu.id 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) (6.0 + (GREATEST(COALESCE(ftp.category, 1), 1))::float8)
END / 100.0 * (
)) / NULLIF(ftp.production_time::float8, 0.0) ) AS worth, 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, 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 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 FROM falukant_data.director fdd