TestConsumeCommand.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Module\Game\Commands;
  3. use App\Module\Game\Services\ConsumeService;
  4. use Illuminate\Console\Command;
  5. /**
  6. * 测试消耗组功能的命令
  7. */
  8. class TestConsumeCommand extends Command
  9. {
  10. /**
  11. * 命令名称
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'game:test-consume {user_id} {consume_group} {--check : 只检查不执行消耗}';
  16. /**
  17. * 命令描述
  18. *
  19. * @var string
  20. */
  21. protected $description = '测试消耗组功能';
  22. /**
  23. * 执行命令
  24. *
  25. * @return int
  26. */
  27. public function handle()
  28. {
  29. $userId = (int) $this->argument('user_id');
  30. $consumeGroup = $this->argument('consume_group');
  31. $checkOnly = $this->option('check');
  32. $this->info("测试消耗组功能");
  33. $this->info("用户ID: {$userId}");
  34. $this->info("消耗组: {$consumeGroup}");
  35. $this->info("模式: " . ($checkOnly ? '只检查' : '检查并执行'));
  36. if ($checkOnly) {
  37. // 只检查不执行
  38. $result = ConsumeService::checkConsume($userId, $consumeGroup);
  39. } else {
  40. // 检查并执行
  41. $result = ConsumeService::executeConsume($userId, $consumeGroup, 'test_command');
  42. }
  43. // 处理 Res 对象或数组结果
  44. $success = is_object($result) ? $result->success : $result['success'];
  45. $message = is_object($result) ? $result->message : $result['message'];
  46. $data = is_object($result) ? $result->data : $result;
  47. if ($success) {
  48. $this->info("结果: 成功");
  49. $this->info("消息: {$message}");
  50. } else {
  51. $this->error("结果: 失败");
  52. $this->error("消息: {$message}");
  53. }
  54. // 输出详细信息
  55. $this->line('');
  56. $this->line('详细信息:');
  57. if (is_object($result)) {
  58. // Res 对象
  59. if (!empty($result->data)) {
  60. foreach ($result->data as $key => $value) {
  61. if (is_array($value)) {
  62. $this->line("{$key}: " . json_encode($value, JSON_UNESCAPED_UNICODE));
  63. } else {
  64. $this->line("{$key}: {$value}");
  65. }
  66. }
  67. }
  68. } else {
  69. // 数组结果
  70. unset($data['success']);
  71. unset($data['message']);
  72. foreach ($data as $key => $value) {
  73. if (is_array($value)) {
  74. $this->line("{$key}: " . json_encode($value, JSON_UNESCAPED_UNICODE));
  75. } else {
  76. $this->line("{$key}: {$value}");
  77. }
  78. }
  79. }
  80. return 0;
  81. }
  82. }