ad_count
This commit is contained in:
parent
74359478bb
commit
9c01187a87
@ -810,6 +810,7 @@ class AdsInsightService
|
||||
$creativeDataQuery->group('i.creative_id, i.platform, i.account_id, c.name, c.type,c.url, c.thumbnail_url') // 按需要的字段分组
|
||||
->field([
|
||||
'i.creative_id',
|
||||
'i.platform',
|
||||
'c.name AS creative_name', // 从 bps_ads_creative 表中选择 name
|
||||
'c.url AS creative_url', // 从 bps_ads_creative 表中选择 url
|
||||
'c.type AS creative_type', // 从 bps_ads_creative 表中选择 url
|
||||
@ -844,6 +845,7 @@ class AdsInsightService
|
||||
if (!isset($creativeSummaryData[$creativeData->creative_id])) {
|
||||
$creativeSummaryData[$creativeData->creative_id] = [
|
||||
'creative_id' => $creativeData->creative_id,
|
||||
'platform' => $creativeData->platform,
|
||||
'creative' => $creativeData->creative_name, // 使用联接查询中的 creative_name
|
||||
'creative_type' => $creativeData->creative_type, // 使用联接查询中的 creative_name
|
||||
'creative_url' => $creativeData->creative_url ?: '', // 使用联接查询中的 creative_url
|
||||
@ -897,8 +899,9 @@ class AdsInsightService
|
||||
$creativeSummaryData[$creativeData->creative_id]['roas'] = $roas > 0 ? number_format($roas, 2) . 'X' : '-';
|
||||
|
||||
// 填充广告计数
|
||||
$creativeSummaryData[$creativeData->creative_id]['ad_count'] = rand(10, 200); // 每个 creative_id 对应一个广告
|
||||
// $creativeSummaryData[$creativeData->creative_id]['ad_count'] = rand(10, 200); // 每个 creative_id 对应一个广告
|
||||
}
|
||||
// dump($creativeSummaryData);
|
||||
|
||||
// 汇总总体统计数据
|
||||
$statisticsData['spend'] = array_sum(array_column($creativeSummaryData, 'spend'));
|
||||
@ -932,6 +935,45 @@ class AdsInsightService
|
||||
'hold_rate' => '-',// 格式化百分比
|
||||
];
|
||||
|
||||
// 获取每个 creative_id 对应的广告数量
|
||||
$creativeIds_from_google = array_keys(array_filter($creativeSummaryData, function ($item) {
|
||||
return $item['platform'] == 2;
|
||||
}));
|
||||
|
||||
if (!empty($creativeIds_from_google)) {
|
||||
// 将整数日期转为 Y-m-d 格式
|
||||
$formattedStartDate = DateTime::createFromFormat('Ymd', $startDate)->format('Y-m-d');
|
||||
$formattedEndDate = DateTime::createFromFormat('Ymd', $endDate)->format('Y-m-d');
|
||||
// 查询满足条件的 asset_id 和唯一 ad_id 计数
|
||||
$ad_count_google = AssetRelation::whereIn('asset_id', $creativeIds_from_google)
|
||||
->whereBetween('date', [$formattedStartDate, $formattedEndDate])
|
||||
->group('asset_id')
|
||||
->column('COUNT(DISTINCT ad_id) AS ad_count', 'asset_id');
|
||||
}
|
||||
$creativeIds_from_fb_tk = array_keys(array_filter($creativeSummaryData, function ($item) {
|
||||
return ($item['platform'] == 1 || $item['platform'] == 3);
|
||||
}));
|
||||
if (!empty($creativeIds_from_fb_tk)) {
|
||||
$ad_count_fb_tk = BpsAdAd::whereIn('creative_id', $creativeIds_from_fb_tk)
|
||||
->group('creative_id')
|
||||
->column('COUNT(DISTINCT ad_id) as ad_count', 'creative_id');
|
||||
}
|
||||
// 确保变量存在,避免未定义错误
|
||||
$ad_count_fb_tk = $ad_count_fb_tk ?? [];
|
||||
$ad_count_google = $ad_count_google ?? [];
|
||||
// 合并数组
|
||||
$ad_count_combined = $ad_count_fb_tk + $ad_count_google;
|
||||
//dump($ad_count_combined);
|
||||
// dump($creativeIds_from_fb_tk);
|
||||
|
||||
// 将广告数量填充到对应的 creativeSummaryData 中
|
||||
foreach ($creativeSummaryData as $creativeId => &$creativeData) {
|
||||
$creativeData['ad_count'] = $ad_count_combined[$creativeId] ?? 0; // 如果没有匹配记录,默认填充 0
|
||||
// dump($creativeData);
|
||||
}
|
||||
unset($creativeData); // 解除引用
|
||||
// dump($creativeSummaryData);
|
||||
|
||||
// 格式化返回的创意数据
|
||||
$formattedData = array_map(function ($item) {
|
||||
return [
|
||||
@ -968,6 +1010,7 @@ class AdsInsightService
|
||||
];
|
||||
}, $creativeSummaryData);
|
||||
|
||||
|
||||
// 9. 返回分页数据
|
||||
return [
|
||||
'data' => array_values($formattedData),
|
||||
|
Loading…
Reference in New Issue
Block a user