TransferProcessCommand.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace App\Module\Transfer\Commands;
  3. use App\Module\Transfer\Logics\OrderLogic;
  4. use App\Module\Transfer\Logics\CallbackLogic;
  5. use App\Module\Transfer\Models\TransferOrder;
  6. use App\Module\Transfer\Enums\TransferStatus;
  7. use App\Module\Transfer\Jobs\ProcessTransferOrderJob;
  8. use App\Module\Transfer\Jobs\SendCallbackJob;
  9. use Illuminate\Console\Command;
  10. /**
  11. * Transfer订单处理命令
  12. */
  13. class TransferProcessCommand extends Command
  14. {
  15. /**
  16. * 命令签名
  17. */
  18. protected $signature = 'transfer:process
  19. {--type=all : 处理类型 (all|orders|callbacks)}
  20. {--limit=100 : 处理数量限制}
  21. {--queue : 使用队列处理}
  22. {--dry-run : 仅显示将要处理的订单,不实际执行}';
  23. /**
  24. * 命令描述
  25. */
  26. protected $description = '处理Transfer模块的待处理订单和回调';
  27. /**
  28. * 执行命令
  29. */
  30. public function handle(): int
  31. {
  32. $type = $this->option('type');
  33. $limit = (int) $this->option('limit');
  34. $useQueue = $this->option('queue');
  35. $dryRun = $this->option('dry-run');
  36. $this->info("开始处理Transfer订单 (类型: {$type}, 限制: {$limit})");
  37. if ($dryRun) {
  38. $this->warn('*** 这是一次试运行,不会实际处理订单 ***');
  39. }
  40. $totalProcessed = 0;
  41. try {
  42. switch ($type) {
  43. case 'orders':
  44. $totalProcessed = $this->processOrders($limit, $useQueue, $dryRun);
  45. break;
  46. case 'callbacks':
  47. $totalProcessed = $this->processCallbacks($limit, $useQueue, $dryRun);
  48. break;
  49. case 'all':
  50. default:
  51. $ordersProcessed = $this->processOrders($limit, $useQueue, $dryRun);
  52. $callbacksProcessed = $this->processCallbacks($limit - $ordersProcessed, $useQueue, $dryRun);
  53. $totalProcessed = $ordersProcessed + $callbacksProcessed;
  54. break;
  55. }
  56. $this->info("处理完成,共处理 {$totalProcessed} 个项目");
  57. return 0;
  58. } catch (\Exception $e) {
  59. $this->error("处理失败: " . $e->getMessage());
  60. return 1;
  61. }
  62. }
  63. /**
  64. * 处理待处理的订单
  65. */
  66. private function processOrders(int $limit, bool $useQueue, bool $dryRun): int
  67. {
  68. $this->line('正在查找待处理的订单...');
  69. // 查找需要处理的订单
  70. $orders = TransferOrder::where('status', TransferStatus::PROCESSING)
  71. ->where('created_at', '>', now()->subHours(24))
  72. ->orderBy('created_at')
  73. ->limit($limit)
  74. ->get();
  75. if ($orders->isEmpty()) {
  76. $this->info('没有找到待处理的订单');
  77. return 0;
  78. }
  79. $this->info("找到 {$orders->count()} 个待处理的订单");
  80. if ($dryRun) {
  81. $this->table(
  82. ['ID', '类型', '状态', '应用', '金额', '创建时间'],
  83. $orders->map(function ($order) {
  84. return [
  85. $order->id,
  86. $order->type->getDescription(),
  87. $order->status->getDescription(),
  88. $order->transferApp->title,
  89. $order->amount,
  90. $order->created_at->format('Y-m-d H:i:s')
  91. ];
  92. })
  93. );
  94. return $orders->count();
  95. }
  96. $processed = 0;
  97. $progressBar = $this->output->createProgressBar($orders->count());
  98. $progressBar->start();
  99. foreach ($orders as $order) {
  100. try {
  101. if ($useQueue) {
  102. // 使用队列处理
  103. ProcessTransferOrderJob::dispatch($order->id, 'query');
  104. $this->line(" 订单 {$order->id} 已加入队列");
  105. } else {
  106. // 直接处理
  107. if ($order->isTransferOut()) {
  108. $success = OrderLogic::queryTransferOutStatus($order);
  109. } else {
  110. $success = OrderLogic::queryTransferInStatus($order);
  111. }
  112. if ($success) {
  113. $this->line(" 订单 {$order->id} 处理成功");
  114. } else {
  115. $this->line(" 订单 {$order->id} 处理失败");
  116. }
  117. }
  118. $processed++;
  119. $progressBar->advance();
  120. } catch (\Exception $e) {
  121. $this->error(" 订单 {$order->id} 处理异常: " . $e->getMessage());
  122. }
  123. }
  124. $progressBar->finish();
  125. $this->line('');
  126. return $processed;
  127. }
  128. /**
  129. * 处理待回调的订单
  130. */
  131. private function processCallbacks(int $limit, bool $useQueue, bool $dryRun): int
  132. {
  133. if ($limit <= 0) {
  134. return 0;
  135. }
  136. $this->line('正在查找待回调的订单...');
  137. // 查找需要回调的订单
  138. $orders = TransferOrder::where('status', TransferStatus::PROCESSING)
  139. ->whereHas('transferApp', function ($query) {
  140. $query->whereNotNull('order_callback_url');
  141. })
  142. ->where('created_at', '>', now()->subHours(24))
  143. ->where('created_at', '<', now()->subMinutes(5)) // 5分钟前创建的订单
  144. ->orderBy('created_at')
  145. ->limit($limit)
  146. ->get();
  147. if ($orders->isEmpty()) {
  148. $this->info('没有找到待回调的订单');
  149. return 0;
  150. }
  151. $this->info("找到 {$orders->count()} 个待回调的订单");
  152. if ($dryRun) {
  153. $this->table(
  154. ['ID', '类型', '状态', '回调URL', '创建时间'],
  155. $orders->map(function ($order) {
  156. return [
  157. $order->id,
  158. $order->type->getDescription(),
  159. $order->status->getDescription(),
  160. $order->transferApp->order_callback_url,
  161. $order->created_at->format('Y-m-d H:i:s')
  162. ];
  163. })
  164. );
  165. return $orders->count();
  166. }
  167. $processed = 0;
  168. $progressBar = $this->output->createProgressBar($orders->count());
  169. $progressBar->start();
  170. foreach ($orders as $order) {
  171. try {
  172. if ($useQueue) {
  173. // 使用队列处理
  174. SendCallbackJob::dispatch($order->id);
  175. $this->line(" 订单 {$order->id} 回调已加入队列");
  176. } else {
  177. // 直接处理
  178. $success = CallbackLogic::sendCallback($order);
  179. if ($success) {
  180. $this->line(" 订单 {$order->id} 回调发送成功");
  181. } else {
  182. $this->line(" 订单 {$order->id} 回调发送失败");
  183. }
  184. }
  185. $processed++;
  186. $progressBar->advance();
  187. } catch (\Exception $e) {
  188. $this->error(" 订单 {$order->id} 回调异常: " . $e->getMessage());
  189. }
  190. }
  191. $progressBar->finish();
  192. $this->line('');
  193. return $processed;
  194. }
  195. }