2026-06-17 12:12:35 +03:30
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Admin\ProductController;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
|
|
|
|
class PriceBroadcastService
|
|
|
|
|
{
|
2026-06-18 16:12:53 +03:30
|
|
|
private const LAST_HASH_KEY = 'price_broadcast:last_hash';
|
2026-06-17 12:12:35 +03:30
|
|
|
|
2026-06-18 16:12:53 +03:30
|
|
|
private const LIST_HASH_KEY = 'price_broadcast:list_hash';
|
2026-06-17 12:12:35 +03:30
|
|
|
|
2026-06-18 20:05:31 +03:30
|
|
|
private const LIST_SNAPSHOT_KEY = 'price_broadcast:list_snapshot';
|
|
|
|
|
|
|
|
|
|
private const LIST_SNAPSHOT_SECONDS = 8;
|
|
|
|
|
|
2026-06-17 12:12:35 +03:30
|
|
|
/**
|
2026-06-18 16:12:53 +03:30
|
|
|
* برادکست SSE — payload کامل last/list وقتی داده عوض شود.
|
2026-06-18 15:25:49 +03:30
|
|
|
*
|
2026-06-17 12:12:35 +03:30
|
|
|
* @param array<string, mixed>|null $lastPayload
|
|
|
|
|
*/
|
2026-06-18 15:25:49 +03:30
|
|
|
public function broadcastAfterPriceUpdate(?array $lastPayload = null, bool $force = false): void
|
2026-06-17 12:12:35 +03:30
|
|
|
{
|
2026-06-18 16:12:53 +03:30
|
|
|
$stream = app(PriceStreamService::class);
|
|
|
|
|
if (! $stream->isEnabled()) {
|
2026-06-18 15:25:49 +03:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-17 12:12:35 +03:30
|
|
|
if ($lastPayload === null) {
|
|
|
|
|
$cache = app(LivePriceCacheService::class);
|
|
|
|
|
$lastPayload = $cache->get() ?? $cache->buildFromDatabase();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 20:58:34 +03:30
|
|
|
if (is_array($lastPayload) && $lastPayload !== []) {
|
2026-08-25 02:29:00 +03:30
|
|
|
$lastPayload = app(ConfigService::class)->applyHomePriceDisplayState($lastPayload);
|
2026-07-12 20:58:34 +03:30
|
|
|
$lastPayload = app(ZaarboxPriceService::class)->attachGoldOunceToPayload($lastPayload);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-18 16:12:53 +03:30
|
|
|
if ($force) {
|
|
|
|
|
if (is_array($lastPayload) && $lastPayload !== []) {
|
|
|
|
|
$stream->publish('last', $lastPayload);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$listPayload = $this->buildProductListPayload();
|
|
|
|
|
if ($listPayload !== null) {
|
|
|
|
|
$stream->publish('list', $listPayload);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-18 15:25:49 +03:30
|
|
|
$lastChanged = is_array($lastPayload) && $lastPayload !== []
|
2026-09-05 10:58:43 +03:30
|
|
|
&& $this->rememberHashIfChanged($this->cacheKey(self::LAST_HASH_KEY), self::lastPriceIdentity($lastPayload));
|
2026-06-18 15:25:49 +03:30
|
|
|
|
2026-06-18 19:32:28 +03:30
|
|
|
if ($lastChanged && is_array($lastPayload)) {
|
2026-06-18 16:12:53 +03:30
|
|
|
$stream->publish('last', $lastPayload);
|
2026-06-18 19:44:23 +03:30
|
|
|
$this->publishListIfChanged(true);
|
|
|
|
|
} else {
|
|
|
|
|
$this->publishListIfChanged();
|
2026-06-18 16:12:53 +03:30
|
|
|
}
|
2026-06-18 19:32:28 +03:30
|
|
|
}
|
2026-06-18 16:12:53 +03:30
|
|
|
|
2026-06-18 19:44:23 +03:30
|
|
|
/**
|
|
|
|
|
* @param array<string, mixed> $payload
|
2026-08-25 02:29:00 +03:30
|
|
|
* @return array<string, int|bool|string>
|
2026-06-18 19:44:23 +03:30
|
|
|
*/
|
|
|
|
|
public static function lastPriceIdentity(array $payload): array
|
|
|
|
|
{
|
2026-07-12 20:58:34 +03:30
|
|
|
$goldOunce = is_array($payload['gold_ounce'] ?? null) ? $payload['gold_ounce'] : null;
|
|
|
|
|
|
2026-06-18 19:44:23 +03:30
|
|
|
return [
|
|
|
|
|
'sell' => (int) ($payload['sell'] ?? 0),
|
|
|
|
|
'buy' => (int) ($payload['buy'] ?? 0),
|
|
|
|
|
'price_last' => (int) ($payload['price_last'] ?? 0),
|
|
|
|
|
'price' => (int) ($payload['price'] ?? 0),
|
|
|
|
|
'closed' => (bool) ($payload['closed'] ?? false),
|
2026-08-25 02:29:00 +03:30
|
|
|
'status' => (string) ($payload['status'] ?? ''),
|
2026-06-18 19:44:23 +03:30
|
|
|
'main_product' => (int) ($payload['main_product'] ?? 0),
|
|
|
|
|
'montzar' => (int) ($payload['montzar'] ?? 0),
|
|
|
|
|
'zaarbox' => (int) ($payload['zaarbox'] ?? 0),
|
2026-07-12 20:58:34 +03:30
|
|
|
'gold_ounce_price' => round((float) ($goldOunce['price'] ?? 0), 2),
|
2026-06-18 19:44:23 +03:30
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-18 19:32:28 +03:30
|
|
|
public function broadcastLastIfChanged(?array $lastPayload): void
|
|
|
|
|
{
|
|
|
|
|
$stream = app(PriceStreamService::class);
|
|
|
|
|
if (! $stream->isEnabled()) {
|
|
|
|
|
return;
|
2026-06-17 12:12:35 +03:30
|
|
|
}
|
2026-06-18 19:32:28 +03:30
|
|
|
|
|
|
|
|
if (! is_array($lastPayload) || $lastPayload === []) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-25 02:29:00 +03:30
|
|
|
$lastPayload = app(ConfigService::class)->applyHomePriceDisplayState($lastPayload);
|
2026-07-12 20:58:34 +03:30
|
|
|
$lastPayload = app(ZaarboxPriceService::class)->attachGoldOunceToPayload($lastPayload);
|
|
|
|
|
|
2026-08-26 21:47:09 +03:30
|
|
|
// no_quote صفر را روی SSE پخش نکن — کلاینت مظنهٔ معتبر را پاک میکرد
|
|
|
|
|
// در حالی که /api/price/last هنوز قیمت داشت.
|
|
|
|
|
if (! app(ConfigService::class)->payloadHasDisplayablePrice($lastPayload)
|
|
|
|
|
&& (string) ($lastPayload['status'] ?? '') === 'no_quote'
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-05 10:58:43 +03:30
|
|
|
if (! $this->rememberHashIfChanged($this->cacheKey(self::LAST_HASH_KEY), self::lastPriceIdentity($lastPayload))) {
|
2026-06-18 19:32:28 +03:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$stream->publish('last', $lastPayload);
|
2026-06-18 19:44:23 +03:30
|
|
|
$this->publishListIfChanged(true);
|
2026-06-18 19:32:28 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-18 20:05:31 +03:30
|
|
|
public function buildListPayload(bool $allowCached = false): ?array
|
2026-06-18 19:44:23 +03:30
|
|
|
{
|
2026-06-18 20:05:31 +03:30
|
|
|
if ($allowCached) {
|
2026-09-05 10:58:43 +03:30
|
|
|
$cached = Cache::get($this->cacheKey(self::LIST_SNAPSHOT_KEY));
|
2026-06-18 20:05:31 +03:30
|
|
|
if (
|
|
|
|
|
is_array($cached)
|
|
|
|
|
&& is_array($cached['body'] ?? null)
|
|
|
|
|
&& isset($cached['stored_at'])
|
|
|
|
|
&& now()->diffInSeconds($cached['stored_at']) <= self::LIST_SNAPSHOT_SECONDS
|
|
|
|
|
) {
|
|
|
|
|
return $cached['body'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$body = $this->buildProductListPayload();
|
|
|
|
|
if ($body !== null && $body !== []) {
|
2026-09-05 10:58:43 +03:30
|
|
|
Cache::put($this->cacheKey(self::LIST_SNAPSHOT_KEY), [
|
2026-06-18 20:05:31 +03:30
|
|
|
'body' => $body,
|
|
|
|
|
'stored_at' => now(),
|
|
|
|
|
], now()->addSeconds(self::LIST_SNAPSHOT_SECONDS + 5));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $body;
|
2026-06-18 19:44:23 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function broadcastListIfChanged(bool $force = false): bool
|
2026-06-18 19:32:28 +03:30
|
|
|
{
|
|
|
|
|
$stream = app(PriceStreamService::class);
|
|
|
|
|
if (! $stream->isEnabled()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-18 19:44:23 +03:30
|
|
|
return $this->publishListIfChanged($force);
|
2026-06-18 19:32:28 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-18 19:44:23 +03:30
|
|
|
private function publishListIfChanged(bool $force = false): bool
|
2026-06-18 19:32:28 +03:30
|
|
|
{
|
|
|
|
|
$listPayload = $this->buildProductListPayload();
|
2026-06-18 19:44:23 +03:30
|
|
|
if ($listPayload === null || $listPayload === []) {
|
2026-06-18 19:32:28 +03:30
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-05 10:58:43 +03:30
|
|
|
if (! $force && ! $this->rememberHashIfChanged($this->cacheKey(self::LIST_HASH_KEY), self::listIdentity($listPayload))) {
|
2026-06-18 19:32:28 +03:30
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-18 19:44:23 +03:30
|
|
|
if ($force) {
|
2026-09-05 10:58:43 +03:30
|
|
|
$this->rememberHashIfChanged($this->cacheKey(self::LIST_HASH_KEY), self::listIdentity($listPayload));
|
2026-06-18 19:44:23 +03:30
|
|
|
}
|
|
|
|
|
|
2026-09-05 10:58:43 +03:30
|
|
|
Cache::forget($this->cacheKey(self::LIST_SNAPSHOT_KEY));
|
2026-06-18 19:32:28 +03:30
|
|
|
app(PriceStreamService::class)->publish('list', $listPayload);
|
|
|
|
|
|
|
|
|
|
return true;
|
2026-06-17 12:12:35 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-18 19:44:23 +03:30
|
|
|
/**
|
|
|
|
|
* @param array<int, array<string, mixed>> $listPayload
|
|
|
|
|
* @return array<int, array<string, int|bool>>
|
|
|
|
|
*/
|
2026-06-18 20:05:31 +03:30
|
|
|
public static function listIdentity(array $listPayload): array
|
2026-06-18 19:44:23 +03:30
|
|
|
{
|
|
|
|
|
$identity = [];
|
|
|
|
|
|
|
|
|
|
foreach ($listPayload as $product) {
|
2026-08-25 20:49:53 +03:30
|
|
|
if ($product instanceof \Illuminate\Database\Eloquent\Model) {
|
|
|
|
|
$product = $product->toArray();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-18 19:44:23 +03:30
|
|
|
if (! is_array($product)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-25 20:49:53 +03:30
|
|
|
$file = $product['file'] ?? null;
|
|
|
|
|
if ($file instanceof \Illuminate\Database\Eloquent\Model) {
|
|
|
|
|
$file = $file->toArray();
|
|
|
|
|
}
|
|
|
|
|
if (! is_array($file)) {
|
|
|
|
|
$file = [];
|
|
|
|
|
}
|
2026-08-25 16:14:46 +03:30
|
|
|
|
2026-06-18 19:44:23 +03:30
|
|
|
$identity[] = [
|
|
|
|
|
'id' => (int) ($product['id'] ?? 0),
|
2026-08-25 16:14:46 +03:30
|
|
|
'file_id' => (int) ($product['file_id'] ?? ($file['id'] ?? 0)),
|
2026-06-18 19:44:23 +03:30
|
|
|
'price' => (int) ($product['price'] ?? 0),
|
|
|
|
|
'price_profit' => (int) ($product['price_profit'] ?? 0),
|
2026-07-14 13:45:59 +03:30
|
|
|
'amount' => (float) ($product['amount'] ?? 0),
|
|
|
|
|
'app_price' => (bool) ($product['app_price'] ?? false),
|
|
|
|
|
'trade_by_quantity' => (bool) ($product['trade_by_quantity'] ?? false),
|
|
|
|
|
'direct_price' => (int) ($product['direct_price'] ?? 0),
|
|
|
|
|
'reading_method' => (string) ($product['reading_method'] ?? ''),
|
|
|
|
|
'price_offset_percent' => (float) ($product['price_offset_percent'] ?? 0),
|
2026-06-18 19:44:23 +03:30
|
|
|
'show' => (bool) ($product['show'] ?? true),
|
2026-09-05 12:58:14 +03:30
|
|
|
'trade_open' => (bool) ($product['trade_open'] ?? $product['show'] ?? true),
|
2026-06-18 19:44:23 +03:30
|
|
|
'active' => (bool) ($product['active'] ?? true),
|
2026-06-27 11:29:09 +03:30
|
|
|
'reference_provider_active' => (bool) ($product['reference_provider_active'] ?? false),
|
2026-06-18 19:44:23 +03:30
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $identity;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-17 12:12:35 +03:30
|
|
|
/**
|
2026-06-18 15:25:49 +03:30
|
|
|
* @param array<string, mixed>|array<int, mixed> $payload
|
2026-06-17 12:12:35 +03:30
|
|
|
*/
|
2026-09-05 10:58:43 +03:30
|
|
|
private function cacheKey(string $base): string
|
|
|
|
|
{
|
|
|
|
|
return $base.':'.app(PriceStreamService::class)->tenantScope();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-18 15:25:49 +03:30
|
|
|
private function rememberHashIfChanged(string $cacheKey, array $payload): bool
|
2026-06-17 12:12:35 +03:30
|
|
|
{
|
|
|
|
|
$hash = md5(json_encode($payload, JSON_UNESCAPED_UNICODE));
|
2026-06-18 15:25:49 +03:30
|
|
|
if (Cache::get($cacheKey) === $hash) {
|
|
|
|
|
return false;
|
2026-06-17 12:12:35 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-18 15:25:49 +03:30
|
|
|
Cache::put($cacheKey, $hash, now()->addHour());
|
2026-06-17 12:12:35 +03:30
|
|
|
|
2026-06-18 15:25:49 +03:30
|
|
|
return true;
|
2026-06-17 12:12:35 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array<int, mixed>|null
|
|
|
|
|
*/
|
|
|
|
|
private function buildProductListPayload(): ?array
|
|
|
|
|
{
|
2026-06-29 18:22:41 +03:30
|
|
|
if (app(ConfigService::class)->isStore()) {
|
2026-08-25 20:49:53 +03:30
|
|
|
return $this->normalizeListPayload(
|
|
|
|
|
app(ProductController::class)->customerStoreProductListPayload()
|
|
|
|
|
);
|
2026-06-29 18:22:41 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-17 12:12:35 +03:30
|
|
|
$request = Request::create('/customer/product/list', 'GET');
|
|
|
|
|
app()->instance('request', $request);
|
|
|
|
|
|
|
|
|
|
$response = app(ProductController::class)->listProduct();
|
|
|
|
|
$decoded = json_decode($response->getContent(), true);
|
|
|
|
|
|
|
|
|
|
return is_array($decoded) ? $decoded : null;
|
|
|
|
|
}
|
2026-08-25 20:49:53 +03:30
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* مدل/کالکشن Eloquent را به آرایهٔ خالص تبدیل میکند تا listIdentity و SSE
|
|
|
|
|
* همیشه file_id و URL تصویر را ببینند (قبلاً is_array روی Model شکست میخورد).
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $payload
|
|
|
|
|
* @return array<int, mixed>|null
|
|
|
|
|
*/
|
|
|
|
|
private function normalizeListPayload(mixed $payload): ?array
|
|
|
|
|
{
|
|
|
|
|
if ($payload === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($payload === []) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$encoded = json_encode($payload, JSON_UNESCAPED_UNICODE);
|
|
|
|
|
if ($encoded === false) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$decoded = json_decode($encoded, true);
|
|
|
|
|
|
|
|
|
|
return is_array($decoded) ? $decoded : null;
|
|
|
|
|
}
|
2026-06-17 12:12:35 +03:30
|
|
|
}
|