| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- namespace App\Module\Transfer\Commands;
- use App\Module\Transfer\Logics\OrderLogic;
- use App\Module\Transfer\Logics\CallbackLogic;
- use App\Module\Transfer\Models\TransferOrder;
- use App\Module\Transfer\Enums\TransferStatus;
- use App\Module\Transfer\Jobs\ProcessTransferOrderJob;
- use App\Module\Transfer\Jobs\SendCallbackJob;
- use Illuminate\Console\Command;
- /**
- * Transfer订单处理命令
- */
- class TransferProcessCommand extends Command
- {
- /**
- * 命令签名
- */
- protected $signature = 'transfer:process
- {--type=all : 处理类型 (all|orders|callbacks)}
- {--limit=100 : 处理数量限制}
- {--queue : 使用队列处理}
- {--dry-run : 仅显示将要处理的订单,不实际执行}';
- /**
- * 命令描述
- */
- protected $description = '处理Transfer模块的待处理订单和回调';
- /**
- * 执行命令
- */
- public function handle(): int
- {
- $type = $this->option('type');
- $limit = (int) $this->option('limit');
- $useQueue = $this->option('queue');
- $dryRun = $this->option('dry-run');
- $this->info("开始处理Transfer订单 (类型: {$type}, 限制: {$limit})");
- if ($dryRun) {
- $this->warn('*** 这是一次试运行,不会实际处理订单 ***');
- }
- $totalProcessed = 0;
- try {
- switch ($type) {
- case 'orders':
- $totalProcessed = $this->processOrders($limit, $useQueue, $dryRun);
- break;
- case 'callbacks':
- $totalProcessed = $this->processCallbacks($limit, $useQueue, $dryRun);
- break;
- case 'all':
- default:
- $ordersProcessed = $this->processOrders($limit, $useQueue, $dryRun);
- $callbacksProcessed = $this->processCallbacks($limit - $ordersProcessed, $useQueue, $dryRun);
- $totalProcessed = $ordersProcessed + $callbacksProcessed;
- break;
- }
- $this->info("处理完成,共处理 {$totalProcessed} 个项目");
- return 0;
- } catch (\Exception $e) {
- $this->error("处理失败: " . $e->getMessage());
- return 1;
- }
- }
- /**
- * 处理待处理的订单
- */
- private function processOrders(int $limit, bool $useQueue, bool $dryRun): int
- {
- $this->line('正在查找待处理的订单...');
- // 查找需要处理的订单
- $orders = TransferOrder::where('status', TransferStatus::PROCESSING)
- ->where('created_at', '>', now()->subHours(24))
- ->orderBy('created_at')
- ->limit($limit)
- ->get();
- if ($orders->isEmpty()) {
- $this->info('没有找到待处理的订单');
- return 0;
- }
- $this->info("找到 {$orders->count()} 个待处理的订单");
- if ($dryRun) {
- $this->table(
- ['ID', '类型', '状态', '应用', '金额', '创建时间'],
- $orders->map(function ($order) {
- return [
- $order->id,
- $order->type->getDescription(),
- $order->status->getDescription(),
- $order->transferApp->title,
- $order->amount,
- $order->created_at->format('Y-m-d H:i:s')
- ];
- })
- );
- return $orders->count();
- }
- $processed = 0;
- $progressBar = $this->output->createProgressBar($orders->count());
- $progressBar->start();
- foreach ($orders as $order) {
- try {
- if ($useQueue) {
- // 使用队列处理
- ProcessTransferOrderJob::dispatch($order->id, 'query');
- $this->line(" 订单 {$order->id} 已加入队列");
- } else {
- // 直接处理
- if ($order->isTransferOut()) {
- $success = OrderLogic::queryTransferOutStatus($order);
- } else {
- $success = OrderLogic::queryTransferInStatus($order);
- }
- if ($success) {
- $this->line(" 订单 {$order->id} 处理成功");
- } else {
- $this->line(" 订单 {$order->id} 处理失败");
- }
- }
- $processed++;
- $progressBar->advance();
- } catch (\Exception $e) {
- $this->error(" 订单 {$order->id} 处理异常: " . $e->getMessage());
- }
- }
- $progressBar->finish();
- $this->line('');
- return $processed;
- }
- /**
- * 处理待回调的订单
- */
- private function processCallbacks(int $limit, bool $useQueue, bool $dryRun): int
- {
- if ($limit <= 0) {
- return 0;
- }
- $this->line('正在查找待回调的订单...');
- // 查找需要回调的订单
- $orders = TransferOrder::where('status', TransferStatus::PROCESSING)
- ->whereHas('transferApp', function ($query) {
- $query->whereNotNull('order_callback_url');
- })
- ->where('created_at', '>', now()->subHours(24))
- ->where('created_at', '<', now()->subMinutes(5)) // 5分钟前创建的订单
- ->orderBy('created_at')
- ->limit($limit)
- ->get();
- if ($orders->isEmpty()) {
- $this->info('没有找到待回调的订单');
- return 0;
- }
- $this->info("找到 {$orders->count()} 个待回调的订单");
- if ($dryRun) {
- $this->table(
- ['ID', '类型', '状态', '回调URL', '创建时间'],
- $orders->map(function ($order) {
- return [
- $order->id,
- $order->type->getDescription(),
- $order->status->getDescription(),
- $order->transferApp->order_callback_url,
- $order->created_at->format('Y-m-d H:i:s')
- ];
- })
- );
- return $orders->count();
- }
- $processed = 0;
- $progressBar = $this->output->createProgressBar($orders->count());
- $progressBar->start();
- foreach ($orders as $order) {
- try {
- if ($useQueue) {
- // 使用队列处理
- SendCallbackJob::dispatch($order->id);
- $this->line(" 订单 {$order->id} 回调已加入队列");
- } else {
- // 直接处理
- $success = CallbackLogic::sendCallback($order);
-
- if ($success) {
- $this->line(" 订单 {$order->id} 回调发送成功");
- } else {
- $this->line(" 订单 {$order->id} 回调发送失败");
- }
- }
- $processed++;
- $progressBar->advance();
- } catch (\Exception $e) {
- $this->error(" 订单 {$order->id} 回调异常: " . $e->getMessage());
- }
- }
- $progressBar->finish();
- $this->line('');
- return $processed;
- }
- }
|