Bet.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. //如果有指定用户和 时间区间,则统计投注额(时间区间需在30天内)
  41. if ((isset($params['member_id']) || isset($params['username'])) && isset($params['start_time'])) {
  42. $start = Carbon::parse($params['start_time']);
  43. $end = Carbon::parse($params['end_time']);
  44. if ($end->diffInDays($start) < 30) $totalAmount = $query->sum('amount');
  45. }
  46. $query->with(['user', 'issue', 'pcIssue'])->orderBy('id', 'desc');
  47. $list = $query->forPage($page, $limit)->get()->toArray();
  48. foreach ($list as &$item) {
  49. $item['is_winner'] = null;
  50. $item['created_at'] = date('Y-m-d H:i', strtotime($item['created_at']));
  51. $issue = $item['issue'] ?: $item['pc_issue'];
  52. $item['winning_array'] = $issue['winning_array'];
  53. unset($item['issue'], $item['pc_issue']);
  54. if ($item['status'] == 2) {
  55. $item['is_winner'] = $item['profit'] > 0;
  56. }
  57. }
  58. $result = ['total' => $count, 'total_amount' => $totalAmount, 'data' => $list];
  59. } catch (ValidationException $e) {
  60. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  61. } catch (Exception $e) {
  62. return $this->error($e->getCode(), $e->getMessage());
  63. }
  64. return $this->success($result);
  65. }
  66. // 模拟下注
  67. public function fake()
  68. {
  69. BetService::randomVirtualBetting(3);
  70. return 'success';
  71. }
  72. }