diff --git a/app/controller/BpsAdController.php b/app/controller/BpsAdController.php index 3d897e2..5f3b381 100644 --- a/app/controller/BpsAdController.php +++ b/app/controller/BpsAdController.php @@ -108,8 +108,9 @@ class BpsAdController $keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索 $platformType = $options['conditions']['platformType'] ?? 0; // 平台类型 // $status = $options['conditions']['status'] ?? 0; // 平台类型 - $startDate = $options['conditions']['startDate'] ?? null; // 开始日期 - $endDate = $options['conditions']['endDate'] ?? null; // 结束日期 + $startDate = $options['conditions']['startDate'] ?? null; // 开始日期 + $endDate = $options['conditions']['endDate'] ?? null; // 结束日期 + $requireSpend = (bool)($options['conditions']['requireSpend'] ?? false); // $dateRange = 'Last Week'; // 默认日期范围 // 根据 platformType 获取广告账户 @@ -150,6 +151,7 @@ class BpsAdController $keyword, // 关键字 $startDate, // 开始日期 $endDate, // 结束日期 + $requireSpend ); @@ -223,6 +225,7 @@ class BpsAdController $status = $options['conditions']['status'] ?? 0; // 平台类型 $startDate = $options['conditions']['startDate'] ?? null; // 开始日期 $endDate = $options['conditions']['endDate'] ?? null; // 结束日期 + $requireSpend = (bool)($options['conditions']['requireSpend']?? false); // $dateRange = 'Last Week'; // 默认日期范围 // 根据 platformType 获取广告账户 @@ -262,7 +265,8 @@ class BpsAdController $keyword, // 关键字 $startDate, // 开始日期 $endDate, // 结束日期 - $status + $status, + $requireSpend ); // 返回结果 @@ -282,6 +286,7 @@ class BpsAdController $status = $options['conditions']['status'] ?? 0; // 平台类型 $startDate = $options['conditions']['startDate'] ?? null; // 开始日期 $endDate = $options['conditions']['endDate'] ?? null; // 结束日期 + $requireSpend = (bool)($options['conditions']['requireSpend'] ?? false); // $dateRange = 'Last Week'; // 默认日期范围 // 根据 platformType 获取广告账户 if ($platformType === 1) { @@ -319,7 +324,8 @@ class BpsAdController $keyword, // 关键字 $startDate, // 开始日期 $endDate, // 结束日期 - $status + $status, + $requireSpend ); return $this->successResponse($result, $request); // return $this->errorResponse(300,'授权失败'); @@ -338,6 +344,7 @@ class BpsAdController $status = $options['conditions']['status'] ?? 0; // 平台类型 $startDate = $options['conditions']['startDate'] ?? null; // 开始日期 $endDate = $options['conditions']['endDate'] ?? null; // 结束日期 + $requireSpend = (bool)($options['conditions']['requireSpend'] ?? false); // $dateRange = 'Last Week'; // 默认日期范围 // 根据 platformType 获取广告账户 if ($platformType === 1) { @@ -380,7 +387,8 @@ class BpsAdController $keyword, // 关键字 $startDate, // 开始日期 $endDate, // 结束日期 - $status + $status, + $requireSpend ); } @@ -397,6 +405,7 @@ class BpsAdController // $status = $options['conditions']['status'] ?? 0; // 平台类型 $startDate = $options['conditions']['startDate'] ?? null; // 开始日期 $endDate = $options['conditions']['endDate'] ?? null; // 结束日期 + $requireSpend = (bool)($options['conditions']['requireSpend']?? false); // $dateRange = 'Last Week'; // 默认日期范围 // 根据 platformType 获取广告账户 if ($platformType === 1) { @@ -439,7 +448,8 @@ class BpsAdController $pageSize, // 每页数量 $keyword, // 关键字 $startDate, // 开始日期 - $endDate // 结束日期 + $endDate, // 结束日期 + $requireSpend ); } @@ -456,6 +466,7 @@ class BpsAdController // $status = $options['conditions']['status'] ?? 0; // 平台类型 $startDate = $options['conditions']['startDate'] ?? null; // 开始日期 $endDate = $options['conditions']['endDate'] ?? null; // 结束日期 + $requireSpend = (bool)($options['conditions']['requireSpend']?? false); // $dateRange = 'Last Week'; // 默认日期范围 // 根据 platformType 获取广告账户 if ($platformType === 1) { @@ -498,7 +509,8 @@ class BpsAdController $pageSize, // 每页数量 $keyword, // 关键字 $startDate, // 开始日期 - $endDate // 结束日期 + $endDate, // 结束日期 + $requireSpend ); } @@ -515,6 +527,7 @@ class BpsAdController $status = $options['conditions']['status'] ?? 0; // 平台类型 $startDate = $options['conditions']['startDate'] ?? null; // 开始日期 $endDate = $options['conditions']['endDate'] ?? null; // 结束日期 + $requireSpend = (bool)($options['conditions']['requireSpend'] ?? false); // $dateRange = 'Last Week'; // 默认日期范围 // 根据 platformType 获取广告账户 if ($platformType === 1) { @@ -558,7 +571,8 @@ class BpsAdController $keyword, // 关键字 $startDate, // 开始日期 $endDate, // 结束日期 - $status + $status, + $requireSpend ); } @@ -575,6 +589,7 @@ class BpsAdController $status = $options['conditions']['status'] ?? 0; // 平台类型 $startDate = $options['conditions']['startDate'] ?? null; // 开始日期 $endDate = $options['conditions']['endDate'] ?? null; // 结束日期 + $requireSpend = (bool)($options['conditions']['requireSpend']?? false); // $dateRange = 'Last Week'; // 默认日期范围 // 根据 platformType 获取广告账户 if ($platformType === 1) { @@ -618,7 +633,8 @@ class BpsAdController $keyword, // 关键字 $startDate, // 开始日期 $endDate, // 结束日期 - $status + $status, + $requireSpend ); } @@ -635,6 +651,7 @@ class BpsAdController $status = $options['conditions']['status'] ?? 0; // 平台类型 $startDate = $options['conditions']['startDate'] ?? null; // 开始日期 $endDate = $options['conditions']['endDate'] ?? null; // 结束日期 + $requireSpend = (bool)($options['conditions']['requireSpend'] ?? false); // $dateRange = 'Last Week'; // 默认日期范围 // 根据 platformType 获取广告账户 @@ -674,7 +691,8 @@ class BpsAdController $keyword, // 关键字 $startDate, // 开始日期 $endDate, // 结束日期 - $status + $status, + $requireSpend ); // 返回结果 @@ -693,6 +711,8 @@ class BpsAdController $platformType = $options['conditions']['platformType'] ?? 0; // 平台类型 $startDate = $options['conditions']['startDate'] ?? null; // 开始日期 $endDate = $options['conditions']['endDate'] ?? null; // 结束日期 + $requireSpend = (bool)($options['conditions']['requireSpend']?? false); + // $dateRange = 'Last Week'; // 默认日期范围 // 根据 platformType 获取广告账户 @@ -731,7 +751,8 @@ class BpsAdController $pageSize, // 每页数量 $keyword, // 关键字 $startDate, // 开始日期 - $endDate // 结束日期 + $endDate, // 结束日期 + $requireSpend ); // 返回结果 diff --git a/app/service/AdsInsightService.php b/app/service/AdsInsightService.php index d529576..5b04ca5 100644 --- a/app/service/AdsInsightService.php +++ b/app/service/AdsInsightService.php @@ -111,7 +111,7 @@ class AdsInsightService */ protected static function getAdListCount(array $customerIds, $startDate, $endDate) { - return self::getAdList(0, $customerIds, 1, 1, '', $startDate, $endDate, 0, true); + return self::getAdList(0, $customerIds, 1, 1, '', $startDate, $endDate, 0, false, true); } /** @@ -119,7 +119,7 @@ class AdsInsightService */ protected static function getCampaignListCount(array $customerIds, $startDate, $endDate) { - return self::getCampaignList(0, $customerIds, 1, 1, '', $startDate, $endDate, 0, true); + return self::getCampaignList(0, $customerIds, 1, 1, '', $startDate, $endDate, 0, false, true); } /** @@ -127,7 +127,7 @@ class AdsInsightService */ protected static function getAdsetListCount(array $customerIds, $startDate, $endDate) { - return self::getAdsetList(0, $customerIds, 1, 1, '', $startDate, $endDate, 0, true); + return self::getAdsetList(0, $customerIds, 1, 1, '', $startDate, $endDate, 0, false, true); } /** @@ -135,7 +135,7 @@ class AdsInsightService */ protected static function getAccountListCount(array $customerIds, $startDate, $endDate) { - return self::getAccountList(0, $customerIds, 1, 1, '', $startDate, $endDate, true); + return self::getAccountList(0, $customerIds, 1, 1, '', $startDate, $endDate, false, true); } /** @@ -143,7 +143,7 @@ class AdsInsightService */ protected static function getCreativeListCount(array $customerIds, $startDate, $endDate) { - return self::getCreativeInsightData(0, $customerIds, 1, 1, '', $startDate, $endDate, true); + return self::getCreativeInsightData(0, $customerIds, 1, 1, '', $startDate, $endDate, false,true); } /** @@ -160,7 +160,7 @@ class AdsInsightService /** * 获取广告系列列表 */ - public static function getCampaignList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $status = 0, $countOnly = false) + public static function getCampaignList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $status = 0, $requireSpend = false, $countOnly = false) { // 检查 customerIds 是否为空,直接返回空结构 if (empty($customerIds)) { @@ -207,6 +207,10 @@ class AdsInsightService -1 as conversion_rate, -1 as roas, -1 as ctr,-1 as net_profit,-1 as net_profit_margin,-1 as net_profit_on_ad_spend') ->group('c.campaign_id, c.status, c.account_id, c.name,c.platform_type') ->where('c.account_id', 'in', $customerIds); // 添加 customerIds 条件 + // 根据 $requireSpend 参数决定是否添加 SUM(d.spend) > 0 的条件 + if ($requireSpend) { + $query->having('SUM(d.spend) > 0'); + } if ($status !== 0) { $query->where('c.status', '=', $status); } @@ -333,7 +337,7 @@ class AdsInsightService * @param string|null $endDate 结束日期 * @param int $status 广告系列状态 */ - public static function exportCampaignsToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $status = 0) + public static function exportCampaignsToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $requireSpend = false, $status = 0) { // 调用 getCampaignList 获取广告系列数据 $campaignList = self::getCampaignList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, $status, false); @@ -465,10 +469,10 @@ class AdsInsightService * @param string|null $endDate 结束日期 * @param int $status 广告系列状态 */ - public static function exportAdsetsToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $status = 0) + public static function exportAdsetsToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $status = 0, $requireSpend = false) { // 调用 getCampaignList 获取广告系列数据 - $adsetList = self::getAdsetList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, $status, false); + $adsetList = self::getAdsetList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, $status, $requireSpend); if (empty($adsetList['data'])) { $data = [ @@ -597,10 +601,10 @@ class AdsInsightService * @param string|null $endDate 结束日期 * @param int $status 广告系列状态 */ - public static function exportAdsToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $status = 0) + public static function exportAdsToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $status = 0, $requireSpend = false) { // 调用 getCampaignList 获取广告系列数据 - $adList = self::getAdList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, $status, false); + $adList = self::getAdList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, $status, $requireSpend, false); if (empty($adList['data'])) { $data = [ @@ -717,10 +721,10 @@ class AdsInsightService } } - public static function exportCreativesToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null) + public static function exportCreativesToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null,$requireSpend = false) { // 获取广告创意数据 - $creativeList = self::getCreativeInsightData($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, false); + $creativeList = self::getCreativeInsightData($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, $requireSpend, false); if (empty($creativeList['data'])) { $data = [ @@ -855,7 +859,7 @@ class AdsInsightService /** * 获取广告组列表 */ - public static function getAdsetList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $status = 0, $countOnly = false) + public static function getAdsetList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $status = 0, $requireSpend = false, $countOnly = false) { // 检查 customerIds 是否为空,直接返回空结构 if (empty($customerIds)) { @@ -907,6 +911,10 @@ class AdsInsightService -1 as conversion_rate, -1 as roas, -1 as ctr,-1 as net_profit,-1 as net_profit_margin,-1 as net_profit_on_ad_spend') ->group('s.ad_set_id, s.status, s.account_id, s.name,s.platform_type,c.name') ->where('s.account_id', 'in', $customerIds); // 添加 customerIds 条件 + // 根据 $requireSpend 参数决定是否添加 SUM(d.spend) > 0 的条件 + if ($requireSpend) { + $query->having('SUM(d.spend) > 0'); + } if ($status !== 0) { $query->where('s.status', '=', $status); } @@ -1036,10 +1044,10 @@ class AdsInsightService * @param string|null $endDate 结束日期 * @param int $status 广告系列状态 */ - public static function exportAccountsToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null) + public static function exportAccountsToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $requireSpend = false) { // 调用 getCampaignList 获取广告系列数据 - $accountList = self::getAccountList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, false); + $accountList = self::getAccountList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, $requireSpend); if (empty($accountList['data'])) { $data = [ @@ -1154,7 +1162,7 @@ class AdsInsightService } } - public static function getAccountList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $countOnly = false) + public static function getAccountList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $requireSpend = false, $countOnly = false) { // 检查 customerIds 是否为空,直接返回计数为 0 if (empty($customerIds)) { @@ -1222,6 +1230,11 @@ class AdsInsightService }); $query->order('u.third_type', 'asc'); + // 根据 $requireSpend 参数决定是否添加 SUM(d.spend) > 0 的条件 + if ($requireSpend) { + $query->having('SUM(d.spend) > 0'); + } + // 获取所有符合条件的数据(不分页) $allAccounts = $query->select()->toArray(); @@ -1323,7 +1336,7 @@ class AdsInsightService } - public static function getAdList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $status = 0, $countOnly = false) + public static function getAdList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $status = 0, $requireSpend = false, $countOnly = false) { // 检查 customerIds 是否为空,直接返回空结构 if (empty($customerIds)) { @@ -1378,6 +1391,10 @@ class AdsInsightService if ($status !== 0) { $query->where('a.status', '=', $status); } + // 根据 $requireSpend 参数决定是否添加 SUM(d.spend) > 0 的条件 + if ($requireSpend) { + $query->having('SUM(d.spend) > 0'); + } // 添加关键字过滤条件 $query->where(function ($query) use ($keyword, $platformType) { if ($keyword) { @@ -1407,7 +1424,7 @@ class AdsInsightService $statistics = [ 'platform_purchase' => number_format(array_sum(array_column($allAds, 'platform_purchase'))), 'pixel_purchase' => number_format(array_sum(array_column($allAds, 'pixel_purchase'))), - 'roas' => $total_spend == 0 ? '-' : round($total_purchases_value / $total_spend, 2). 'x', + 'roas' => $total_spend == 0 ? '-' : round($total_purchases_value / $total_spend, 2) . 'x', 'amount_spend' => '$' . number_format($total_spend, 2) ?: '$0.00', // 格式化支出 'clicks' => number_format($total_clicks), 'impressions' => number_format($total_impressions), @@ -1454,7 +1471,7 @@ class AdsInsightService 'status' => $item['status'], 'platform_purchase' => number_format($item['platform_purchase']), 'pixel_purchase' => number_format($item['pixel_purchase']), - 'roas' => $item['spend'] == 0 ? '-' : round($item['purchases_value'] / $item['spend'], 2).'x', + 'roas' => $item['spend'] == 0 ? '-' : round($item['purchases_value'] / $item['spend'], 2) . 'x', 'spend' => '$' . number_format($item['spend'], 2), // 格式化支出 'impressions' => number_format($item['impressions']), 'clicks' => number_format($item['clicks']), @@ -1772,7 +1789,7 @@ class AdsInsightService } - public static function getCreativeInsightData($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $countOnly = false) + public static function getCreativeInsightData($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $requireSpend = false, $countOnly = false) { // 1. 创建查询对象,初始化 BpsAdCreativeInsight 查询 $creativeDataQuery = BpsAdCreativeInsight::alias('i') @@ -1840,6 +1857,11 @@ class AdsInsightService ThinkDb::raw('-1 AS hold_rate') ]); + // 根据 $requireSpend 参数决定是否添加 SUM(i.spend) > 0 的条件 + if ($requireSpend) { + $creativeDataQuery->having('SUM(i.spend) > 0'); + } + // 6. 执行查询并获取聚合结果 // $aggregatedData = $creativeDataQuery->select(); // 6. 执行查询并获取聚合结果