info('开始处理划转订单回调...'); try { // 获取命令参数 $orderId = $this->option('order-id'); $status = $this->option('status'); $limit = (int) $this->option('limit'); $force = $this->option('force'); $dryRun = $this->option('dry-run'); // 构建查询 $query = TransferOrder::with('transferApp'); if ($orderId) { $query->where('id', $orderId); } else { // 默认查询已完成但未发送回调的订单 if ($status) { $statusEnum = TransferStatus::tryFrom((int) $status); if (!$statusEnum) { $this->error("无效的状态值: {$status}"); return 1; } $query->where('status', $statusEnum); } else { $query->where('status', TransferStatus::COMPLETED); } // 只处理支持回调的应用 if (!$force) { $query->whereHas('transferApp', function ($q) { $q->whereNotNull('order_callback_url') ->where('order_callback_url', '!=', ''); }); // 排除已发送回调的订单 $query->whereNull('callback_at'); } $query->orderBy('created_at', 'asc')->limit($limit); } $orders = $query->get(); if ($orders->isEmpty()) { $this->info('没有找到需要处理的订单'); return 0; } $this->info("找到 {$orders->count()} 个订单需要处理"); if ($dryRun) { $this->displayOrders($orders); return 0; } // 处理订单 $successCount = 0; $failCount = 0; foreach ($orders as $order) { try { $this->processOrder($order, $force); $successCount++; $this->line("✓ 订单 {$order->id} 回调已加入队列"); } catch (\Exception $e) { $failCount++; $this->error("✗ 订单 {$order->id} 处理失败: {$e->getMessage()}"); Log::error('Transfer callback command error', [ 'order_id' => $order->id, 'error' => $e->getMessage() ]); } } $this->info("处理完成: 成功 {$successCount} 个,失败 {$failCount} 个"); return $failCount > 0 ? 1 : 0; } catch (\Exception $e) { $this->error("命令执行失败: {$e->getMessage()}"); Log::error('Transfer callback command failed', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return 1; } } /** * 处理单个订单 * * @param TransferOrder $order * @param bool $force * @return void */ protected function processOrder(TransferOrder $order, bool $force): void { // 检查应用是否支持回调 if (!$force && !$order->transferApp->supportsCallback()) { throw new \Exception('应用不支持回调'); } // 检查是否已发送回调 if (!$force && $order->callback_at) { throw new \Exception('回调已发送'); } // 发送回调任务 SendCallbackJob::dispatch($order); Log::info('Transfer callback job dispatched', [ 'order_id' => $order->id, 'force' => $force ]); } /** * 显示订单列表 * * @param \Illuminate\Support\Collection $orders * @return void */ protected function displayOrders($orders): void { $headers = ['订单ID', '类型', '状态', '金额', '用户ID', '应用', '创建时间', '回调时间']; $rows = []; foreach ($orders as $order) { $rows[] = [ $order->id, $order->type->getDescription(), $order->status->getDescription(), $order->amount, $order->user_id, $order->transferApp->title, $order->created_at->format('Y-m-d H:i:s'), $order->callback_at ? $order->callback_at->format('Y-m-d H:i:s') : '-' ]; } $this->table($headers, $rows); } /** * 获取状态选项说明 * * @return void */ protected function showStatusOptions(): void { $this->info('可用的状态值:'); foreach (TransferStatus::cases() as $status) { $this->line(" {$status->value} - {$status->getDescription()}"); } } }