From efd48c12a9601bd3dcc4355234056c170ce7eb45 Mon Sep 17 00:00:00 2001 From: adonis Date: Wed, 10 Jun 2026 18:25:05 +0330 Subject: [PATCH] added merge wallet 4 --- app/Console/Commands/SyncProductPrices.php | 20 +++++ app/Console/Commands/UpdateProductStatus.php | 78 ++-------------- app/Console/Kernel.php | 18 ++-- app/Services/ProductPriceSyncService.php | 95 ++++++++++++++++++++ 4 files changed, 127 insertions(+), 84 deletions(-) create mode 100644 app/Console/Commands/SyncProductPrices.php create mode 100644 app/Services/ProductPriceSyncService.php diff --git a/app/Console/Commands/SyncProductPrices.php b/app/Console/Commands/SyncProductPrices.php new file mode 100644 index 0000000..16a7333 --- /dev/null +++ b/app/Console/Commands/SyncProductPrices.php @@ -0,0 +1,20 @@ +syncAll(); + + return self::SUCCESS; + } +} diff --git a/app/Console/Commands/UpdateProductStatus.php b/app/Console/Commands/UpdateProductStatus.php index f1f5fa6..698faf6 100644 --- a/app/Console/Commands/UpdateProductStatus.php +++ b/app/Console/Commands/UpdateProductStatus.php @@ -1,87 +1,21 @@ info('Starting update...'); - - // 0. فید zaarbox — reading_method به شکل key (مثل 1_29_5) با این فید sync می‌شود - try { - app(\App\Services\ZaarboxPriceService::class)->syncProductsFromApi(); - } catch (\Throwable $e) { - \Illuminate\Support\Facades\Log::warning('Zaarbox product sync failed', ['message' => $e->getMessage()]); - } - - // 1. فید قدیمی — reading_method به شکل name (سازگاری با پروژه‌های قدیمی) - try { - $RPrices = Http::timeout(40)->get('http://212.23.201.173:8381/price_list', [])->json(); - } catch (\Throwable $e) { - $RPrices = null; - } - - $CP = null; - try{ - $cooper =Http::timeout(40)->get('https://www.livedata.ir/main/static/p.json', [])->json(); - if(isset($cooper['s_301006']) && isset($cooper['s_200103'])){ - $CP = (($cooper['s_301006']/1000000)*$cooper['s_200103'])*10; -// Cnt::where('key', 'cu_price') - } - }catch (\Exception $e){} - $products = Product::with([])->whereNotNull('reading_method')->get(); - $current_time = now()->format('H:i:s'); - - foreach ($products as $product) { - $show = ($current_time >= $product->auto_open_at && $current_time <= $product->auto_close_at); - - foreach (is_array($RPrices) ? $RPrices : [] as $RPrice) { - if ($product->reading_method == $RPrice['name']) { - ProductPriceService::syncFromReference( - $product, - (int) $RPrice['price'], - (int) ($RPrice['profit'] ?? 0) - ); - - if($product->bubble== 1){ - $ReportController = new ReportController(); - $liquidGold= $ReportController->lastPrice(); - $p = Product::find($product->id); - - $p->amount = $RPrice['price'] / $liquidGold['price']; - $p->show = $show; - $p->save(); - - } - } - } - - if ($product->reading_method == 'مس' && $CP) { - ProductPriceService::syncFromReference($product, (int) $CP, 0); - Product::where('id', $product->id)->update(['show' => $show]); - } - } - // 2. Auto Close/Open Logic - // $this->auto_close_open(); -> کد داخل این تابع را اینجا قرار دهید - + $syncService->syncAll(); $this->info('Product statuses updated successfully.'); + + return self::SUCCESS; } } diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 1e080be..1f83475 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -19,21 +19,15 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - // $schedule->command('inspire')->hourly(); + // همگام‌سازی قیمت محصولات متغیر (reading_method) — هر ۳۰ ثانیه $schedule->call(function () { - // حلقه برای اجرای 6 بار در یک دقیقه (هر 10 ثانیه) - for ($i = 0; $i < 6; $i++) { -// \Illuminate\Support\Facades\Log::info("Update process ran at: " . now()); - - // فراخوانی کامند ما - \Illuminate\Support\Facades\Artisan::call('product:update-status'); - - // 10 ثانیه صبر کن (به جز دور آخر که نیاز نیست اسکریپت باز بماند) - if ($i < 5) { - sleep(10); + for ($i = 0; $i < 2; $i++) { + \Illuminate\Support\Facades\Artisan::call('product:sync-prices'); + if ($i < 1) { + sleep(30); } } - })->everyMinute()->name('update_products_every_20s')->withoutOverlapping(); + })->everyMinute()->name('sync_product_prices_30s')->withoutOverlapping(); $schedule->command('prices:refresh-zaarbox') ->everyThirtyMinutes() diff --git a/app/Services/ProductPriceSyncService.php b/app/Services/ProductPriceSyncService.php new file mode 100644 index 0000000..8bc10d2 --- /dev/null +++ b/app/Services/ProductPriceSyncService.php @@ -0,0 +1,95 @@ +syncProductsFromApi()) { + $synced = true; + } + } catch (\Throwable $e) { + Log::warning('Zaarbox product sync failed', ['message' => $e->getMessage()]); + } + + if ($this->syncFromLegacyFeed()) { + $synced = true; + } + + return $synced; + } + + /** فید قدیمی — reading_method به شکل name (سازگاری با پروژه‌های قدیمی) */ + private function syncFromLegacyFeed(): bool + { + try { + $RPrices = Http::timeout(40)->get('http://212.23.201.173:8381/price_list', [])->json(); + } catch (\Throwable $e) { + return false; + } + + if (!is_array($RPrices)) { + return false; + } + + $CP = null; + try { + $cooper = Http::timeout(40)->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) { + $reportController = new ReportController(); + $liquidGold = $reportController->lastPrice(); + $p = Product::find($product->id); + if ($p && ($liquidGold['price'] ?? 0) > 0) { + $p->amount = $RPrice['price'] / $liquidGold['price']; + $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; + } +}