Home.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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)->whereBetween('created_at', [$start, $end])->sum('amount');
  24. $totalSuccess = (float)$query->where($where)->whereBetween('created_at', [$start, $end])->whereIn('status', [1, 2])->sum('amount');
  25. $totalFail = (float)$query->where($where)->whereBetween('created_at', [$start, $end])->where('status', 3)->sum('amount');
  26. $result = [
  27. 'withdraw_rmb' => [
  28. 'total_fail' => $totalFail,
  29. 'total_success' => $totalSuccess,
  30. 'total_amount' => $totalAmount,
  31. ]
  32. ];
  33. } catch (ValidationException $e) {
  34. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  35. } catch (Exception $e) {
  36. return $this->error($e->getCode(), $e->getmessage());
  37. }
  38. return $this->success($result);
  39. }
  40. }