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

View File

@@ -231,7 +231,22 @@ ORDER BY total_capacity DESC;
"#;
pub const QUERY_INSERT_INVENTORY: &str = r#"
INSERT INTO falukant_data.inventory (stock_id, product_id, quantity, quality, produced_at) VALUES ($1, $2, $3, $4, NOW());
INSERT INTO falukant_data.inventory (stock_id, product_id, quantity, quality, produced_at)
VALUES ($1, $2, $3, $4, NOW())
RETURNING id;
"#;
pub const QUERY_UPDATE_INVENTORY_BY_STOCK_PRODUCT: &str = r#"
UPDATE falukant_data.inventory
SET quantity = quantity + $3,
quality = LEAST(
100,
ROUND(
((quantity * quality) + ($3 * $4))::numeric / NULLIF(quantity + $3, 0)
)
)
WHERE stock_id = $1 AND product_id = $2
RETURNING id;
"#;
pub const QUERY_UPDATE_VEHICLE_AFTER_TRANSPORT: &str = r#"
@@ -1361,7 +1376,7 @@ WHERE b.falukant_user_id = $1 AND s.stock_type_id = $2;
"#;
// Produce worker queries
pub const QUERY_GET_FINISHED_PRODUCTIONS: &str = r#"
SELECT
SELECT
p.id AS production_id,
p.branch_id,
p.product_id,
@@ -1369,7 +1384,7 @@ pub const QUERY_GET_FINISHED_PRODUCTIONS: &str = r#"
p.start_timestamp,
pr.production_time,
br.region_id,
br.falukant_user_id,
br.falukant_user_id AS user_id,
ROUND(
GREATEST(
0,
@@ -1390,7 +1405,7 @@ pub const QUERY_GET_FINISHED_PRODUCTIONS: &str = r#"
ON p.branch_id = br.id
JOIN falukant_data.character c
ON c.user_id = br.falukant_user_id
JOIN falukant_data.knowledge k
LEFT JOIN falukant_data.knowledge k
ON p.product_id = k.product_id
AND k.character_id = c.id
LEFT JOIN falukant_data.director d