Files
boursecu_server/app/Services/ProductPriceSyncService.php
T

190 lines
6.0 KiB
PHP
Raw Normal View History

2026-06-10 18:25:05 +03:30
<?php
namespace App\Services;
use App\Models\Product;
2026-06-11 21:37:17 +03:30
use Illuminate\Support\Collection;
2026-06-11 16:09:52 +03:30
use Illuminate\Support\Facades\Cache;
2026-06-10 18:25:05 +03:30
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
/**
2026-06-10 18:38:01 +03:30
* همگام‌سازی قیمت محصولات متغیر از فید zaarbox و فید قدیمی.
2026-06-10 18:25:05 +03:30
*/
class ProductPriceSyncService
{
2026-06-11 16:09:52 +03:30
private const LEGACY_FEED_CACHE_KEY = 'legacy_price_list_feed';
private const LEGACY_FEED_CACHE_SECONDS = 25;
2026-06-10 18:38:01 +03:30
public function __construct(
2026-06-11 16:09:52 +03:30
private readonly ZaarboxPriceService $zaarboxPriceService,
private readonly ConfigService $configService
2026-06-10 18:38:01 +03:30
) {
}
/** @param bool $forceRefresh درخواست تازه از https://price.zaarbox.ir/api/free (کران) */
public function syncAll(bool $forceRefresh = false): bool
2026-06-10 18:25:05 +03:30
{
$synced = false;
try {
2026-06-10 18:38:01 +03:30
if ($this->zaarboxPriceService->syncProductsFromApi($forceRefresh)) {
2026-06-10 18:25:05 +03:30
$synced = true;
}
} catch (\Throwable $e) {
Log::warning('Zaarbox product sync failed', ['message' => $e->getMessage()]);
}
2026-06-11 16:09:52 +03:30
if ($this->syncFromLegacyFeed($forceRefresh)) {
2026-06-10 18:25:05 +03:30
$synced = true;
}
2026-06-11 21:17:40 +03:30
if ($this->syncAppPriceProducts()) {
$synced = true;
}
return $synced;
}
/** محصولات آبشده با مرجع «قیمت اپ» — ذخیره آخرین قیمت در DB برای fallback */
private function syncAppPriceProducts(): bool
{
$display = app(ProductDisplayPriceService::class);
$products = Product::query()
->where('active', 1)
->get()
->filter(static fn (Product $product) => $display->usesAppPrice($product));
if ($products->isEmpty()) {
return false;
}
$basePrice = $display->resolveAppBasePrice();
if ($basePrice <= 0) {
2026-06-11 21:37:17 +03:30
return $this->restoreAppProductsFromHistorical($products);
2026-06-11 21:17:40 +03:30
}
$synced = false;
foreach ($products as $product) {
ProductPriceService::syncFromReference($product, $basePrice, 0);
$synced = true;
}
2026-06-10 18:25:05 +03:30
return $synced;
}
2026-06-11 21:37:17 +03:30
/** provider صفر ولی use_last_price_if_zero — بازیابی از جدول prices به products.price */
private function restoreAppProductsFromHistorical(Collection $products): bool
{
$historical = app(ProductDisplayPriceService::class)->resolveHistoricalBasePrice();
if ($historical <= 0) {
return false;
}
$synced = false;
foreach ($products as $product) {
if (!Product::hasUseLastPriceIfZeroColumn() || !($product->use_last_price_if_zero ?? false)) {
continue;
}
if ((int) ($product->price ?? 0) > 0) {
continue;
}
ProductPriceService::syncFromReference($product, $historical, 0);
$synced = true;
}
return $synced;
}
2026-06-10 18:25:05 +03:30
/** فید قدیمی — reading_method به شکل name (سازگاری با پروژه‌های قدیمی) */
2026-06-11 16:09:52 +03:30
private function syncFromLegacyFeed(bool $forceRefresh = false): bool
2026-06-10 18:25:05 +03:30
{
2026-06-11 16:09:52 +03:30
$RPrices = $this->cachedLegacyPriceList($forceRefresh);
if ($RPrices === null) {
2026-06-10 18:25:05 +03:30
return false;
}
$CP = null;
try {
2026-06-11 16:09:52 +03:30
$cooper = Http::timeout(8)->get('https://www.livedata.ir/main/static/p.json', [])->json();
2026-06-10 18:25:05 +03:30
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) {
2026-06-11 19:23:16 +03:30
$liquidGoldPrice = app(ProductDisplayPriceService::class)->resolveAppBasePrice();
2026-06-10 18:25:05 +03:30
$p = Product::find($product->id);
2026-06-11 19:23:16 +03:30
if ($p && $liquidGoldPrice > 0) {
$p->amount = $RPrice['price'] / $liquidGoldPrice;
2026-06-10 18:25:05 +03:30
$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;
}
2026-06-11 16:09:52 +03:30
/** @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;
}
2026-06-10 18:25:05 +03:30
}