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'; if ($feed === null) { // حتی بدون فید مرجع، محصولات قیمت‌اپ را با مظنه زنده همگام کن return $this->syncAppPriceProducts(); } $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; $logItems = []; foreach ($products as $product) { $row = $this->zaarboxPriceService->findFeedRow($feed, (string) $product->reading_method); if ($row === null) { continue; } $row = $this->zaarboxPriceService->normalizeFeedRow($row); $basePrice = (int) ($row['price'] ?? 0); $baseProfit = (int) ($row['profit'] ?? 0); $sourceName = trim((string) ($row['name'] ?? $product->reading_method)); $result = ProductPriceService::syncFromReference($product, $basePrice, $baseProfit, $sourceName); if ($result !== null) { $logItems[] = $result; $synced = true; } if ((int) ($product->bubble ?? 0) === 1 && $liquidGoldPrice > 0 && $basePrice > 0) { $p = Product::find($product->id); if ($p) { $p->amount = $basePrice / $liquidGoldPrice; $p->save(); } } } app(PriceSyncLogService::class)->record($source, $sourceUrl, $liquidGoldPrice, $logItems); // مثل خانگی: آبشده / قیمت‌اپ را هم با مظنه زنده در DB همگام کن (fallback نمایش) if ($this->syncAppPriceProducts()) { $synced = true; } if ($synced) { app(PriceBroadcastService::class)->broadcastListIfChanged(); } return $synced; } /** @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; } if ($this->syncAppPriceProducts()) { $synced = true; } if ($synced) { app(PriceBroadcastService::class)->broadcastListIfChanged(); } 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) { return $this->restoreAppProductsFromHistorical($products); } $synced = false; foreach ($products as $product) { ProductPriceService::syncFromReference($product, $basePrice, 0); $synced = true; } return $synced; } /** 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; } /** فید قدیمی — 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(); $synced = false; foreach ($products as $product) { $show = app(ConfigService::class)->isProductWithinTradingHours($product); 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>|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; } }