Bet.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\BetService;
  6. use Carbon\Carbon;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Validation\ValidationException;
  9. use Exception;
  10. use App\Models\Bet as BetModel;
  11. class Bet extends Controller
  12. {
  13. function index()
  14. {
  15. try {
  16. $params = request()->validate([
  17. 'page' => ['nullable', 'integer', 'min:1'],
  18. 'limit' => ['nullable', 'integer', 'min:1'],
  19. 'member_id' => ['nullable', 'string'],
  20. 'keywords' => ['nullable', 'string', 'min:1'],
  21. 'issue_no' => ['nullable', 'string'],
  22. 'id' => ['nullable', 'string'],
  23. 'status' => ['nullable', 'integer', 'in:1,2'],
  24. 'username' => ['nullable', 'string', 'min:1'],
  25. 'start_time' => ['required', 'date', 'date_format:Y-m-d', 'required_with:end_time'],
  26. 'end_time' => ['required', 'date', 'date_format:Y-m-d', 'required_with:start_time',
  27. function ($attribute, $value, $fail) {
  28. $startTime = request('start_time');
  29. if ($startTime) {
  30. $start = Carbon::parse($startTime);
  31. $end = Carbon::parse($value);
  32. if ($end->diffInDays($start) > 30) {
  33. $fail('The end time must be within a month of the start time.');
  34. }
  35. }
  36. }],
  37. 'is_winner' => ['nullable', 'integer', 'in:0,1'],
  38. ]);
  39. $page = request()->input('page', 1);
  40. $limit = request()->input('limit', 10);
  41. $query = BetModel::where(BetService::getWhere($params));
  42. if (isset($params['username']) && !empty($params['username'])) {
  43. $username = $params['username'];
  44. $query = $query->whereHas('user', function ($query) use ($username) {
  45. $query->where('username', $username);
  46. });
  47. }
  48. $count = $query->count();
  49. $query1 = clone $query;
  50. $totalAmount = $query1->sum('amount');
  51. $query->with(['user', 'issue', 'pcIssue'])->orderBy('id', 'desc');
  52. $list = $query->forPage($page, $limit)->get()->toArray();
  53. foreach ($list as &$item) {
  54. $item['is_winner'] = null;
  55. $item['created_at'] = date('Y-m-d H:i', strtotime($item['created_at']));
  56. $issue = $item['issue'] ?: $item['pc_issue'];
  57. $item['winning_array'] = $issue['winning_array'];
  58. unset($item['issue'], $item['pc_issue']);
  59. if ($item['status'] == 2) {
  60. $item['is_winner'] = $item['profit'] > 0;
  61. }
  62. }
  63. $result = ['total' => $count, 'total_amount' => $totalAmount, 'data' => $list];
  64. } catch (ValidationException $e) {
  65. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  66. } catch (Exception $e) {
  67. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  68. }
  69. return $this->success($result);
  70. }
  71. // 模拟下注
  72. public function fake()
  73. {
  74. BetService::randomVirtualBetting(3);
  75. return 'success';
  76. }
  77. }