FeeCalculatedEvent.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Module\Transfer\Events;
  3. use App\Module\Transfer\Models\TransferApp;
  4. use Illuminate\Foundation\Events\Dispatchable;
  5. use Illuminate\Queue\SerializesModels;
  6. /**
  7. * 手续费计算完成事件
  8. *
  9. * 在手续费计算完成后触发,用于记录日志、统计等
  10. */
  11. class FeeCalculatedEvent
  12. {
  13. use Dispatchable, SerializesModels;
  14. /**
  15. * 划转应用
  16. */
  17. public TransferApp $app;
  18. /**
  19. * 原始金额
  20. */
  21. public string $amount;
  22. /**
  23. * 手续费类型:'in' 或 'out'
  24. */
  25. public string $type;
  26. /**
  27. * 最终的手续费率
  28. */
  29. public float $feeRate;
  30. /**
  31. * 最终的手续费金额
  32. */
  33. public string $feeAmount;
  34. /**
  35. * 最终的实际到账金额
  36. */
  37. public string $actualAmount;
  38. /**
  39. * 是否被修改过
  40. */
  41. public bool $isModified;
  42. /**
  43. * 修改原因
  44. */
  45. public ?string $modificationReason;
  46. /**
  47. * 修改者标识
  48. */
  49. public ?string $modifiedBy;
  50. /**
  51. * 额外的上下文数据
  52. */
  53. public array $context;
  54. /**
  55. * 创建事件实例
  56. *
  57. * @param TransferApp $app 划转应用
  58. * @param string $amount 原始金额
  59. * @param string $type 手续费类型
  60. * @param float $feeRate 手续费率
  61. * @param string $feeAmount 手续费金额
  62. * @param string $actualAmount 实际到账金额
  63. * @param bool $isModified 是否被修改过
  64. * @param string|null $modificationReason 修改原因
  65. * @param string|null $modifiedBy 修改者标识
  66. * @param array $context 额外的上下文数据
  67. */
  68. public function __construct(
  69. TransferApp $app,
  70. string $amount,
  71. string $type,
  72. float $feeRate,
  73. string $feeAmount,
  74. string $actualAmount,
  75. bool $isModified = false,
  76. ?string $modificationReason = null,
  77. ?string $modifiedBy = null,
  78. array $context = []
  79. ) {
  80. $this->app = $app;
  81. $this->amount = $amount;
  82. $this->type = $type;
  83. $this->feeRate = $feeRate;
  84. $this->feeAmount = $feeAmount;
  85. $this->actualAmount = $actualAmount;
  86. $this->isModified = $isModified;
  87. $this->modificationReason = $modificationReason;
  88. $this->modifiedBy = $modifiedBy;
  89. $this->context = $context;
  90. }
  91. /**
  92. * 从FeeCalculatingEvent创建
  93. *
  94. * @param FeeCalculatingEvent $calculatingEvent
  95. * @return static
  96. */
  97. public static function fromCalculatingEvent(FeeCalculatingEvent $calculatingEvent): static
  98. {
  99. return new static(
  100. app: $calculatingEvent->app,
  101. amount: $calculatingEvent->amount,
  102. type: $calculatingEvent->type,
  103. feeRate: $calculatingEvent->feeRate,
  104. feeAmount: $calculatingEvent->feeAmount,
  105. actualAmount: $calculatingEvent->actualAmount,
  106. isModified: $calculatingEvent->isModified,
  107. modificationReason: $calculatingEvent->modificationReason,
  108. modifiedBy: $calculatingEvent->modifiedBy,
  109. context: $calculatingEvent->context
  110. );
  111. }
  112. /**
  113. * 获取手续费计算结果
  114. *
  115. * @return array
  116. */
  117. public function getResult(): array
  118. {
  119. return [
  120. 'fee_rate' => $this->feeRate,
  121. 'fee_amount' => $this->feeAmount,
  122. 'actual_amount' => $this->actualAmount,
  123. 'is_modified' => $this->isModified,
  124. 'modification_reason' => $this->modificationReason,
  125. 'modified_by' => $this->modifiedBy,
  126. ];
  127. }
  128. /**
  129. * 获取上下文数据
  130. *
  131. * @param string $key 键名
  132. * @param mixed $default 默认值
  133. * @return mixed
  134. */
  135. public function getContext(string $key, $default = null)
  136. {
  137. return $this->context[$key] ?? $default;
  138. }
  139. /**
  140. * 检查是否为转入手续费
  141. *
  142. * @return bool
  143. */
  144. public function isTransferIn(): bool
  145. {
  146. return $this->type === 'in';
  147. }
  148. /**
  149. * 检查是否为转出手续费
  150. *
  151. * @return bool
  152. */
  153. public function isTransferOut(): bool
  154. {
  155. return $this->type === 'out';
  156. }
  157. }