Verbessere SQL-Abfragen im Produktionsworker: Optimiere die Abfrage für abgeschlossene Produktionen und verbessere die Lesbarkeit der Preisberechnung.
This commit is contained in:
@@ -8,6 +8,7 @@ pub const QUERY_GET_MONEY: &str = r#"
|
|||||||
SELECT money FROM falukant_data.falukant_user WHERE id = $1;
|
SELECT money FROM falukant_data.falukant_user WHERE id = $1;
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
|
|
||||||
pub const QUERY_GET_RANDOM_USER: &str = r#"
|
pub const QUERY_GET_RANDOM_USER: &str = r#"
|
||||||
SELECT id FROM falukant_data.falukant_user ORDER BY RANDOM() LIMIT 1;
|
SELECT id FROM falukant_data.falukant_user ORDER BY RANDOM() LIMIT 1;
|
||||||
"#;
|
"#;
|
||||||
@@ -1353,64 +1354,48 @@ 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,
|
||||||
p.quantity,
|
p.quantity,
|
||||||
p.start_timestamp,
|
p.start_timestamp,
|
||||||
pr.production_time,
|
pr.production_time,
|
||||||
-- Aggregierte Qualitätsbewertung pro Produktion inkl. Wettereinfluss
|
br.region_id,
|
||||||
MAX(
|
br.falukant_user_id,
|
||||||
GREATEST(
|
ROUND(
|
||||||
0,
|
|
||||||
LEAST(
|
|
||||||
100,
|
|
||||||
MAX(
|
|
||||||
GREATEST(
|
GREATEST(
|
||||||
0,
|
0,
|
||||||
LEAST(
|
LEAST(
|
||||||
100,
|
100,
|
||||||
ROUND(
|
(
|
||||||
(
|
COALESCE(k.knowledge, 0) * 0.6
|
||||||
COALESCE(k.knowledge, 0) + COALESCE(k2.knowledge, 0)
|
+ COALESCE(k2.knowledge, 0) * 0.3
|
||||||
)::numeric
|
+ COALESCE(pwe.worth_percent, 100) * 0.1
|
||||||
+ COALESCE(pwe.quality_effect, 0) * 2.5
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)::int
|
)::int AS quality
|
||||||
) AS quality,
|
FROM falukant_data.production p
|
||||||
JOIN falukant_type.product pr
|
JOIN falukant_type.product pr
|
||||||
ON p.product_id = pr.id
|
ON p.product_id = pr.id
|
||||||
JOIN falukant_data.branch br
|
JOIN falukant_data.branch br
|
||||||
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
|
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
|
||||||
JOIN falukant_data.stock s
|
LEFT JOIN falukant_data.director d
|
||||||
ON s.branch_id = br.id
|
ON d.employer_user_id = c.user_id
|
||||||
-- Optionaler Wettereinfluss: pro (Produkt, Wetter) genau ein Datensatz
|
LEFT JOIN falukant_data.knowledge k2
|
||||||
LEFT JOIN falukant_type.product_weather_effect pwe
|
ON k2.character_id = d.director_character_id
|
||||||
ON pwe.product_id = p.product_id
|
AND k2.product_id = p.product_id
|
||||||
AND pwe.weather_type_id = p.weather_type_id
|
LEFT JOIN falukant_type.product_weather_effect pwe
|
||||||
LEFT JOIN falukant_data.director d
|
ON pwe.product_id = p.product_id
|
||||||
ON d.employer_user_id = c.user_id
|
AND pwe.weather_type_id = p.weather_type_id
|
||||||
LEFT JOIN falukant_data.knowledge k2
|
WHERE p.start_timestamp + INTERVAL '1 minute' * pr.production_time <= NOW()
|
||||||
ON k2.character_id = d.director_character_id
|
ORDER BY p.start_timestamp;
|
||||||
AND k2.product_id = p.product_id
|
|
||||||
WHERE p.start_timestamp + INTERVAL '1 minute' * pr.production_time <= NOW()
|
|
||||||
GROUP BY
|
|
||||||
p.id,
|
|
||||||
p.branch_id,
|
|
||||||
p.product_id,
|
|
||||||
p.quantity,
|
|
||||||
p.start_timestamp,
|
|
||||||
pr.production_time,
|
|
||||||
br.region_id,
|
|
||||||
br.falukant_user_id
|
|
||||||
ORDER BY p.start_timestamp;
|
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
pub const QUERY_DELETE_PRODUCTION: &str = r#"
|
pub const QUERY_DELETE_PRODUCTION: &str = r#"
|
||||||
@@ -1587,10 +1572,10 @@ pub const QUERY_HOURLY_PRICE_RECALCULATION: &str = r#"
|
|||||||
COALESCE(prs.avg_sold, 0) AS parent_avg,
|
COALESCE(prs.avg_sold, 0) AS parent_avg,
|
||||||
acwp.price_after_world AS current_price,
|
acwp.price_after_world AS current_price,
|
||||||
CASE
|
CASE
|
||||||
WHEN acwp.total_sold > COALESCE(prs.avg_sold, 0) * 1.05
|
WHEN acwp.total_sold > COALESCE(prs.avg_sold, 0) * 1.05
|
||||||
THEN acwp.price_after_world * 1.05
|
THEN acwp.price_after_world * 1.05
|
||||||
WHEN acwp.total_sold < COALESCE(prs.avg_sold, 0) * 0.95
|
WHEN acwp.total_sold < COALESCE(prs.avg_sold, 0) * 0.95
|
||||||
THEN acwp.price_after_world * 0.95
|
THEN acwp.price_after_world * 0.95
|
||||||
ELSE acwp.price_after_world
|
ELSE acwp.price_after_world
|
||||||
END AS new_price
|
END AS new_price
|
||||||
FROM all_cities_with_prices acwp
|
FROM all_cities_with_prices acwp
|
||||||
@@ -1598,10 +1583,10 @@ pub const QUERY_HOURLY_PRICE_RECALCULATION: &str = r#"
|
|||||||
ON prs.parent_region_id = acwp.parent_region_id
|
ON prs.parent_region_id = acwp.parent_region_id
|
||||||
AND prs.product_id = acwp.product_id
|
AND prs.product_id = acwp.product_id
|
||||||
WHERE acwp.parent_region_id IS NOT NULL
|
WHERE acwp.parent_region_id IS NOT NULL
|
||||||
AND (
|
AND (
|
||||||
acwp.total_sold > COALESCE(prs.avg_sold, 0) * 1.05
|
acwp.total_sold > COALESCE(prs.avg_sold, 0) * 1.05
|
||||||
OR acwp.total_sold < COALESCE(prs.avg_sold, 0) * 0.95
|
OR acwp.total_sold < COALESCE(prs.avg_sold, 0) * 0.95
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
final_price_updates AS (
|
final_price_updates AS (
|
||||||
SELECT
|
SELECT
|
||||||
|
|||||||
Reference in New Issue
Block a user