Enhance product price retrieval by including currentRegionId in FalukantController and FalukantService
- Updated the FalukantController to accept currentRegionId as a parameter for fetching product prices in cities. - Modified the FalukantService to incorporate currentRegionId in the price calculation logic, allowing exclusion of the current region from results. - Adjusted frontend components to pass currentRegionId, improving the accuracy of price comparisons and user experience.
This commit is contained in:
@@ -155,10 +155,11 @@ class FalukantController {
|
|||||||
this.getProductPricesInCities = this._wrapWithUser((userId, req) => {
|
this.getProductPricesInCities = this._wrapWithUser((userId, req) => {
|
||||||
const productId = parseInt(req.query.productId, 10);
|
const productId = parseInt(req.query.productId, 10);
|
||||||
const currentPrice = parseFloat(req.query.currentPrice);
|
const currentPrice = parseFloat(req.query.currentPrice);
|
||||||
|
const currentRegionId = req.query.currentRegionId ? parseInt(req.query.currentRegionId, 10) : null;
|
||||||
if (Number.isNaN(productId) || Number.isNaN(currentPrice)) {
|
if (Number.isNaN(productId) || Number.isNaN(currentPrice)) {
|
||||||
throw new Error('productId and currentPrice are required');
|
throw new Error('productId and currentPrice are required');
|
||||||
}
|
}
|
||||||
return this.service.getProductPricesInCities(userId, productId, currentPrice);
|
return this.service.getProductPricesInCities(userId, productId, currentPrice, currentRegionId);
|
||||||
});
|
});
|
||||||
this.renovate = this._wrapWithUser((userId, req) => this.service.renovate(userId, req.body.element));
|
this.renovate = this._wrapWithUser((userId, req) => this.service.renovate(userId, req.body.element));
|
||||||
this.renovateAll = this._wrapWithUser((userId) => this.service.renovateAll(userId));
|
this.renovateAll = this._wrapWithUser((userId) => this.service.renovateAll(userId));
|
||||||
|
|||||||
@@ -3670,7 +3670,7 @@ class FalukantService extends BaseService {
|
|||||||
return { price };
|
return { price };
|
||||||
}
|
}
|
||||||
|
|
||||||
async getProductPricesInCities(hashedUserId, productId, currentPrice) {
|
async getProductPricesInCities(hashedUserId, productId, currentPrice, currentRegionId = null) {
|
||||||
const user = await this.getFalukantUserByHashedId(hashedUserId);
|
const user = await this.getFalukantUserByHashedId(hashedUserId);
|
||||||
const character = await FalukantCharacter.findOne({ where: { userId: user.id } });
|
const character = await FalukantCharacter.findOne({ where: { userId: user.id } });
|
||||||
if (!character) {
|
if (!character) {
|
||||||
@@ -3725,8 +3725,12 @@ class FalukantService extends BaseService {
|
|||||||
|
|
||||||
// Für jede Stadt den Preis berechnen und Branch-Typ bestimmen
|
// Für jede Stadt den Preis berechnen und Branch-Typ bestimmen
|
||||||
const results = [];
|
const results = [];
|
||||||
console.log(`[getProductPricesInCities] productId: ${productId}, currentPrice: ${currentPrice}, knowledgeFactor: ${knowledgeFactor}, product.sellCost: ${product.sellCost}, cities: ${cities.length}`);
|
|
||||||
for (const city of cities) {
|
for (const city of cities) {
|
||||||
|
// Aktuelle Stadt ausschließen
|
||||||
|
if (currentRegionId && city.id === currentRegionId) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Regionaler Preis-Faktor (worthPercent zwischen 40-60)
|
// Regionaler Preis-Faktor (worthPercent zwischen 40-60)
|
||||||
const worthPercent = worthMap.get(city.id) || 50; // Default 50% wenn nicht gefunden
|
const worthPercent = worthMap.get(city.id) || 50; // Default 50% wenn nicht gefunden
|
||||||
|
|
||||||
@@ -3738,11 +3742,9 @@ class FalukantService extends BaseService {
|
|||||||
const max = basePrice;
|
const max = basePrice;
|
||||||
const priceInCity = min + (max - min) * (knowledgeFactor / 100);
|
const priceInCity = min + (max - min) * (knowledgeFactor / 100);
|
||||||
|
|
||||||
console.log(`[getProductPricesInCities] City: ${city.name} (${city.id}), worthPercent: ${worthPercent}, basePrice: ${basePrice}, priceInCity: ${priceInCity}, currentPrice: ${currentPrice}, isBetter: ${priceInCity > currentPrice + 0.001}`);
|
|
||||||
|
|
||||||
// Nur Städte zurückgeben, wo der Preis höher ist
|
// Nur Städte zurückgeben, wo der Preis höher ist
|
||||||
// Verwende eine kleine Toleranz (0.001) um Rundungsfehler zu vermeiden
|
// Keine Toleranz, da wir die aktuelle Stadt bereits ausschließen
|
||||||
if (priceInCity > currentPrice + 0.001) {
|
if (priceInCity > currentPrice) {
|
||||||
// Branch-Typ bestimmen
|
// Branch-Typ bestimmen
|
||||||
let branchType = null; // null = kein Branch
|
let branchType = null; // null = kein Branch
|
||||||
if (city.branches && city.branches.length > 0) {
|
if (city.branches && city.branches.length > 0) {
|
||||||
@@ -3767,7 +3769,6 @@ class FalukantService extends BaseService {
|
|||||||
// Sortiere nach Preis (höchster zuerst)
|
// Sortiere nach Preis (höchster zuerst)
|
||||||
results.sort((a, b) => b.price - a.price);
|
results.sort((a, b) => b.price - a.price);
|
||||||
|
|
||||||
console.log(`[getProductPricesInCities] Returning ${results.length} cities with better prices`);
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,6 +53,7 @@
|
|||||||
products: { type: Array, required: true },
|
products: { type: Array, required: true },
|
||||||
calculateProductRevenue: { type: Function, required: true },
|
calculateProductRevenue: { type: Function, required: true },
|
||||||
calculateProductProfit: { type: Function, required: true },
|
calculateProductProfit: { type: Function, required: true },
|
||||||
|
currentRegionId: { type: Number, default: null },
|
||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
@@ -103,14 +104,13 @@
|
|||||||
this.loadingPrices.add(product.id);
|
this.loadingPrices.add(product.id);
|
||||||
try {
|
try {
|
||||||
const currentPrice = parseFloat(this.calculateProductRevenue(product).absolute);
|
const currentPrice = parseFloat(this.calculateProductRevenue(product).absolute);
|
||||||
console.log(`[RevenueSection] Loading prices for product ${product.id} (${product.labelTr}), currentPrice: ${currentPrice}`);
|
|
||||||
const { data } = await apiClient.get('/api/falukant/products/prices-in-cities', {
|
const { data } = await apiClient.get('/api/falukant/products/prices-in-cities', {
|
||||||
params: {
|
params: {
|
||||||
productId: product.id,
|
productId: product.id,
|
||||||
currentPrice: currentPrice
|
currentPrice: currentPrice,
|
||||||
|
currentRegionId: this.currentRegionId
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
console.log(`[RevenueSection] Received better prices for product ${product.id}:`, data);
|
|
||||||
this.$set(product, 'betterPrices', data || []);
|
this.$set(product, 'betterPrices', data || []);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error loading prices for product ${product.id}:`, error);
|
console.error(`Error loading prices for product ${product.id}:`, error);
|
||||||
|
|||||||
@@ -49,6 +49,7 @@
|
|||||||
:products="products"
|
:products="products"
|
||||||
:calculateProductRevenue="calculateProductRevenue"
|
:calculateProductRevenue="calculateProductRevenue"
|
||||||
:calculateProductProfit="calculateProductProfit"
|
:calculateProductProfit="calculateProductProfit"
|
||||||
|
:currentRegionId="selectedBranch?.regionId"
|
||||||
ref="revenueSection"
|
ref="revenueSection"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user