FeeCalculatingEvent.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 FeeCalculatingEvent
  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 string $feeAmount;
  30. /**
  31. * 是否被修改过
  32. */
  33. public bool $isModified = false;
  34. /**
  35. * 修改原因(用于日志记录)
  36. */
  37. public ?string $modificationReason = null;
  38. /**
  39. * 修改者标识(模块名或服务名)
  40. */
  41. public ?string $modifiedBy = null;
  42. /**
  43. * 额外的上下文数据
  44. */
  45. public array $context = [];
  46. /**
  47. * 创建事件实例
  48. *
  49. * @param TransferApp $app 划转应用
  50. * @param string $amount 原始金额
  51. * @param string $type 手续费类型
  52. * @param string $feeAmount 手续费金额
  53. * @param array $context 额外的上下文数据
  54. */
  55. public function __construct(
  56. TransferApp $app,
  57. string $amount,
  58. string $type,
  59. string $feeAmount,
  60. array $context = []
  61. ) {
  62. $this->app = $app;
  63. $this->amount = $amount;
  64. $this->type = $type;
  65. $this->feeAmount = $feeAmount;
  66. $this->context = $context;
  67. }
  68. /**
  69. * 修改手续费金额
  70. *
  71. * @param string $newFeeAmount 新的手续费金额
  72. * @param string $reason 修改原因
  73. * @param string $modifiedBy 修改者标识
  74. * @return void
  75. */
  76. public function modifyFeeAmount(string $newFeeAmount, string $reason, string $modifiedBy): void
  77. {
  78. $this->feeAmount = $newFeeAmount;
  79. $this->isModified = true;
  80. $this->modificationReason = $reason;
  81. $this->modifiedBy = $modifiedBy;
  82. }
  83. /**
  84. * 修改手续费率(会重新计算手续费金额)
  85. *
  86. * @param float $newFeeRate 新的手续费率
  87. * @param string $reason 修改原因
  88. * @param string $modifiedBy 修改者标识
  89. * @return void
  90. */
  91. public function modifyFeeRate(float $newFeeRate, string $reason, string $modifiedBy): void
  92. {
  93. $this->feeAmount = bcmul($this->amount, (string)$newFeeRate, 4);
  94. $this->isModified = true;
  95. $this->modificationReason = $reason;
  96. $this->modifiedBy = $modifiedBy;
  97. }
  98. /**
  99. * 设置免手续费
  100. *
  101. * @param string $reason 免费原因
  102. * @param string $modifiedBy 修改者标识
  103. * @return void
  104. */
  105. public function setFree(string $reason, string $modifiedBy): void
  106. {
  107. $this->feeAmount = '0.0000';
  108. $this->isModified = true;
  109. $this->modificationReason = $reason;
  110. $this->modifiedBy = $modifiedBy;
  111. }
  112. /**
  113. * 增加手续费金额
  114. *
  115. * @param string $additionalFee 额外手续费
  116. * @param string $reason 增加原因
  117. * @param string $modifiedBy 修改者标识
  118. * @return void
  119. */
  120. public function addFee(string $additionalFee, string $reason, string $modifiedBy): void
  121. {
  122. $this->feeAmount = bcadd($this->feeAmount, $additionalFee, 4);
  123. $this->isModified = true;
  124. $this->modificationReason = $reason;
  125. $this->modifiedBy = $modifiedBy;
  126. }
  127. /**
  128. * 减少手续费金额
  129. *
  130. * @param string $discountFee 减免手续费
  131. * @param string $reason 减免原因
  132. * @param string $modifiedBy 修改者标识
  133. * @return void
  134. */
  135. public function reduceFee(string $discountFee, string $reason, string $modifiedBy): void
  136. {
  137. $newFeeAmount = bcsub($this->feeAmount, $discountFee, 4);
  138. // 确保手续费不为负数
  139. if (bccomp($newFeeAmount, '0', 4) < 0) {
  140. $newFeeAmount = '0.0000';
  141. }
  142. $this->feeAmount = $newFeeAmount;
  143. $this->isModified = true;
  144. $this->modificationReason = $reason;
  145. $this->modifiedBy = $modifiedBy;
  146. }
  147. /**
  148. * 获取手续费计算结果
  149. *
  150. * @return array
  151. */
  152. public function getResult(): array
  153. {
  154. return [
  155. 'fee_amount' => $this->feeAmount,
  156. 'is_modified' => $this->isModified,
  157. 'modification_reason' => $this->modificationReason,
  158. 'modified_by' => $this->modifiedBy,
  159. ];
  160. }
  161. /**
  162. * 获取上下文数据
  163. *
  164. * @param string $key 键名
  165. * @param mixed $default 默认值
  166. * @return mixed
  167. */
  168. public function getContext(string $key, $default = null)
  169. {
  170. return $this->context[$key] ?? $default;
  171. }
  172. /**
  173. * 设置上下文数据
  174. *
  175. * @param string $key 键名
  176. * @param mixed $value 值
  177. * @return void
  178. */
  179. public function setContext(string $key, $value): void
  180. {
  181. $this->context[$key] = $value;
  182. }
  183. /**
  184. * 检查是否为转入手续费
  185. *
  186. * @return bool
  187. */
  188. public function isTransferIn(): bool
  189. {
  190. return $this->type === 'in';
  191. }
  192. /**
  193. * 检查是否为转出手续费
  194. *
  195. * @return bool
  196. */
  197. public function isTransferOut(): bool
  198. {
  199. return $this->type === 'out';
  200. }
  201. }