Refactor ProductionPlan to use f64 for money: Updated the money field in ProductionPlan from i32 to f64 to accommodate decimal values from the database. Adjusted related calculations in DirectorWorker to handle floating-point arithmetic for production costs and money management, ensuring accurate financial computations.
This commit is contained in:
@@ -19,7 +19,7 @@ struct Director {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct ProductionPlan {
|
struct ProductionPlan {
|
||||||
falukant_user_id: i32,
|
falukant_user_id: i32,
|
||||||
money: i32,
|
money: f64,
|
||||||
certificate: i32,
|
certificate: i32,
|
||||||
branch_id: i32,
|
branch_id: i32,
|
||||||
product_id: i32,
|
product_id: i32,
|
||||||
@@ -369,7 +369,9 @@ impl DirectorWorker {
|
|||||||
fn map_row_to_production_plan(row: &Row) -> Option<ProductionPlan> {
|
fn map_row_to_production_plan(row: &Row) -> Option<ProductionPlan> {
|
||||||
Some(ProductionPlan {
|
Some(ProductionPlan {
|
||||||
falukant_user_id: row.get("falukant_user_id")?.parse().ok()?,
|
falukant_user_id: row.get("falukant_user_id")?.parse().ok()?,
|
||||||
money: row.get("money")?.parse().ok()?,
|
// money kommt in der DB typischerweise als Dezimalzahl (z.B. "96284894.40").
|
||||||
|
// Daher als f64 parsen; bei Parsefehlern den Plan verwerfen.
|
||||||
|
money: row.get("money")?.parse::<f64>().ok()?,
|
||||||
certificate: row.get("certificate")?.parse().ok()?,
|
certificate: row.get("certificate")?.parse().ok()?,
|
||||||
branch_id: row.get("branch_id")?.parse().ok()?,
|
branch_id: row.get("branch_id")?.parse().ok()?,
|
||||||
product_id: row.get("product_id")?.parse().ok()?,
|
product_id: row.get("product_id")?.parse().ok()?,
|
||||||
@@ -389,30 +391,29 @@ impl DirectorWorker {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let free_capacity =
|
let free_capacity = plan.stock_size - plan.used_in_stock - plan.running_productions;
|
||||||
plan.stock_size - plan.used_in_stock - plan.running_productions;
|
|
||||||
|
|
||||||
let one_piece_cost = plan.certificate * 6;
|
// Stückkosten monetär berechnen. Da money ein f64 ist, arbeiten wir hier ebenfalls
|
||||||
let max_money_production = if one_piece_cost > 0 {
|
// mit Gleitkomma und runden erst am Ende auf eine ganze Stückzahl ab.
|
||||||
plan.money / one_piece_cost
|
let one_piece_cost = (plan.certificate * 6) as f64;
|
||||||
|
let max_money_production: i32 = if one_piece_cost > 0.0 {
|
||||||
|
// Anzahl Stück, die sich mit dem verfügbaren Geld finanzieren lassen
|
||||||
|
(plan.money / one_piece_cost).floor() as i32
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
let to_produce = free_capacity
|
let to_produce = free_capacity.min(max_money_production).min(300).max(0);
|
||||||
.min(max_money_production)
|
|
||||||
.min(300)
|
|
||||||
.max(0);
|
|
||||||
|
|
||||||
if to_produce < 1 {
|
if to_produce < 1 {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let production_cost = to_produce * one_piece_cost;
|
let production_cost = to_produce as f64 * one_piece_cost;
|
||||||
|
|
||||||
if let Err(err) = self.base.change_falukant_user_money(
|
if let Err(err) = self.base.change_falukant_user_money(
|
||||||
plan.falukant_user_id,
|
plan.falukant_user_id,
|
||||||
-(production_cost as f64),
|
-production_cost,
|
||||||
"director starts production",
|
"director starts production",
|
||||||
) {
|
) {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
|
|||||||
Reference in New Issue
Block a user