diff --git a/app/service/AdsInsightService.php b/app/service/AdsInsightService.php index 80ccade..39c2096 100644 --- a/app/service/AdsInsightService.php +++ b/app/service/AdsInsightService.php @@ -7,6 +7,7 @@ use app\model\BpsAdCreativeInsight; use app\model\BpsAdInsight; //use app\model\BpsAdsMerchantRelation; +use app\model\BpsAdsMerchantRelation; use app\model\BpsAdsMerchantStoreRelation; use app\model\DayData; use app\model\Campaign; @@ -271,8 +272,12 @@ class AdsInsightService // $ads = $query->page($page, $pageSize)->select(); $campaigns = $query->limit(($page - 1) * $pageSize, $pageSize)->select(); + + $accountIds = array_unique(array_column($campaigns, 'account_id')); + $accountNames = BpsAdsMerchantRelation::whereIn('account_id', $accountIds) + ->column('account_name', 'account_id'); // 确保数据格式统一 - $result = array_map(function ($item) { + $result = array_map(function ($item) use ($accountNames){ // CTR 的计算:点击率 = 点击数 / 展示数 $ctr = $item['impressions'] > 0 ? number_format(($item['clicks'] / $item['impressions']) * 100, 2) . '%' : '-'; // Conversion Rate 的计算:转换率 = 购买数 / 点击数 @@ -287,6 +292,7 @@ class AdsInsightService 'id' => $item['campaign_id'], 'platform_type' => $item['platform_type'], 'account_id' => $item['account_id'], // 映射为 customer_id + 'account_name' => $accountNames[$item['account_id']] ?? '-', // 新增字段 'name' => $item['name'] ?: '-', // 若 name 为空则显示 '-' 'status' => $item['status'], 'platform_purchase' => number_format($item['platform_purchase']), @@ -393,7 +399,8 @@ class AdsInsightService $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->setCellValueExplicit('D' . $row, (string)$campaign['account_id'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING); + $sheet->setCellValue('D' . $row, $campaign['account_name']); $sheet->setCellValue('E' . $row, $campaign['platform_purchase']); $sheet->setCellValue('F' . $row, $campaign['pixel_purchase']); $sheet->setCellValue('G' . $row, $campaign['roas']); // ROAS @@ -626,7 +633,7 @@ class AdsInsightService 'A1' => 'Ads', 'B1' => 'Status', 'C1' => 'Ad Platform', // 修正为 Ad Platform - 'D1' => 'Ad Accounts', // 修正为 Ad Accounts + 'D1' => 'Ad Sets', 'E1' => 'Platform Purchases', 'F1' => 'Best Pixel Purchases', 'G1' => 'ROAS', @@ -657,7 +664,8 @@ class AdsInsightService $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->setCellValueExplicit('D' . $row, (string)$ad['account_id'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING); + $sheet->setCellValue('D' . $row, $ad['ad_set_name']); $sheet->setCellValue('E' . $row, $ad['platform_purchase']); $sheet->setCellValue('F' . $row, $ad['pixel_purchase']); $sheet->setCellValue('G' . $row, $ad['roas']); // ROAS