| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace App\Module\Transfer\Dtos;
- use UCore\Dto\BaseDto;
- /**
- * 划转手续费计算结果数据传输对象
- *
- * 用于表示手续费计算的结果,包含手续费率、手续费金额、实际到账金额等信息
- */
- class TransferFeeDto extends BaseDto
- {
- /**
- * 构造函数
- *
- * @param float $feeRate 手续费率(小数形式,如 0.01 表示 1%)
- * @param string $feeAmount 手续费金额(字符串格式,保持精度)
- * @param string $actualAmount 实际到账金额(字符串格式,保持精度),本金
- * @param string $totleAmount 总需金额(字符串格式,保持精度)
- * @param string $originalAmount 原始金额(字符串格式,保持精度),外部金额
- * @param bool $hasError 是否有错误
- * @param string|null $errorMessage 错误信息
- * @param array $additionalInfo 额外信息
- */
- public function __construct(
- public readonly float $feeRate,
- public readonly string $feeAmount,
- public readonly string $actualAmount,
- public readonly string $totleAmount,
- public readonly string $originalAmount,
- public readonly bool $hasError = false,
- public readonly ?string $errorMessage = null,
- public readonly array $additionalInfo = []
- )
- {
- }
- /**
- * 创建成功的手续费计算结果
- *
- * @param string $originalAmount 原始金额
- * @param float $feeRate 手续费率
- * @param string $feeAmount 手续费金额
- * @param string $actualAmount 实际到账金额
- * @param string $totleAmount 总支付金额
- * @param array $additionalInfo 额外信息
- * @return self
- */
- public static function success(
- string $originalAmount,
- float $feeRate,
- string $feeAmount,
- string $actualAmount,
- string $totleAmount,
- array $additionalInfo = []
- ): self
- {
- return new self(
- feeRate: $feeRate,
- feeAmount: $feeAmount,
- actualAmount: $actualAmount,
- originalAmount: $originalAmount,
- totleAmount: $totleAmount,
- hasError: false,
- errorMessage: null,
- additionalInfo: $additionalInfo
- );
- }
- /**
- * 创建错误的手续费计算结果
- *
- * @param string $originalAmount 原始金额
- * @param string $errorMessage 错误信息
- * @param array $additionalInfo 额外信息
- * @return self
- */
- public static function error(
- string $originalAmount,
- string $errorMessage,
- array $additionalInfo = []
- ): self
- {
- return new self(
- feeRate: 0.0000,
- feeAmount: '0.0000',
- actualAmount: $originalAmount,
- originalAmount: $originalAmount,
- totleAmount: 0,
- hasError: true,
- errorMessage: $errorMessage,
- additionalInfo: $additionalInfo
- );
- }
- /**
- * 转换为数组
- *
- * @return array
- */
- public function toArray(): array
- {
- $result = [
- 'fee_rate' => $this->feeRate,
- 'fee_amount' => $this->feeAmount,
- 'actual_amount' => $this->actualAmount,
- 'original_amount' => $this->originalAmount,
- 'has_error' => $this->hasError,
- ];
- $result['error'] = $this->errorMessage;
- $result['additionalInfo'] = $this->additionalInfo;
- return $result;
- }
- /**
- * 判断是否计算成功
- *
- * @return bool
- */
- public function isSuccess(): bool
- {
- return !$this->hasError;
- }
- }
|