| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace App\Module\Transfer\Events;
- use App\Module\Transfer\Models\TransferOrder;
- use App\Module\Transfer\Enums\TransferStatus;
- use Illuminate\Broadcasting\InteractsWithSockets;
- use Illuminate\Foundation\Events\Dispatchable;
- use Illuminate\Queue\SerializesModels;
- /**
- * 划转订单完成事件
- */
- class TransferOrderCompleted
- {
- use Dispatchable, InteractsWithSockets, SerializesModels;
- public TransferOrder $order;
- public TransferStatus $previousStatus;
- public array $context;
- /**
- * 创建事件实例
- */
- public function __construct(TransferOrder $order, TransferStatus $previousStatus, array $context = [])
- {
- $this->order = $order;
- $this->previousStatus = $previousStatus;
- $this->context = $context;
- }
- /**
- * 获取订单信息
- */
- public function getOrder(): TransferOrder
- {
- return $this->order;
- }
- /**
- * 获取之前的状态
- */
- public function getPreviousStatus(): TransferStatus
- {
- return $this->previousStatus;
- }
- /**
- * 获取上下文信息
- */
- public function getContext(): array
- {
- return $this->context;
- }
- /**
- * 判断是否为转入订单
- */
- public function isTransferIn(): bool
- {
- return $this->order->isTransferIn();
- }
- /**
- * 判断是否为转出订单
- */
- public function isTransferOut(): bool
- {
- return $this->order->isTransferOut();
- }
- /**
- * 获取处理时长(秒)
- */
- public function getProcessingDuration(): int
- {
- if ($this->order->completed_at && $this->order->created_at) {
- return $this->order->completed_at->diffInSeconds($this->order->created_at);
- }
-
- return 0;
- }
- /**
- * 获取事件数据
- */
- public function toArray(): array
- {
- return [
- 'order_id' => $this->order->id,
- 'transfer_app_id' => $this->order->transfer_app_id,
- 'user_id' => $this->order->user_id,
- 'type' => $this->order->type->value,
- 'amount' => $this->order->amount,
- 'out_amount' => $this->order->out_amount,
- 'previous_status' => $this->previousStatus->value,
- 'current_status' => $this->order->status->value,
- 'processing_duration' => $this->getProcessingDuration(),
- 'created_at' => $this->order->created_at->toDateTimeString(),
- 'completed_at' => $this->order->completed_at?->toDateTimeString(),
- 'context' => $this->context,
- ];
- }
- }
|