| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace App\Console\Commands;
- use App\Module\Farm\Services\CropService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class TestCropRemove extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'test:crop-remove {user_id} {land_id} {--tool_id=0}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '测试铲除作物功能,验证事件日志记录';
- /**
- * Execute the console command.
- */
- public function handle()
- {
- $userId = (int) $this->argument('user_id');
- $landId = (int) $this->argument('land_id');
- $toolId = (int) $this->option('tool_id');
- $this->info("开始测试铲除作物功能");
- $this->info("用户ID: {$userId}");
- $this->info("土地ID: {$landId}");
- $this->info("工具ID: {$toolId}");
- try {
- // 查询铲除前的作物信息
- $crop = \App\Module\Farm\Models\FarmCrop::where('land_id', $landId)
- ->where('user_id', $userId)
- ->first();
- if (!$crop) {
- $this->error("土地上没有作物");
- return 1;
- }
- $this->info("找到作物: ID={$crop->id}, 种子ID={$crop->seed_id}, 生长阶段={$crop->growth_stage->value}");
- // 查询铲除前的事件日志数量
- $logCountBefore = \App\Module\Farm\Models\FarmCropLog::where('crop_id', $crop->id)
- ->where('event_type', 'removed')
- ->count();
- $this->info("铲除前的铲除事件日志数量: {$logCountBefore}");
- // 开启事务
- DB::beginTransaction();
- // 调用铲除作物服务
- $result = CropService::removeCrop($userId, $landId, $toolId);
- if ($result['success']) {
- $this->info("铲除作物成功");
- $this->info("状态变更: " . ($result['status_changed'] ? '是' : '否'));
- $this->info("旧状态: {$result['old_status']}");
- $this->info("新状态: {$result['new_status']}");
- // 提交事务
- DB::commit();
- // 查询铲除后的事件日志数量
- $logCountAfter = \App\Module\Farm\Models\FarmCropLog::where('crop_id', $crop->id)
- ->where('event_type', 'removed')
- ->count();
- $this->info("铲除后的铲除事件日志数量: {$logCountAfter}");
- if ($logCountAfter > $logCountBefore) {
- $this->info("✅ 事件日志记录成功!");
-
- // 查询最新的铲除事件日志
- $latestLog = \App\Module\Farm\Models\FarmCropLog::where('crop_id', $crop->id)
- ->where('event_type', 'removed')
- ->orderBy('id', 'desc')
- ->first();
- if ($latestLog) {
- $this->info("最新事件日志详情:");
- $this->info("- ID: {$latestLog->id}");
- $this->info("- 事件类型: {$latestLog->event_type}");
- $this->info("- 生长阶段: {$latestLog->growth_stage}");
- $eventData = is_string($latestLog->event_data) ? json_decode($latestLog->event_data, true) : $latestLog->event_data;
- $this->info("- 事件数据: " . json_encode($eventData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
- $this->info("- 创建时间: {$latestLog->created_at}");
- }
- } else {
- $this->error("❌ 事件日志记录失败!");
- }
- } else {
- $this->error("铲除作物失败");
- DB::rollBack();
- return 1;
- }
- } catch (\Exception $e) {
- DB::rollBack();
- $this->error("测试过程中发生异常: " . $e->getMessage());
- $this->error("异常堆栈: " . $e->getTraceAsString());
- return 1;
- }
- $this->info("测试完成");
- return 0;
- }
- }
|