| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- namespace App\Module\Transfer\Events;
- use App\Module\Transfer\Models\TransferApp;
- use Illuminate\Foundation\Events\Dispatchable;
- use Illuminate\Queue\SerializesModels;
- /**
- * 手续费计算事件
- *
- * 在计算手续费时触发,允许其他模块修改手续费数额
- */
- class FeeCalculatingEvent
- {
- use Dispatchable, SerializesModels;
- /**
- * 划转应用
- */
- public TransferApp $app;
- /**
- * 原始金额
- */
- public string $amount;
- /**
- * 手续费类型:'in' 或 'out'
- */
- public string $type;
- /**
- * 计算出的手续费金额
- */
- public string $feeAmount;
- /**
- * 是否被修改过
- */
- public bool $isModified = false;
- /**
- * 修改原因(用于日志记录)
- */
- public ?string $modificationReason = null;
- /**
- * 修改者标识(模块名或服务名)
- */
- public ?string $modifiedBy = null;
- /**
- * 额外的上下文数据
- */
- public array $context = [];
- /**
- * 创建事件实例
- *
- * @param TransferApp $app 划转应用
- * @param string $amount 原始金额
- * @param string $type 手续费类型
- * @param string $feeAmount 手续费金额
- * @param array $context 额外的上下文数据
- */
- public function __construct(
- TransferApp $app,
- string $amount,
- string $type,
- string $feeAmount,
- array $context = []
- ) {
- $this->app = $app;
- $this->amount = $amount;
- $this->type = $type;
- $this->feeAmount = $feeAmount;
- $this->context = $context;
- }
- /**
- * 修改手续费金额
- *
- * @param string $newFeeAmount 新的手续费金额
- * @param string $reason 修改原因
- * @param string $modifiedBy 修改者标识
- * @return void
- */
- public function modifyFeeAmount(string $newFeeAmount, string $reason, string $modifiedBy): void
- {
- $this->feeAmount = $newFeeAmount;
- $this->isModified = true;
- $this->modificationReason = $reason;
- $this->modifiedBy = $modifiedBy;
- }
- /**
- * 修改手续费率(会重新计算手续费金额)
- *
- * @param float $newFeeRate 新的手续费率
- * @param string $reason 修改原因
- * @param string $modifiedBy 修改者标识
- * @return void
- */
- public function modifyFeeRate(float $newFeeRate, string $reason, string $modifiedBy): void
- {
- $this->feeAmount = bcmul($this->amount, (string)$newFeeRate, 4);
- $this->isModified = true;
- $this->modificationReason = $reason;
- $this->modifiedBy = $modifiedBy;
- }
- /**
- * 设置免手续费
- *
- * @param string $reason 免费原因
- * @param string $modifiedBy 修改者标识
- * @return void
- */
- public function setFree(string $reason, string $modifiedBy): void
- {
- $this->feeAmount = '0.0000';
- $this->isModified = true;
- $this->modificationReason = $reason;
- $this->modifiedBy = $modifiedBy;
- }
- /**
- * 增加手续费金额
- *
- * @param string $additionalFee 额外手续费
- * @param string $reason 增加原因
- * @param string $modifiedBy 修改者标识
- * @return void
- */
- public function addFee(string $additionalFee, string $reason, string $modifiedBy): void
- {
- $this->feeAmount = bcadd($this->feeAmount, $additionalFee, 4);
- $this->isModified = true;
- $this->modificationReason = $reason;
- $this->modifiedBy = $modifiedBy;
- }
- /**
- * 减少手续费金额
- *
- * @param string $discountFee 减免手续费
- * @param string $reason 减免原因
- * @param string $modifiedBy 修改者标识
- * @return void
- */
- public function reduceFee(string $discountFee, string $reason, string $modifiedBy): void
- {
- $newFeeAmount = bcsub($this->feeAmount, $discountFee, 4);
- // 确保手续费不为负数
- if (bccomp($newFeeAmount, '0', 4) < 0) {
- $newFeeAmount = '0.0000';
- }
- $this->feeAmount = $newFeeAmount;
- $this->isModified = true;
- $this->modificationReason = $reason;
- $this->modifiedBy = $modifiedBy;
- }
- /**
- * 获取手续费计算结果
- *
- * @return array
- */
- public function getResult(): array
- {
- return [
- 'fee_amount' => $this->feeAmount,
- '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;
- }
- /**
- * 设置上下文数据
- *
- * @param string $key 键名
- * @param mixed $value 值
- * @return void
- */
- public function setContext(string $key, $value): void
- {
- $this->context[$key] = $value;
- }
- /**
- * 检查是否为转入手续费
- *
- * @return bool
- */
- public function isTransferIn(): bool
- {
- return $this->type === 'in';
- }
- /**
- * 检查是否为转出手续费
- *
- * @return bool
- */
- public function isTransferOut(): bool
- {
- return $this->type === 'out';
- }
- }
|