Füge erweiterte SQL-Abfragen und Logging in ProduceWorker hinzu: Implementiere Update- und Insert-Logik für das Inventar und verbessere die Fehlerausgaben.

This commit is contained in:
Torsten Schulz (local)
2025-12-16 10:52:06 +01:00
parent 74fee2d4c9
commit d2e253b79a
3 changed files with 113 additions and 9 deletions

View File

@@ -126,6 +126,15 @@ impl ProduceWorker {
self.base
.set_current_step("Process Finished Productions");
if finished_productions.is_empty() {
eprintln!("[ProduceWorker] Keine abgeschlossenen Produktionen gefunden.");
} else {
eprintln!(
"[ProduceWorker] Verarbeite {} abgeschlossene Produktionen.",
finished_productions.len()
);
}
for production in finished_productions {
self.handle_finished_production(&production);
}
@@ -173,21 +182,51 @@ impl ProduceWorker {
}
};
if stocks.is_empty() {
eprintln!("[ProduceWorker] Keine Stocks für Branch {branch_id} gefunden.");
} else {
eprintln!(
"[ProduceWorker] {} Stocks gefunden für Branch {}.",
stocks.len(),
branch_id
);
}
for stock in stocks {
if remaining_quantity <= 0 {
break;
}
let free_capacity = stock.total_capacity - stock.filled;
eprintln!(
"[ProduceWorker] Stock {}: capacity={}, filled={}, free={}",
stock.stock_id,
stock.total_capacity,
stock.filled,
free_capacity
);
if free_capacity <= 0 {
continue;
}
let to_store = min(remaining_quantity, free_capacity);
eprintln!(
"[ProduceWorker] Versuche Einlagerung: stock={}, product={}, qty={}, quality={}",
stock.stock_id,
product_id,
to_store,
quality
);
if !self.store_in_stock(stock.stock_id, product_id, to_store, quality) {
return false;
}
remaining_quantity -= to_store;
eprintln!(
"[ProduceWorker] Eingelagert: stock={}, qty={} (remaining={})",
stock.stock_id,
to_store,
remaining_quantity
);
}
if remaining_quantity == 0 {
@@ -216,11 +255,24 @@ impl ProduceWorker {
quantity: i32,
quality: i32,
) -> bool {
if let Err(err) = self.insert_inventory(stock_id, product_id, quantity, quality) {
eprintln!("[ProduceWorker] Fehler in storeInStock: {err}");
return false;
// Versuch: vorhandenen Inventory-Posten für (stock, product) erhöhen
match self.update_inventory_by_stock_product(stock_id, product_id, quantity, quality) {
Ok(updated) => {
if updated {
return true;
}
// Wenn kein Update stattfand, Insert versuchen
if let Err(err) = self.insert_inventory(stock_id, product_id, quantity, quality) {
eprintln!("[ProduceWorker] Fehler beim Insert in storeInStock: {err}");
return false;
}
true
}
Err(err) => {
eprintln!("[ProduceWorker] Fehler beim Update in storeInStock: {err}");
false
}
}
true
}
fn delete_production(&self, production_id: i32) {
@@ -306,10 +358,37 @@ impl ProduceWorker {
.map_err(|e| crate::db::DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
conn.prepare("insert_inventory", QUERY_INSERT_INVENTORY)?;
conn.execute("insert_inventory", &[&stock_id, &product_id, &quantity, &quality])?;
let rows = conn.execute("insert_inventory", &[&stock_id, &product_id, &quantity, &quality])?;
if let Some(row) = rows.get(0) {
if let Some(id) = row.get("id") {
eprintln!("[ProduceWorker] Insert inventory id={}", id);
}
}
Ok(())
}
fn update_inventory_by_stock_product(
&self,
stock_id: i32,
product_id: i32,
quantity: i32,
quality: i32,
) -> Result<bool, crate::db::DbError> {
use crate::worker::sql::QUERY_UPDATE_INVENTORY_BY_STOCK_PRODUCT;
let mut conn = self
.base
.pool
.get()
.map_err(|e| crate::db::DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
conn.prepare("update_inventory_by_stock_product", QUERY_UPDATE_INVENTORY_BY_STOCK_PRODUCT)?;
let rows = conn.execute(
"update_inventory_by_stock_product",
&[&stock_id, &product_id, &quantity, &quality],
)?;
Ok(!rows.is_empty())
}
fn remove_production(&self, production_id: i32) -> Result<(), crate::db::DbError> {
let mut conn = self
.base