diff --git a/app/service/AdsInsightService.php b/app/service/AdsInsightService.php index 33e2f78..cbb83fd 100644 --- a/app/service/AdsInsightService.php +++ b/app/service/AdsInsightService.php @@ -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) { @@ -163,16 +167,16 @@ class AdsInsightService 'net_profit_margin' => $net_profit_margin, 'net_profit_on_ad_spend' => $net_profit_on_ad_spend, ]; - }, $campaigns->items()); + }, $campaigns->toArray()); // Pagination 数据 $pagination = [ - 'startIndex' => ($page - 1) * $pageSize, - 'maxResults' => $pageSize, - 'count' => $campaigns->total(), - 'pageNo' => $campaigns->currentPage(), - 'pageSize' => $pageSize, - 'pages' => $campaigns->lastPage(), + 'startIndex' => ($page - 1) * $pageSize + 1, // 确保索引从 1 开始 + 'maxResults' => count($campaigns), // 当前页实际返回数据数量 + 'count' => count($allCampaigns), // 符合条件的总记录数 + 'pageNo' => $page, // 当前页码 + 'pageSize' => $pageSize, // 每页条数 + 'pages' => (int)ceil(count($allCampaigns) / $pageSize), // 总页数 ]; 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) { @@ -321,16 +327,16 @@ class AdsInsightService 'net_profit_margin' => $net_profit_margin, // 没有提供 net_profit_margin 计算,保持为 '-' 'net_profit_on_ad_spend' => $net_profit_on_ad_spend, // 没有提供 net_profit_on_ad_spend 计算,保持为 '-' ]; - }, $adsets->items()); + }, $adsets->toArray()); // Pagination 数据 $pagination = [ - 'startIndex' => ($page - 1) * $pageSize, - 'maxResults' => $pageSize, - 'count' => $adsets->total(), - 'pageNo' => $adsets->currentPage(), - 'pageSize' => $pageSize, - 'pages' => $adsets->lastPage(), + 'startIndex' => ($page - 1) * $pageSize + 1, // 确保索引从 1 开始 + 'maxResults' => count($adsets), // 当前页实际返回数据数量 + 'count' => count($allAdsets), // 符合条件的总记录数 + 'pageNo' => $page, // 当前页码 + 'pageSize' => $pageSize, // 每页条数 + 'pages' => (int)ceil(count($allAdsets) / $pageSize), // 总页数 ]; return [ @@ -433,8 +439,13 @@ class AdsInsightService '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) { @@ -475,16 +486,16 @@ class AdsInsightService 'net_profit_margin' => $net_profit_margin, 'net_profit_on_ad_spend' => $net_profit_on_ad_spend, ]; - }, $ads->items()); + }, $ads->toArray()); // Pagination 数据 $pagination = [ - 'startIndex' => ($page - 1) * $pageSize, - 'maxResults' => $pageSize, - 'count' => $ads->total(), - 'pageNo' => $ads->currentPage(), - 'pageSize' => $pageSize, - 'pages' => $ads->lastPage(), + 'startIndex' => ($page - 1) * $pageSize + 1, // 确保索引从 1 开始 + 'maxResults' => count($ads), // 当前页实际返回数据数量 + 'count' => count($allAds), // 符合条件的总记录数 + 'pageNo' => $page, // 当前页码 + 'pageSize' => $pageSize, // 每页条数 + 'pages' => (int)ceil(count($allAds) / $pageSize), // 总页数 ]; return [ @@ -752,13 +763,20 @@ class AdsInsightService $dataWithoutKeys = array_values($aggregatedUsers); // 最终分页信息 $pagination = [ - 'startIndex' => ($page - 1) * $pageSize, - 'maxResults' => $pageSize, -// 'count' => $users->total(), - 'count' => $userCount, - 'pageNo' => $page, - 'pageSize' => $pageSize, - 'pages' => ceil($users->total() / $pageSize), +// 'startIndex' => ($page - 1) * $pageSize, +// 'maxResults' => $pageSize, +//// 'count' => $users->total(), +// 'count' => $userCount, +// 'pageNo' => $page, +// 'pageSize' => $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 [