翻页优化

This commit is contained in:
huangguancheng 2025-01-13 19:39:33 +08:00
parent 69732e01e1
commit 3d973e378e

View File

@ -124,7 +124,11 @@ class AdsInsightService
]; ];
// 获取分页数据 // 获取分页数据
$campaigns = $query->paginate($pageSize, false, ['page' => $page]); // $campaigns = $query->paginate($pageSize, false, ['page' => $page]);
// 获取分页数据
$page = max(1, (int)$page); // 确保页码不小于 1
// $ads = $query->page($page, $pageSize)->select();
$campaigns = $query->limit(($page - 1) * $pageSize, $pageSize)->select();
// 确保数据格式统一 // 确保数据格式统一
$result = array_map(function ($item) { $result = array_map(function ($item) {
@ -163,16 +167,16 @@ class AdsInsightService
'net_profit_margin' => $net_profit_margin, 'net_profit_margin' => $net_profit_margin,
'net_profit_on_ad_spend' => $net_profit_on_ad_spend, 'net_profit_on_ad_spend' => $net_profit_on_ad_spend,
]; ];
}, $campaigns->items()); }, $campaigns->toArray());
// Pagination 数据 // Pagination 数据
$pagination = [ $pagination = [
'startIndex' => ($page - 1) * $pageSize, 'startIndex' => ($page - 1) * $pageSize + 1, // 确保索引从 1 开始
'maxResults' => $pageSize, 'maxResults' => count($campaigns), // 当前页实际返回数据数量
'count' => $campaigns->total(), 'count' => count($allCampaigns), // 符合条件的总记录数
'pageNo' => $campaigns->currentPage(), 'pageNo' => $page, // 当前页码
'pageSize' => $pageSize, 'pageSize' => $pageSize, // 每页条数
'pages' => $campaigns->lastPage(), 'pages' => (int)ceil(count($allCampaigns) / $pageSize), // 总页数
]; ];
return [ return [
@ -281,7 +285,9 @@ class AdsInsightService
]; ];
// 获取分页数据 // 获取分页数据
$adsets = $query->paginate($pageSize, false, ['page' => $page]); // $adsets = $query->paginate($pageSize, false, ['page' => $page]);
$page = max(1, (int)$page); // 确保页码不小于 1
$adsets = $query->limit(($page - 1) * $pageSize, $pageSize)->select();
// 确保数据格式统一 // 确保数据格式统一
$result = array_map(function ($item) { $result = array_map(function ($item) {
@ -321,16 +327,16 @@ class AdsInsightService
'net_profit_margin' => $net_profit_margin, // 没有提供 net_profit_margin 计算,保持为 '-' 'net_profit_margin' => $net_profit_margin, // 没有提供 net_profit_margin 计算,保持为 '-'
'net_profit_on_ad_spend' => $net_profit_on_ad_spend, // 没有提供 net_profit_on_ad_spend 计算,保持为 '-' 'net_profit_on_ad_spend' => $net_profit_on_ad_spend, // 没有提供 net_profit_on_ad_spend 计算,保持为 '-'
]; ];
}, $adsets->items()); }, $adsets->toArray());
// Pagination 数据 // Pagination 数据
$pagination = [ $pagination = [
'startIndex' => ($page - 1) * $pageSize, 'startIndex' => ($page - 1) * $pageSize + 1, // 确保索引从 1 开始
'maxResults' => $pageSize, 'maxResults' => count($adsets), // 当前页实际返回数据数量
'count' => $adsets->total(), 'count' => count($allAdsets), // 符合条件的总记录数
'pageNo' => $adsets->currentPage(), 'pageNo' => $page, // 当前页码
'pageSize' => $pageSize, 'pageSize' => $pageSize, // 每页条数
'pages' => $adsets->lastPage(), 'pages' => (int)ceil(count($allAdsets) / $pageSize), // 总页数
]; ];
return [ return [
@ -433,8 +439,13 @@ class AdsInsightService
'ctr' => ($total_impressions > 0) ? number_format(($total_clicks / $total_impressions) * 100, 2) . '%' : '-', // 格式化为百分比 'ctr' => ($total_impressions > 0) ? number_format(($total_clicks / $total_impressions) * 100, 2) . '%' : '-', // 格式化为百分比
]; ];
// 获取分页数据 // 获取分页数据
$ads = $query->paginate($pageSize, false, ['page' => $page]); $page = max(1, (int)$page); // 确保页码不小于 1
// $ads = $query->page($page, $pageSize)->select();
$ads = $query->limit(($page - 1) * $pageSize, $pageSize)->select();
// dump($ads);
// 确保数据格式统一 // 确保数据格式统一
$result = array_map(function ($item) { $result = array_map(function ($item) {
@ -475,16 +486,16 @@ class AdsInsightService
'net_profit_margin' => $net_profit_margin, 'net_profit_margin' => $net_profit_margin,
'net_profit_on_ad_spend' => $net_profit_on_ad_spend, 'net_profit_on_ad_spend' => $net_profit_on_ad_spend,
]; ];
}, $ads->items()); }, $ads->toArray());
// Pagination 数据 // Pagination 数据
$pagination = [ $pagination = [
'startIndex' => ($page - 1) * $pageSize, 'startIndex' => ($page - 1) * $pageSize + 1, // 确保索引从 1 开始
'maxResults' => $pageSize, 'maxResults' => count($ads), // 当前页实际返回数据数量
'count' => $ads->total(), 'count' => count($allAds), // 符合条件的总记录数
'pageNo' => $ads->currentPage(), 'pageNo' => $page, // 当前页码
'pageSize' => $pageSize, 'pageSize' => $pageSize, // 每页条数
'pages' => $ads->lastPage(), 'pages' => (int)ceil(count($allAds) / $pageSize), // 总页数
]; ];
return [ return [
@ -752,13 +763,20 @@ class AdsInsightService
$dataWithoutKeys = array_values($aggregatedUsers); $dataWithoutKeys = array_values($aggregatedUsers);
// 最终分页信息 // 最终分页信息
$pagination = [ $pagination = [
'startIndex' => ($page - 1) * $pageSize, // 'startIndex' => ($page - 1) * $pageSize,
'maxResults' => $pageSize, // 'maxResults' => $pageSize,
// 'count' => $users->total(), //// 'count' => $users->total(),
'count' => $userCount, // 'count' => $userCount,
'pageNo' => $page, // 'pageNo' => $page,
'pageSize' => $pageSize, // 'pageSize' => $pageSize,
'pages' => ceil($users->total() / $pageSize), // 'pages' => ceil($users->total() / $pageSize),
'startIndex' => ($page - 1) * $pageSize + 1, // 确保索引从 1 开始
'maxResults' => count($dataWithoutKeys), // 当前页实际返回数据数量
'count' => count($allUsers), // 符合条件的总记录数
'pageNo' => $page, // 当前页码
'pageSize' => $pageSize, // 每页条数
'pages' => (int)ceil(count($allUsers) / $pageSize), // 总页数
]; ];
return [ return [