Home.php 2.9 KB

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