限制 使用当前主体下的所有账号ID查询数据

This commit is contained in:
hgc 2025-01-04 16:01:37 +08:00
parent dbdca8658d
commit f3cbaa3854
3 changed files with 105 additions and 44 deletions

View File

@ -6,6 +6,7 @@ use app\service\GoogleAdsCampaignService;
use app\service\GoogleAdsGroupService;
use app\service\GoogleAdsAdService;
use app\service\GoogleAdsReportService;
use app\service\GoogleOAuthService;
use support\Request;
use support\Response;
use DI\Annotation\Inject;
@ -38,6 +39,12 @@ class AdController
*/
private $googleAdsCampaignService;
/**
* @Inject
* @var GoogleOAuthService
*/
private $googleOAuthService;
public function listAds(Request $request)
{
// 获取请求参数
@ -57,7 +64,10 @@ class AdController
// if ($endDate && !strtotime($endDate)) {
// return response()->json(['error' => 'Invalid end date format'], 400);
// }
$customerIds = [8452924576,6977154211,1401879025];
// $customerIds = [8452924576,6977154211,1401879025];
$customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]);
$customerIds = array_column($customers, 'customer_id');
// dump($customers);
// 调用 Service 层查询
$result = $this->googleAdsReportService::getAdList($customerIds, $page, $pageSize, $keyword, $dateRange, $startDate, $endDate);
return $this->successResponse($result);
@ -75,7 +85,9 @@ class AdController
$startDate = $request->input('start_date', null); // 开始日期,默认为 null
$endDate = $request->input('end_date', null); // 结束日期,默认为 null
$customerId = 4060397299;
// $customerId = 4060397299;
$customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]);
$customerIds = array_column($customers, 'customer_id');
// 你可以进一步验证日期格式(可选)
// if ($startDate && !strtotime($startDate)) {
@ -86,7 +98,7 @@ class AdController
// }
// 调用 Service 层查询
$result = $this->googleAdsReportService->getAssetConversionData($customerId,$page, $pageSize, $keyword, $dateRange, $startDate, $endDate);
$result = $this->googleAdsReportService->getAssetConversionData($customerIds, $page, $pageSize, $keyword, $dateRange, $startDate, $endDate);
return $this->successResponse($result);
}
@ -109,7 +121,9 @@ class AdController
// if ($endDate && !strtotime($endDate)) {
// return response()->json(['error' => 'Invalid end date format'], 400);
// }
$customerIds = [8452924576,6977154211,1401879025];
// $customerIds = [8452924576, 6977154211, 1401879025];
$customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]);
$customerIds = array_column($customers, 'customer_id');
// 调用 Service 层查询
$result = $this->googleAdsReportService::getCampaignList($customerIds, $page, $pageSize, $keyword, $dateRange, $startDate, $endDate);
return $this->successResponse($result);
@ -133,8 +147,10 @@ class AdController
// return response()->json(['error' => 'Invalid end date format'], 400);
// }
$customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]);
$customerIds = array_column($customers, 'customer_id');
// 调用 service 层导出数据
return $this->googleAdsReportService::exportAdListToExcel($keyword, $dateRange, $startDate, $endDate);
return $this->googleAdsReportService::exportAdListToExcel($customerIds, $keyword, $dateRange, $startDate, $endDate);
}
public function exportCampaignsToExcel(Request $request)
@ -153,9 +169,11 @@ class AdController
// if ($endDate && !strtotime($endDate)) {
// return response()->json(['error' => 'Invalid end date format'], 400);
// }
$customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]);
$customerIds = array_column($customers, 'customer_id');
// dump($customerIds);
// 调用 service 层导出数据
return $this->googleAdsReportService::exportCampaignsToExcel($keyword, $dateRange, $startDate, $endDate);
return $this->googleAdsReportService->exportCampaignsToExcel($customerIds,$keyword, $dateRange, $startDate, $endDate);
}
public function exportGroupsToExcel(Request $request)
@ -167,6 +185,9 @@ class AdController
$startDate = $request->input('start_date', null); // 开始日期,默认为 null
$endDate = $request->input('end_date', null); // 结束日期,默认为 null
$customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]);
$customerIds = array_column($customers, 'customer_id');
// 你可以进一步验证日期格式(可选)
// if ($startDate && !strtotime($startDate)) {
// return response()->json(['error' => 'Invalid start date format'], 400);
@ -176,7 +197,7 @@ class AdController
// }
// 调用 service 层导出数据
return $this->googleAdsReportService::exportAdGroupsToExcel($keyword, $dateRange, $startDate, $endDate);
return $this->googleAdsReportService->exportAdGroupsToExcel($customerIds,$keyword, $dateRange, $startDate, $endDate);
}
public function listGroups(Request $request)
@ -198,7 +219,9 @@ class AdController
// if ($endDate && !strtotime($endDate)) {
// return response()->json(['error' => 'Invalid end date format'], 400);
// }
$customerIds = [8452924576,6977154211,1401879025];
//$customerIds = [8452924576,6977154211,1401879025];
$customers = $this->googleOAuthService->getGoogleAdCustomers(['refresh_token' => $request->refresh_token]);
$customerIds = array_column($customers, 'customer_id');
// 调用 Service 层查询
$result = $this->googleAdsReportService::getAdGroupList($customerIds, $page, $pageSize, $keyword, $dateRange, $startDate, $endDate);
return $this->successResponse($result);

View File

