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:
@@ -370,11 +370,42 @@ impl DirectorWorker {
|
||||
conn: &mut DbConnection,
|
||||
plan: &ProductionPlan,
|
||||
) -> Result<(), DbError> {
|
||||
let free_capacity = Self::calc_free_capacity(plan);
|
||||
// Hole aktuelle Kapazitätswerte direkt aus der DB, um Race Conditions zu vermeiden
|
||||
let capacity_rows = conn.execute("get_branch_capacity", &[&plan.branch_id])?;
|
||||
if capacity_rows.is_empty() {
|
||||
eprintln!("[DirectorWorker] Keine Kapazitätsdaten für Branch {} gefunden", plan.branch_id);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let row = &capacity_rows[0];
|
||||
let stock_size: i32 = row
|
||||
.get("stock_size")
|
||||
.and_then(|v| v.parse::<i32>().ok())
|
||||
.unwrap_or(0);
|
||||
let used_in_stock: i32 = row
|
||||
.get("used_in_stock")
|
||||
.and_then(|v| v.parse::<i32>().ok())
|
||||
.unwrap_or(0);
|
||||
let running_productions_quantity: i32 = row
|
||||
.get("running_productions_quantity")
|
||||
.and_then(|v| v.parse::<i32>().ok())
|
||||
.unwrap_or(0);
|
||||
|
||||
// Berechne freie Kapazität mit aktuellen Werten
|
||||
let free_capacity = stock_size - used_in_stock - running_productions_quantity;
|
||||
|
||||
if free_capacity <= 0 {
|
||||
eprintln!(
|
||||
"[DirectorWorker] Keine Produktion gestartet: Kein freier Lagerplatz (stock_size={}, used={}, running_qty={})",
|
||||
stock_size, used_in_stock, running_productions_quantity
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let one_piece_cost = Self::calc_one_piece_cost(plan);
|
||||
let max_money_production = Self::calc_max_money_production(plan, one_piece_cost);
|
||||
|
||||
let to_produce = (free_capacity.min(max_money_production)).clamp(0, 100);
|
||||
let to_produce = (free_capacity.min(max_money_production)).clamp(0, 100);
|
||||
|
||||
eprintln!(
|
||||
"[DirectorWorker] Produktionsberechnung: free_capacity={}, one_piece_cost={}, max_money_production={}, to_produce={}, running_productions={}",
|
||||
@@ -437,9 +468,6 @@ impl DirectorWorker {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn calc_free_capacity(plan: &ProductionPlan) -> i32 {
|
||||
plan.stock_size - plan.used_in_stock - plan.running_productions_quantity
|
||||
}
|
||||
|
||||
fn calc_one_piece_cost(plan: &ProductionPlan) -> f64 {
|
||||
(plan.certificate * 6) as f64
|
||||
|
||||
Reference in New Issue
Block a user