Bet.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 Illuminate\Support\Facades\DB;
  7. use Illuminate\Validation\ValidationException;
  8. use Exception;
  9. use App\Models\Bet as BetModel;
  10. class Bet extends Controller
  11. {
  12. function index()
  13. {
  14. try {
  15. $params = request()->validate([
  16. 'page' => ['nullable', 'integer', 'min:1'],
  17. 'limit' => ['nullable', 'integer', 'min:1'],
  18. 'member_id' => ['nullable', 'string'],
  19. 'keywords' => ['nullable', 'string', 'min:1'],
  20. 'issue_no' => ['nullable', 'string'],
  21. 'id' => ['nullable', 'string'],
  22. 'status' => ['nullable', 'integer', 'in:1,2'],
  23. 'username' => ['nullable', 'string', 'min:1'],
  24. 'start_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:end_time'],
  25. 'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:start_time'],
  26. 'is_winner' => ['nullable', 'integer', 'in:0,1'],
  27. ]);
  28. $page = request()->input('page', 1);
  29. $limit = request()->input('limit', 10);
  30. $query = BetModel::where(BetService::getWhere($params))
  31. ->with(['user', 'issue', 'pcIssue']);
  32. if (isset($params['username']) && !empty($params['username'])) {
  33. $username = $params['username'];
  34. $query = $query->whereHas('user', function ($query) use ($username) {
  35. $query->where('username', $username);
  36. });
  37. }
  38. $count = $query->count();
  39. $query->orderBy('id', 'desc');
  40. $list = $query->forPage($page, $limit)->get()->toArray();
  41. $result = ['total' => $count, 'data' => $list];
  42. foreach ($result['data'] as &$item) {
  43. $item['is_winner'] = null;
  44. $issue = $item['issue'] ?: $item['pc_issue'];
  45. $item['winning_array'] = $issue['winning_array'];
  46. unset($item['issue'], $item['pc_issue']);
  47. if ($item['status'] == 2) {
  48. $item['is_winner'] = $item['profit'] > 0;
  49. }
  50. }
  51. } catch (ValidationException $e) {
  52. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  53. } catch (Exception $e) {
  54. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  55. }
  56. return $this->success($result);
  57. }
  58. // 模拟下注
  59. public function fake()
  60. {
  61. BetService::randomVirtualBetting(3);
  62. return 'success';
  63. }
  64. }