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:
10
.vscode/tasks.json
vendored
10
.vscode/tasks.json
vendored
@@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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,12 +255,25 @@ impl ProduceWorker {
|
||||
quantity: i32,
|
||||
quality: i32,
|
||||
) -> bool {
|
||||
// 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 in storeInStock: {err}");
|
||||
eprintln!("[ProduceWorker] Fehler beim Insert in storeInStock: {err}");
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("[ProduceWorker] Fehler beim Update in storeInStock: {err}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn delete_production(&self, production_id: i32) {
|
||||
if let Err(err) = self.remove_production(production_id) {
|
||||
@@ -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
|
||||
|
||||
@@ -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#"
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user