TransferOrderCompleted.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Module\Transfer\Events;
  3. use App\Module\Transfer\Models\TransferOrder;
  4. use App\Module\Transfer\Enums\TransferStatus;
  5. use Illuminate\Broadcasting\InteractsWithSockets;
  6. use Illuminate\Foundation\Events\Dispatchable;
  7. use Illuminate\Queue\SerializesModels;
  8. /**
  9. * 划转订单完成事件
  10. */
  11. class TransferOrderCompleted
  12. {
  13. use Dispatchable, InteractsWithSockets, SerializesModels;
  14. public TransferOrder $order;
  15. public TransferStatus $previousStatus;
  16. public array $context;
  17. /**
  18. * 创建事件实例
  19. */
  20. public function __construct(TransferOrder $order, TransferStatus $previousStatus, array $context = [])
  21. {
  22. $this->order = $order;
  23. $this->previousStatus = $previousStatus;
  24. $this->context = $context;
  25. }
  26. /**
  27. * 获取订单信息
  28. */
  29. public function getOrder(): TransferOrder
  30. {
  31. return $this->order;
  32. }
  33. /**
  34. * 获取之前的状态
  35. */
  36. public function getPreviousStatus(): TransferStatus
  37. {
  38. return $this->previousStatus;
  39. }
  40. /**
  41. * 获取上下文信息
  42. */
  43. public function getContext(): array
  44. {
  45. return $this->context;
  46. }
  47. /**
  48. * 判断是否为转入订单
  49. */
  50. public function isTransferIn(): bool
  51. {
  52. return $this->order->isTransferIn();
  53. }
  54. /**
  55. * 判断是否为转出订单
  56. */
  57. public function isTransferOut(): bool
  58. {
  59. return $this->order->isTransferOut();
  60. }
  61. /**
  62. * 获取处理时长(秒)
  63. */
  64. public function getProcessingDuration(): int
  65. {
  66. if ($this->order->completed_at && $this->order->created_at) {
  67. return $this->order->completed_at->diffInSeconds($this->order->created_at);
  68. }
  69. return 0;
  70. }
  71. /**
  72. * 获取事件数据
  73. */
  74. public function toArray(): array
  75. {
  76. return [
  77. 'order_id' => $this->order->id,
  78. 'transfer_app_id' => $this->order->transfer_app_id,
  79. 'user_id' => $this->order->user_id,
  80. 'type' => $this->order->type->value,
  81. 'amount' => $this->order->amount,
  82. 'out_amount' => $this->order->out_amount,
  83. 'previous_status' => $this->previousStatus->value,
  84. 'current_status' => $this->order->status->value,
  85. 'processing_duration' => $this->getProcessingDuration(),
  86. 'created_at' => $this->order->created_at->toDateTimeString(),
  87. 'completed_at' => $this->order->completed_at?->toDateTimeString(),
  88. 'context' => $this->context,
  89. ];
  90. }
  91. }