TestCropRemove.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Module\Farm\Services\CropService;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. class TestCropRemove extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'test:crop-remove {user_id} {land_id} {--tool_id=0}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '测试铲除作物功能,验证事件日志记录';
  21. /**
  22. * Execute the console command.
  23. */
  24. public function handle()
  25. {
  26. $userId = (int) $this->argument('user_id');
  27. $landId = (int) $this->argument('land_id');
  28. $toolId = (int) $this->option('tool_id');
  29. $this->info("开始测试铲除作物功能");
  30. $this->info("用户ID: {$userId}");
  31. $this->info("土地ID: {$landId}");
  32. $this->info("工具ID: {$toolId}");
  33. try {
  34. // 查询铲除前的作物信息
  35. $crop = \App\Module\Farm\Models\FarmCrop::where('land_id', $landId)
  36. ->where('user_id', $userId)
  37. ->first();
  38. if (!$crop) {
  39. $this->error("土地上没有作物");
  40. return 1;
  41. }
  42. $this->info("找到作物: ID={$crop->id}, 种子ID={$crop->seed_id}, 生长阶段={$crop->growth_stage->value}");
  43. // 查询铲除前的事件日志数量
  44. $logCountBefore = \App\Module\Farm\Models\FarmCropLog::where('crop_id', $crop->id)
  45. ->where('event_type', 'removed')
  46. ->count();
  47. $this->info("铲除前的铲除事件日志数量: {$logCountBefore}");
  48. // 开启事务
  49. DB::beginTransaction();
  50. // 调用铲除作物服务
  51. $result = CropService::removeCrop($userId, $landId, $toolId);
  52. if ($result['success']) {
  53. $this->info("铲除作物成功");
  54. $this->info("状态变更: " . ($result['status_changed'] ? '是' : '否'));
  55. $this->info("旧状态: {$result['old_status']}");
  56. $this->info("新状态: {$result['new_status']}");
  57. // 提交事务
  58. DB::commit();
  59. // 查询铲除后的事件日志数量
  60. $logCountAfter = \App\Module\Farm\Models\FarmCropLog::where('crop_id', $crop->id)
  61. ->where('event_type', 'removed')
  62. ->count();
  63. $this->info("铲除后的铲除事件日志数量: {$logCountAfter}");
  64. if ($logCountAfter > $logCountBefore) {
  65. $this->info("✅ 事件日志记录成功!");
  66. // 查询最新的铲除事件日志
  67. $latestLog = \App\Module\Farm\Models\FarmCropLog::where('crop_id', $crop->id)
  68. ->where('event_type', 'removed')
  69. ->orderBy('id', 'desc')
  70. ->first();
  71. if ($latestLog) {
  72. $this->info("最新事件日志详情:");
  73. $this->info("- ID: {$latestLog->id}");
  74. $this->info("- 事件类型: {$latestLog->event_type}");
  75. $this->info("- 生长阶段: {$latestLog->growth_stage}");
  76. $eventData = is_string($latestLog->event_data) ? json_decode($latestLog->event_data, true) : $latestLog->event_data;
  77. $this->info("- 事件数据: " . json_encode($eventData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
  78. $this->info("- 创建时间: {$latestLog->created_at}");
  79. }
  80. } else {
  81. $this->error("❌ 事件日志记录失败!");
  82. }
  83. } else {
  84. $this->error("铲除作物失败");
  85. DB::rollBack();
  86. return 1;
  87. }
  88. } catch (\Exception $e) {
  89. DB::rollBack();
  90. $this->error("测试过程中发生异常: " . $e->getMessage());
  91. $this->error("异常堆栈: " . $e->getTraceAsString());
  92. return 1;
  93. }
  94. $this->info("测试完成");
  95. return 0;
  96. }
  97. }