itemCacheService = $itemCacheService; } /** * 执行命令 */ public function handle() { $userId = (int)$this->argument('user_id'); $itemId = (int)$this->argument('item_id'); $oldQuantity = (int)$this->argument('old_quantity'); $newQuantity = (int)$this->argument('new_quantity'); $this->info('测试物品变更事件监听和缓存功能'); $this->info("用户ID: {$userId}"); $this->info("物品ID: {$itemId}"); $this->info("旧数量: {$oldQuantity}"); $this->info("新数量: {$newQuantity}"); // 清除之前的缓存 $this->itemCacheService->clearUserItemChanges($userId); $this->info('已清除之前的缓存'); // 触发物品数量变更事件 $event = new ItemQuantityChanged( $userId, $itemId, null, // instanceId $oldQuantity, $newQuantity, 1, // userItemId ['source_type' => 'test', 'source_id' => 1] ); Event::dispatch($event); $this->info('已触发物品数量变更事件'); // 等待事件处理完成 sleep(2); // 获取缓存数据 $itemChanges = $this->itemCacheService->getUserItemChanges($userId); if (empty($itemChanges)) { $this->error('缓存数据为空,事件监听可能未正常工作'); return 1; } $this->info('缓存数据:'); $this->table( ['物品ID', '旧数量', '新数量', '变化量'], collect($itemChanges)->map(function ($item) { return [ 'item_id' => $item['item_id'], 'old_quantity' => $item['old_quantity'], 'new_quantity' => $item['new_quantity'], 'change_amount' => $item['change_amount'], ]; })->toArray() ); $this->info('测试完成'); return 0; } }