Settle.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Http\Controllers\api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Bet;
  5. use App\Models\Issue;
  6. use App\Models\PcIssue;
  7. use App\Services\BalanceLogService;
  8. use App\Services\GameplayRuleService;
  9. use App\Services\WalletService;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Log;
  13. class Settle extends Controller
  14. {
  15. public function inject(Request $request)
  16. {
  17. $issue_no = $request->input('issue_no');
  18. $member_id = $request->input('member_id');
  19. $keywords = $request->input('keywords');
  20. $amount = floatval($request->input('amount', 0));
  21. $fake_time = $request->input('fake_time');
  22. $game = $request->input('game', 'pc28');
  23. $gameTypeMap = ['pc28' => 6, 'ca28' => 5];
  24. if (!isset($gameTypeMap[$game])) {
  25. return response()->json(['code' => 400, 'msg' => 'unsupported game, use: ' . implode(',', array_keys($gameTypeMap))]);
  26. }
  27. $type = $gameTypeMap[$game];
  28. if (!$issue_no || !$member_id || !$keywords || $amount <= 0) {
  29. return response()->json(['code' => 400, 'msg' => 'missing params']);
  30. }
  31. $issueModel = ($game === 'ca28') ? Issue::class : PcIssue::class;
  32. $issue = $issueModel::where('issue_no', $issue_no)->first();
  33. if (!$issue) {
  34. return response()->json(['code' => 404, 'msg' => 'issue not found']);
  35. }
  36. if ($issue->status != PcIssue::STATUS_DRAW) {
  37. return response()->json(['code' => 400, 'msg' => 'issue not drawn']);
  38. }
  39. $awards = $issue->award;
  40. if (!in_array($keywords, $awards)) {
  41. return response()->json(['code' => 400, 'msg' => 'not winning, awards: ' . implode(',', $awards)]);
  42. }
  43. $rule = GameplayRuleService::getGameplayRules($keywords);
  44. if (!$rule || floatval($rule['odds']) <= 0) {
  45. return response()->json(['code' => 400, 'msg' => 'odds not found']);
  46. }
  47. $odds = floatval($rule['odds']);
  48. $win_amount = bcmul($amount, $odds, 2);
  49. if ($win_amount > 1000000) $win_amount = 1000000;
  50. $yl = bcsub($win_amount, $amount, 2);
  51. $bet_ts = $fake_time
  52. ? strtotime($fake_time)
  53. : (strtotime($issue->end_time) - rand(60, 300));
  54. DB::beginTransaction();
  55. try {
  56. $order = new Bet();
  57. $order->fill([
  58. 'issue_no' => $issue_no,
  59. 'keywords' => $keywords,
  60. 'amount' => $amount,
  61. 'odds' => $odds,
  62. 'status' => Bet::STATUS_SETTLED,
  63. 'profit' => $win_amount,
  64. 'issue_id' => $issue->id,
  65. 'member_id' => $member_id,
  66. 'user_id' => $member_id,
  67. ]);
  68. $order->timestamps = false;
  69. $order->created_at = $bet_ts;
  70. $order->updated_at = $bet_ts;
  71. $order->save();
  72. $walletInfo = WalletService::findOne(['member_id' => $member_id]);
  73. if (!$walletInfo) {
  74. DB::rollBack();
  75. return response()->json(['code' => 404, 'msg' => 'wallet not found']);
  76. }
  77. $balance = floatval($walletInfo['available_balance']);
  78. $betType = ($game === 'ca28') ? '加拿大28投注' : '极速28投注';
  79. // 扣下注金额
  80. WalletService::updateBalance($member_id, -$amount);
  81. $balance_after_bet = bcsub($balance, $amount, 2);
  82. BalanceLogService::addLog(
  83. $member_id,
  84. $amount,
  85. $balance,
  86. $balance_after_bet,
  87. $betType,
  88. $order->id,
  89. "期号:{$issue_no}"
  90. );
  91. // 加中奖金额
  92. WalletService::updateBalance($member_id, $win_amount);
  93. $balance_after_win = bcadd($balance_after_bet, $win_amount, 2);
  94. BalanceLogService::addLog(
  95. $member_id,
  96. $win_amount,
  97. $balance_after_bet,
  98. $balance_after_win,
  99. '中奖',
  100. $order->id,
  101. "盈利:{$yl}"
  102. );
  103. DB::commit();
  104. return response()->json([
  105. 'code' => 200,
  106. 'issue_no' => $issue_no,
  107. 'keywords' => $keywords,
  108. 'awards' => $awards,
  109. 'amount' => $amount,
  110. 'odds' => $odds,
  111. 'win_amount' => $win_amount,
  112. 'profit' => $yl,
  113. 'balance_before' => $balance,
  114. 'balance_after' => $balance_after_win,
  115. 'order_id' => $order->id,
  116. 'bet_time' => date('Y-m-d H:i:s', $bet_ts),
  117. ]);
  118. } catch (\Exception $e) {
  119. DB::rollBack();
  120. Log::error('settle_inject: ' . $e->getMessage());
  121. return response()->json(['code' => 500, 'msg' => $e->getMessage()]);
  122. }
  123. }
  124. }