| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace App\Module\Transfer\Exceptions;
- /**
- * 余额不足异常
- */
- class InsufficientBalanceException extends TransferException
- {
- private int $userId;
- private int $fundId;
- private string $requiredAmount;
- private string $availableAmount;
- public function __construct(
- int $userId,
- int $fundId,
- string $requiredAmount,
- string $availableAmount,
- string $message = '余额不足'
- ) {
- $this->userId = $userId;
- $this->fundId = $fundId;
- $this->requiredAmount = $requiredAmount;
- $this->availableAmount = $availableAmount;
- $context = [
- 'user_id' => $userId,
- 'fund_id' => $fundId,
- 'required_amount' => $requiredAmount,
- 'available_amount' => $availableAmount,
- 'shortage' => bcsub($requiredAmount, $availableAmount, 10),
- ];
- parent::__construct($message, 1001, null, $context);
- }
- /**
- * 获取用户ID
- */
- public function getUserId(): int
- {
- return $this->userId;
- }
- /**
- * 获取资金类型ID
- */
- public function getFundId(): int
- {
- return $this->fundId;
- }
- /**
- * 获取所需金额
- */
- public function getRequiredAmount(): string
- {
- return $this->requiredAmount;
- }
- /**
- * 获取可用金额
- */
- public function getAvailableAmount(): string
- {
- return $this->availableAmount;
- }
- /**
- * 获取缺少的金额
- */
- public function getShortage(): string
- {
- return bcsub($this->requiredAmount, $this->availableAmount, 10);
- }
- /**
- * 获取详细错误信息
- */
- public function getDetailedMessage(): string
- {
- return sprintf(
- '用户 %d 的资金类型 %d 余额不足,需要 %s,可用 %s,缺少 %s',
- $this->userId,
- $this->fundId,
- $this->requiredAmount,
- $this->availableAmount,
- $this->getShortage()
- );
- }
- }
|