AgentWalletService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Services\Agent;
  3. use App\Models\Agent\Agent;
  4. use App\Models\Agent\AgentBalanceLog;
  5. use Illuminate\Support\Facades\DB;
  6. class AgentWalletService
  7. {
  8. public function transfer(int $fromAgentId, int $toAgentId, string $amount, string $remark, string $idempotencyKey, int $operatorId): array
  9. {
  10. if ($fromAgentId === $toAgentId) {
  11. throw new \InvalidArgumentException('转出与转入代理不能相同');
  12. }
  13. if (bccomp($amount, '0', 4) <= 0) {
  14. throw new \InvalidArgumentException('金额必须大于0');
  15. }
  16. return DB::transaction(function () use ($fromAgentId, $toAgentId, $amount, $remark, $idempotencyKey, $operatorId) {
  17. $existing = AgentBalanceLog::query()->where('idempotency_key', $idempotencyKey . ':out')->first();
  18. if ($existing) {
  19. $this->assertSameTransfer($existing, $fromAgentId, $toAgentId, $amount);
  20. return [
  21. 'from_balance' => (string) Agent::query()->findOrFail($fromAgentId)->balance,
  22. 'to_balance' => (string) Agent::query()->findOrFail($toAgentId)->balance,
  23. ];
  24. }
  25. $locked = Agent::query()->whereIn('id', [$fromAgentId, $toAgentId])->orderBy('id')->lockForUpdate()->get()->keyBy('id');
  26. $existing = AgentBalanceLog::query()->where('idempotency_key', $idempotencyKey . ':out')->first();
  27. if ($existing) {
  28. $this->assertSameTransfer($existing, $fromAgentId, $toAgentId, $amount);
  29. return [
  30. 'from_balance' => (string) Agent::query()->findOrFail($fromAgentId)->balance,
  31. 'to_balance' => (string) Agent::query()->findOrFail($toAgentId)->balance,
  32. ];
  33. }
  34. $from = $locked->get($fromAgentId);
  35. $to = $locked->get($toAgentId);
  36. if (!$from || !$to) {
  37. throw new \RuntimeException('代理不存在');
  38. }
  39. if (bccomp((string) $from->balance, $amount, 4) < 0) {
  40. throw new \RuntimeException('代理余额不足');
  41. }
  42. $fromBefore = bcadd((string) $from->balance, '0', 4);
  43. $toBefore = bcadd((string) $to->balance, '0', 4);
  44. $from->balance = bcsub($fromBefore, $amount, 4);
  45. $to->balance = bcadd($toBefore, $amount, 4);
  46. $from->save();
  47. $to->save();
  48. AgentBalanceLog::query()->create([
  49. 'agent_id' => $fromAgentId, 'type' => 'transfer_out', 'amount' => bcsub('0', $amount, 4),
  50. 'before_balance' => $fromBefore, 'after_balance' => $from->balance,
  51. 'reference_type' => 'agent_transfer', 'reference_id' => $toAgentId,
  52. 'operator_type' => 'agent', 'operator_id' => $operatorId,
  53. 'idempotency_key' => $idempotencyKey . ':out', 'remark' => $remark,
  54. ]);
  55. AgentBalanceLog::query()->create([
  56. 'agent_id' => $toAgentId, 'type' => 'transfer_in', 'amount' => $amount,
  57. 'before_balance' => $toBefore, 'after_balance' => $to->balance,
  58. 'reference_type' => 'agent_transfer', 'reference_id' => $fromAgentId,
  59. 'operator_type' => 'agent', 'operator_id' => $operatorId,
  60. 'idempotency_key' => $idempotencyKey . ':in', 'remark' => $remark,
  61. ]);
  62. return ['from_balance' => (string) $from->balance, 'to_balance' => (string) $to->balance];
  63. });
  64. }
  65. public function adjust(
  66. int $agentId,
  67. string $amount,
  68. string $type,
  69. string $remark,
  70. string $idempotencyKey,
  71. string $operatorType,
  72. ?int $operatorId,
  73. string $referenceType = '',
  74. ?int $referenceId = null
  75. ): AgentBalanceLog {
  76. if (!in_array($type, ['credit', 'debit', 'commission', 'withdraw_refund'], true)) {
  77. throw new \InvalidArgumentException('资金变动类型错误');
  78. }
  79. if (bccomp($amount, '0', 4) <= 0) {
  80. throw new \InvalidArgumentException('金额必须大于0');
  81. }
  82. return DB::transaction(function () use ($agentId, $amount, $type, $remark, $idempotencyKey, $operatorType, $operatorId, $referenceType, $referenceId) {
  83. $existing = AgentBalanceLog::query()->where('idempotency_key', $idempotencyKey)->first();
  84. if ($existing) {
  85. $this->assertSameAdjustment($existing, $agentId, $type, $amount);
  86. return $existing;
  87. }
  88. $agent = Agent::query()->lockForUpdate()->findOrFail($agentId);
  89. $existing = AgentBalanceLog::query()->where('idempotency_key', $idempotencyKey)->first();
  90. if ($existing) {
  91. $this->assertSameAdjustment($existing, $agentId, $type, $amount);
  92. return $existing;
  93. }
  94. $before = bcadd((string) $agent->balance, '0', 4);
  95. $signed = $type === 'debit' ? bcsub('0', $amount, 4) : bcadd($amount, '0', 4);
  96. $after = bcadd($before, $signed, 4);
  97. if (bccomp($after, '0', 4) < 0) {
  98. throw new \RuntimeException('代理余额不足');
  99. }
  100. $agent->balance = $after;
  101. $agent->save();
  102. return AgentBalanceLog::query()->create([
  103. 'agent_id' => $agentId, 'type' => $type, 'amount' => $signed,
  104. 'before_balance' => $before, 'after_balance' => $after,
  105. 'operator_type' => $operatorType, 'operator_id' => $operatorId,
  106. 'idempotency_key' => $idempotencyKey, 'remark' => $remark,
  107. 'reference_type' => $referenceType, 'reference_id' => $referenceId,
  108. ]);
  109. });
  110. }
  111. private function assertSameTransfer(AgentBalanceLog $log, int $fromAgentId, int $toAgentId, string $amount): void
  112. {
  113. $expected = bcsub('0', $amount, 4);
  114. if ((int) $log->agent_id !== $fromAgentId || (int) $log->reference_id !== $toAgentId || bccomp((string) $log->amount, $expected, 4) !== 0) {
  115. throw new \RuntimeException('request_id 已用于其他资金操作');
  116. }
  117. }
  118. private function assertSameAdjustment(AgentBalanceLog $log, int $agentId, string $type, string $amount): void
  119. {
  120. $expected = $type === 'debit' ? bcsub('0', $amount, 4) : bcadd($amount, '0', 4);
  121. if ((int) $log->agent_id !== $agentId || $log->type !== $type || bccomp((string) $log->amount, $expected, 4) !== 0) {
  122. throw new \RuntimeException('request_id 已用于其他资金操作');
  123. }
  124. }
  125. }