| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace App\Module\Transfer\Commands;
- use App\Module\Transfer\Models\TransferOrder;
- use App\Module\Transfer\Enums\TransferStatus;
- use App\Module\Transfer\Jobs\SendCallbackJob;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- /**
- * 划转回调处理命令
- */
- class TransferCallbackCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'transfer:callback
- {--order-id= : 指定订单ID}
- {--status= : 指定订单状态}
- {--limit=100 : 处理数量限制}
- {--force : 强制发送回调,忽略状态检查}
- {--dry-run : 仅显示将要处理的订单,不实际发送}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '处理划转订单回调发送';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- $this->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()}");
- }
- }
- }
|