fix price force refresh 2

This commit is contained in:
2026-06-14 12:33:29 +03:30
parent 1abddfbab8
commit db0cc5f1a6
8 changed files with 246 additions and 120 deletions
+20 -29
View File
@@ -37,19 +37,15 @@ class LivePriceCacheService
return $body;
}
/** پاسخ سریع بدون API خارجی — از آخرین رکورد prices + تنظیمات */
/** پاسخ سریع بدون API خارجی — از آخرین رکورد prices + تنظیمات (همان فرمول lastPrice) */
public function buildFromDatabase(): array
{
$config = app(ConfigService::class);
$display = app(ProductDisplayPriceService::class);
$last = Price::orderByDesc('id')->first();
$base = $last ? (int) $last->price : 0;
$base = $last ? (float) $last->price : 0.0;
$provider = $config->priceProvider();
$montzarPrice = 0;
$zaarboxPrice = 0;
$mainProductPrice = 0;
if ($base <= 0) {
$payload = [
'sell' => 0,
@@ -63,35 +59,32 @@ class LivePriceCacheService
'time' => now(),
];
} else {
$diffBuy = (int) $config->displayBuySpreadRial();
$diffSell = (int) $config->displaySellSpreadRial();
$openBuy = true;
$openSell = true;
$montzarSource = 0;
$zaarboxSource = 0;
$mainProductSource = 0;
$priceSource = 'database_last';
if ($provider === 'montzar') {
$montzarPrice = $config->getInt('montzar_live_price', 0) ?: $base;
$montzarDiff = (int) $config->getInt('montzar_diff', 0);
$diffBuy += $montzarDiff;
$diffSell += $montzarDiff;
$openBuy = $config->getBool('montzar_open_buy', true);
$openSell = $config->getBool('montzar_open_sell', true);
$liveMontzar = (float) $config->get('montzar_live_price', 0);
$montzarSource = (int) ($liveMontzar > 0 ? $liveMontzar : $base);
// DB از آخرین tick montzar پر شده — همان فرمول نمایش معامله
$priceSource = 'montzar';
} elseif ($provider === 'zaarbox') {
$zaarboxPrice = $base;
$zaarboxSource = (int) $base;
} elseif ($provider === 'main_product') {
$mainProductPrice = $base;
$mainProductSource = (int) $base;
}
$buy = $openBuy ? (int) ceil($base + $diffBuy) : 0;
$sell = $openSell ? max(0, (int) ceil($base - $diffSell)) : 0;
$trade = $config->buildTradeDisplayPrices($base, $priceSource);
$payload = [
'sell' => $sell,
'buy' => $buy,
'price_last' => $buy,
'price' => $base,
'main_product' => $mainProductPrice,
'montzar' => $montzarPrice,
'zaarbox' => $zaarboxPrice,
'sell' => $trade['sell'],
'buy' => $trade['buy'],
'price_last' => $trade['priceLast'],
'price' => (int) $base,
'main_product' => $mainProductSource,
'montzar' => $montzarSource,
'zaarbox' => $zaarboxSource,
'closed' => false,
'time' => now(),
];
@@ -105,8 +98,6 @@ class LivePriceCacheService
: ['sell' => $payload['sell'] ?? 0, 'buy' => $payload['buy'] ?? 0];
$payload['last_historical'] = $display->resolveHistoricalBasePrice();
$display->cacheLivePricePayload($payload);
return $payload;
}
}