| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace App\Module\Transfer\Dtos;
- use App\Module\Transfer\Enums\TransferStatus;
- use App\Module\Transfer\Enums\TransferType;
- /**
- * 划转订单数据传输对象
- */
- class TransferOrderDto
- {
- public function __construct(
- public readonly int $id,
- public readonly int $transfer_app_id,
- public readonly int $out_id,
- public readonly string $out_order_id,
- public readonly ?string $out_user_id,
- public readonly int $user_id,
- public readonly int $currency_id,
- public readonly int $fund_id,
- public readonly TransferType $type,
- public readonly TransferStatus $status,
- public readonly string $out_amount,
- public readonly string $amount,
- public readonly float $exchange_rate,
- public readonly float $fee_rate,
- public readonly string $fee_amount,
- public readonly string $actual_amount,
- public readonly array $callback_data,
- public readonly ?string $error_message,
- public readonly ?string $remark,
- public readonly ?string $processed_at,
- public readonly ?string $callback_at,
- public readonly ?string $completed_at,
- public readonly string $created_at,
- public readonly string $updated_at,
- ) {}
- /**
- * 从模型创建DTO
- */
- public static function fromModel($model): self
- {
- return new self(
- id: $model->id,
- transfer_app_id: $model->transfer_app_id,
- out_id: $model->out_id,
- out_order_id: $model->out_order_id,
- out_user_id: $model->out_user_id,
- user_id: $model->user_id,
- currency_id: $model->currency_id,
- fund_id: $model->fund_id,
- type: $model->type,
- status: $model->status,
- out_amount: (string) $model->out_amount,
- amount: (string) $model->amount,
- exchange_rate: (float) $model->exchange_rate,
- fee_rate: (float) ($model->fee_rate ?? 0.0000),
- fee_amount: (string) ($model->fee_amount ?? '0.0000'),
- actual_amount: (string) ($model->actual_amount ?? $model->amount),
- callback_data: $model->callback_data ?? [],
- error_message: $model->error_message,
- remark: $model->remark,
- processed_at: $model->processed_at?->toDateTimeString(),
- callback_at: $model->callback_at?->toDateTimeString(),
- completed_at: $model->completed_at?->toDateTimeString(),
- created_at: $model->created_at->toDateTimeString(),
- updated_at: $model->updated_at->toDateTimeString(),
- );
- }
- /**
- * 转换为数组
- */
- public function toArray(): array
- {
- return [
- 'id' => $this->id,
- 'transfer_app_id' => $this->transfer_app_id,
- 'out_id' => $this->out_id,
- 'out_order_id' => $this->out_order_id,
- 'out_user_id' => $this->out_user_id,
- 'user_id' => $this->user_id,
- 'currency_id' => $this->currency_id,
- 'fund_id' => $this->fund_id,
- 'type' => $this->type->value,
- 'type_text' => $this->type->getDescription(),
- 'status' => $this->status->value,
- 'status_text' => $this->status->getDescription(),
- 'out_amount' => $this->out_amount,
- 'amount' => $this->amount,
- 'exchange_rate' => $this->exchange_rate,
- 'fee_rate' => $this->fee_rate,
- 'fee_amount' => $this->fee_amount,
- 'actual_amount' => $this->actual_amount,
- 'has_fee' => $this->fee_amount > 0,
- 'fee_rate_percent' => number_format($this->fee_rate * 100, 2) . '%',
- 'callback_data' => $this->callback_data,
- 'error_message' => $this->error_message,
- 'remark' => $this->remark,
- 'processed_at' => $this->processed_at,
- 'callback_at' => $this->callback_at,
- 'completed_at' => $this->completed_at,
- 'created_at' => $this->created_at,
- 'updated_at' => $this->updated_at,
- ];
- }
- /**
- * 判断是否为转入订单
- */
- public function isTransferIn(): bool
- {
- return $this->type === TransferType::IN;
- }
- /**
- * 判断是否为转出订单
- */
- public function isTransferOut(): bool
- {
- return $this->type === TransferType::OUT;
- }
- /**
- * 判断是否为最终状态
- */
- public function isFinalStatus(): bool
- {
- return $this->status->isFinal();
- }
- /**
- * 判断是否可以重试
- */
- public function canRetry(): bool
- {
- return $this->status->canRetry();
- }
- /**
- * 判断是否收取了手续费
- */
- public function hasFee(): bool
- {
- return bccomp($this->fee_amount, '0', 4) > 0;
- }
- /**
- * 获取手续费率百分比文本
- */
- public function getFeeRatePercent(): string
- {
- return number_format($this->fee_rate * 100, 2) . '%';
- }
- /**
- * 获取手续费金额格式化文本
- */
- public function getFeeAmountText(): string
- {
- return number_format((float)$this->fee_amount, 4);
- }
- /**
- * 获取实际到账金额格式化文本
- */
- public function getActualAmountText(): string
- {
- return number_format((float)$this->actual_amount, 4);
- }
- }
|