Home.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\PaymentOrder;
  6. use App\Services\PaymentOrderService;
  7. use Illuminate\Validation\ValidationException;
  8. use Exception;
  9. use Carbon\Carbon;
  10. use App\Models\Withdraw;
  11. class Home extends Controller
  12. {
  13. public function index()
  14. {
  15. try {
  16. request()->validate([
  17. 'start_date' => ['nullable', 'date_format:Y-m-d'],
  18. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  19. ]);
  20. $start = request()->input('start_date');
  21. $end = request()->input('end_date');
  22. if(empty($end))$end = date('Y-m-d');
  23. if(empty($start))$start = date('Y-m-d');
  24. return $this->success($end);
  25. $end = Carbon::createFromFormat('Y-m-d', $end)->addDay()->format('Y-m-d');
  26. $query = PaymentOrder::query();
  27. $params['type'] = 2;
  28. $where = PaymentOrderService::getWhere($params);
  29. $query->where($where)->whereBetween('created_at', [$start, $end]);
  30. $query1 = clone $query;
  31. $totalAmount = (float)$query1->sum('amount');
  32. $query1 = clone $query;
  33. $totalSuccess = (float)$query1->whereIn('status', [1, 2])->sum('amount');
  34. $query1 = clone $query;
  35. $totalFail = (float)$query1->where('status', 3)->sum('amount');
  36. $query = Withdraw::query();
  37. $query->whereBetween('created_at', [$start, $end]);
  38. $query1 = clone $query;
  39. $usdTotalAmount = (float)$query1->sum('amount');
  40. $query1 = clone $query;
  41. $usdTotalSuccess = (float)$query1->where('status', 1)->sum('amount');
  42. $query1 = clone $query;
  43. $usdTotalFail = (float)$query1->where('status', 2)
  44. ->sum('amount');
  45. $query1 = clone $query;
  46. $sql = $query1->where('status', 2)->toSql();
  47. $bindings = $query1->getBindings();
  48. // 将绑定的参数替换到 SQL 查询中
  49. foreach ($bindings as $binding) {
  50. // 使用 ? 替换成绑定的值
  51. $sql = preg_replace('/\?/', "'" . addslashes($binding) . "'", $sql, 1);
  52. }
  53. $result = [
  54. 'withdraw_usdt' => [
  55. 'total_fail' => $usdTotalFail,
  56. 'total_success' => $usdTotalSuccess,
  57. 'total_amount' => $usdTotalAmount,
  58. 'sql'=>$sql
  59. ],
  60. 'withdraw_rmb' => [
  61. 'total_fail' => $totalFail,
  62. 'total_success' => $totalSuccess,
  63. 'total_amount' => $totalAmount,
  64. ]
  65. ];
  66. } catch (ValidationException $e) {
  67. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  68. } catch (Exception $e) {
  69. return $this->error($e->getCode(), $e->getmessage());
  70. }
  71. return $this->success($result);
  72. }
  73. }