FeeCalculatedEvent.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 string $totleAmount;
  42. /**
  43. * 是否被修改过
  44. */
  45. public bool $isModified;
  46. /**
  47. * 修改原因
  48. */
  49. public ?string $modificationReason;
  50. /**
  51. * 修改者标识
  52. */
  53. public ?string $modifiedBy;
  54. /**
  55. * 额外的上下文数据
  56. */
  57. public array $context;
  58. /**
  59. * 创建事件实例
  60. *
  61. * @param TransferApp $app 划转应用
  62. * @param string $amount 原始金额
  63. * @param string $type 手续费类型
  64. * @param float $feeRate 手续费率
  65. * @param string $feeAmount 手续费金额
  66. * @param string $actualAmount 实际到账金额
  67. * @param string $totleAmount 付款方支付金额
  68. * @param bool $isModified 是否被修改过
  69. * @param string|null $modificationReason 修改原因
  70. * @param string|null $modifiedBy 修改者标识
  71. * @param array $context 额外的上下文数据
  72. */
  73. public function __construct(
  74. TransferApp $app,
  75. string $amount,
  76. string $type,
  77. float $feeRate,
  78. string $feeAmount,
  79. string $actualAmount,
  80. string $totleAmount,
  81. bool $isModified = false,
  82. ?string $modificationReason = null,
  83. ?string $modifiedBy = null,
  84. array $context = []
  85. )
  86. {
  87. $this->app = $app;
  88. $this->amount = $amount;
  89. $this->type = $type;
  90. $this->feeRate = $feeRate;
  91. $this->feeAmount = $feeAmount;
  92. $this->actualAmount = $actualAmount;
  93. $this->isModified = $isModified;
  94. $this->totleAmount = $totleAmount;
  95. $this->modificationReason = $modificationReason;
  96. $this->modifiedBy = $modifiedBy;
  97. $this->context = $context;
  98. }
  99. /**
  100. * 从FeeCalculatingEvent创建
  101. *
  102. * @param FeeCalculatingEvent $calculatingEvent
  103. * @return static
  104. */
  105. public static function fromCalculatingEvent(FeeCalculatingEvent $calculatingEvent): static
  106. {
  107. if ($calculatingEvent->type == 'in') {
  108. // 转入
  109. $actualAmount = bcsub($calculatingEvent->amount, $calculatingEvent->feeAmount, 10);
  110. $totleAmout = $calculatingEvent->amount;
  111. } else {
  112. $actualAmount = $calculatingEvent->amount;
  113. $totleAmout = bcadd($calculatingEvent->amount, $calculatingEvent->feeAmount, 10);
  114. }
  115. if($calculatingEvent->feeAmount > 0){
  116. $feeRate = bcdiv($calculatingEvent->amount, $calculatingEvent->feeAmount, 10);
  117. }else{
  118. $feeRate = 0;
  119. }
  120. return new static(
  121. app: $calculatingEvent->app,
  122. amount: $calculatingEvent->amount,
  123. type: $calculatingEvent->type,
  124. feeRate: $feeRate,
  125. feeAmount: $calculatingEvent->feeAmount,
  126. actualAmount: $actualAmount,
  127. totleAmount: $totleAmout,
  128. isModified: $calculatingEvent->isModified,
  129. modificationReason: $calculatingEvent->modificationReason,
  130. modifiedBy: $calculatingEvent->modifiedBy,
  131. context: $calculatingEvent->context
  132. );
  133. }
  134. /**
  135. * 获取手续费计算结果
  136. *
  137. * @return array
  138. */
  139. public function getResult(): array
  140. {
  141. return [
  142. 'fee_rate' => $this->feeRate,
  143. 'fee_amount' => $this->feeAmount,
  144. 'actual_amount' => $this->actualAmount,
  145. 'is_modified' => $this->isModified,
  146. 'modification_reason' => $this->modificationReason,
  147. 'modified_by' => $this->modifiedBy,
  148. ];
  149. }
  150. /**
  151. * 获取上下文数据
  152. *
  153. * @param string $key 键名
  154. * @param mixed $default 默认值
  155. * @return mixed
  156. */
  157. public function getContext(string $key, $default = null)
  158. {
  159. return $this->context[$key] ?? $default;
  160. }
  161. /**
  162. * 检查是否为转入手续费
  163. *
  164. * @return bool
  165. */
  166. public function isTransferIn(): bool
  167. {
  168. return $this->type === 'in';
  169. }
  170. /**
  171. * 检查是否为转出手续费
  172. *
  173. * @return bool
  174. */
  175. public function isTransferOut(): bool
  176. {
  177. return $this->type === 'out';
  178. }
  179. }