TransferOrderDto.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Module\Transfer\Dtos;
  3. use App\Module\Transfer\Enums\TransferStatus;
  4. use App\Module\Transfer\Enums\TransferType;
  5. /**
  6. * 划转订单数据传输对象
  7. */
  8. class TransferOrderDto
  9. {
  10. public function __construct(
  11. public readonly int $id,
  12. public readonly int $transfer_app_id,
  13. public readonly int $out_id,
  14. public readonly string $out_order_id,
  15. public readonly ?string $out_user_id,
  16. public readonly int $user_id,
  17. public readonly int $currency_id,
  18. public readonly int $fund_id,
  19. public readonly TransferType $type,
  20. public readonly TransferStatus $status,
  21. public readonly string $out_amount,
  22. public readonly string $amount,
  23. public readonly float $exchange_rate,
  24. public readonly float $fee_rate,
  25. public readonly string $fee_amount,
  26. public readonly string $actual_amount,
  27. public readonly array $callback_data,
  28. public readonly ?string $error_message,
  29. public readonly ?string $remark,
  30. public readonly ?string $processed_at,
  31. public readonly ?string $callback_at,
  32. public readonly ?string $completed_at,
  33. public readonly string $created_at,
  34. public readonly string $updated_at,
  35. ) {}
  36. /**
  37. * 从模型创建DTO
  38. */
  39. public static function fromModel($model): self
  40. {
  41. return new self(
  42. id: $model->id,
  43. transfer_app_id: $model->transfer_app_id,
  44. out_id: $model->out_id,
  45. out_order_id: $model->out_order_id,
  46. out_user_id: $model->out_user_id,
  47. user_id: $model->user_id,
  48. currency_id: $model->currency_id,
  49. fund_id: $model->fund_id,
  50. type: $model->type,
  51. status: $model->status,
  52. out_amount: (string) $model->out_amount,
  53. amount: (string) $model->amount,
  54. exchange_rate: (float) $model->exchange_rate,
  55. fee_rate: (float) ($model->fee_rate ?? 0.0000),
  56. fee_amount: (string) ($model->fee_amount ?? '0.0000'),
  57. actual_amount: (string) ($model->actual_amount ?? $model->amount),
  58. callback_data: $model->callback_data ?? [],
  59. error_message: $model->error_message,
  60. remark: $model->remark,
  61. processed_at: $model->processed_at?->toDateTimeString(),
  62. callback_at: $model->callback_at?->toDateTimeString(),
  63. completed_at: $model->completed_at?->toDateTimeString(),
  64. created_at: $model->created_at->toDateTimeString(),
  65. updated_at: $model->updated_at->toDateTimeString(),
  66. );
  67. }
  68. /**
  69. * 转换为数组
  70. */
  71. public function toArray(): array
  72. {
  73. return [
  74. 'id' => $this->id,
  75. 'transfer_app_id' => $this->transfer_app_id,
  76. 'out_id' => $this->out_id,
  77. 'out_order_id' => $this->out_order_id,
  78. 'out_user_id' => $this->out_user_id,
  79. 'user_id' => $this->user_id,
  80. 'currency_id' => $this->currency_id,
  81. 'fund_id' => $this->fund_id,
  82. 'type' => $this->type->value,
  83. 'type_text' => $this->type->getDescription(),
  84. 'status' => $this->status->value,
  85. 'status_text' => $this->status->getDescription(),
  86. 'out_amount' => $this->out_amount,
  87. 'amount' => $this->amount,
  88. 'exchange_rate' => $this->exchange_rate,
  89. 'fee_rate' => $this->fee_rate,
  90. 'fee_amount' => $this->fee_amount,
  91. 'actual_amount' => $this->actual_amount,
  92. 'has_fee' => $this->fee_amount > 0,
  93. 'fee_rate_percent' => number_format($this->fee_rate * 100, 2) . '%',
  94. 'callback_data' => $this->callback_data,
  95. 'error_message' => $this->error_message,
  96. 'remark' => $this->remark,
  97. 'processed_at' => $this->processed_at,
  98. 'callback_at' => $this->callback_at,
  99. 'completed_at' => $this->completed_at,
  100. 'created_at' => $this->created_at,
  101. 'updated_at' => $this->updated_at,
  102. ];
  103. }
  104. /**
  105. * 判断是否为转入订单
  106. */
  107. public function isTransferIn(): bool
  108. {
  109. return $this->type === TransferType::IN;
  110. }
  111. /**
  112. * 判断是否为转出订单
  113. */
  114. public function isTransferOut(): bool
  115. {
  116. return $this->type === TransferType::OUT;
  117. }
  118. /**
  119. * 判断是否为最终状态
  120. */
  121. public function isFinalStatus(): bool
  122. {
  123. return $this->status->isFinal();
  124. }
  125. /**
  126. * 判断是否可以重试
  127. */
  128. public function canRetry(): bool
  129. {
  130. return $this->status->canRetry();
  131. }
  132. /**
  133. * 判断是否收取了手续费
  134. */
  135. public function hasFee(): bool
  136. {
  137. return bccomp($this->fee_amount, '0', 4) > 0;
  138. }
  139. /**
  140. * 获取手续费率百分比文本
  141. */
  142. public function getFeeRatePercent(): string
  143. {
  144. return number_format($this->fee_rate * 100, 2) . '%';
  145. }
  146. /**
  147. * 获取手续费金额格式化文本
  148. */
  149. public function getFeeAmountText(): string
  150. {
  151. return number_format((float)$this->fee_amount, 4);
  152. }
  153. /**
  154. * 获取实际到账金额格式化文本
  155. */
  156. public function getActualAmountText(): string
  157. {
  158. return number_format((float)$this->actual_amount, 4);
  159. }
  160. }