Balance.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. 'account' => ['nullable', 'string', 'max:64'],
  25. 'first_name' => ['nullable'],
  26. 'is_fancai' => ['nullable', 'integer','in:0,1'],
  27. // 用户类型/注册来源:-1 tg用户,0 后台客服账号,1 真实用户,2 虚拟用户
  28. 'from' => ['nullable'],
  29. ]);
  30. $page = request()->input('page', 1);
  31. $limit = request()->input('limit', 10);
  32. $query = BalanceLog::join('users', 'users.member_id', '=', 'balance_logs.member_id')
  33. ->where(BalanceLogService::getWhere($params));
  34. if (!empty($params['is_fancai'])) {
  35. $data['change_types'] = ['即充即送','老用户回归','余额宝利息'];
  36. $query = $query->whereIn('balance_logs.change_type', $data['change_types'])
  37. ->where('balance_logs.type', 2);
  38. } else {
  39. $data['change_types'] = BalanceLogService::$RW;
  40. }
  41. // 统计与列表共用筛选条件;clone 避免 count/sum 影响后续分页查询
  42. $data['total'] = (clone $query)->count();
  43. $data['total_amount'] = bcadd((clone $query)->sum('balance_logs.amount'), 0, 2);
  44. $data['data'] = $query->select("balance_logs.*", "users.first_name", "users.account", "users.admin_note as user_admin_note", "users.from as user_from")
  45. ->orderByDesc('balance_logs.id')
  46. ->forPage($page, $limit)
  47. ->with(['member'])
  48. ->get()
  49. ->toArray();
  50. } catch (ValidationException $e) {
  51. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  52. } catch (Exception $e) {
  53. return $this->error($e->getCode(),$e->getMessage());
  54. }
  55. return $this->success($data);
  56. }
  57. }