Home.php 1.8 KB

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