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; } 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(); $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>|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; } }