From d2e253b79a6438dafe19907085a6ace3e7aac440 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Tue, 16 Dec 2025 10:52:06 +0100 Subject: [PATCH] =?UTF-8?q?F=C3=BCge=20erweiterte=20SQL-Abfragen=20und=20L?= =?UTF-8?q?ogging=20in=20ProduceWorker=20hinzu:=20Implementiere=20Update-?= =?UTF-8?q?=20und=20Insert-Logik=20f=C3=BCr=20das=20Inventar=20und=20verbe?= =?UTF-8?q?ssere=20die=20Fehlerausgaben.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/tasks.json | 10 +++++ src/worker/produce.rs | 89 ++++++++++++++++++++++++++++++++++++++++--- src/worker/sql.rs | 23 +++++++++-- 3 files changed, 113 insertions(+), 9 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 6835c77..22a22de 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -12,6 +12,16 @@ "type": "shell", "command": "cargo build", "group": "build" + }, + { + "label": "Build YpDaemon", + "type": "shell", + "command": "cargo build", + "isBackground": false, + "problemMatcher": [ + "$rustc" + ], + "group": "build" } ] } \ No newline at end of file diff --git a/src/worker/produce.rs b/src/worker/produce.rs index ddd5db2..0b9aa6e 100644 --- a/src/worker/produce.rs +++ b/src/worker/produce.rs @@ -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 { + 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 diff --git a/src/worker/sql.rs b/src/worker/sql.rs index 8d8e5da..1c8a585 100644 --- a/src/worker/sql.rs +++ b/src/worker/sql.rs @@ -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