Home.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. $start = Carbon::createFromFormat('Y-m-d', $start);
  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. $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. }