Files
boursecu_server/app/Services/ProductPriceSyncService.php
T

260 lines
8.6 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
) {
}
2026-06-12 19:49:53 +03:30
/**
2026-06-12 20:10:14 +03:30
* فروشگاه (قطره / کشکول) — فید legacy + تطبیق reading_method با name
* + اعمال price_offset_percent روی قیمت ذخیره‌شده.
2026-06-12 19:49:53 +03:30
*/
public function syncStorePrices(bool $forceRefresh = true): bool
{
2026-06-13 12:42:42 +03:30
$zaarboxFeed = $this->zaarboxPriceService->cachedPriceList($forceRefresh);
$feed = $zaarboxFeed ?? $this->cachedLegacyPriceList($forceRefresh);
$source = $zaarboxFeed !== null ? 'zaarbox' : 'legacy';
$sourceUrl = $source === 'zaarbox'
? $this->zaarboxPriceService->apiUrl()
: 'http://212.23.201.173:8381/price_list';
2026-06-12 20:10:14 +03:30
if ($feed === null) {
2026-07-16 14:33:57 +03:30
// حتی بدون فید مرجع، محصولات قیمت‌اپ را با مظنه زنده همگام کن
return $this->syncAppPriceProducts();
2026-06-12 19:49:53 +03:30
}
$products = Product::query()->whereNotNull('reading_method')->get();
$liquidGoldPrice = (int) $this->configService->getInt('montzar_live_price', 0);
if ($liquidGoldPrice <= 0) {
$liquidGoldPrice = app(ProductDisplayPriceService::class)->resolveHistoricalBasePrice();
}
$synced = false;
2026-06-13 12:42:42 +03:30
$logItems = [];
2026-06-12 19:49:53 +03:30
foreach ($products as $product) {
2026-06-12 20:10:14 +03:30
$row = $this->zaarboxPriceService->findFeedRow($feed, (string) $product->reading_method);
if ($row === null) {
continue;
}
2026-06-12 19:49:53 +03:30
2026-06-12 20:10:14 +03:30
$row = $this->zaarboxPriceService->normalizeFeedRow($row);
$basePrice = (int) ($row['price'] ?? 0);
$baseProfit = (int) ($row['profit'] ?? 0);
2026-06-13 12:42:42 +03:30
$sourceName = trim((string) ($row['name'] ?? $product->reading_method));
2026-06-12 19:49:53 +03:30
2026-06-13 12:42:42 +03:30
$result = ProductPriceService::syncFromReference($product, $basePrice, $baseProfit, $sourceName);
if ($result !== null) {
$logItems[] = $result;
$synced = true;
}
2026-06-12 20:10:14 +03:30
if ((int) ($product->bubble ?? 0) === 1 && $liquidGoldPrice > 0 && $basePrice > 0) {
$p = Product::find($product->id);
if ($p) {
$p->amount = $basePrice / $liquidGoldPrice;
$p->save();
2026-06-12 19:49:53 +03:30
}
}
}
2026-06-13 12:42:42 +03:30
app(PriceSyncLogService::class)->record($source, $sourceUrl, $liquidGoldPrice, $logItems);
2026-07-16 14:33:57 +03:30
// مثل خانگی: آبشده / قیمت‌اپ را هم با مظنه زنده در DB همگام کن (fallback نمایش)
if ($this->syncAppPriceProducts()) {
$synced = true;
}
2026-06-18 19:32:28 +03:30
if ($synced) {
app(PriceBroadcastService::class)->broadcastListIfChanged();
}
2026-06-12 19:49:53 +03:30
return $synced;
}
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;
}
2026-06-18 19:32:28 +03:30
if ($synced) {
app(PriceBroadcastService::class)->broadcastListIfChanged();
}
2026-06-11 21:17:40 +03:30
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();
$synced = false;
foreach ($products as $product) {
2026-06-14 22:00:21 +03:30
$show = app(ConfigService::class)->isProductWithinTradingHours($product);
2026-06-10 18:25:05 +03:30
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
}