Balance.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\BalanceLog;
  6. use App\Services\BalanceLogService;
  7. use Carbon\Carbon;
  8. use Illuminate\Validation\ValidationException;
  9. use Exception;
  10. class Balance extends Controller
  11. {
  12. public function log()
  13. {
  14. try {
  15. $params = request()->validate([
  16. 'page' => ['nullable', 'integer', 'min:1'],
  17. 'limit' => ['nullable', 'integer', 'min:1'],
  18. 'type' => ['nullable', 'integer'],
  19. 'member_id' => ['nullable', 'string', 'min:1'],
  20. 'related_id' => ['nullable', 'string', 'min:1'],
  21. 'change_type' => ['nullable', 'string', 'in:' . implode(',', BalanceLogService::$RW)],
  22. 'start_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:end_time'],
  23. 'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:start_time'],
  24. 'first_name' => ['nullable'],
  25. ]);
  26. $page = request()->input('page', 1);
  27. $limit = request()->input('limit', 10);
  28. $query = BalanceLog::join('users', 'users.member_id', '=', 'balance_logs.member_id')
  29. ->where(BalanceLogService::getWhere($params));
  30. $data['total'] = $query->count();
  31. $totalAmount = '--';
  32. //如果指定用户和类型,则统计变动金额
  33. if (isset($params['member_id']) && isset($params['change_type'])) {
  34. $totalAmount = $query->sum('amount');
  35. $totalAmount = bcadd($totalAmount, 0, 2);
  36. }
  37. $data['total_amount'] =$totalAmount;
  38. $data['data'] = $query->select("balance_logs.*", "users.first_name","users.admin_note as user_admin_note")->orderByDesc('id')
  39. ->forPage($page, $limit)->with(['member'])
  40. ->get()->toArray();
  41. $data['change_types'] = BalanceLogService::$RW;
  42. } catch (ValidationException $e) {
  43. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  44. } catch (Exception $e) {
  45. return $this->error($e->getCode(),$e->getMessage());
  46. }
  47. return $this->success($data);
  48. }
  49. }