input('pageSize', 10); $paginator = HomeSpecialModel::query() ->where('status', HomeSpecialModel::STATUS_NORMAL) ->whereHas('product', static function ($q) { $q->where('status', ProductModel::STATUS_ON); }) ->with('product') ->orderBy('sort') ->orderBy('id') ->paginate($pageSize); // 当前门店的等级(售价 = 成本价 × (100 + 等级上浮比例) / 100)与购物车行 $store = $this->optionalStore($request); $level = ($store !== null && $store->level_id > 0) ? $store->level : null; $cartService = app(CartService::class); $cartRows = $store !== null ? $cartService->cartRowMap($store->id) : []; $paginator->getCollection()->transform( static function (HomeSpecialModel $special) use ($level, $cartRows): array { $product = $special->product; $row = $product->toArray(); // 实际价(按等级上浮比例换算;成本价不随序列化输出) $row['price'] = $level !== null ? CustomerLevelModel::calcLevelPrice($product->cost_price, $level->percent) : null; // 购物车数量(列表直接加减用;不在购物车为 0/'0.00') $row['cart_id'] = $cartRows[$product->id]['id'] ?? 0; $row['cart_quantity'] = $cartRows[$product->id]['quantity'] ?? '0.00'; return $row; } ); $data = $paginator->toArray(); $data['cart'] = $cartService->summary($store); return $this->success($data); } }