FeeCalculatedEvent.php 5.5 KB

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