@ -200,8 +200,11 @@ class GoogleAdsReportService
* @param string $dateRange
* @return void
*/
public static function exportAdListToExcel($keyword, $dateRange, $startDate = null, $endDate = null)
public static function exportAdListToExcel($customerIds,$keyword, $dateRange, $startDate = null, $endDate = null)
{
if (empty($customerIds)) {
return [];
}
// 基础查询:广告表和日数据表联接
$query = Ad::alias('a')
->cache(false) // 强制不使用缓存
@ -244,6 +247,7 @@ class GoogleAdsReportService
COALESCE(SUM(d.impressions), -1) as reach,
-1 as roas, -1 as be_roas, -1 as revenue, -1 as profit, -1 as delivery')
->group('a.ad_id, a.ad_name, a.status, a.customer_id, a.ad_group_id, g.ad_group_name, a.campaign_id, c.campaign_name')
->where('a.customer_id', 'in', $customerIds)
->where(function ($query) use ($keyword) {
if ($keyword) {
$query->where('a.ad_name', 'like', '%' . $keyword . '%');
@ -531,8 +535,12 @@ class GoogleAdsReportService
* @param string $dateRange
* @return void
*/
public static function exportCampaignsToExcel($keyword, $dateRange, $startDate = null, $endDate = null)
public function exportCampaignsToExcel($customerIds,$keyword, $dateRange, $startDate = null, $endDate = null)
{
if(empty($customerIds)){
return [];
}
// 动态构建日期条件
$dateCondition = '';
if ($startDate && $endDate) {
@ -571,6 +579,7 @@ class GoogleAdsReportService
COALESCE(SUM(d.impressions), -1) as reach,
-1 as roas, -1 as be_roas, -1 as revenue, -1 as profit, -1 as delivery')
->group('c.campaign_id, c.status, c.customer_id, c.campaign_name')
->where('c.customer_id', 'in', $customerIds)
->where(function ($query) use ($keyword) {
if ($keyword) {
$query->where('c.campaign_name', 'like', '%' . $keyword . '%');
@ -842,8 +851,11 @@ class GoogleAdsReportService
/**
* 将广告组数据导出到 Excel 文件
*/
public static function exportAdGroupsToExcel($keyword = '', $dateRange = 'Today', $startDate = null, $endDate = null)
public function exportAdGroupsToExcel($customerIds,$keyword = '', $dateRange = 'Today', $startDate = null, $endDate = null)
{
if(empty($customerIds)){
return [];
}
// 动态构建日期条件
$dateCondition = '';
if ($startDate && $endDate) {
@ -883,6 +895,7 @@ class GoogleAdsReportService
COALESCE(SUM(d.impressions), -1) as reach,
-1 as roas, -1 as be_roas, -1 as revenue, -1 as profit, -1 as delivery')
->group('ag.ad_group_id, ag.ad_group_name, ag.status, ag.campaign_id, c.campaign_name, ag.customer_id')
->where('ag.customer_id', 'in', $customerIds) // 添加 customerIds 条件
->where(function ($query) use ($keyword) {
if ($keyword) {
$query->where('ag.ad_group_name', 'like', '%' . $keyword . '%');
@ -977,7 +990,7 @@ class GoogleAdsReportService
* @param int $pageSize 每页数量
* @return array
*/
public function getAssetConversionData($customerId, $page, $pageSize, $keyword, $dateRange, $startDate = null, $endDate = null)
public function getAssetConversionData($customerIds, $page, $pageSize, $keyword, $dateRange, $startDate = null, $endDate = null)
{
$isSameMonth = true; // 判断是否跨月
@ -998,7 +1011,20 @@ class GoogleAdsReportService
$query->where('a.asset_name', 'like', '%' . $keyword . '%'); // 关键词模糊匹配
}
});
$adIdsQuery->where('a.customer_id', '=', $customerId);
// 如果提供了 customerIds增加查询条件
if (!empty($customerIds)) {
$adIdsQuery->whereIn('a.customer_id', $customerIds); // 添加 customer_id 的查询约束
} else {
return [
'data' => [],
'chat_1_data' => [], // 返回按月汇总的 chat_data
'total' => 0,
'current_page' => 1,
'last_page' => 1,
];
}
// 日期范围处理,筛选 AssetRelation 的 date 字段
if ($startDate && $endDate) {
$adIdsQuery->whereBetween('r.date', [$startDate, $endDate]);

View File

@ -271,12 +271,24 @@ class GoogleOAuthService
*/
public function getGoogleAdCustomers($options = [])
{
if (!empty($options)) {
$refreshToken = $options['refresh_token'];
// 获取符合条件的客户ID数组
$customers = ThirdUserAdvertiser::alias('tua')
->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
->where('tu.third_type', 'google') // 筛选 third_type 为 google 的记录
->where('tu.access_token', $refreshToken) // 筛选 third_type 为 google 的记录
->field('CAST(tua.advertiser_id AS BIGINT) as customer_id,tua.google_login_customer_id as login_customer_id,tua.google_test_account as test_account,tua.google_manager as manager, tu.access_token as refresh_token') // 获取 advertiser_id 字段
->select(); // 执行查询
} else {
// 获取符合条件的客户ID数组
$customers = ThirdUserAdvertiser::alias('tua')
->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
->where('tu.third_type', 'google') // 筛选 third_type 为 google 的记录
->field('CAST(tua.advertiser_id AS BIGINT) as customer_id,tua.google_login_customer_id as login_customer_id,tua.google_test_account as test_account,tua.google_manager as manager, tu.access_token as refresh_token') // 获取 advertiser_id 字段
->select(); // 执行查询
}
// 如果没有找到符合条件的广告主,抛出异常
if ($customers->isEmpty()) {