FeeCalculatingEvent.php 6.3 KB

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