Home.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Models\Recharge;
  7. use App\Services\PaymentOrderService;
  8. use Illuminate\Validation\ValidationException;
  9. use Exception;
  10. use Carbon\Carbon;
  11. use App\Models\Withdraw;
  12. class Home extends Controller
  13. {
  14. public function index()
  15. {
  16. try {
  17. request()->validate([
  18. 'start_date' => ['nullable', 'date_format:Y-m-d'],
  19. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  20. ]);
  21. $start = request()->input('start_date');
  22. $end = request()->input('end_date');
  23. if (empty($end)) $end = date('Y-m-d');
  24. if (empty($start)) $start = date('Y-m-d');
  25. $end = Carbon::createFromFormat('Y-m-d', $end)->addDay()->format('Y-m-d');
  26. $query = PaymentOrder::query();
  27. $query->where('type', 2)->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)->sum('amount');
  42. $query = Recharge::query();
  43. $query->whereBetween('created_at', [$start, $end]);
  44. $query1 = clone $query;
  45. $rechargeTotalAmount = (float)$query1->sum('amount');
  46. $query1 = clone $query;
  47. $rechargeTotalSuccess = (float)$query1->where('status', 1)->sum('amount');
  48. $query1 = clone $query;
  49. $rechargeTotalFail = (float)$query1->where('status', 2)->sum('amount');
  50. $result = [
  51. 'start' => $start,
  52. 'end' => $end,
  53. 'recharge' => [
  54. 'total_amount' => $rechargeTotalAmount,
  55. 'total_success' => $rechargeTotalSuccess,
  56. 'total_fail' => $rechargeTotalFail,
  57. ],
  58. 'withdraw_usdt' => [
  59. 'total_fail' => $usdTotalFail,
  60. 'total_success' => $usdTotalSuccess,
  61. 'total_amount' => $usdTotalAmount,
  62. ],
  63. 'withdraw_rmb' => [
  64. 'total_fail' => $totalFail,
  65. 'total_success' => $totalSuccess,
  66. 'total_amount' => $totalAmount,
  67. ]
  68. ];
  69. } catch (ValidationException $e) {
  70. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  71. } catch (Exception $e) {
  72. return $this->error($e->getCode(), $e->getmessage());
  73. }
  74. return $this->success($result);
  75. }
  76. }