Settle.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Http\Controllers\api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\LhcOrder;
  5. use App\Models\LhcOrderItem;
  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. if (!app()->environment('local')) {
  18. abort(404);
  19. }
  20. $issue_no = $request->input('issue_no');
  21. $member_id = $request->input('member_id');
  22. $keywords = $request->input('keywords');
  23. $amount = floatval($request->input('amount', 0));
  24. $fake_time = $request->input('fake_time');
  25. $game = $request->input('game', 'pc28');
  26. $gameTypeMap = ['pc28' => 6, 'ca28' => 5];
  27. if (!isset($gameTypeMap[$game])) {
  28. return response()->json(['code' => 400, 'msg' => 'unsupported game, use: ' . implode(',', array_keys($gameTypeMap))]);
  29. }
  30. $type = $gameTypeMap[$game];
  31. if (!$issue_no || !$member_id || !$keywords || $amount <= 0) {
  32. return response()->json(['code' => 400, 'msg' => 'missing params']);
  33. }
  34. $issue = PcIssue::where('issue_no', $issue_no)->first();
  35. if (!$issue) {
  36. return response()->json(['code' => 404, 'msg' => 'issue not found']);
  37. }
  38. if ($issue->status != PcIssue::STATUS_DRAW) {
  39. return response()->json(['code' => 400, 'msg' => 'issue not drawn']);
  40. }
  41. $awards = $issue->award;
  42. if (!in_array($keywords, $awards)) {
  43. return response()->json(['code' => 400, 'msg' => 'not winning, awards: ' . implode(',', $awards)]);
  44. }
  45. $rule = GameplayRuleService::getGameplayRules($keywords);
  46. if (!$rule || floatval($rule['odds']) <= 0) {
  47. return response()->json(['code' => 400, 'msg' => 'odds not found']);
  48. }
  49. $odds = floatval($rule['odds']);
  50. $win_amount = bcmul($amount, $odds, 2);
  51. if ($win_amount > 1000000) $win_amount = 1000000;
  52. $yl = bcsub($win_amount, $amount, 2);
  53. $bet_ts = $fake_time
  54. ? strtotime($fake_time)
  55. : (strtotime($issue->end_time) - rand(60, 300));
  56. DB::beginTransaction();
  57. try {
  58. $order = LhcOrder::create([
  59. 'amount' => $amount,
  60. 'number' => $keywords,
  61. 'member_id' => $member_id,
  62. 'issue' => $issue_no,
  63. 'lhc_number_id' => $issue->id,
  64. 'odds' => $odds,
  65. 'type' => $type,
  66. 'lottery_status' => LhcOrder::STATUS_WIN,
  67. 'win_amount' => $win_amount,
  68. 'profit_and_loss' => $yl,
  69. 'total_amount' => $amount,
  70. 'created_at' => $bet_ts,
  71. 'updated_at' => $bet_ts,
  72. ]);
  73. LhcOrderItem::create([
  74. 'ordernum' => $order->ordernum,
  75. 'number' => $keywords,
  76. 'member_id' => $member_id,
  77. 'lottery_status' => LhcOrder::STATUS_WIN,
  78. 'win_amount' => $win_amount,
  79. 'profit_and_loss' => $yl,
  80. 'created_at' => $bet_ts,
  81. 'updated_at' => $bet_ts,
  82. ]);
  83. $walletInfo = WalletService::findOne(['member_id' => $member_id]);
  84. if (!$walletInfo) {
  85. DB::rollBack();
  86. return response()->json(['code' => 404, 'msg' => 'wallet not found']);
  87. }
  88. $balance = floatval($walletInfo['available_balance']);
  89. WalletService::updateBalance($member_id, $win_amount);
  90. BalanceLogService::addLog(
  91. $member_id,
  92. $win_amount,
  93. $balance,
  94. bcadd($balance, $win_amount, 2),
  95. '中奖',
  96. $order->id,
  97. "盈利:{$yl}"
  98. );
  99. DB::commit();
  100. return response()->json([
  101. 'code' => 200,
  102. 'issue_no' => $issue_no,
  103. 'keywords' => $keywords,
  104. 'awards' => $awards,
  105. 'amount' => $amount,
  106. 'odds' => $odds,
  107. 'win_amount' => $win_amount,
  108. 'profit' => $yl,
  109. 'balance_before' => $balance,
  110. 'balance_after' => bcadd($balance, $win_amount, 2),
  111. 'order_id' => $order->id,
  112. 'bet_time' => date('Y-m-d H:i:s', $bet_ts),
  113. ]);
  114. } catch (\Exception $e) {
  115. DB::rollBack();
  116. Log::error('settle_inject: ' . $e->getMessage());
  117. return response()->json(['code' => 500, 'msg' => $e->getMessage()]);
  118. }
  119. }
  120. }