Home.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. class Home extends Controller
  10. {
  11. public function index()
  12. {
  13. try {
  14. request()->validate([
  15. 'start_date' => ['nullable', 'date_format:Y-m-d'],
  16. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after:start_date'],
  17. ]);
  18. $start = request()->input('start_date', date('Y-m-d'));
  19. $end = request()->input('end_date', date('Y-m-d'));
  20. $query = PaymentOrder::query();
  21. $params['type'] = 2;
  22. $where = PaymentOrderService::getWhere($params);
  23. $totalAmount = (float)$query->where($where)
  24. ->whereDate('created_at', date('Y-m-d'))->sum('amount');
  25. $totalSuccess = (float)$query->where($where)->whereDate('created_at', date('Y-m-d'))
  26. ->whereIn('status', [1, 2])->sum('amount');
  27. $totalFail = (float)$query->where($where)->whereDate('created_at', date('Y-m-d'))
  28. ->where('status', 3)->sum('amount');
  29. $result = [
  30. 'withdraw_rmb' => [
  31. 'total_fail' => $totalFail,
  32. 'total_success' => $totalSuccess,
  33. 'total_amount' => $totalAmount,
  34. ]
  35. ];
  36. } catch (ValidationException $e) {
  37. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  38. } catch (Exception $e) {
  39. return $this->error($e->getCode(), $e->getmessage());
  40. }
  41. return $this->success($result);
  42. }
  43. }