Home.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. $end = Carbon::createFromFormat('Y-m-d', $end)->addDay()->format('Y-m-d');
  25. $query = PaymentOrder::query();
  26. $params['type'] = 2;
  27. $where = PaymentOrderService::getWhere($params);
  28. $query->where($where)->whereBetween('created_at', [$start, $end]);
  29. $query1 = clone $query;
  30. $totalAmount = (float)$query1->sum('amount');
  31. $query1 = clone $query;
  32. $totalSuccess = (float)$query1->whereIn('status', [1, 2])->sum('amount');
  33. $query1 = clone $query;
  34. $totalFail = (float)$query1->where('status', 3)->sum('amount');
  35. $query = Withdraw::query();
  36. $query->whereBetween('created_at', [$start, $end]);
  37. $query1 = clone $query;
  38. $usdTotalAmount = (float)$query1->sum('amount');
  39. $query1 = clone $query;
  40. $usdTotalSuccess = (float)$query1->where('status', 1)->sum('amount');
  41. $query1 = clone $query;
  42. $usdTotalFail = (float)$query1->where('status', 2)
  43. ->sum('amount');
  44. $result = [
  45. 'withdraw_usdt' => [
  46. 'total_fail' => $usdTotalFail,
  47. 'total_success' => $usdTotalSuccess,
  48. 'total_amount' => $usdTotalAmount,
  49. ],
  50. 'withdraw_rmb' => [
  51. 'total_fail' => $totalFail,
  52. 'total_success' => $totalSuccess,
  53. 'total_amount' => $totalAmount,
  54. ]
  55. ];
  56. } catch (ValidationException $e) {
  57. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  58. } catch (Exception $e) {
  59. return $this->error($e->getCode(), $e->getmessage());
  60. }
  61. return $this->success($result);
  62. }
  63. }