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",
|
"type": "shell",
|
||||||
"command": "cargo build",
|
"command": "cargo build",
|
||||||
"group": "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
|
self.base
|
||||||
.set_current_step("Process Finished Productions");
|
.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 {
|
for production in finished_productions {
|
||||||
self.handle_finished_production(&production);
|
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 {
|
for stock in stocks {
|
||||||
if remaining_quantity <= 0 {
|
if remaining_quantity <= 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
let free_capacity = stock.total_capacity - stock.filled;
|
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 {
|
if free_capacity <= 0 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let to_store = min(remaining_quantity, free_capacity);
|
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) {
|
if !self.store_in_stock(stock.stock_id, product_id, to_store, quality) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
remaining_quantity -= to_store;
|
remaining_quantity -= to_store;
|
||||||
|
eprintln!(
|
||||||
|
"[ProduceWorker] Eingelagert: stock={}, qty={} (remaining={})",
|
||||||
|
stock.stock_id,
|
||||||
|
to_store,
|
||||||
|
remaining_quantity
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if remaining_quantity == 0 {
|
if remaining_quantity == 0 {
|
||||||
@@ -216,11 +255,24 @@ impl ProduceWorker {
|
|||||||
quantity: i32,
|
quantity: i32,
|
||||||
quality: i32,
|
quality: i32,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
if let Err(err) = self.insert_inventory(stock_id, product_id, quantity, quality) {
|
// Versuch: vorhandenen Inventory-Posten für (stock, product) erhöhen
|
||||||
eprintln!("[ProduceWorker] Fehler in storeInStock: {err}");
|
match self.update_inventory_by_stock_product(stock_id, product_id, quantity, quality) {
|
||||||
return false;
|
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) {
|
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}")))?;
|
.map_err(|e| crate::db::DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
|
||||||
|
|
||||||
conn.prepare("insert_inventory", QUERY_INSERT_INVENTORY)?;
|
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(())
|
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> {
|
fn remove_production(&self, production_id: i32) -> Result<(), crate::db::DbError> {
|
||||||
let mut conn = self
|
let mut conn = self
|
||||||
.base
|
.base
|
||||||
|
|||||||
@@ -231,7 +231,22 @@ ORDER BY total_capacity DESC;
|
|||||||
"#;
|
"#;
|
||||||
|
|
||||||
pub const QUERY_INSERT_INVENTORY: &str = r#"
|
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#"
|
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
|
// Produce worker queries
|
||||||
pub const QUERY_GET_FINISHED_PRODUCTIONS: &str = r#"
|
pub const QUERY_GET_FINISHED_PRODUCTIONS: &str = r#"
|
||||||
SELECT
|
SELECT
|
||||||
p.id AS production_id,
|
p.id AS production_id,
|
||||||
p.branch_id,
|
p.branch_id,
|
||||||
p.product_id,
|
p.product_id,
|
||||||
@@ -1369,7 +1384,7 @@ pub const QUERY_GET_FINISHED_PRODUCTIONS: &str = r#"
|
|||||||
p.start_timestamp,
|
p.start_timestamp,
|
||||||
pr.production_time,
|
pr.production_time,
|
||||||
br.region_id,
|
br.region_id,
|
||||||
br.falukant_user_id,
|
br.falukant_user_id AS user_id,
|
||||||
ROUND(
|
ROUND(
|
||||||
GREATEST(
|
GREATEST(
|
||||||
0,
|
0,
|
||||||
@@ -1390,7 +1405,7 @@ pub const QUERY_GET_FINISHED_PRODUCTIONS: &str = r#"
|
|||||||
ON p.branch_id = br.id
|
ON p.branch_id = br.id
|
||||||
JOIN falukant_data.character c
|
JOIN falukant_data.character c
|
||||||
ON c.user_id = br.falukant_user_id
|
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
|
ON p.product_id = k.product_id
|
||||||
AND k.character_id = c.id
|
AND k.character_id = c.id
|
||||||
LEFT JOIN falukant_data.director d
|
LEFT JOIN falukant_data.director d
|
||||||
|
|||||||
Reference in New Issue
Block a user