Refactor SQL queries into a dedicated module
- Moved SQL queries from multiple worker files into `src/worker/sql.rs` for better organization and maintainability. - Updated references in `stockage_manager.rs`, `transport.rs`, `underground.rs`, `user_character.rs`, and `value_recalculation.rs` to use the new centralized SQL queries. - Improved code readability by replacing `.get(0)` with `.first()` for better clarity when retrieving the first row from query results. - Cleaned up unnecessary comments and consolidated related SQL queries.
This commit is contained in:
@@ -31,6 +31,10 @@ use crate::worker::sql::{
|
||||
QUERY_GET_BRANCH_REGION,
|
||||
QUERY_GET_AVERAGE_WORTH,
|
||||
QUERY_UPDATE_INVENTORY_QTY,
|
||||
QUERY_GET_PRODUCT_COST,
|
||||
QUERY_GET_USER_OFFICES,
|
||||
QUERY_CUMULATIVE_TAX_NO_EXEMPT,
|
||||
QUERY_CUMULATIVE_TAX_WITH_EXEMPT,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -103,7 +107,7 @@ impl DirectorWorker {
|
||||
}
|
||||
}
|
||||
|
||||
fn run_iteration(&mut self, state: &WorkerState) {
|
||||
fn run_iteration(&mut self, _state: &WorkerState) {
|
||||
self.base.set_current_step("DirectorWorker iteration");
|
||||
|
||||
let now = Instant::now();
|
||||
@@ -119,11 +123,7 @@ impl DirectorWorker {
|
||||
self.last_run = Some(now);
|
||||
}
|
||||
|
||||
std::thread::sleep(Duration::from_secs(1));
|
||||
|
||||
if !state.running_worker.load(Ordering::Relaxed) {
|
||||
return;
|
||||
}
|
||||
std::thread::sleep(Duration::from_secs(1));
|
||||
}
|
||||
|
||||
fn perform_all_tasks(&mut self) -> Result<(), DbError> {
|
||||
@@ -222,7 +222,7 @@ impl DirectorWorker {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut base_plan = match Self::map_row_to_production_plan(&rows[0]) {
|
||||
let mut base_plan = match rows.first().and_then(Self::map_row_to_production_plan) {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
eprintln!(
|
||||
@@ -374,7 +374,7 @@ impl DirectorWorker {
|
||||
let one_piece_cost = Self::calc_one_piece_cost(plan);
|
||||
let max_money_production = Self::calc_max_money_production(plan, one_piece_cost);
|
||||
|
||||
let to_produce = free_capacity.min(max_money_production).min(100).max(0);
|
||||
let to_produce = (free_capacity.min(max_money_production)).clamp(0, 100);
|
||||
|
||||
eprintln!(
|
||||
"[DirectorWorker] Produktionsberechnung: free_capacity={}, one_piece_cost={}, max_money_production={}, to_produce={}, running_productions={}",
|
||||
@@ -718,15 +718,19 @@ impl DirectorWorker {
|
||||
|
||||
// Helper: get one_piece_cost from DB row fallback logic
|
||||
fn resolve_one_piece_cost(conn: &mut DbConnection, product_id: i32, fallback: f64) -> Result<f64, DbError> {
|
||||
conn.prepare("get_product_cost", "SELECT original_sell_cost, sell_cost FROM falukant_type.product WHERE id = $1")?;
|
||||
conn.prepare("get_product_cost", QUERY_GET_PRODUCT_COST)?;
|
||||
let rows = conn.execute("get_product_cost", &[&product_id])?;
|
||||
if let Some(row) = rows.get(0) {
|
||||
if let Some(osc) = row.get("original_sell_cost") {
|
||||
if let Ok(v) = osc.parse::<f64>() { return Ok(v); }
|
||||
}
|
||||
if let Some(sc) = row.get("sell_cost") {
|
||||
if let Ok(v) = sc.parse::<f64>() { return Ok(v); }
|
||||
}
|
||||
if let Some(row) = rows.first()
|
||||
&& let Some(osc) = row.get("original_sell_cost")
|
||||
&& let Ok(v) = osc.parse::<f64>()
|
||||
{
|
||||
return Ok(v);
|
||||
}
|
||||
if let Some(row) = rows.first()
|
||||
&& let Some(sc) = row.get("sell_cost")
|
||||
&& let Ok(v) = sc.parse::<f64>()
|
||||
{
|
||||
return Ok(v);
|
||||
}
|
||||
Ok(fallback)
|
||||
}
|
||||
@@ -736,15 +740,12 @@ impl DirectorWorker {
|
||||
// Default
|
||||
let mut cumulative_tax_percent = DEFAULT_TAX_PERCENT;
|
||||
|
||||
conn.prepare("get_branch_region", "SELECT region_id FROM falukant_data.branch WHERE id = $1;")?;
|
||||
conn.prepare("get_branch_region", QUERY_GET_BRANCH_REGION)?;
|
||||
let branch_rows = conn.execute("get_branch_region", &[&branch_id])?;
|
||||
let branch_region_id: Option<i32> = branch_rows.get(0).and_then(|r| r.get("region_id")).and_then(|v| v.parse().ok());
|
||||
let branch_region_id: Option<i32> = branch_rows.first().and_then(|r| r.get("region_id")).and_then(|v| v.parse().ok());
|
||||
|
||||
if let Some(region_id) = branch_region_id {
|
||||
conn.prepare(
|
||||
"get_user_offices",
|
||||
"SELECT po.id AS office_id, pot.name AS office_name, po.region_id, rt.label_tr AS region_type FROM falukant_data.political_office po JOIN falukant_type.political_office_type pot ON pot.id = po.office_type_id JOIN falukant_data.region r ON r.id = po.region_id JOIN falukant_type.region_type rt ON rt.id = r.region_type_id WHERE po.holder_id = $1 AND (po.end_date IS NULL OR po.end_date > NOW());",
|
||||
)?;
|
||||
conn.prepare("get_user_offices", QUERY_GET_USER_OFFICES)?;
|
||||
let offices = conn.execute("get_user_offices", &[&user_id])?;
|
||||
|
||||
let mut exempt_types: Vec<String> = Vec::new();
|
||||
@@ -767,27 +768,19 @@ impl DirectorWorker {
|
||||
}
|
||||
|
||||
if exempt_types.is_empty() {
|
||||
conn.prepare(
|
||||
"cumulative_tax_no_exempt",
|
||||
"WITH RECURSIVE ancestors AS (SELECT id, parent_id, COALESCE(tax_percent,0.0) AS tax_percent FROM falukant_data.region WHERE id = $1 UNION ALL SELECT r.id, r.parent_id, COALESCE(r.tax_percent,0.0) FROM falukant_data.region r JOIN ancestors a ON r.id = a.parent_id) SELECT COALESCE(SUM(tax_percent),0.0) AS total_percent FROM ancestors;",
|
||||
)?;
|
||||
conn.prepare("cumulative_tax_no_exempt", QUERY_CUMULATIVE_TAX_NO_EXEMPT)?;
|
||||
let res = conn.execute("cumulative_tax_no_exempt", &[®ion_id])?;
|
||||
if let Some(row) = res.get(0) {
|
||||
if let Some(tp) = row.get("total_percent") {
|
||||
cumulative_tax_percent = tp.parse::<f64>().unwrap_or(DEFAULT_TAX_PERCENT);
|
||||
}
|
||||
if let Some(row) = res.first()
|
||||
&& let Some(tp) = row.get("total_percent")
|
||||
{
|
||||
cumulative_tax_percent = tp.parse::<f64>().unwrap_or(DEFAULT_TAX_PERCENT);
|
||||
}
|
||||
} else {
|
||||
conn.prepare(
|
||||
"cumulative_tax_with_exempt",
|
||||
"WITH RECURSIVE ancestors AS (SELECT r.id, r.parent_id, CASE WHEN rt.label_tr = ANY($2::text[]) THEN 0.0 ELSE COALESCE(r.tax_percent,0.0) END AS tax_percent FROM falukant_data.region r JOIN falukant_type.region_type rt ON rt.id = r.region_type_id WHERE r.id = $1 UNION ALL SELECT r.id, r.parent_id, CASE WHEN rt.label_tr = ANY($2::text[]) THEN 0.0 ELSE COALESCE(r.tax_percent,0.0) END FROM falukant_data.region r JOIN falukant_type.region_type rt ON rt.id = r.region_type_id JOIN ancestors a ON r.id = a.parent_id) SELECT COALESCE(SUM(tax_percent),0.0) AS total_percent FROM ancestors;",
|
||||
)?;
|
||||
conn.prepare("cumulative_tax_with_exempt", QUERY_CUMULATIVE_TAX_WITH_EXEMPT)?;
|
||||
let exempt_array: Vec<&str> = exempt_types.iter().map(|s| s.as_str()).collect();
|
||||
let res = conn.execute("cumulative_tax_with_exempt", &[®ion_id, &exempt_array])?;
|
||||
if let Some(row) = res.get(0) {
|
||||
if let Some(tp) = row.get("total_percent") {
|
||||
cumulative_tax_percent = tp.parse::<f64>().unwrap_or(DEFAULT_TAX_PERCENT);
|
||||
}
|
||||
if let Some(row) = res.first() && let Some(tp) = row.get("total_percent") {
|
||||
cumulative_tax_percent = tp.parse::<f64>().unwrap_or(DEFAULT_TAX_PERCENT);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -828,10 +821,8 @@ impl DirectorWorker {
|
||||
}
|
||||
|
||||
let payout_amount = (payout_cents as f64) / 100.0;
|
||||
if payout_cents != 0 {
|
||||
if let Err(err) = self.base.change_falukant_user_money(item.user_id, payout_amount, "sell products") {
|
||||
eprintln!("[DirectorWorker] Fehler bei change_falukant_user_money (sell products): {err}");
|
||||
}
|
||||
if payout_cents != 0 && let Err(err) = self.base.change_falukant_user_money(item.user_id, payout_amount, "sell products") {
|
||||
eprintln!("[DirectorWorker] Fehler bei change_falukant_user_money (sell products): {err}");
|
||||
}
|
||||
|
||||
// Debug: Log vor dem DB-Aufruf
|
||||
|
||||
Reference in New Issue
Block a user