ThirdGameCommissionBackfill.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\ThirdGameCommissionService;
  4. use App\Support\SyncCommandLock;
  5. use Illuminate\Console\Command;
  6. class ThirdGameCommissionBackfill extends Command
  7. {
  8. protected $signature = 'third-game:backfill-commission {--retry-missing : 同时按当前费率补录上线后仍缺费率的订单}';
  9. protected $description = '在本地补录三方游戏抽成,已保存的费率保持固定';
  10. public function handle(ThirdGameCommissionService $service, SyncCommandLock $lock): int
  11. {
  12. if (!$service->migrated()) {
  13. // 定时任务在迁移前也会触发;未迁移时没有可补录的范围,跳过而不是每次报错。
  14. $this->info('三方抽成迁移尚未执行,跳过补录');
  15. return self::SUCCESS;
  16. }
  17. $exit = $lock->run('third-game:backfill-commission', function () use ($service) {
  18. // 历史订单(上线前)按上线费率快照补录;定时任务也会执行,直到历史订单补完,部署后无需手动运行。
  19. $exit = self::SUCCESS;
  20. try {
  21. $history = $service->backfill(false);
  22. $this->info(sprintf('历史订单:已补录 %d 笔,仍缺费率 %d 笔', $history['updated'], $history['remaining']));
  23. } catch (\RuntimeException $e) {
  24. $this->error('历史订单:' . $e->getMessage());
  25. $exit = self::FAILURE;
  26. }
  27. if ($this->option('retry-missing')) {
  28. $result = $service->backfill(true);
  29. $this->info(sprintf('上线后订单:已补录 %d 笔,仍缺费率 %d 笔', $result['updated'], $result['remaining']));
  30. }
  31. return $exit;
  32. });
  33. if ($exit === null) {
  34. $this->info('三方抽成补录正在运行');
  35. }
  36. return $exit ?? self::SUCCESS;
  37. }
  38. }