Files
boursecu_server/app/Services/ProductPriceSyncService.php
T
2026-06-11 19:23:16 +03:30

134 lines
4.2 KiB
PHP

<?php
namespace App\Services;
use App\Models\Product;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
/**
* همگام‌سازی قیمت محصولات متغیر از فید zaarbox و فید قدیمی.
*/
class ProductPriceSyncService
{
private const LEGACY_FEED_CACHE_KEY = 'legacy_price_list_feed';
private const LEGACY_FEED_CACHE_SECONDS = 25;
public function __construct(
private readonly ZaarboxPriceService $zaarboxPriceService,
private readonly ConfigService $configService
) {
}
/** @param bool $forceRefresh درخواست تازه از https://price.zaarbox.ir/api/free (کران) */
public function syncAll(bool $forceRefresh = false): bool
{
$synced = false;
try {
if ($this->zaarboxPriceService->syncProductsFromApi($forceRefresh)) {
$synced = true;
}
} catch (\Throwable $e) {
Log::warning('Zaarbox product sync failed', ['message' => $e->getMessage()]);
}
if ($this->syncFromLegacyFeed($forceRefresh)) {
$synced = true;
}
return $synced;
}
/** فید قدیمی — reading_method به شکل name (سازگاری با پروژه‌های قدیمی) */
private function syncFromLegacyFeed(bool $forceRefresh = false): bool
{
$RPrices = $this->cachedLegacyPriceList($forceRefresh);
if ($RPrices === null) {
return false;
}
$CP = null;
try {
$cooper = Http::timeout(8)->get('https://www.livedata.ir/main/static/p.json', [])->json();
if (isset($cooper['s_301006'], $cooper['s_200103'])) {
$CP = (($cooper['s_301006'] / 1000000) * $cooper['s_200103']) * 10;
}
} catch (\Throwable $e) {
// optional copper feed
}
$products = Product::query()->whereNotNull('reading_method')->get();
$currentTime = now()->format('H:i:s');
$synced = false;
foreach ($products as $product) {
$show = ($currentTime >= $product->auto_open_at && $currentTime <= $product->auto_close_at);
foreach ($RPrices as $RPrice) {
if ($product->reading_method == ($RPrice['name'] ?? null)) {
ProductPriceService::syncFromReference(
$product,
(int) ($RPrice['price'] ?? 0),
(int) ($RPrice['profit'] ?? 0)
);
$synced = true;
if ($product->bubble == 1) {
$liquidGoldPrice = app(ProductDisplayPriceService::class)->resolveAppBasePrice();
$p = Product::find($product->id);
if ($p && $liquidGoldPrice > 0) {
$p->amount = $RPrice['price'] / $liquidGoldPrice;
$p->show = $show;
$p->save();
}
}
}
}
if ($product->reading_method == 'مس' && $CP) {
ProductPriceService::syncFromReference($product, (int) $CP, 0);
Product::where('id', $product->id)->update(['show' => $show]);
$synced = true;
}
}
return $synced;
}
/** @return array<int, array<string, mixed>>|null */
private function cachedLegacyPriceList(bool $forceRefresh = false): ?array
{
if ($forceRefresh) {
Cache::forget(self::LEGACY_FEED_CACHE_KEY);
}
$cached = Cache::get(self::LEGACY_FEED_CACHE_KEY);
if (is_array($cached)) {
return $cached;
}
try {
$RPrices = Http::timeout(8)
->get('http://212.23.201.173:8381/price_list', [])
->json();
} catch (\Throwable $e) {
return null;
}
if (!is_array($RPrices)) {
return null;
}
Cache::put(
self::LEGACY_FEED_CACHE_KEY,
$RPrices,
now()->addSeconds(self::LEGACY_FEED_CACHE_SECONDS)
);
return $RPrices;
}
}