ProcessTransferOrderJob.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace App\Module\Transfer\Jobs;
  3. use App\Module\Transfer\Logics\OrderLogic;
  4. use App\Module\Transfer\Models\TransferOrder;
  5. use App\Module\Transfer\Enums\TransferStatus;
  6. use App\Module\Transfer\Enums\TransferType;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Support\Facades\Log;
  13. /**
  14. * 处理划转订单任务
  15. */
  16. class ProcessTransferOrderJob implements ShouldQueue
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  19. private int $orderId;
  20. private string $action;
  21. /**
  22. * 任务最大尝试次数
  23. */
  24. public int $tries = 3;
  25. /**
  26. * 任务超时时间(秒)
  27. */
  28. public int $timeout = 120;
  29. /**
  30. * 创建任务实例
  31. */
  32. public function __construct(int $orderId, string $action = 'process')
  33. {
  34. $this->orderId = $orderId;
  35. $this->action = $action;
  36. // 设置队列名称
  37. $this->onQueue('transfer');
  38. }
  39. /**
  40. * 执行任务
  41. */
  42. public function handle(): void
  43. {
  44. try {
  45. $order = TransferOrder::find($this->orderId);
  46. if (!$order) {
  47. Log::warning('Transfer order not found for processing', [
  48. 'order_id' => $this->orderId,
  49. 'action' => $this->action
  50. ]);
  51. return;
  52. }
  53. Log::info('Processing transfer order', [
  54. 'order_id' => $this->orderId,
  55. 'action' => $this->action,
  56. 'type' => $order->type->value,
  57. 'status' => $order->status->value
  58. ]);
  59. // 根据动作类型执行不同的处理
  60. match ($this->action) {
  61. 'process' => $this->processOrder($order),
  62. 'query' => $this->queryOrderStatus($order),
  63. 'retry' => $this->retryOrder($order),
  64. default => Log::warning('Unknown action for transfer order', [
  65. 'order_id' => $this->orderId,
  66. 'action' => $this->action
  67. ])
  68. };
  69. } catch (\Exception $e) {
  70. Log::error('Transfer order processing failed', [
  71. 'order_id' => $this->orderId,
  72. 'action' => $this->action,
  73. 'error' => $e->getMessage(),
  74. 'trace' => $e->getTraceAsString()
  75. ]);
  76. // 重新抛出异常以触发重试机制
  77. throw $e;
  78. }
  79. }
  80. /**
  81. * 处理订单
  82. */
  83. private function processOrder(TransferOrder $order): void
  84. {
  85. // 检查订单状态
  86. if ($order->isFinalStatus()) {
  87. Log::info('Transfer order already in final status', [
  88. 'order_id' => $order->id,
  89. 'status' => $order->status->value
  90. ]);
  91. return;
  92. }
  93. // 根据订单类型处理
  94. if ($order->isTransferOut()) {
  95. $this->processTransferOut($order);
  96. } else {
  97. $this->processTransferIn($order);
  98. }
  99. }
  100. /**
  101. * 处理转出订单
  102. */
  103. private function processTransferOut(TransferOrder $order): void
  104. {
  105. $success = OrderLogic::processTransferOut($order);
  106. if ($success) {
  107. Log::info('Transfer out order processed successfully', [
  108. 'order_id' => $order->id
  109. ]);
  110. // 如果需要查询状态,延迟调度查询任务
  111. if ($order->status === TransferStatus::PROCESSING) {
  112. self::dispatch($order->id, 'query')
  113. ->delay(now()->addMinutes(2));
  114. }
  115. } else {
  116. Log::error('Transfer out order processing failed', [
  117. 'order_id' => $order->id
  118. ]);
  119. }
  120. }
  121. /**
  122. * 处理转入订单
  123. */
  124. private function processTransferIn(TransferOrder $order): void
  125. {
  126. // 转入订单主要是发送回调
  127. if ($order->transferApp->supportsCallback()) {
  128. SendCallbackJob::dispatch($order->id);
  129. } else {
  130. // 没有回调配置,直接完成
  131. $order->updateStatus(TransferStatus::COMPLETED);
  132. }
  133. }
  134. /**
  135. * 查询订单状态
  136. */
  137. private function queryOrderStatus(TransferOrder $order): void
  138. {
  139. if ($order->isTransferOut()) {
  140. $success = OrderLogic::queryTransferOutStatus($order);
  141. } else {
  142. $success = OrderLogic::queryTransferInStatus($order);
  143. }
  144. if ($success) {
  145. Log::info('Transfer order status queried successfully', [
  146. 'order_id' => $order->id,
  147. 'new_status' => $order->fresh()->status->value
  148. ]);
  149. } else {
  150. Log::warning('Transfer order status query failed', [
  151. 'order_id' => $order->id
  152. ]);
  153. // 如果查询失败且订单仍在处理中,安排下次查询
  154. if ($order->status === TransferStatus::PROCESSING) {
  155. self::dispatch($order->id, 'query')
  156. ->delay(now()->addMinutes(5));
  157. }
  158. }
  159. }
  160. /**
  161. * 重试订单
  162. */
  163. private function retryOrder(TransferOrder $order): void
  164. {
  165. if (!$order->canRetry()) {
  166. Log::warning('Transfer order cannot be retried', [
  167. 'order_id' => $order->id,
  168. 'status' => $order->status->value
  169. ]);
  170. return;
  171. }
  172. // 重置状态并重新处理
  173. $order->updateStatus(TransferStatus::CREATED);
  174. $this->processOrder($order);
  175. }
  176. /**
  177. * 任务失败时的处理
  178. */
  179. public function failed(\Throwable $exception): void
  180. {
  181. Log::error('Transfer order job failed permanently', [
  182. 'order_id' => $this->orderId,
  183. 'action' => $this->action,
  184. 'error' => $exception->getMessage(),
  185. 'attempts' => $this->attempts()
  186. ]);
  187. // 更新订单状态为失败
  188. $order = TransferOrder::find($this->orderId);
  189. if ($order && !$order->isFinalStatus()) {
  190. $order->updateStatus(TransferStatus::FAILED, '任务处理失败: ' . $exception->getMessage());
  191. }
  192. }
  193. /**
  194. * 获取任务的唯一ID
  195. */
  196. public function uniqueId(): string
  197. {
  198. return "transfer_order_{$this->orderId}_{$this->action}";
  199. }
  200. /**
  201. * 静态方法:调度处理订单任务
  202. */
  203. public static function scheduleProcess(int $orderId, int $delayMinutes = 0): void
  204. {
  205. $job = new self($orderId, 'process');
  206. if ($delayMinutes > 0) {
  207. $job->delay(now()->addMinutes($delayMinutes));
  208. }
  209. dispatch($job);
  210. }
  211. /**
  212. * 静态方法:调度查询状态任务
  213. */
  214. public static function scheduleQuery(int $orderId, int $delayMinutes = 2): void
  215. {
  216. self::dispatch($orderId, 'query')
  217. ->delay(now()->addMinutes($delayMinutes));
  218. }
  219. /**
  220. * 静态方法:调度重试任务
  221. */
  222. public static function scheduleRetry(int $orderId, int $delayMinutes = 1): void
  223. {
  224. self::dispatch($orderId, 'retry')
  225. ->delay(now()->addMinutes($delayMinutes));
  226. }
  227. }