| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- namespace App\Module\Transfer\Events;
- use App\Module\Transfer\Models\TransferApp;
- use Illuminate\Foundation\Events\Dispatchable;
- use Illuminate\Queue\SerializesModels;
- /**
- * 手续费计算完成事件
- *
- * 在手续费计算完成后触发,用于记录日志、统计等
- */
- class FeeCalculatedEvent
- {
- use Dispatchable, SerializesModels;
- /**
- * 划转应用
- */
- public TransferApp $app;
- /**
- * 原始金额
- */
- public string $amount;
- /**
- * 手续费类型:'in' 或 'out'
- */
- public string $type;
- /**
- * 最终的手续费率
- */
- public float $feeRate;
- /**
- * 最终的手续费金额
- */
- public string $feeAmount;
- /**
- * 最终的实际到账金额
- */
- public string $actualAmount;
- /**
- * 是否被修改过
- */
- public bool $isModified;
- /**
- * 修改原因
- */
- public ?string $modificationReason;
- /**
- * 修改者标识
- */
- public ?string $modifiedBy;
- /**
- * 额外的上下文数据
- */
- public array $context;
- /**
- * 创建事件实例
- *
- * @param TransferApp $app 划转应用
- * @param string $amount 原始金额
- * @param string $type 手续费类型
- * @param float $feeRate 手续费率
- * @param string $feeAmount 手续费金额
- * @param string $actualAmount 实际到账金额
- * @param bool $isModified 是否被修改过
- * @param string|null $modificationReason 修改原因
- * @param string|null $modifiedBy 修改者标识
- * @param array $context 额外的上下文数据
- */
- public function __construct(
- TransferApp $app,
- string $amount,
- string $type,
- float $feeRate,
- string $feeAmount,
- string $actualAmount,
- bool $isModified = false,
- ?string $modificationReason = null,
- ?string $modifiedBy = null,
- array $context = []
- ) {
- $this->app = $app;
- $this->amount = $amount;
- $this->type = $type;
- $this->feeRate = $feeRate;
- $this->feeAmount = $feeAmount;
- $this->actualAmount = $actualAmount;
- $this->isModified = $isModified;
- $this->modificationReason = $modificationReason;
- $this->modifiedBy = $modifiedBy;
- $this->context = $context;
- }
- /**
- * 从FeeCalculatingEvent创建
- *
- * @param FeeCalculatingEvent $calculatingEvent
- * @return static
- */
- public static function fromCalculatingEvent(FeeCalculatingEvent $calculatingEvent): static
- {
- return new static(
- app: $calculatingEvent->app,
- amount: $calculatingEvent->amount,
- type: $calculatingEvent->type,
- feeRate: $calculatingEvent->feeRate,
- feeAmount: $calculatingEvent->feeAmount,
- actualAmount: $calculatingEvent->actualAmount,
- isModified: $calculatingEvent->isModified,
- modificationReason: $calculatingEvent->modificationReason,
- modifiedBy: $calculatingEvent->modifiedBy,
- context: $calculatingEvent->context
- );
- }
- /**
- * 获取手续费计算结果
- *
- * @return array
- */
- public function getResult(): array
- {
- return [
- 'fee_rate' => $this->feeRate,
- 'fee_amount' => $this->feeAmount,
- 'actual_amount' => $this->actualAmount,
- 'is_modified' => $this->isModified,
- 'modification_reason' => $this->modificationReason,
- 'modified_by' => $this->modifiedBy,
- ];
- }
- /**
- * 获取上下文数据
- *
- * @param string $key 键名
- * @param mixed $default 默认值
- * @return mixed
- */
- public function getContext(string $key, $default = null)
- {
- return $this->context[$key] ?? $default;
- }
- /**
- * 检查是否为转入手续费
- *
- * @return bool
- */
- public function isTransferIn(): bool
- {
- return $this->type === 'in';
- }
- /**
- * 检查是否为转出手续费
- *
- * @return bool
- */
- public function isTransferOut(): bool
- {
- return $this->type === 'out';
- }
- }
|