input('page', 1)); $pageSize = (int)($request->input('page_size', 20)); $piNo = $request->input('pi_no', ''); if ($page < 1) $page = 1; if ($pageSize < 1 || $pageSize > 200) $pageSize = 20; $query = StockOrders::where('approval_status', 2) ->whereNotNull('pi_no') ->where('pi_no', '<>', ''); if ($piNo !== '') { $query->where('pi_no', 'like', '%' . addcslashes($piNo, '%_') . '%'); } $query->with(['items' => function ($q) { $q->select('id', 'order_id', 'vin', 'model_snapshot'); }]); $paginator = $query->orderBy('id', 'desc') ->paginate($pageSize, ['id', 'pi_no'], 'page', $page); $data = $paginator->getCollection()->map(function ($order) { return [ 'pi_no' => $order->pi_no, 'vins' => $order->items->pluck('vin')->filter()->values()->toArray(), 'model_names' => $order->items->pluck('model_snapshot')->filter()->values()->toArray(), ]; })->toArray(); return $this->success([ 'total' => $paginator->total(), 'per_page' => $paginator->perPage(), 'current_page' => $paginator->currentPage(), 'data' => $data, ]); } catch (\Throwable $e) { return $this->error('查询失败,请稍后重试'); } } /** * VIN 号列表(含车型名称) * GET /common/public/vin/list */ public function vinList(Request $request) { try { $page = (int)($request->input('page', 1)); $pageSize = (int)($request->input('page_size', 20)); $vin = $request->input('vin', ''); if ($page < 1) $page = 1; if ($pageSize < 1 || $pageSize > 200) $pageSize = 20; $query = StockOrderItems::whereHas('order', function ($q) { $q->where('approval_status', 2); })->whereNotNull('vin') ->where('vin', '<>', ''); if ($vin !== '') { $query->where('vin', 'like', '%' . addcslashes($vin, '%_') . '%'); } $paginator = $query->orderBy('id', 'desc') ->paginate($pageSize, ['id', 'vin', 'model_snapshot'], 'page', $page); $data = $paginator->getCollection()->map(function ($item) { return [ 'vin' => $item->vin, 'model_name' => $item->model_snapshot, ]; })->toArray(); return $this->success([ 'total' => $paginator->total(), 'per_page' => $paginator->perPage(), 'current_page' => $paginator->currentPage(), 'data' => $data, ]); } catch (\Throwable $e) { return $this->error('查询失败,请稍后重试'); } } /** * 业务人员列表(role_id=2,在职,全量返回) * GET /common/public/staff/list */ public function staffList(Request $request) { try { $keyword = $request->input('keyword', ''); // 通过 user_roles 关联,筛选指定角色的用户 $query = User::whereHas('roles', function ($q) { $q->where('roles.id', static::STAFF_ROLE_ID); })->where('status', 1); if ($keyword !== '') { $kw = '%' . addcslashes($keyword, '%_') . '%'; $query->where(function ($q) use ($kw) { $q->where('name', 'like', $kw) ->orWhere('sn', 'like', $kw) ->orWhere('phone', 'like', $kw) ->orWhere('email', 'like', $kw); }); } $list = $query->orderBy('id', 'asc') ->get(['id', 'sn', 'name', 'phone', 'email']); $data = $list->map(function ($user) { return [ 'id' => $user->id, 'name' => $user->name, 'sn' => $user->sn, 'phone' => $user->phone, 'email' => $user->email, ]; })->values()->toArray(); return $this->success($data); } catch (\Throwable $e) { return $this->error('查询失败,请稍后重试'); } } }