Enhance pricing logic in DirectorWorker and implement hourly price recalculation: Added a new worth_percent field to the InventoryItem struct and updated SQL queries to incorporate this value in price calculations. Refactored price computation logic to use a base price derived from worth_percent. Introduced a new hourly price recalculation mechanism that adjusts prices based on sales data from the last hour, ensuring dynamic pricing adjustments. Enhanced logging for better monitoring of price updates.
This commit is contained in:
@@ -40,6 +40,7 @@ struct InventoryItem {
|
||||
user_id: i32,
|
||||
region_id: i32,
|
||||
branch_id: i32,
|
||||
worth_percent: f64, // Regionaler worth_percent-Wert für die Preisberechnung
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -186,7 +187,8 @@ const QUERY_GET_INVENTORY: &str = r#"
|
||||
p.sell_cost,
|
||||
fu.id AS user_id,
|
||||
b.region_id,
|
||||
b.id AS branch_id
|
||||
b.id AS branch_id,
|
||||
COALESCE(tpw.worth_percent, 100.0) AS worth_percent
|
||||
FROM falukant_data.inventory i
|
||||
JOIN falukant_data.stock s
|
||||
ON s.id = i.stock_id
|
||||
@@ -198,6 +200,9 @@ const QUERY_GET_INVENTORY: &str = r#"
|
||||
ON d.employer_user_id = fu.id
|
||||
JOIN falukant_type.product p
|
||||
ON p.id = i.product_id
|
||||
LEFT JOIN falukant_data.town_product_worth tpw
|
||||
ON tpw.region_id = b.region_id
|
||||
AND tpw.product_id = i.product_id
|
||||
WHERE d.id = $1
|
||||
AND b.id = $2;
|
||||
"#;
|
||||
@@ -728,6 +733,10 @@ impl DirectorWorker {
|
||||
user_id: row.get("user_id")?.parse().ok()?,
|
||||
region_id: row.get("region_id")?.parse().ok()?,
|
||||
branch_id: row.get("branch_id")?.parse().ok()?,
|
||||
worth_percent: row
|
||||
.get("worth_percent")
|
||||
.and_then(|v| v.parse::<f64>().ok())
|
||||
.unwrap_or(100.0),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -741,9 +750,19 @@ impl DirectorWorker {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let min_price = item.sell_cost * 0.6;
|
||||
let piece_sell_price =
|
||||
min_price + (item.sell_cost - min_price) * (item.quality as f64 / 100.0);
|
||||
// Neue Preisberechnung gemäß Spezifikation:
|
||||
// 1. Basispreis = product.sellCost * (worthPercent / 100)
|
||||
let base_price = item.sell_cost * (item.worth_percent / 100.0);
|
||||
|
||||
// 2. min = basePrice * 0.6, max = basePrice
|
||||
let min_price = base_price * 0.6;
|
||||
let max_price = base_price;
|
||||
|
||||
// 3. price = min + (max - min) * (knowledgeFactor / 100)
|
||||
// knowledgeFactor ist hier item.quality
|
||||
let knowledge_factor = item.quality as f64;
|
||||
let piece_sell_price = min_price + (max_price - min_price) * (knowledge_factor / 100.0);
|
||||
|
||||
let sell_price = piece_sell_price * item.quantity as f64;
|
||||
|
||||
if let Err(err) = self.base.change_falukant_user_money(
|
||||
@@ -836,17 +855,22 @@ impl DirectorWorker {
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
// Lokalen Stückpreis berechnen
|
||||
// Lokalen Stückpreis berechnen (neue Preisberechnung)
|
||||
let local_percent = worth_by_region
|
||||
.get(&item.region_id)
|
||||
.copied()
|
||||
.unwrap_or(100.0);
|
||||
|
||||
let local_sell_cost = item.sell_cost * local_percent / 100.0;
|
||||
let local_min_price = local_sell_cost * 0.6;
|
||||
let quality_factor = item.quality as f64 / 100.0;
|
||||
let local_piece_price =
|
||||
local_min_price + (local_sell_cost - local_min_price) * quality_factor;
|
||||
// 1. Basispreis = product.sellCost * (worthPercent / 100)
|
||||
let local_base_price = item.sell_cost * (local_percent / 100.0);
|
||||
|
||||
// 2. min = basePrice * 0.6, max = basePrice
|
||||
let local_min_price = local_base_price * 0.6;
|
||||
let local_max_price = local_base_price;
|
||||
|
||||
// 3. price = min + (max - min) * (knowledgeFactor / 100)
|
||||
let knowledge_factor = item.quality as f64;
|
||||
let local_piece_price = local_min_price + (local_max_price - local_min_price) * (knowledge_factor / 100.0);
|
||||
|
||||
let mut best_target_region: Option<i32> = None;
|
||||
let mut best_quantity: i32 = 0;
|
||||
@@ -859,11 +883,17 @@ impl DirectorWorker {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Remote-Stückpreis
|
||||
let remote_sell_cost = item.sell_cost * remote_percent / 100.0;
|
||||
let remote_min_price = remote_sell_cost * 0.6;
|
||||
let remote_piece_price =
|
||||
remote_min_price + (remote_sell_cost - remote_min_price) * quality_factor;
|
||||
// Remote-Stückpreis berechnen (neue Preisberechnung)
|
||||
// 1. Basispreis = product.sellCost * (worthPercent / 100)
|
||||
let remote_base_price = item.sell_cost * (remote_percent / 100.0);
|
||||
|
||||
// 2. min = basePrice * 0.6, max = basePrice
|
||||
let remote_min_price = remote_base_price * 0.6;
|
||||
let remote_max_price = remote_base_price;
|
||||
|
||||
// 3. price = min + (max - min) * (knowledgeFactor / 100)
|
||||
let knowledge_factor = item.quality as f64;
|
||||
let remote_piece_price = remote_min_price + (remote_max_price - remote_min_price) * (knowledge_factor / 100.0);
|
||||
|
||||
let delta_per_unit = remote_piece_price - local_piece_price;
|
||||
if delta_per_unit <= 0.0 {
|
||||
|
||||
Reference in New Issue
Block a user