Enhance capacity management in DirectorWorker: Retrieve current capacity values directly from the database to prevent race conditions, calculate free capacity based on real-time data, and ensure production only occurs if sufficient storage is available. Update SQL query to check for adequate storage before returning production results.

This commit is contained in:
Torsten Schulz (local)
2026-01-28 16:36:30 +01:00
parent 4b4d84b15c
commit 05e6155e38
2 changed files with 51 additions and 5 deletions

View File

@@ -1419,8 +1419,26 @@ pub const QUERY_GET_FINISHED_PRODUCTIONS: &str = r#"
LEFT JOIN falukant_type.product_weather_effect pwe
ON pwe.product_id = p.product_id
AND pwe.weather_type_id = w.weather_type_id
-- Prüfe, ob genug freier Lagerplatz vorhanden ist
LEFT JOIN (
SELECT
br2.id AS branch_id,
COALESCE(SUM(fds.quantity), 0) AS stock_size,
COALESCE(SUM(COALESCE(fdi.quantity, 0)), 0) AS used_in_stock,
COALESCE(SUM(COALESCE(fdp2.quantity, 0)), 0) AS running_productions_quantity
FROM falukant_data.branch br2
LEFT JOIN falukant_data.stock fds ON fds.branch_id = br2.id
LEFT JOIN falukant_data.inventory fdi ON fdi.stock_id = fds.id
LEFT JOIN falukant_data.production fdp2 ON fdp2.branch_id = br2.id
GROUP BY br2.id
) capacity ON capacity.branch_id = p.branch_id
-- Wetter-Effekte derzeit aus der Qualitätsberechnung entfernt
WHERE p.start_timestamp + INTERVAL '1 minute' * pr.production_time <= NOW()
-- Nur Produktionen zurückgeben, für die genug Lagerplatz vorhanden ist
-- running_productions_quantity enthält bereits p.quantity, daher prüfen wir:
-- (stock_size - used_in_stock - running_productions_quantity) >= 0
-- Das bedeutet: Nach Abzug aller laufenden Produktionen muss noch Platz vorhanden sein
AND (capacity.stock_size - capacity.used_in_stock - capacity.running_productions_quantity) >= 0
ORDER BY p.start_timestamp;
"#;