Settle.php 5.2 KB

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