(Accounts、Campaigns、Ad Sets 、Ads、Creatives)导出到 Excel 更新2
This commit is contained in:
parent
0abc7a77a2
commit
cb5253e8cb
@ -317,7 +317,7 @@ class BpsAdController
|
||||
|
||||
// 获取请求参数
|
||||
$page = $options['pageNo'] ?? 1; // 页码
|
||||
$pageSize = $options['pageSize'] ?? 1000; // 每页数量
|
||||
$pageSize = 10000; // 每页数量
|
||||
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
||||
$platformType = $options['conditions']['platformType'] ?? 0; // 关键字搜索
|
||||
$status = $options['conditions']['status'] ?? 0; // 平台类型
|
||||
@ -364,6 +364,219 @@ class BpsAdController
|
||||
);
|
||||
}
|
||||
|
||||
public function exportAccountsToExcel(Request $request)
|
||||
{
|
||||
$options = $request->all();
|
||||
$options['jwtClaims'] = $request->jwtClaims;
|
||||
|
||||
// 获取请求参数
|
||||
$page = $options['pageNo'] ?? 1; // 页码
|
||||
$pageSize = 10000; // 每页数量
|
||||
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
||||
$platformType = $options['conditions']['platformType'] ?? 0; // 关键字搜索
|
||||
// $status = $options['conditions']['status'] ?? 0; // 平台类型
|
||||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||||
// $dateRange = 'Last Week'; // 默认日期范围
|
||||
// 根据 platformType 获取广告账户
|
||||
if ($platformType === 1) {
|
||||
if (!$request->refresh_token_facebook) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
$accounts = $this->bpsAdAccountService->getMetaAdAccounts(['refresh_token' => $request->refresh_token_facebook]);
|
||||
} elseif ($platformType === 2) {
|
||||
if (!$request->refresh_token_google) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
$accounts = $this->bpsAdAccountService->getGoogleAdAccounts(['refresh_token' => $request->refresh_token_google]);
|
||||
} elseif ($platformType === 3) {
|
||||
if (!$request->refresh_token_tiktok) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
|
||||
} else {
|
||||
// TODO: 匹配jwt的商户id还是登录用户id
|
||||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]);
|
||||
}
|
||||
|
||||
if (empty($accounts)) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
|
||||
// 获取客户ID数组
|
||||
$accountIds = array_column($accounts, 'account_id');
|
||||
// 调用 Service 层查询
|
||||
return $this->adsInsightService::exportAccountsToExcel(
|
||||
$platformType,
|
||||
$accountIds, // 客户 ID 数组
|
||||
$page, // 页码
|
||||
$pageSize, // 每页数量
|
||||
$keyword, // 关键字
|
||||
$startDate, // 开始日期
|
||||
$endDate // 结束日期
|
||||
);
|
||||
}
|
||||
|
||||
public function exportCreativesToExcel(Request $request)
|
||||
{
|
||||
$options = $request->all();
|
||||
$options['jwtClaims'] = $request->jwtClaims;
|
||||
|
||||
// 获取请求参数
|
||||
$page = $options['pageNo'] ?? 1; // 页码
|
||||
$pageSize = 10000; // 每页数量
|
||||
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
||||
$platformType = $options['conditions']['platformType'] ?? 0; // 关键字搜索
|
||||
// $status = $options['conditions']['status'] ?? 0; // 平台类型
|
||||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||||
// $dateRange = 'Last Week'; // 默认日期范围
|
||||
// 根据 platformType 获取广告账户
|
||||
if ($platformType === 1) {
|
||||
if (!$request->refresh_token_facebook) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
$accounts = $this->bpsAdAccountService->getMetaAdAccounts(['refresh_token' => $request->refresh_token_facebook]);
|
||||
} elseif ($platformType === 2) {
|
||||
if (!$request->refresh_token_google) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
$accounts = $this->bpsAdAccountService->getGoogleAdAccounts(['refresh_token' => $request->refresh_token_google]);
|
||||
} elseif ($platformType === 3) {
|
||||
if (!$request->refresh_token_tiktok) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
|
||||
} else {
|
||||
// TODO: 匹配jwt的商户id还是登录用户id
|
||||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]);
|
||||
}
|
||||
|
||||
if (empty($accounts)) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
|
||||
// 获取客户ID数组
|
||||
$accountIds = array_column($accounts, 'account_id');
|
||||
// 调用 Service 层查询
|
||||
return $this->adsInsightService::exportCreativesToExcel(
|
||||
$platformType,
|
||||
$accountIds, // 客户 ID 数组
|
||||
$page, // 页码
|
||||
$pageSize, // 每页数量
|
||||
$keyword, // 关键字
|
||||
$startDate, // 开始日期
|
||||
$endDate // 结束日期
|
||||
);
|
||||
}
|
||||
|
||||
public function exportAdsetsToExcel(Request $request)
|
||||
{
|
||||
$options = $request->all();
|
||||
$options['jwtClaims'] = $request->jwtClaims;
|
||||
|
||||
// 获取请求参数
|
||||
$page = $options['pageNo'] ?? 1; // 页码
|
||||
$pageSize = 10000; // 每页数量
|
||||
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
||||
$platformType = $options['conditions']['platformType'] ?? 0; // 关键字搜索
|
||||
$status = $options['conditions']['status'] ?? 0; // 平台类型
|
||||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||||
// $dateRange = 'Last Week'; // 默认日期范围
|
||||
// 根据 platformType 获取广告账户
|
||||
if ($platformType === 1) {
|
||||
if (!$request->refresh_token_facebook) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
$accounts = $this->bpsAdAccountService->getMetaAdAccounts(['refresh_token' => $request->refresh_token_facebook]);
|
||||
} elseif ($platformType === 2) {
|
||||
if (!$request->refresh_token_google) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
$accounts = $this->bpsAdAccountService->getGoogleAdAccounts(['refresh_token' => $request->refresh_token_google]);
|
||||
} elseif ($platformType === 3) {
|
||||
if (!$request->refresh_token_tiktok) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
|
||||
} else {
|
||||
// TODO: 匹配jwt的商户id还是登录用户id
|
||||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]);
|
||||
}
|
||||
|
||||
if (empty($accounts)) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
|
||||
// 获取客户ID数组
|
||||
$accountIds = array_column($accounts, 'account_id');
|
||||
// 调用 Service 层查询
|
||||
return $this->adsInsightService::exportAdsetsToExcel(
|
||||
$platformType,
|
||||
$accountIds, // 客户 ID 数组
|
||||
$page, // 页码
|
||||
$pageSize, // 每页数量
|
||||
$keyword, // 关键字
|
||||
$startDate, // 开始日期
|
||||
$endDate, // 结束日期
|
||||
$status
|
||||
);
|
||||
}
|
||||
public function exportAdsToExcel(Request $request)
|
||||
{
|
||||
$options = $request->all();
|
||||
$options['jwtClaims'] = $request->jwtClaims;
|
||||
|
||||
// 获取请求参数
|
||||
$page = $options['pageNo'] ?? 1; // 页码
|
||||
$pageSize = 10000; // 每页数量
|
||||
$keyword = $options['conditions']['keyword'] ?? ''; // 关键字搜索
|
||||
$platformType = $options['conditions']['platformType'] ?? 0; // 关键字搜索
|
||||
$status = $options['conditions']['status'] ?? 0; // 平台类型
|
||||
$startDate = $options['conditions']['startDate'] ?? null; // 开始日期
|
||||
$endDate = $options['conditions']['endDate'] ?? null; // 结束日期
|
||||
// $dateRange = 'Last Week'; // 默认日期范围
|
||||
// 根据 platformType 获取广告账户
|
||||
if ($platformType === 1) {
|
||||
if (!$request->refresh_token_facebook) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
$accounts = $this->bpsAdAccountService->getMetaAdAccounts(['refresh_token' => $request->refresh_token_facebook]);
|
||||
} elseif ($platformType === 2) {
|
||||
if (!$request->refresh_token_google) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
$accounts = $this->bpsAdAccountService->getGoogleAdAccounts(['refresh_token' => $request->refresh_token_google]);
|
||||
} elseif ($platformType === 3) {
|
||||
if (!$request->refresh_token_tiktok) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
$accounts = $this->bpsAdAccountService->getTiktokAdAccounts(['refresh_token' => $request->refresh_token_tiktok]);
|
||||
} else {
|
||||
// TODO: 匹配jwt的商户id还是登录用户id
|
||||
$accounts = $this->bpsAdAccountService->getAllAdAccounts(['uid' => $options['jwtClaims']['uid']]);
|
||||
}
|
||||
|
||||
if (empty($accounts)) {
|
||||
return $this->successResponse(['data' => []], $request);
|
||||
}
|
||||
|
||||
// 获取客户ID数组
|
||||
$accountIds = array_column($accounts, 'account_id');
|
||||
// 调用 Service 层查询
|
||||
return $this->adsInsightService::exportAdsToExcel(
|
||||
$platformType,
|
||||
$accountIds, // 客户 ID 数组
|
||||
$page, // 页码
|
||||
$pageSize, // 每页数量
|
||||
$keyword, // 关键字
|
||||
$startDate, // 开始日期
|
||||
$endDate, // 结束日期
|
||||
$status
|
||||
);
|
||||
}
|
||||
|
||||
public function listAdsets(Request $request)
|
||||
{
|
||||
$options = $request->all();
|
||||
|
@ -25,9 +25,24 @@ class AdsInsightService
|
||||
{
|
||||
// 状态映射数组
|
||||
private static $statusMapping = [
|
||||
2 => 'ENABLE', // 2 代表广告已启用
|
||||
3 => 'PAUSED', // 3 代表广告待审核等状态
|
||||
// 其他状态可以继续添加
|
||||
0 => 'UNSPECIFIED', // UNSPECIFIED
|
||||
1 => 'UNKNOWN', // UNKNOWN
|
||||
2 => 'ENABLED', // ENABLED
|
||||
3 => 'PAUSED', // PAUSED
|
||||
4 => 'REMOVED', // REMOVED
|
||||
5 => 'FROZEN', // FROZEN
|
||||
6 => 'ARCHIVED', // ARCHIVED
|
||||
];
|
||||
// 状态映射数组
|
||||
private static $platformMapping = [
|
||||
1 => 'meta',
|
||||
2 => 'google',
|
||||
3 => 'tiktok',
|
||||
];
|
||||
// 状态映射数组
|
||||
private static $creativeTypeMapping = [
|
||||
1 => 'image',
|
||||
2 => 'video',
|
||||
];
|
||||
|
||||
// Redis 键名前缀
|
||||
@ -317,19 +332,15 @@ class AdsInsightService
|
||||
*/
|
||||
public static function exportCampaignsToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $status = 0)
|
||||
{
|
||||
$campaignsStatus = [
|
||||
0 => 'UNSPECIFIED', // UNSPECIFIED
|
||||
1 => 'UNKNOWN', // UNKNOWN
|
||||
2 => 'ENABLED', // ENABLED
|
||||
3 => 'PAUSED', // PAUSED
|
||||
4 => 'REMOVED', // REMOVED
|
||||
];
|
||||
|
||||
// 调用 getCampaignList 获取广告系列数据
|
||||
$campaignList = self::getCampaignList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, $status, false);
|
||||
|
||||
if (empty($campaignList['data'])) {
|
||||
return ['message' => 'No campaign data available for export.'];
|
||||
return Json([
|
||||
'code' => 901,
|
||||
'msg' => 'No data available for export.',
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
|
||||
// 创建 Spreadsheet 对象
|
||||
@ -338,9 +349,28 @@ class AdsInsightService
|
||||
|
||||
// 设置表头
|
||||
$headers = [
|
||||
'A1' => 'Campaign ID', 'B1' => 'Status', 'C1' => 'Name',
|
||||
'D1' => 'Platform', 'E1' => 'Clicks', 'F1' => 'Spend',
|
||||
'G1' => 'Impressions', 'H1' => 'Revenue', 'I1' => 'Purchases'
|
||||
'A1' => 'Ad Campaigns',
|
||||
'B1' => 'Status',
|
||||
'C1' => 'Ad Platform', // 修正为 Ad Platform
|
||||
'D1' => 'Ad Accounts', // 修正为 Ad Accounts
|
||||
'E1' => 'Assisted Purchases',
|
||||
'F1' => 'Last-clicked Purchases',
|
||||
'G1' => 'ROAS',
|
||||
'H1' => 'Amount Spent',
|
||||
'I1' => 'Impressions',
|
||||
'J1' => 'Clicks',
|
||||
'K1' => 'Click Through Rate',
|
||||
'L1' => 'Adds to Cart',
|
||||
'M1' => 'Cost per ATC',
|
||||
'N1' => 'Purchases',
|
||||
'O1' => 'Purchases Value',
|
||||
'P1' => 'Cost per Purchase',
|
||||
'Q1' => 'Conversion Rate',
|
||||
'R1' => 'Revenue',
|
||||
'S1' => 'Total Cost',
|
||||
'T1' => 'Net Profit',
|
||||
'U1' => 'Net Profit Margin',
|
||||
'V1' => 'Net Profit on Ad Spend' // 新增 V 列
|
||||
];
|
||||
foreach ($headers as $cell => $header) {
|
||||
$sheet->setCellValue($cell, $header);
|
||||
@ -350,19 +380,55 @@ class AdsInsightService
|
||||
$row = 2;
|
||||
foreach ($campaignList['data'] as $campaign) {
|
||||
// $sheet->setCellValueExplicit('A' . $row, (string)$campaign['campaign_id'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
|
||||
$sheet->setCellValue('A' . $row, $campaign['id']);
|
||||
$sheet->setCellValue('B' . $row, $campaignsStatus[$campaign['status']]);
|
||||
$sheet->setCellValue('C' . $row, $campaign['name']);
|
||||
$sheet->setCellValue('D' . $row, $campaign['platform_type']);
|
||||
$sheet->setCellValue('E' . $row, $campaign['clicks']);
|
||||
$sheet->setCellValue('F' . $row, $campaign['spend']);
|
||||
$sheet->setCellValue('G' . $row, $campaign['impressions']);
|
||||
$sheet->setCellValue('H' . $row, $campaign['revenue']);
|
||||
$sheet->setCellValue('I' . $row, $campaign['purchases']);
|
||||
$sheet->setCellValue('A' . $row, $campaign['name']); // Ad Campaigns
|
||||
$sheet->setCellValue('B' . $row, self::$statusMapping[$campaign['status']]); // Status
|
||||
$sheet->setCellValue('C' . $row, self::$platformMapping[$campaign['platform_type']]); // Platform Type
|
||||
$sheet->setCellValueExplicit('D' . $row, (string)$campaign['account_id'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
|
||||
$sheet->setCellValue('E' . $row, $campaign['assisted_purchases']); // Assisted Purchases
|
||||
$sheet->setCellValue('F' . $row, $campaign['last_clicked_purchases']); // Last-clicked Purchases
|
||||
$sheet->setCellValue('G' . $row, $campaign['roas']); // ROAS
|
||||
$sheet->setCellValue('H' . $row, $campaign['spend']); // Amount Spent
|
||||
$sheet->setCellValue('I' . $row, $campaign['impressions']); // Impressions
|
||||
$sheet->setCellValue('J' . $row, $campaign['clicks']); // Clicks
|
||||
$sheet->setCellValue('K' . $row, $campaign['ctr']); // Click Through Rate
|
||||
$sheet->setCellValue('L' . $row, $campaign['adds_to_cart']); // Adds to Cart
|
||||
$sheet->setCellValue('M' . $row, $campaign['cost_per_atc']); // Cost per ATC
|
||||
$sheet->setCellValue('N' . $row, $campaign['purchases']); // Purchases
|
||||
$sheet->setCellValue('O' . $row, $campaign['purchases_value']); // Purchases Value
|
||||
$sheet->setCellValue('P' . $row, $campaign['cost_per_purchase']); // Cost per Purchase
|
||||
$sheet->setCellValue('Q' . $row, $campaign['conversion_rate']); // Conversion Rate
|
||||
$sheet->setCellValue('R' . $row, $campaign['revenue']); // Revenue
|
||||
$sheet->setCellValue('S' . $row, $campaign['total_cost']); // Total Cost
|
||||
$sheet->setCellValue('T' . $row, $campaign['net_profit']); // Net Profit
|
||||
$sheet->setCellValue('U' . $row, $campaign['net_profit_margin']); // Net Profit Margin
|
||||
$sheet->setCellValue('V' . $row, $campaign['net_profit_on_ad_spend']); // Net Profit on Ad Spend
|
||||
$row++;
|
||||
}
|
||||
// 填充统计信息到表格的最后一行
|
||||
$statistics = $campaignList['statistics'];
|
||||
$statisticsRow = $row; // 统计信息从当前行开始
|
||||
$sheet->setCellValue('D' . $statisticsRow, 'Totals'); // 统计信息标题
|
||||
$sheet->setCellValue('E' . $statisticsRow, $statistics['assisted_purchases']); // Assisted Purchases
|
||||
$sheet->setCellValue('F' . $statisticsRow, $statistics['last_clicked_purchases']); // Last-clicked Purchases
|
||||
$sheet->setCellValue('G' . $statisticsRow, $statistics['roas']); // ROAS
|
||||
$sheet->setCellValue('H' . $statisticsRow, $statistics['amount_spend']); // Amount Spent
|
||||
$sheet->setCellValue('I' . $statisticsRow, $statistics['impressions']); // Impressions
|
||||
$sheet->setCellValue('J' . $statisticsRow, $statistics['clicks']); // Clicks
|
||||
$sheet->setCellValue('K' . $statisticsRow, $statistics['ctr']); // Click Through Rate
|
||||
$sheet->setCellValue('L' . $statisticsRow, $statistics['adds_to_cart']); // Adds to Cart
|
||||
$sheet->setCellValue('M' . $statisticsRow, $statistics['cost_per_atc']); // Cost per ATC
|
||||
$sheet->setCellValue('N' . $statisticsRow, $statistics['purchases']); // Purchases
|
||||
$sheet->setCellValue('O' . $statisticsRow, $statistics['purchases_value']); // Purchases Value
|
||||
$sheet->setCellValue('P' . $statisticsRow, $statistics['cost_per_purchase']); // Cost per Purchase
|
||||
$sheet->setCellValue('Q' . $statisticsRow, $statistics['conversion_rate']); // Conversion Rate
|
||||
$sheet->setCellValue('R' . $statisticsRow, $statistics['revenue']); // Revenue
|
||||
$sheet->setCellValue('S' . $statisticsRow, $statistics['total_cost']); // Total Cost
|
||||
$sheet->setCellValue('T' . $statisticsRow, $statistics['net_profit']); // Net Profit
|
||||
$sheet->setCellValue('U' . $statisticsRow, $statistics['net_profit_margin']); // Net Profit Margin
|
||||
$sheet->setCellValue('V' . $statisticsRow, $statistics['net_profit_on_ad_spend']); // Net Profit on Ad Spend
|
||||
|
||||
|
||||
unset($statistics);
|
||||
unset($campaignList);
|
||||
// 文件名与路径
|
||||
$fileName = 'Exported_Campaigns_' . date('Y-m-d_H-i-s') . '.xlsx';
|
||||
$filePath = public_path() . '/' . $fileName;
|
||||
@ -373,7 +439,402 @@ class AdsInsightService
|
||||
$writer->save($filePath);
|
||||
return response()->download($filePath, $fileName);
|
||||
} catch (\Exception $e) {
|
||||
return ['success' => false, 'message' => $e->getMessage()];
|
||||
// return ['success' => false, 'message' => $e->getMessage()];
|
||||
return Json([
|
||||
'code' => 901,
|
||||
'msg' => 'error',
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出广告组列表到 Excel
|
||||
*
|
||||
* @param int $platformType 平台类型
|
||||
* @param array $customerIds 客户ID列表
|
||||
* @param int $page 当前页码
|
||||
* @param int $pageSize 每页条数
|
||||
* @param string $keyword 关键字
|
||||
* @param string|null $startDate 开始日期
|
||||
* @param string|null $endDate 结束日期
|
||||
* @param int $status 广告系列状态
|
||||
*/
|
||||
public static function exportAdsetsToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $status = 0)
|
||||
{
|
||||
// 调用 getCampaignList 获取广告系列数据
|
||||
$adsetList = self::getAdsetList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, $status, false);
|
||||
|
||||
if (empty($adsetList['data'])) {
|
||||
return Json([
|
||||
'code' => 901,
|
||||
'msg' => 'No data available for export.',
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
|
||||
// 创建 Spreadsheet 对象
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// 设置表头
|
||||
$headers = [
|
||||
'A1' => 'Ad Sets',
|
||||
'B1' => 'Status',
|
||||
'C1' => 'Ad Platform', // 修正为 Ad Platform
|
||||
'D1' => 'Ad Accounts', // 修正为 Ad Accounts
|
||||
'E1' => 'Assisted Purchases',
|
||||
'F1' => 'Last-clicked Purchases',
|
||||
'G1' => 'ROAS',
|
||||
'H1' => 'Amount Spent',
|
||||
'I1' => 'Impressions',
|
||||
'J1' => 'Clicks',
|
||||
'K1' => 'Click Through Rate',
|
||||
'L1' => 'Adds to Cart',
|
||||
'M1' => 'Cost per ATC',
|
||||
'N1' => 'Purchases',
|
||||
'O1' => 'Purchases Value',
|
||||
'P1' => 'Cost per Purchase',
|
||||
'Q1' => 'Conversion Rate',
|
||||
'R1' => 'Revenue',
|
||||
'S1' => 'Total Cost',
|
||||
'T1' => 'Net Profit',
|
||||
'U1' => 'Net Profit Margin',
|
||||
'V1' => 'Net Profit on Ad Spend' // 新增 V 列
|
||||
];
|
||||
foreach ($headers as $cell => $header) {
|
||||
$sheet->setCellValue($cell, $header);
|
||||
}
|
||||
|
||||
// 填充数据
|
||||
$row = 2;
|
||||
foreach ($adsetList['data'] as $adset) {
|
||||
// $sheet->setCellValueExplicit('A' . $row, (string)$campaign['campaign_id'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
|
||||
$sheet->setCellValue('A' . $row, $adset['name']); // Ad Campaigns
|
||||
$sheet->setCellValue('B' . $row, self::$statusMapping[$adset['status']]); // Status
|
||||
$sheet->setCellValue('C' . $row, self::$platformMapping[$adset['platform_type']]); // Platform Type
|
||||
$sheet->setCellValueExplicit('D' . $row, (string)$adset['account_id'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
|
||||
$sheet->setCellValue('E' . $row, $adset['assisted_purchases']); // Assisted Purchases
|
||||
$sheet->setCellValue('F' . $row, $adset['last_clicked_purchases']); // Last-clicked Purchases
|
||||
$sheet->setCellValue('G' . $row, $adset['roas']); // ROAS
|
||||
$sheet->setCellValue('H' . $row, $adset['spend']); // Amount Spent
|
||||
$sheet->setCellValue('I' . $row, $adset['impressions']); // Impressions
|
||||
$sheet->setCellValue('J' . $row, $adset['clicks']); // Clicks
|
||||
$sheet->setCellValue('K' . $row, $adset['ctr']); // Click Through Rate
|
||||
$sheet->setCellValue('L' . $row, $adset['adds_to_cart']); // Adds to Cart
|
||||
$sheet->setCellValue('M' . $row, $adset['cost_per_atc']); // Cost per ATC
|
||||
$sheet->setCellValue('N' . $row, $adset['purchases']); // Purchases
|
||||
$sheet->setCellValue('O' . $row, $adset['purchases_value']); // Purchases Value
|
||||
$sheet->setCellValue('P' . $row, $adset['cost_per_purchase']); // Cost per Purchase
|
||||
$sheet->setCellValue('Q' . $row, $adset['conversion_rate']); // Conversion Rate
|
||||
$sheet->setCellValue('R' . $row, $adset['revenue']); // Revenue
|
||||
$sheet->setCellValue('S' . $row, $adset['total_cost']); // Total Cost
|
||||
$sheet->setCellValue('T' . $row, $adset['net_profit']); // Net Profit
|
||||
$sheet->setCellValue('U' . $row, $adset['net_profit_margin']); // Net Profit Margin
|
||||
$sheet->setCellValue('V' . $row, $adset['net_profit_on_ad_spend']); // Net Profit on Ad Spend
|
||||
$row++;
|
||||
}
|
||||
// 填充统计信息到表格的最后一行
|
||||
$statistics = $adsetList['statistics'];
|
||||
$statisticsRow = $row; // 统计信息从当前行开始
|
||||
$sheet->setCellValue('D' . $statisticsRow, 'Totals'); // 统计信息标题
|
||||
$sheet->setCellValue('E' . $statisticsRow, $statistics['assisted_purchases']); // Assisted Purchases
|
||||
$sheet->setCellValue('F' . $statisticsRow, $statistics['last_clicked_purchases']); // Last-clicked Purchases
|
||||
$sheet->setCellValue('G' . $statisticsRow, $statistics['roas']); // ROAS
|
||||
$sheet->setCellValue('H' . $statisticsRow, $statistics['amount_spend']); // Amount Spent
|
||||
$sheet->setCellValue('I' . $statisticsRow, $statistics['impressions']); // Impressions
|
||||
$sheet->setCellValue('J' . $statisticsRow, $statistics['clicks']); // Clicks
|
||||
$sheet->setCellValue('K' . $statisticsRow, $statistics['ctr']); // Click Through Rate
|
||||
$sheet->setCellValue('L' . $statisticsRow, $statistics['adds_to_cart']); // Adds to Cart
|
||||
$sheet->setCellValue('M' . $statisticsRow, $statistics['cost_per_atc']); // Cost per ATC
|
||||
$sheet->setCellValue('N' . $statisticsRow, $statistics['purchases']); // Purchases
|
||||
$sheet->setCellValue('O' . $statisticsRow, $statistics['purchases_value']); // Purchases Value
|
||||
$sheet->setCellValue('P' . $statisticsRow, $statistics['cost_per_purchase']); // Cost per Purchase
|
||||
$sheet->setCellValue('Q' . $statisticsRow, $statistics['conversion_rate']); // Conversion Rate
|
||||
$sheet->setCellValue('R' . $statisticsRow, $statistics['revenue']); // Revenue
|
||||
$sheet->setCellValue('S' . $statisticsRow, $statistics['total_cost']); // Total Cost
|
||||
$sheet->setCellValue('T' . $statisticsRow, $statistics['net_profit']); // Net Profit
|
||||
$sheet->setCellValue('U' . $statisticsRow, $statistics['net_profit_margin']); // Net Profit Margin
|
||||
$sheet->setCellValue('V' . $statisticsRow, $statistics['net_profit_on_ad_spend']); // Net Profit on Ad Spend
|
||||
|
||||
unset($statistics);
|
||||
unset($adsetList);
|
||||
// 文件名与路径
|
||||
$fileName = 'Exported_Adsets_' . date('Y-m-d_H-i-s') . '.xlsx';
|
||||
$filePath = public_path() . '/' . $fileName;
|
||||
|
||||
// 保存 Excel 文件
|
||||
$writer = new Xlsx($spreadsheet);
|
||||
try {
|
||||
$writer->save($filePath);
|
||||
return response()->download($filePath, $fileName);
|
||||
} catch (\Exception $e) {
|
||||
// return ['success' => false, 'message' => $e->getMessage()];
|
||||
return Json([
|
||||
'code' => 901,
|
||||
'msg' => 'error',
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出广告列表到 Excel
|
||||
*
|
||||
* @param int $platformType 平台类型
|
||||
* @param array $customerIds 客户ID列表
|
||||
* @param int $page 当前页码
|
||||
* @param int $pageSize 每页条数
|
||||
* @param string $keyword 关键字
|
||||
* @param string|null $startDate 开始日期
|
||||
* @param string|null $endDate 结束日期
|
||||
* @param int $status 广告系列状态
|
||||
*/
|
||||
public static function exportAdsToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $status = 0)
|
||||
{
|
||||
// 调用 getCampaignList 获取广告系列数据
|
||||
$adList = self::getAdList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, $status, false);
|
||||
|
||||
if (empty($adList['data'])) {
|
||||
return Json([
|
||||
'code' => 901,
|
||||
'msg' => 'No data available for export.',
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
|
||||
// 创建 Spreadsheet 对象
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// 设置表头
|
||||
$headers = [
|
||||
'A1' => 'Ads',
|
||||
'B1' => 'Status',
|
||||
'C1' => 'Ad Platform', // 修正为 Ad Platform
|
||||
'D1' => 'Ad Accounts', // 修正为 Ad Accounts
|
||||
'E1' => 'Assisted Purchases',
|
||||
'F1' => 'Last-clicked Purchases',
|
||||
'G1' => 'ROAS',
|
||||
'H1' => 'Amount Spent',
|
||||
'I1' => 'Impressions',
|
||||
'J1' => 'Clicks',
|
||||
'K1' => 'Click Through Rate',
|
||||
'L1' => 'Adds to Cart',
|
||||
'M1' => 'Cost per ATC',
|
||||
'N1' => 'Purchases',
|
||||
'O1' => 'Purchases Value',
|
||||
'P1' => 'Cost per Purchase',
|
||||
'Q1' => 'Conversion Rate',
|
||||
'R1' => 'Revenue',
|
||||
'S1' => 'Total Cost',
|
||||
'T1' => 'Net Profit',
|
||||
'U1' => 'Net Profit Margin',
|
||||
'V1' => 'Net Profit on Ad Spend' // 新增 V 列
|
||||
];
|
||||
foreach ($headers as $cell => $header) {
|
||||
$sheet->setCellValue($cell, $header);
|
||||
}
|
||||
|
||||
// 填充数据
|
||||
$row = 2;
|
||||
foreach ($adList['data'] as $ad) {
|
||||
// $sheet->setCellValueExplicit('A' . $row, (string)$campaign['campaign_id'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
|
||||
$sheet->setCellValue('A' . $row, $ad['name']); // Ad Campaigns
|
||||
$sheet->setCellValue('B' . $row, self::$statusMapping[$ad['status']]); // Status
|
||||
$sheet->setCellValue('C' . $row, self::$platformMapping[$ad['platform_type']]); // Platform Type
|
||||
$sheet->setCellValueExplicit('D' . $row, (string)$ad['account_id'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
|
||||
$sheet->setCellValue('E' . $row, $ad['assisted_purchases']); // Assisted Purchases
|
||||
$sheet->setCellValue('F' . $row, $ad['last_clicked_purchases']); // Last-clicked Purchases
|
||||
$sheet->setCellValue('G' . $row, $ad['roas']); // ROAS
|
||||
$sheet->setCellValue('H' . $row, $ad['spend']); // Amount Spent
|
||||
$sheet->setCellValue('I' . $row, $ad['impressions']); // Impressions
|
||||
$sheet->setCellValue('J' . $row, $ad['clicks']); // Clicks
|
||||
$sheet->setCellValue('K' . $row, $ad['ctr']); // Click Through Rate
|
||||
$sheet->setCellValue('L' . $row, $ad['adds_to_cart']); // Adds to Cart
|
||||
$sheet->setCellValue('M' . $row, $ad['cost_per_atc']); // Cost per ATC
|
||||
$sheet->setCellValue('N' . $row, $ad['purchases']); // Purchases
|
||||
$sheet->setCellValue('O' . $row, $ad['purchases_value']); // Purchases Value
|
||||
$sheet->setCellValue('P' . $row, $ad['cost_per_purchase']); // Cost per Purchase
|
||||
$sheet->setCellValue('Q' . $row, $ad['conversion_rate']); // Conversion Rate
|
||||
$sheet->setCellValue('R' . $row, $ad['revenue']); // Revenue
|
||||
$sheet->setCellValue('S' . $row, $ad['total_cost']); // Total Cost
|
||||
$sheet->setCellValue('T' . $row, $ad['net_profit']); // Net Profit
|
||||
$sheet->setCellValue('U' . $row, $ad['net_profit_margin']); // Net Profit Margin
|
||||
$sheet->setCellValue('V' . $row, $ad['net_profit_on_ad_spend']); // Net Profit on Ad Spend
|
||||
$row++;
|
||||
}
|
||||
// 填充统计信息到表格的最后一行
|
||||
$statistics = $adList['statistics'];
|
||||
$statisticsRow = $row; // 统计信息从当前行开始
|
||||
$sheet->setCellValue('D' . $statisticsRow, 'Totals'); // 统计信息标题
|
||||
$sheet->setCellValue('E' . $statisticsRow, $statistics['assisted_purchases']); // Assisted Purchases
|
||||
$sheet->setCellValue('F' . $statisticsRow, $statistics['last_clicked_purchases']); // Last-clicked Purchases
|
||||
$sheet->setCellValue('G' . $statisticsRow, $statistics['roas']); // ROAS
|
||||
$sheet->setCellValue('H' . $statisticsRow, $statistics['amount_spend']); // Amount Spent
|
||||
$sheet->setCellValue('I' . $statisticsRow, $statistics['impressions']); // Impressions
|
||||
$sheet->setCellValue('J' . $statisticsRow, $statistics['clicks']); // Clicks
|
||||
$sheet->setCellValue('K' . $statisticsRow, $statistics['ctr']); // Click Through Rate
|
||||
$sheet->setCellValue('L' . $statisticsRow, $statistics['adds_to_cart']); // Adds to Cart
|
||||
$sheet->setCellValue('M' . $statisticsRow, $statistics['cost_per_atc']); // Cost per ATC
|
||||
$sheet->setCellValue('N' . $statisticsRow, $statistics['purchases']); // Purchases
|
||||
$sheet->setCellValue('O' . $statisticsRow, $statistics['purchases_value']); // Purchases Value
|
||||
$sheet->setCellValue('P' . $statisticsRow, $statistics['cost_per_purchase']); // Cost per Purchase
|
||||
$sheet->setCellValue('Q' . $statisticsRow, $statistics['conversion_rate']); // Conversion Rate
|
||||
$sheet->setCellValue('R' . $statisticsRow, $statistics['revenue']); // Revenue
|
||||
$sheet->setCellValue('S' . $statisticsRow, $statistics['total_cost']); // Total Cost
|
||||
$sheet->setCellValue('T' . $statisticsRow, $statistics['net_profit']); // Net Profit
|
||||
$sheet->setCellValue('U' . $statisticsRow, $statistics['net_profit_margin']); // Net Profit Margin
|
||||
$sheet->setCellValue('V' . $statisticsRow, $statistics['net_profit_on_ad_spend']); // Net Profit on Ad Spend
|
||||
|
||||
unset($statistics);
|
||||
unset($adList);
|
||||
// 文件名与路径
|
||||
$fileName = 'Exported_Ads_' . date('Y-m-d_H-i-s') . '.xlsx';
|
||||
$filePath = public_path() . '/' . $fileName;
|
||||
|
||||
// 保存 Excel 文件
|
||||
$writer = new Xlsx($spreadsheet);
|
||||
try {
|
||||
$writer->save($filePath);
|
||||
return response()->download($filePath, $fileName);
|
||||
} catch (\Exception $e) {
|
||||
// return ['success' => false, 'message' => $e->getMessage()];
|
||||
return Json([
|
||||
'code' => 901,
|
||||
'msg' => 'error',
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function exportCreativesToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null)
|
||||
{
|
||||
// 获取广告创意数据
|
||||
$creativeList = self::getCreativeInsightData($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, false);
|
||||
|
||||
if (empty($creativeList['data'])) {
|
||||
return Json([
|
||||
'code' => 901,
|
||||
'msg' => 'No data available for export.',
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
|
||||
// 创建 Spreadsheet 对象
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// 设置表头
|
||||
$headers = [
|
||||
'A1' => 'Creative',
|
||||
'B1' => 'Thumbnail/Creative URL',
|
||||
'C1' => 'Creative Type',
|
||||
'D1' => 'Ad Count',
|
||||
'E1' => 'Spend',
|
||||
'F1' => 'Purchase value',
|
||||
'G1' => 'ROAS',
|
||||
'H1' => 'CPA',
|
||||
'I1' => 'CPC (link click)',
|
||||
'J1' => 'CPM',
|
||||
'K1' => 'CPC (all)',
|
||||
'L1' => 'AOV',
|
||||
'M1' => 'Click to ATC ratio',
|
||||
'N1' => 'ATC to purchase ratio',
|
||||
'O1' => 'Purchases',
|
||||
'P1' => '1st frame retention',
|
||||
'Q1' => 'Thumbstop',
|
||||
'R1' => 'CTR (outbound)',
|
||||
'S1' => 'Click to purchase',
|
||||
'T1' => 'CTR (all)',
|
||||
'U1' => '25% video plays (rate)',
|
||||
'V1' => '50% video plays (rate)',
|
||||
'W1' => '75% video plays (rate)',
|
||||
'X1' => '100% video plays (rate)',
|
||||
'Y1' => 'Hold rate'
|
||||
];
|
||||
|
||||
// 填充表头
|
||||
foreach ($headers as $cell => $header) {
|
||||
$sheet->setCellValue($cell, $header);
|
||||
}
|
||||
|
||||
// 填充数据
|
||||
$row = 2;
|
||||
foreach ($creativeList['data'] as $creative) {
|
||||
$sheet->setCellValue('A' . $row, $creative['creative']); // Creative
|
||||
$sheet->setCellValue('B' . $row, $creative['thumbnail_url'] ?: $creative['creative_url']); // Thumbnail/Creative URL
|
||||
$sheet->setCellValue('C' . $row, self::$creativeTypeMapping[$creative['creative_type']]); // Creative Type
|
||||
$sheet->setCellValue('D' . $row, $creative['ad_count']); // Ad Count
|
||||
$sheet->setCellValue('E' . $row, $creative['spend']); // Spend
|
||||
$sheet->setCellValue('F' . $row, $creative['purchases_value']); // Purchase value
|
||||
$sheet->setCellValue('G' . $row, $creative['roas']); // ROAS
|
||||
$sheet->setCellValue('H' . $row, $creative['cpa']); // CPA
|
||||
$sheet->setCellValue('I' . $row, $creative['cpc_link_click']); // CPC (link click)
|
||||
$sheet->setCellValue('J' . $row, $creative['cpm']); // CPM
|
||||
$sheet->setCellValue('K' . $row, $creative['cpc_all']); // CPC (all)
|
||||
$sheet->setCellValue('L' . $row, $creative['aov']); // AOV
|
||||
$sheet->setCellValue('M' . $row, $creative['click_to_atc_ratio']); // Click to ATC ratio
|
||||
$sheet->setCellValue('N' . $row, $creative['atc_to_purchase_ratio']); // ATC to purchase ratio
|
||||
$sheet->setCellValue('O' . $row, $creative['purchases']); // Purchases
|
||||
$sheet->setCellValue('P' . $row, $creative['first_frame_retention']); // 1st frame retention
|
||||
$sheet->setCellValue('Q' . $row, $creative['thumbstop']); // Thumbstop
|
||||
$sheet->setCellValue('R' . $row, $creative['ctr_outbound']); // CTR (outbound)
|
||||
$sheet->setCellValue('S' . $row, $creative['click_to_purchase']); // Click to purchase
|
||||
$sheet->setCellValue('T' . $row, $creative['ctr_all']); // CTR (all)
|
||||
$sheet->setCellValue('U' . $row, $creative['video_plays_25_rate']); // 25% video plays (rate)
|
||||
$sheet->setCellValue('V' . $row, $creative['video_plays_50_rate']); // 50% video plays (rate)
|
||||
$sheet->setCellValue('W' . $row, $creative['video_plays_75_rate']); // 75% video plays (rate)
|
||||
$sheet->setCellValue('X' . $row, $creative['video_plays_100_rate']); // 100% video plays (rate)
|
||||
$sheet->setCellValue('Y' . $row, $creative['hold_rate']); // Hold rate
|
||||
$row++;
|
||||
}
|
||||
|
||||
// 填充统计信息到表格的最后一行
|
||||
$statistics = $creativeList['statistics'];
|
||||
$statisticsRow = $row; // 统计信息从当前行开始
|
||||
$sheet->setCellValue('D' . $statisticsRow, 'Totals'); // 统计信息标题
|
||||
$sheet->setCellValue('E' . $statisticsRow, $statistics['spend']); // Spend
|
||||
$sheet->setCellValue('F' . $statisticsRow, $statistics['purchases_value']); // Purchase value
|
||||
$sheet->setCellValue('G' . $statisticsRow, $statistics['roas']); // ROAS
|
||||
$sheet->setCellValue('H' . $statisticsRow, $statistics['cpa']); // CPA
|
||||
$sheet->setCellValue('I' . $statisticsRow, $statistics['cpc_link_click']); // CPC (link click)
|
||||
$sheet->setCellValue('J' . $statisticsRow, $statistics['cpm']); // CPM
|
||||
$sheet->setCellValue('K' . $statisticsRow, $statistics['cpc_all']); // CPC (all)
|
||||
$sheet->setCellValue('L' . $statisticsRow, $statistics['aov']); // AOV
|
||||
$sheet->setCellValue('M' . $statisticsRow, $statistics['click_to_atc_ratio']); // Click to ATC ratio
|
||||
$sheet->setCellValue('N' . $statisticsRow, $statistics['atc_to_purchase_ratio']); // ATC to purchase ratio
|
||||
$sheet->setCellValue('O' . $statisticsRow, $statistics['purchases']); // Purchases
|
||||
$sheet->setCellValue('P' . $statisticsRow, $statistics['first_frame_retention']); // 1st frame retention
|
||||
$sheet->setCellValue('Q' . $statisticsRow, $statistics['thumbstop']); // Thumbstop
|
||||
$sheet->setCellValue('R' . $statisticsRow, $statistics['ctr_outbound']); // CTR (outbound)
|
||||
$sheet->setCellValue('S' . $statisticsRow, $statistics['click_to_purchase']); // Click to purchase
|
||||
$sheet->setCellValue('T' . $statisticsRow, $statistics['ctr_all']); // CTR (all)
|
||||
$sheet->setCellValue('U' . $statisticsRow, $statistics['video_plays_25_rate']); // 25% video plays (rate)
|
||||
$sheet->setCellValue('V' . $statisticsRow, $statistics['video_plays_50_rate']); // 50% video plays (rate)
|
||||
$sheet->setCellValue('W' . $statisticsRow, $statistics['video_plays_75_rate']); // 75% video plays (rate)
|
||||
$sheet->setCellValue('X' . $statisticsRow, $statistics['video_plays_100_rate']); // 100% video plays (rate)
|
||||
$sheet->setCellValue('Y' . $statisticsRow, $statistics['hold_rate']); // Hold rate
|
||||
|
||||
// 释放内存
|
||||
unset($statistics);
|
||||
unset($creativeList);
|
||||
|
||||
// 文件名与路径
|
||||
$fileName = 'Exported_Creatives_' . date('Y-m-d_H-i-s') . '.xlsx';
|
||||
$filePath = public_path() . '/' . $fileName;
|
||||
|
||||
// 保存 Excel 文件
|
||||
$writer = new Xlsx($spreadsheet);
|
||||
try {
|
||||
$writer->save($filePath);
|
||||
return response()->download($filePath, $fileName);
|
||||
} catch (\Exception $e) {
|
||||
return Json([
|
||||
'code' => 901,
|
||||
'msg' => 'error',
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -547,6 +1008,137 @@ class AdsInsightService
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 导出广告账号列表到 Excel
|
||||
*
|
||||
* @param int $platformType 平台类型
|
||||
* @param array $customerIds 客户ID列表
|
||||
* @param int $page 当前页码
|
||||
* @param int $pageSize 每页条数
|
||||
* @param string $keyword 关键字
|
||||
* @param string|null $startDate 开始日期
|
||||
* @param string|null $endDate 结束日期
|
||||
* @param int $status 广告系列状态
|
||||
*/
|
||||
public static function exportAccountsToExcel($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null)
|
||||
{
|
||||
// 调用 getCampaignList 获取广告系列数据
|
||||
$accountList = self::getAccountList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate, $endDate, false);
|
||||
|
||||
// dump($accountList);
|
||||
|
||||
if (empty($accountList['data'])) {
|
||||
return Json([
|
||||
'code' => 901,
|
||||
'msg' => 'No data available for export.',
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
|
||||
// 创建 Spreadsheet 对象
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// 设置表头
|
||||
$headers = [
|
||||
'A1' => 'Ad Account Id',
|
||||
'B1' => 'Advertiser Name',
|
||||
'C1' => 'Ad Platform',
|
||||
'D1' => 'Assisted Purchases',
|
||||
'E1' => 'Last-clicked Purchases',
|
||||
'F1' => 'ROAS',
|
||||
'G1' => 'Amount Spent',
|
||||
'H1' => 'Impressions',
|
||||
'I1' => 'Clicks',
|
||||
'J1' => 'Click Through Rate',
|
||||
'K1' => 'Adds to Cart',
|
||||
'L1' => 'Cost per ATC',
|
||||
'M1' => 'Purchases',
|
||||
'N1' => 'Purchases Value',
|
||||
'O1' => 'Cost per Purchase',
|
||||
'P1' => 'Conversion Rate',
|
||||
'Q1' => 'Revenue',
|
||||
'R1' => 'Total Cost',
|
||||
'S1' => 'Net Profit',
|
||||
'T1' => 'Net Profit Margin',
|
||||
'U1' => 'Net Profit on Ad Spend'
|
||||
];
|
||||
foreach ($headers as $cell => $header) {
|
||||
$sheet->setCellValue($cell, $header);
|
||||
}
|
||||
|
||||
// 填充数据
|
||||
// 填充数据
|
||||
$row = 2;
|
||||
foreach ($accountList['data'] as $account) {
|
||||
$sheet->setCellValueExplicit('A' . $row, (string)$account['user_id'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING); // Ad Account Id
|
||||
$sheet->setCellValue('B' . $row, $account['advertiser_name']); // Advertiser Name
|
||||
$sheet->setCellValue('C' . $row, self::$platformMapping[$account['platform_type']]); // Ad Platform
|
||||
$sheet->setCellValue('D' . $row, $account['assisted_purchases']); // Assisted Purchases(原 E 列)
|
||||
$sheet->setCellValue('E' . $row, $account['last_clicked_purchases']); // Last-clicked Purchases(原 F 列)
|
||||
$sheet->setCellValue('F' . $row, $account['roas']); // ROAS(原 G 列)
|
||||
$sheet->setCellValue('G' . $row, $account['spend']); // Amount Spent(原 H 列)
|
||||
$sheet->setCellValue('H' . $row, $account['impressions']); // Impressions(原 I 列)
|
||||
$sheet->setCellValue('I' . $row, $account['clicks']); // Clicks(原 J 列)
|
||||
$sheet->setCellValue('J' . $row, $account['ctr']); // Click Through Rate(原 K 列)
|
||||
$sheet->setCellValue('K' . $row, $account['adds_to_cart']); // Adds to Cart(原 L 列)
|
||||
$sheet->setCellValue('L' . $row, $account['cost_per_atc']); // Cost per ATC(原 M 列)
|
||||
$sheet->setCellValue('M' . $row, $account['purchases']); // Purchases(原 N 列)
|
||||
$sheet->setCellValue('N' . $row, $account['purchases_value']); // Purchases Value(原 O 列)
|
||||
$sheet->setCellValue('O' . $row, $account['cost_per_purchase']); // Cost per Purchase(原 P 列)
|
||||
$sheet->setCellValue('P' . $row, $account['conversion_rate']); // Conversion Rate(原 Q 列)
|
||||
$sheet->setCellValue('Q' . $row, $account['revenue']); // Revenue(原 R 列)
|
||||
$sheet->setCellValue('R' . $row, $account['total_cost']); // Total Cost(原 S 列)
|
||||
$sheet->setCellValue('S' . $row, $account['net_profit']); // Net Profit(原 T 列)
|
||||
$sheet->setCellValue('T' . $row, $account['net_profit_margin']); // Net Profit Margin(原 U 列)
|
||||
$sheet->setCellValue('U' . $row, $account['net_profit_on_ad_spend']); // Net Profit on Ad Spend(原 V 列)
|
||||
$row++;
|
||||
}
|
||||
// 填充统计信息到表格的最后一行
|
||||
$statistics = $accountList['statistics'];
|
||||
$statisticsRow = $row; // 统计信息从当前行开始
|
||||
$sheet->setCellValue('C' . $statisticsRow, 'Totals'); // 统计信息标题(调整到 A 列)
|
||||
$sheet->setCellValue('D' . $statisticsRow, $statistics['assisted_purchases']); // Assisted Purchases(原 E 列)
|
||||
$sheet->setCellValue('E' . $statisticsRow, $statistics['last_clicked_purchases']); // Last-clicked Purchases(原 F 列)
|
||||
$sheet->setCellValue('F' . $statisticsRow, $statistics['roas']); // ROAS(原 G 列)
|
||||
$sheet->setCellValue('G' . $statisticsRow, $statistics['amount_spend']); // Amount Spent(原 H 列)
|
||||
$sheet->setCellValue('H' . $statisticsRow, $statistics['impressions']); // Impressions(原 I 列)
|
||||
$sheet->setCellValue('I' . $statisticsRow, $statistics['clicks']); // Clicks(原 J 列)
|
||||
$sheet->setCellValue('J' . $statisticsRow, $statistics['ctr']); // Click Through Rate(原 K 列)
|
||||
$sheet->setCellValue('K' . $statisticsRow, $statistics['adds_to_cart']); // Adds to Cart(原 L 列)
|
||||
$sheet->setCellValue('L' . $statisticsRow, $statistics['cost_per_atc']); // Cost per ATC(原 M 列)
|
||||
$sheet->setCellValue('M' . $statisticsRow, $statistics['purchases']); // Purchases(原 N 列)
|
||||
$sheet->setCellValue('N' . $statisticsRow, $statistics['purchases_value']); // Purchases Value(原 O 列)
|
||||
$sheet->setCellValue('O' . $statisticsRow, $statistics['cost_per_purchase']); // Cost per Purchase(原 P 列)
|
||||
$sheet->setCellValue('P' . $statisticsRow, $statistics['conversion_rate']); // Conversion Rate(原 Q 列)
|
||||
$sheet->setCellValue('Q' . $statisticsRow, $statistics['revenue']); // Revenue(原 R 列)
|
||||
$sheet->setCellValue('R' . $statisticsRow, $statistics['total_cost']); // Total Cost(原 S 列)
|
||||
$sheet->setCellValue('S' . $statisticsRow, $statistics['net_profit']); // Net Profit(原 T 列)
|
||||
$sheet->setCellValue('T' . $statisticsRow, $statistics['net_profit_margin']); // Net Profit Margin(原 U 列)
|
||||
$sheet->setCellValue('U' . $statisticsRow, $statistics['net_profit_on_ad_spend']); // Net Profit on Ad Spend(原 V 列)
|
||||
|
||||
unset($statistics);
|
||||
unset($accountList);
|
||||
// 文件名与路径
|
||||
$fileName = 'Exported_Accounts_' . date('Y-m-d_H-i-s') . '.xlsx';
|
||||
$filePath = public_path() . '/' . $fileName;
|
||||
|
||||
// 保存 Excel 文件
|
||||
$writer = new Xlsx($spreadsheet);
|
||||
try {
|
||||
$writer->save($filePath);
|
||||
return response()->download($filePath, $fileName);
|
||||
} catch (\Exception $e) {
|
||||
// return ['success' => false, 'message' => $e->getMessage()];
|
||||
return Json([
|
||||
'code' => 901,
|
||||
'msg' => 'error',
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getAccountList($platformType, $customerIds, $page, $pageSize, $keyword, $startDate = null, $endDate = null, $countOnly = false)
|
||||
{
|
||||
// 检查 customerIds 是否为空,直接返回计数为 0
|
||||
@ -571,7 +1163,7 @@ class AdsInsightService
|
||||
|
||||
// 仅计数时优化查询
|
||||
if ($countOnly) {
|
||||
return $query->count(); // 只查询总记录数
|
||||
return $query->count('distinct(a.advertiser_id)'); // 只查询总记录数
|
||||
}
|
||||
// 动态构建日期条件
|
||||
$dateCondition = '';
|
||||
@ -1198,7 +1790,7 @@ class AdsInsightService
|
||||
}
|
||||
// 仅返回记录数时的逻辑
|
||||
if ($countOnly) {
|
||||
return $creativeDataQuery->count() ?: 0;
|
||||
return $creativeDataQuery->count('distinct(i.creative_id)') ?: 0;
|
||||
}
|
||||
|
||||
// 4. 关键词过滤
|
||||
|
@ -36,7 +36,7 @@ Route::group('/marketing', function () {
|
||||
Route::group('/v18', function () {
|
||||
Route::group('/ad', function () {
|
||||
Route::post('/list', [BpsAdController::class, 'listAds']);
|
||||
// Route::post('/export', [BpsAdController::class, 'exportAdsToExcel']);
|
||||
Route::post('/export', [BpsAdController::class, 'exportAdsToExcel']);
|
||||
// Route::group('/status', function () {
|
||||
// Route::post('/update', [BpsAdController::class, 'updateAdStatus']);
|
||||
//// Route::post('/get', [AdController::class, 'getAdStatus']);
|
||||
@ -59,7 +59,7 @@ Route::group('/marketing', function () {
|
||||
]);
|
||||
Route::group('/adset', function () {
|
||||
Route::post('/list', [BpsAdController::class, 'listAdsets']);
|
||||
// Route::post('/export', [BpsAdController::class, 'exportGroupsToExcel']);
|
||||
Route::post('/export', [BpsAdController::class, 'exportAdsetsToExcel']);
|
||||
// Route::group('/status', function () {
|
||||
// Route::post('/update', [BpsAdController::class, 'updateGroupStatus']);
|
||||
//// Route::post('/get', [BpsAdController::class, 'getGroupStatus']);
|
||||
@ -68,8 +68,9 @@ Route::group('/marketing', function () {
|
||||
app\middleware\JwtLocal::class,
|
||||
app\middleware\OauthThirdCheck::class,
|
||||
]);
|
||||
Route::group('/third_user', function () {
|
||||
Route::group('/account', function () {
|
||||
Route::post('/list', [BpsAdController::class, 'listThirdUsers']);
|
||||
Route::post('/export', [BpsAdController::class, 'exportAccountsToExcel']);
|
||||
})->middleware([
|
||||
app\middleware\JwtLocal::class,
|
||||
app\middleware\OauthThirdCheck::class,
|
||||
@ -82,6 +83,7 @@ Route::group('/marketing', function () {
|
||||
]);
|
||||
Route::group('/creative', function () {
|
||||
Route::post('/list', [BpsAdController::class, 'listCreatives']);
|
||||
Route::post('/export', [BpsAdController::class, 'exportCreativesToExcel']);
|
||||
Route::post('/chart', [BpsAdController::class, 'listCharts']);
|
||||
})->middleware([
|
||||
app\middleware\JwtLocal::class,
|
||||
|
Loading…
Reference in New Issue
Block a user