Bet.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:end_time'],
  26. 'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:start_time'],
  27. 'is_winner' => ['nullable', 'integer', 'in:0,1'],
  28. ]);
  29. $page = request()->input('page', 1);
  30. $limit = request()->input('limit', 10);
  31. $query = BetModel::where(BetService::getWhere($params));
  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. $totalAmount = '--';
  40. if ((isset($params['member_id']) || isset($params['username'])) && isset($params['start_time'])) {
  41. $start = Carbon::parse($params['start_time']);
  42. $end = Carbon::parse($params['end_time']);
  43. if ($end->diffInDays($start) < 30) $totalAmount = $query->sum('amount');
  44. }
  45. $query->with(['user', 'issue', 'pcIssue'])->orderBy('id', 'desc');
  46. $list = $query->forPage($page, $limit)->get()->toArray();
  47. foreach ($list as &$item) {
  48. $item['is_winner'] = null;
  49. $item['created_at'] = date('Y-m-d H:i', strtotime($item['created_at']));
  50. $issue = $item['issue'] ?: $item['pc_issue'];
  51. $item['winning_array'] = $issue['winning_array'];
  52. unset($item['issue'], $item['pc_issue']);
  53. if ($item['status'] == 2) {
  54. $item['is_winner'] = $item['profit'] > 0;
  55. }
  56. }
  57. $result = ['total' => $count, 'total_amount' => $totalAmount, 'data' => $list];
  58. } catch (ValidationException $e) {
  59. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  60. } catch (Exception $e) {
  61. return $this->error($e->getCode(), $e->getMessage());
  62. }
  63. return $this->success($result);
  64. }
  65. // 模拟下注
  66. public function fake()
  67. {
  68. BetService::randomVirtualBetting(3);
  69. return 'success';
  70. }
  71. }