app = $app; $this->amount = $amount; $this->type = $type; $this->feeRate = $feeRate; $this->feeAmount = $feeAmount; $this->actualAmount = $actualAmount; $this->isModified = $isModified; $this->totleAmount = $totleAmount; $this->modificationReason = $modificationReason; $this->modifiedBy = $modifiedBy; $this->context = $context; } /** * 从FeeCalculatingEvent创建 * * @param FeeCalculatingEvent $calculatingEvent * @return static */ public static function fromCalculatingEvent(FeeCalculatingEvent $calculatingEvent): static { Logger::debug('fromCalculatingEvent'." {$calculatingEvent->amount}, {$calculatingEvent->feeAmount}"); if ($calculatingEvent->type == 'in') { // 转入 $actualAmount = bcsub($calculatingEvent->amount, $calculatingEvent->feeAmount, 10); $totleAmout = $calculatingEvent->amount; } else { $actualAmount = $calculatingEvent->amount; $totleAmout = bcadd($calculatingEvent->amount, $calculatingEvent->feeAmount, 10); } if($calculatingEvent->feeAmount > 0){ $feeRate = bcdiv($calculatingEvent->feeAmount * 100,$calculatingEvent->amount, 4) ; }else{ $feeRate = 0; } Logger::debug('fromCalculatingEvent feerate'." {$calculatingEvent->amount}, {$calculatingEvent->feeAmount} ,$feeRate"); return new static( app: $calculatingEvent->app, amount: $calculatingEvent->amount, type: $calculatingEvent->type, feeRate: $feeRate, feeAmount: $calculatingEvent->feeAmount, actualAmount: $actualAmount, totleAmount: $totleAmout, 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'; } }