ThirdGameBalanceUsers.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\BalanceLog;
  4. use App\Services\ThirdGameBalanceService;
  5. use Carbon\Carbon;
  6. use Illuminate\Console\Command;
  7. class ThirdGameBalanceUsers extends Command
  8. {
  9. protected $signature = 'third-game:balance-users
  10. {--from= : 开始时间,例如 2026-07-14 10:00:00}
  11. {--to= : 结束时间,例如 2026-07-14 23:59:59}
  12. {--member_id=* : 只查询指定 member_id,可重复传入}';
  13. protected $description = '显示有三方游戏流水的玩家及当前三方总余额(只读,不回收余额)';
  14. public function handle(ThirdGameBalanceService $service): int
  15. {
  16. try {
  17. $from = $this->parseTimeOption('from');
  18. $to = $this->parseTimeOption('to');
  19. } catch (\InvalidArgumentException $e) {
  20. $this->error($e->getMessage());
  21. return self::FAILURE;
  22. }
  23. if ($from !== null && $to !== null && $from->greaterThan($to)) {
  24. $this->error('--from 不能晚于 --to');
  25. return self::FAILURE;
  26. }
  27. $query = BalanceLog::query()
  28. ->whereNotNull('member_id')
  29. ->where('member_id', '<>', '')
  30. ->whereIn('change_type', ['三方游戏转入', '三方游戏转出'])
  31. ->select('member_id');
  32. if ($from !== null) {
  33. $query->where('created_at', '>=', $from->format('Y-m-d H:i:s'));
  34. }
  35. if ($to !== null) {
  36. $query->where('created_at', '<=', $to->format('Y-m-d H:i:s'));
  37. }
  38. $specifiedMemberIds = array_values(array_filter(
  39. array_map('strval', (array) $this->option('member_id'))
  40. ));
  41. if ($specifiedMemberIds !== []) {
  42. $query->whereIn('member_id', $specifiedMemberIds);
  43. }
  44. $memberIds = $query->distinct()->orderBy('member_id')->pluck('member_id');
  45. $this->line("member_id\tthird_game_id\tthird_game_balance");
  46. $count = 0;
  47. foreach ($memberIds as $memberId) {
  48. $memberId = (string) $memberId;
  49. $detail = $service->detail($memberId, true);
  50. $balance = ($detail['ok'] ?? false)
  51. ? $this->decimal($detail['total_game_balance'] ?? 0)
  52. : '查询失败:' . (string) ($detail['msg'] ?? '未知错误');
  53. $this->line(implode("\t", [$memberId, $service->playerId($memberId), $balance]));
  54. $count++;
  55. }
  56. $this->info('共找到 ' . $count . ' 个玩家');
  57. $this->comment('本命令只读:已请求三方当前余额,未回收余额,未修改钱包或流水。');
  58. return self::SUCCESS;
  59. }
  60. private function parseTimeOption(string $name): ?Carbon
  61. {
  62. $value = trim((string) $this->option($name));
  63. if ($value === '') {
  64. return null;
  65. }
  66. try {
  67. return Carbon::createFromFormat('Y-m-d H:i:s', $value);
  68. } catch (\Throwable $e) {
  69. throw new \InvalidArgumentException('--' . $name . ' 格式必须为 YYYY-MM-DD HH:MM:SS');
  70. }
  71. }
  72. private function decimal($value): string
  73. {
  74. return bcadd(is_numeric($value) ? (string) $value : '0', '0', 10);
  75. }
  76. }