Home.php 2.4 KB

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