Bet.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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' => ['required', 'date', 'date_format:Y-m-d', 'required_with:end_time'],
  25. 'end_time' => ['required', '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. if (isset($params['username']) && !empty($params['username'])) {
  32. $username = $params['username'];
  33. $query = $query->whereHas('user', function ($query) use ($username) {
  34. $query->where('username', $username);
  35. });
  36. }
  37. $count = $query->count();
  38. $query1 = clone $query;
  39. $totalAmount = $query1->sum('amount');
  40. $query->with(['user', 'issue', 'pcIssue'])->orderBy('id', 'desc');
  41. $list = $query->forPage($page, $limit)->get()->toArray();
  42. foreach ($list as &$item) {
  43. $item['is_winner'] = null;
  44. $item['created_at'] = date('Y-m-d H:i', strtotime($item['created_at']));
  45. $issue = $item['issue'] ?: $item['pc_issue'];
  46. $item['winning_array'] = $issue['winning_array'];
  47. unset($item['issue'], $item['pc_issue']);
  48. if ($item['status'] == 2) {
  49. $item['is_winner'] = $item['profit'] > 0;
  50. }
  51. }
  52. $result = ['total' => $count,'total_amount'=>$totalAmount, 'data' => $list];
  53. } catch (ValidationException $e) {
  54. return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
  55. } catch (Exception $e) {
  56. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  57. }
  58. return $this->success($result);
  59. }
  60. // 模拟下注
  61. public function fake()
  62. {
  63. BetService::randomVirtualBetting(3);
  64. return 'success';
  65. }
  66. }