Home.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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->whereBetween('created_at', [$start, $end]);
  28. $query1 = clone $query;
  29. $totalAmount = (float)$query1->where('type', 2)->sum('amount');
  30. $query1 = clone $query;
  31. $totalSuccess = (float)$query1->where('type', 2)->whereIn('status', [1, 2])->sum('amount');
  32. $query1 = clone $query;
  33. $totalFail = (float)$query1->where('type', 2)->where('status', 3)->sum('amount');
  34. $query1 = clone $query;
  35. $rechargeRmbTotalAmount = (float)$query1->where('type', 1)->sum('amount');
  36. $query1 = clone $query;
  37. $rechargeRmbTotalSuccess = (float)$query1->where('type', 1)->whereIn('status', [1, 2])->sum('amount');
  38. $query1 = clone $query;
  39. $rechargeRmbTotalFail = (float)$query1->where('type', 1)->where('status', 3)->sum('amount');
  40. $query = Withdraw::query();
  41. $query->whereBetween('created_at', [$start, $end]);
  42. $query1 = clone $query;
  43. $usdTotalAmount = (float)$query1->sum('amount');
  44. $query1 = clone $query;
  45. $usdTotalSuccess = (float)$query1->where('status', 1)->sum('amount');
  46. $query1 = clone $query;
  47. $usdTotalFail = (float)$query1->where('status', 2)->sum('amount');
  48. $query = Recharge::query();
  49. $query->whereBetween('created_at', [$start, $end]);
  50. $query1 = clone $query;
  51. $rechargeTotalAmount = (float)$query1->sum('amount');
  52. $query1 = clone $query;
  53. $rechargeTotalSuccess = (float)$query1->where('status', 1)->sum('amount');
  54. $query1 = clone $query;
  55. $rechargeTotalFail = (float)$query1->where('status', 2)->sum('amount');
  56. $result = [
  57. 'start' => $start,
  58. 'end' => $end,
  59. 'recharge_usd' => [
  60. 'total_amount' => $rechargeTotalAmount,
  61. 'total_success' => $rechargeTotalSuccess,
  62. 'total_fail' => $rechargeTotalFail,
  63. ],
  64. 'recharge_rmb' => [
  65. 'total_amount' => $rechargeRmbTotalAmount,
  66. 'total_success' => $rechargeRmbTotalSuccess,
  67. 'total_fail' => $rechargeRmbTotalFail,
  68. ],
  69. 'withdraw_usdt' => [
  70. 'total_fail' => $usdTotalFail,
  71. 'total_success' => $usdTotalSuccess,
  72. 'total_amount' => $usdTotalAmount,
  73. ],
  74. 'withdraw_rmb' => [
  75. 'total_fail' => $totalFail,
  76. 'total_success' => $totalSuccess,
  77. 'total_amount' => $totalAmount,
  78. ]
  79. ];
  80. } catch (ValidationException $e) {
  81. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  82. } catch (Exception $e) {
  83. return $this->error($e->getCode(), $e->getmessage());
  84. }
  85. return $this->success($result);
  86. }
  87. }