RewardGrantService.php 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\HttpStatus;
  4. use App\Models\Backflow;
  5. use App\Models\Rebate;
  6. use App\Models\Wallet;
  7. use App\Models\WalletBonus;
  8. use Carbon\Carbon;
  9. use Illuminate\Support\Facades\DB;
  10. use RuntimeException;
  11. /** Shared transaction for manual and automatic payouts. Never trusts a previously loaded reward row. */
  12. class RewardGrantService
  13. {
  14. public static function grant(string $type, int $id, string $operator, bool $skipPaid = false, ?string $scheduledCutoff = null): bool
  15. {
  16. if (!in_array($type, RewardGrantSettingsService::TYPES, true)) {
  17. throw new RuntimeException('奖励类型错误', HttpStatus::CUSTOM_ERROR);
  18. }
  19. return DB::transaction(function () use ($type, $id, $operator, $skipPaid, $scheduledCutoff) {
  20. $model = $type === 'rebate' ? Rebate::class : Backflow::class;
  21. $reward = $model::whereKey($id)->lockForUpdate()->first();
  22. if (!$reward) throw new RuntimeException('数据不存在', HttpStatus::CUSTOM_ERROR);
  23. if ((int) $reward->status !== 0) {
  24. if ($skipPaid) return false;
  25. throw new RuntimeException('奖励已发放,无需再次发放', HttpStatus::CUSTOM_ERROR);
  26. }
  27. $cutoff = $type === 'rebate'
  28. ? Carbon::now('America/New_York')->format('Y-m-d')
  29. : Carbon::now('Asia/Shanghai')->format('Y-m');
  30. if ((string) $reward->date >= $cutoff) {
  31. throw new RuntimeException('未到发放时间', HttpStatus::CUSTOM_ERROR);
  32. }
  33. if ($scheduledCutoff !== null && (string) $reward->date >= $scheduledCutoff) return false;
  34. if ($type === 'rebate') {
  35. $result = RewardBasisService::rebate($reward);
  36. $basis = $result['effective_amount'];
  37. $reward->betting_amount = $result['betting_amount'];
  38. $reward->effective_betting_amount = $basis;
  39. $reward->rebate_ratio = bccomp($basis, '0', 2) > 0 ? bcdiv($result['amount'], $basis, 6) : '0';
  40. $reward->audited_by = $operator;
  41. $changeType = '笔笔返';
  42. $remark = "日期:{$reward->date};有效投注:{$basis}";
  43. foreach ($result['details'] as $detail) {
  44. if ($detail['rule_id'] === null) continue;
  45. $remark .= ";{$detail['label']}:{$detail['effective_amount']}×{$detail['percentage']}%";
  46. }
  47. } else {
  48. $result = RewardBasisService::backflow($reward);
  49. $reward->loss_amount = $result['loss_amount'];
  50. $reward->backflow_ratio = '0';
  51. $changeType = '回水';
  52. $remark = "日期:{$reward->date};亏损:{$result['loss_amount']}";
  53. foreach ($result['details'] as $detail) {
  54. if ($detail['rule_id'] === null) continue;
  55. $remark .= ";{$detail['label']}亏损{$detail['loss_amount']}奖励{$detail['amount']}";
  56. }
  57. }
  58. $amount = $result['amount'];
  59. $reward->details = $result['details'];
  60. $reward->need_flow = $result['need_flow'];
  61. if (bccomp($amount, '0', 2) > 0) {
  62. $wallet = Wallet::where('member_id', $reward->member_id)->lockForUpdate()->first();
  63. if (!$wallet) throw new RuntimeException('会员钱包不存在', HttpStatus::CUSTOM_ERROR);
  64. $before = (string) $wallet->available_balance;
  65. $wallet->available_balance = bcadd($before, $amount, 10);
  66. if (!$wallet->save()) throw new RuntimeException('余额更新失败', HttpStatus::CUSTOM_ERROR);
  67. BalanceLogService::addLog($reward->member_id, $amount, $before, $wallet->available_balance,
  68. $changeType, $reward->id, $remark);
  69. if (bccomp($result['need_flow'], '0', 2) > 0) {
  70. WalletBonus::addData($reward->member_id, $type === 'rebate' ? 4 : 5, $amount, 0, $reward->id, $result['need_flow']);
  71. }
  72. }
  73. $reward->amount = $amount;
  74. $reward->status = 1;
  75. $reward->grant_time = Carbon::now()->timestamp;
  76. if (!$reward->save()) throw new RuntimeException('发放失败', HttpStatus::CUSTOM_ERROR);
  77. return true;
  78. }, 3);
  79. }
  80. }