ThirdGameOrderSync.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\ThirdGameOrderService;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Log;
  6. class ThirdGameOrderSync extends Command
  7. {
  8. protected $signature = 'third-game:sync-orders';
  9. protected $description = '同步 api-bet 最新10分钟(不含当前分钟)的实时游戏订单';
  10. public function handle(ThirdGameOrderService $service): int
  11. {
  12. $page = 1;
  13. $synced = 0;
  14. do {
  15. try {
  16. $result = $service->syncRealtime($page, 2000);
  17. } catch (\Throwable $e) {
  18. Log::error('third_game_sync_orders_failed', ['page' => $page, 'exception' => $e]);
  19. $this->error($e->getMessage());
  20. return self::FAILURE;
  21. }
  22. if (empty($result['ok'])) {
  23. $message = (string) ($result['msg'] ?? '三方实时订单同步失败');
  24. // The provider allows one realtime query per minute and the admin list shares that quota. Each
  25. // query covers the last 10 minutes, so a throttled run is skipped and the next run catches up.
  26. if (!empty($result['throttled'])) {
  27. $this->warn("page={$page} skipped: {$message}");
  28. return self::SUCCESS;
  29. }
  30. Log::error('third_game_sync_orders_failed', ['page' => $page, 'message' => $message]);
  31. $this->error($message);
  32. return self::FAILURE;
  33. }
  34. $synced += (int) ($result['synced'] ?? 0);
  35. $this->line(sprintf(
  36. 'page=%d received=%d synced=%d unmatched=%d',
  37. $page,
  38. (int) ($result['received'] ?? 0),
  39. (int) ($result['synced'] ?? 0),
  40. (int) ($result['unmatched_orders'] ?? 0)
  41. ));
  42. if (empty($result['has_more'])) {
  43. break;
  44. }
  45. if ($page >= 1000) {
  46. $this->error('实时订单分页超过1000页,已停止同步');
  47. return self::FAILURE;
  48. }
  49. $page++;
  50. } while (true);
  51. $this->info("实时订单同步完成,共写入或更新 {$synced} 条");
  52. return self::SUCCESS;
  53. }
  54. }