Home.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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', date('Y-m-d'));
  21. $end = request()->input('end_date', date('Y-m-d'));
  22. $end = Carbon::createFromFormat('Y-m-d', $end)->addDay()->format('Y-m-d');
  23. $query = PaymentOrder::query();
  24. $params['type'] = 2;
  25. $where = PaymentOrderService::getWhere($params);
  26. $query->where($where)->whereBetween('created_at', [$start, $end]);
  27. $query1 = clone $query;
  28. $totalAmount = (float)$query1->sum('amount');
  29. $query1 = clone $query;
  30. $totalSuccess = (float)$query1->whereIn('status', [1, 2])->sum('amount');
  31. $query1 = clone $query;
  32. $totalFail = (float)$query1->where('status', 3)->sum('amount');
  33. $query = Withdraw::query();
  34. $query->whereBetween('created_at', [$start, $end]);
  35. $query1 = clone $query;
  36. $usdTotalAmount = (float)$query1->sum('amount');
  37. $query1 = clone $query;
  38. $usdTotalSuccess = (float)$query1->where('status', 1)->sum('amount');
  39. $query1 = clone $query;
  40. $usdTotalFail = (float)$query1->where('status', 2)
  41. ->sum('amount');
  42. $query1 = clone $query;
  43. $sql = $query1->where('status', 2)->toSql();
  44. $bindings = $query1->getBindings();
  45. // 将绑定的参数替换到 SQL 查询中
  46. foreach ($bindings as $binding) {
  47. // 使用 ? 替换成绑定的值
  48. $sql = preg_replace('/\?/', "'" . addslashes($binding) . "'", $sql, 1);
  49. }
  50. $result = [
  51. 'withdraw_usdt' => [
  52. 'total_fail' => $usdTotalFail,
  53. 'total_success' => $usdTotalSuccess,
  54. 'total_amount' => $usdTotalAmount,
  55. 'sql'=>$sql
  56. ],
  57. 'withdraw_rmb' => [
  58. 'total_fail' => $totalFail,
  59. 'total_success' => $totalSuccess,
  60. 'total_amount' => $totalAmount,
  61. ]
  62. ];
  63. } catch (ValidationException $e) {
  64. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  65. } catch (Exception $e) {
  66. return $this->error($e->getCode(), $e->getmessage());
  67. }
  68. return $this->success($result);
  69. }
  70. }