846 lines
		
	
	
		
			35 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			846 lines
		
	
	
		
			35 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | ||
| 
 | ||
| namespace app\controller;
 | ||
| 
 | ||
| use app\service\AdsInsightService;
 | ||
| use app\service\AdsDashboardService;
 | ||
| use app\service\LandingUrlInsightService;
 | ||
| use app\service\BpsAdAccountService;
 | ||
| use DI\Annotation\Inject;
 | ||
| use support\Request;
 | ||
| use support\Response;
 | ||
| 
 | ||
| 
 | ||
| class BpsAdController
 | ||
| {
 | ||
| 
 | ||
|     /**
 | ||
|      * @Inject
 | ||
|      * @var AdsInsightService
 | ||
|      */
 | ||
|     private $adsInsightService;
 | ||
| 
 | ||
|     /**
 | ||
|      * /**
 | ||
|      * @Inject
 | ||
|      * @var AdsDashboardService
 | ||
|      */
 | ||
|     private $adsDashboardService;
 | ||
| 
 | ||
|     /**
 | ||
|      * @Inject
 | ||
|      * @var LandingUrlInsightService
 | ||
|      */
 | ||
|     private $landingUrlInsightService;
 | ||
| 
 | ||
|     /**
 | ||
|      * @Inject
 | ||
|      * @var BpsAdAccountService
 | ||
|      */
 | ||
|     private $bpsAdAccountService;
 | ||
| 
 | ||
|     /**
 | ||
|      * 广告数据聚合接口
 | ||
|      */
 | ||
|     public function aggregateAd(Request $request)
 | ||
|     {
 | ||
|         $options = $request->all();
 | ||
| 
 | ||
|         //是否远程调用
 | ||
|         $request->rpc = true;
 | ||
| 
 | ||
| //        $startDate = $options['conditions']['startDate'] ?? null;
 | ||
| //        $endDate = $options['conditions']['endDate'] ?? null;
 | ||
|         $metrics = explode(',', $options['conditions']['metrics'] ?? '');
 | ||
| 
 | ||
|         $result = [];
 | ||
| //        $merchantId = $options['jwtClaims']['merchant_id'];
 | ||
|         $preloadedData = [];
 | ||
| 
 | ||
| 
 | ||
|         // 预处理指标类型
 | ||
|         $needsDashboard    = false;
 | ||
|         $analyticsEntities = [];
 | ||
|         foreach ($metrics as $metric) {
 | ||
|             $parts = explode('#', $metric);
 | ||
|             if (count($parts) !== 4) continue;
 | ||
| 
 | ||
|             [$sourceType, $platform, $field, $format] = $parts;
 | ||
|             $sourceType  = strtolower($sourceType);
 | ||
|             $platformKey = strtolower($platform);
 | ||
| 
 | ||
|             if ($sourceType === 'dashboard') {
 | ||
|                 if (in_array($platformKey, ['meta_ads', 'google_ads', 'tiktok_ads'])) {
 | ||
|                     $needsDashboard = true;
 | ||
|                 }
 | ||
|             } elseif ($sourceType === 'analytics') {
 | ||
|                 if (in_array($platformKey, ['accounts', 'campaigns', 'adsets', 'ads', 'creatives'])) {
 | ||
|                     $analyticsEntities[$platformKey] = true;
 | ||
|                 }
 | ||
|             }
 | ||
|         }
 | ||
| //调试用        return $this->successResponse(['data' => [$needsDashboard,$analyticsEntities]], $request);
 | ||
|         // 批量预加载数据
 | ||
|         if ($needsDashboard) {
 | ||
|             $dashboardData = $this->listCards($request);
 | ||
|             // 转换平台 key 添加 _ads 后缀
 | ||
|             $preloadedData['dashboard'] = [];
 | ||
|             foreach ($dashboardData as $platform => $data) {
 | ||
|                 $preloadedData['dashboard'][$platform . '_ads'] = $data; //键名适配
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         foreach (array_keys($analyticsEntities) as $entity) {
 | ||
|             $methodName = 'list' . ucfirst($entity);
 | ||
| 
 | ||
|             if (method_exists($this, $methodName)) {
 | ||
|                 $preloadedData[$entity] = $this->$methodName($request);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         // 处理每个指标
 | ||
|         foreach ($metrics as $metric) {
 | ||
|             $parts = explode('#', $metric);
 | ||
|             if (count($parts) !== 4) continue;
 | ||
| 
 | ||
|             [$sourceType, $platform, $field, $format] = $parts;
 | ||
|             $sourceType  = strtolower($sourceType);
 | ||
|             $platformKey = strtolower($platform);
 | ||
| 
 | ||
|             try {
 | ||
|                 if ($sourceType === 'dashboard') {
 | ||
|                     if (isset($preloadedData['dashboard'][$platformKey][$field])) {
 | ||
|                         $result[$platformKey][$field] = $preloadedData['dashboard'][$platformKey][$field];
 | ||
|                     }
 | ||
|                 } elseif ($sourceType === 'analytics' && isset($preloadedData[$platformKey])) {
 | ||
|                     if (isset($preloadedData[$platformKey][$field])) {
 | ||
|                         $result[$platformKey][$field] = $preloadedData[$platformKey][$field];
 | ||
|                     }
 | ||
|                 }
 | ||
|             } catch (\Exception $e) {
 | ||
|                 $result[$platformKey]['errors'][$field] = $e->getMessage();
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         return $this->successResponse(['data' => $result], $request);
 | ||
|     }
 | ||
| 
 | ||
|     /*
 | ||
|      * 返回标签页角标数字接口
 | ||
|      *
 | ||
|      */
 | ||
|     public function listAdBadges(Request $request)
 | ||
|     {
 | ||
|         $options              = $request->all();
 | ||
|         $options['jwtClaims'] = $request->jwtClaims;
 | ||
| 
 | ||
|         // 获取请求参数
 | ||
| //        $startDate         = $options['conditions']['startDate'] ?? null;  // 开始日期
 | ||
| //        $endDate           = $options['conditions']['endDate'] ?? null;  // 结束日期
 | ||
|         $store_id = $options['conditions']['store_id'] ?? null;  // 新引入店铺id
 | ||
|         if ($store_id === null) {
 | ||
|             return $this->successResponse(['data' => []], $request);
 | ||
|         }
 | ||
|         $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'store_id' => $store_id]);
 | ||
|         if (empty($accounts)) {
 | ||
|             return $this->successResponse(['data' => []], $request);
 | ||
|         }
 | ||
|         $accountIds        = array_unique(array_column($accounts, 'account_id'));
 | ||
|         $endDateLastWeek   = (int)date('Ymd');
 | ||
|         $startDateLastWeek = (int)date('Ymd', strtotime('-6 days'));
 | ||
| //        dump($startDateLastWeek, $endDateLastWeek);
 | ||
| //        if ($startDateLastWeek === $startDate && $endDateLastWeek === $endDate) {
 | ||
|         $ad_data_count = $this->adsInsightService::getAdCountData($options['jwtClaims']['merchant_id'], $store_id, $accountIds, $startDateLastWeek, $endDateLastWeek);
 | ||
| //        } else {
 | ||
| //            $ad_data_count = [];
 | ||
| //        }
 | ||
|         // 返回结果
 | ||
|         return $this->successResponse($ad_data_count, $request);
 | ||
|     }
 | ||
| 
 | ||
|     public function listAccounts(Request $request)
 | ||
|     {
 | ||
|         $options              = $request->all();
 | ||
|         $options['jwtClaims'] = $request->jwtClaims;
 | ||
|         $options['rpc']       = $request->rpc ?? false;
 | ||
| 
 | ||
| 
 | ||
|         // 获取请求参数
 | ||
|         $page         = $options['pageNo'] ?? 1;  // 页码
 | ||
|         $pageSize     = $options['pageSize'] ?? 1000;  // 每页数量
 | ||
|         $keyword      = $options['conditions']['keyword'] ?? '';  // 关键字搜索
 | ||
|         $platformType = $options['conditions']['platformType'] ?? 0;  // 平台类型
 | ||
|         $store_id     = $options['conditions']['store_id'] ?? null;
 | ||
| //        $status       = $options['conditions']['status'] ?? 0;  // 平台类型
 | ||
|         $startDate    = $options['conditions']['startDate'] ?? null;  // 开始日期
 | ||
|         $endDate      = $options['conditions']['endDate'] ?? null;  // 结束日期
 | ||
|         $requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
 | ||
| //    $dateRange    = 'Last Week';  // 默认日期范围
 | ||
|         if ($options['rpc']) {
 | ||
|             //$options['conditions']['startDate']由2025-02-14处理成纯整数
 | ||
|             $options['conditions']['startDate'] = (int)str_replace('-', '', $options['conditions']['startDate']);
 | ||
|             $options['conditions']['endDate']   = (int)str_replace('-', '', $options['conditions']['endDate']);
 | ||
|             $options['jwtClaims']               = $options['conditions']['user'];
 | ||
|             $store_id                           = $options['jwtClaims']['store_id'] ?? null;
 | ||
|         }
 | ||
| 
 | ||
|         // 根据 platformType 获取广告账户
 | ||
|         $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType, 'store_id' => $store_id]);
 | ||
| 
 | ||
| 
 | ||
|         if (empty($accounts) || $store_id === null) {
 | ||
|             if ($options['rpc']) {
 | ||
|                 return [];
 | ||
|             }
 | ||
|             return $this->successResponse(['data' => []], $request);
 | ||
|         }
 | ||
| //        dump($accounts);
 | ||
|         // 获取客户ID数组
 | ||
|         $accountIds = array_unique(array_column($accounts, 'account_id'));
 | ||
| 
 | ||
| 
 | ||
|         // 调用 Service 层查询广告列表
 | ||
|         $this->adsInsightService::enableSqlLog();
 | ||
|         $result = $this->adsInsightService::getAccountList(
 | ||
|             $platformType,    // 平台类型
 | ||
|             $options['jwtClaims']['merchant_id'],    // 店铺
 | ||
|             $store_id,
 | ||
|             $accountIds,      // 客户 ID 数组
 | ||
|             $page,            // 页码
 | ||
|             $pageSize,        // 每页数量
 | ||
|             $keyword,         // 关键字
 | ||
|             $startDate,       // 开始日期
 | ||
|             $endDate,          // 结束日期
 | ||
|             $requireSpend
 | ||
|         );
 | ||
| 
 | ||
|         if ($options['rpc']) {
 | ||
|             return $result['statistics'];
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
|         // 返回结果
 | ||
|         return $this->successResponse($result, $request);
 | ||
|     }
 | ||
| 
 | ||
|     public function listAds(Request $request)
 | ||
|     {
 | ||
|         $options              = $request->all();
 | ||
|         $options['jwtClaims'] = $request->jwtClaims;
 | ||
|         $options['rpc']       = $request->rpc ?? false;
 | ||
| 
 | ||
| 
 | ||
|         // 获取请求参数
 | ||
|         $page         = $options['pageNo'] ?? 1;  // 页码
 | ||
|         $pageSize     = $options['pageSize'] ?? 1000;  // 每页数量
 | ||
|         $keyword      = $options['conditions']['keyword'] ?? '';  // 关键字搜索
 | ||
|         $platformType = $options['conditions']['platformType'] ?? 0;  // 平台类型
 | ||
|         $store_id     = $options['conditions']['store_id'] ?? null;
 | ||
|         $status       = $options['conditions']['status'] ?? 0;  // 平台类型
 | ||
|         $startDate    = $options['conditions']['startDate'] ?? null;  // 开始日期
 | ||
|         $endDate      = $options['conditions']['endDate'] ?? null;  // 结束日期
 | ||
|         $requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
 | ||
| //    $dateRange    = 'Last Week';  // 默认日期范围
 | ||
| 
 | ||
|         if ($options['rpc']) {
 | ||
|             //$options['conditions']['startDate']由2025-02-14处理成纯整数
 | ||
|             $options['conditions']['startDate'] = (int)str_replace('-', '', $options['conditions']['startDate']);
 | ||
|             $options['conditions']['endDate']   = (int)str_replace('-', '', $options['conditions']['endDate']);
 | ||
|             $options['jwtClaims']               = $options['conditions']['user'];
 | ||
|             $store_id                           = $options['jwtClaims']['store_id'] ?? null;
 | ||
|         }
 | ||
| 
 | ||
|         // 根据 platformType 获取广告账户
 | ||
|         $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType, 'store_id' => $store_id]);
 | ||
| 
 | ||
|         if (empty($accounts) || $store_id === null) {
 | ||
|             if ($options['rpc']) {
 | ||
|                 return [];
 | ||
|             }
 | ||
|             return $this->successResponse(['data' => []], $request);
 | ||
|         }
 | ||
| 
 | ||
|         // 获取客户ID数组
 | ||
|         $accountIds = array_unique(array_column($accounts, 'account_id'));
 | ||
| 
 | ||
|         // 调用 Service 层查询广告列表
 | ||
|         $result = $this->adsInsightService::getAdList(
 | ||
|             $platformType,    // 平台类型
 | ||
|             $accountIds,      // 客户 ID 数组
 | ||
|             $page,            // 页码
 | ||
|             $pageSize,        // 每页数量
 | ||
|             $keyword,         // 关键字
 | ||
|             $startDate,       // 开始日期
 | ||
|             $endDate,          // 结束日期
 | ||
|             $status,
 | ||
|             $requireSpend
 | ||
|         );
 | ||
| 
 | ||
|         if ($options['rpc']) {
 | ||
|             return $result['statistics'];
 | ||
|         }
 | ||
| 
 | ||
|         // 返回结果
 | ||
|         return $this->successResponse($result, $request);
 | ||
|     }
 | ||
| 
 | ||
|     public function listCampaigns(Request $request)
 | ||
|     {
 | ||
|         $options              = $request->all();
 | ||
|         $options['jwtClaims'] = $request->jwtClaims;
 | ||
|         $options['rpc']       = $request->rpc ?? false;
 | ||
| 
 | ||
|         // 获取请求参数
 | ||
|         $page         = $options['pageNo'] ?? 1;  // 页码
 | ||
|         $pageSize     = $options['pageSize'] ?? 1000;  // 每页数量
 | ||
|         $keyword      = $options['conditions']['keyword'] ?? '';  // 关键字搜索
 | ||
|         $platformType = $options['conditions']['platformType'] ?? 0;  // 关键字搜索
 | ||
|         $store_id     = $options['conditions']['store_id'] ?? null;
 | ||
|         $status       = $options['conditions']['status'] ?? 0;  // 平台类型
 | ||
|         $startDate    = $options['conditions']['startDate'] ?? null;  // 开始日期
 | ||
|         $endDate      = $options['conditions']['endDate'] ?? null;  // 结束日期
 | ||
|         $requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
 | ||
| //        $dateRange    = 'Last Week';  // 默认日期范围
 | ||
| 
 | ||
|         if ($options['rpc']) {
 | ||
|             //$options['conditions']['startDate']由2025-02-14处理成纯整数
 | ||
|             $options['conditions']['startDate'] = (int)str_replace('-', '', $options['conditions']['startDate']);
 | ||
|             $options['conditions']['endDate']   = (int)str_replace('-', '', $options['conditions']['endDate']);
 | ||
|             $options['jwtClaims']               = $options['conditions']['user'];
 | ||
|             $store_id                           = $options['jwtClaims']['store_id'] ?? null;
 | ||
|         }
 | ||
|         // 根据 platformType 获取广告账户
 | ||
|         $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType, 'store_id' => $store_id]);
 | ||
| 
 | ||
|         if (empty($accounts) || $store_id === null) {
 | ||
|             if ($options['rpc']) {
 | ||
|                 return [];
 | ||
|             }
 | ||
|             return $this->successResponse(['data' => []], $request);
 | ||
|         }
 | ||
| 
 | ||
|         // 获取客户ID数组
 | ||
|         $accountIds = array_column($accounts, 'account_id');
 | ||
|         // 调用 Service 层查询
 | ||
|         $this->adsInsightService::enableSqlLog();
 | ||
|         $result = $this->adsInsightService::getCampaignList(
 | ||
|             $platformType,
 | ||
|             $accountIds,   // 客户 ID 数组
 | ||
|             $page,          // 页码
 | ||
|             $pageSize,      // 每页数量
 | ||
|             $keyword,       // 关键字
 | ||
|             $startDate,     // 开始日期
 | ||
|             $endDate,        // 结束日期
 | ||
|             $status,
 | ||
|             $requireSpend
 | ||
|         );
 | ||
| 
 | ||
|         if ($options['rpc']) {
 | ||
|             return $result['statistics'];
 | ||
|         }
 | ||
| 
 | ||
|         return $this->successResponse($result, $request);
 | ||
| //        return $this->errorResponse(300,'授权失败');
 | ||
|     }
 | ||
| 
 | ||
|     public function exportCampaignsToExcel(Request $request)
 | ||
|     {
 | ||
|         $options              = $request->all();
 | ||
|         $options['jwtClaims'] = $request->jwtClaims;
 | ||
| 
 | ||
|         // 获取请求参数
 | ||
|         $page         = $options['pageNo'] ?? 1;  // 页码
 | ||
|         $pageSize     = 10000;  // 每页数量
 | ||
|         $keyword      = $options['conditions']['keyword'] ?? '';  // 关键字搜索
 | ||
|         $platformType = $options['conditions']['platformType'] ?? 0;  // 关键字搜索
 | ||
|         $store_id     = $options['conditions']['store_id'] ?? null;
 | ||
|         $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 获取广告账户
 | ||
|         $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType, 'store_id' => $store_id]);
 | ||
| 
 | ||
|         if (empty($accounts) || $store_id === null) {
 | ||
|             $data = [
 | ||
|                 'code' => 901,
 | ||
|                 'msg' => 'No data available for export.',
 | ||
|                 'data' => []
 | ||
|             ];
 | ||
| //            return $this->successResponse(['data' => []], $request);
 | ||
|         }
 | ||
| 
 | ||
|         // 获取客户ID数组
 | ||
|         $accountIds = array_column($accounts, 'account_id');
 | ||
|         // 调用 Service 层查询
 | ||
|         return $this->adsInsightService::exportCampaignsToExcel(
 | ||
|             $platformType,
 | ||
|             $accountIds,   // 客户 ID 数组
 | ||
|             $page,          // 页码
 | ||
|             $pageSize,      // 每页数量
 | ||
|             $keyword,       // 关键字
 | ||
|             $startDate,     // 开始日期
 | ||
|             $endDate,        // 结束日期
 | ||
|             $status,
 | ||
|             $requireSpend
 | ||
|         );
 | ||
|     }
 | ||
| 
 | ||
|     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;  // 关键字搜索
 | ||
|         $store_id     = $options['conditions']['store_id'] ?? null;
 | ||
| //        $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 获取广告账户
 | ||
|         $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType, 'store_id' => $store_id]);
 | ||
| 
 | ||
|         if (empty($accounts) || $store_id === null) {
 | ||
|             $data = [
 | ||
|                 'code' => 901,
 | ||
|                 'msg' => 'No data available for export.',
 | ||
|                 'data' => []
 | ||
|             ];
 | ||
|             return new Response(400, ['Content-Type' => 'application/json'], json_encode($data, JSON_UNESCAPED_UNICODE));
 | ||
| //            return $this->successResponse(['data' => []], $request);
 | ||
|         }
 | ||
| 
 | ||
|         // 获取客户ID数组
 | ||
|         $accountIds = array_column($accounts, 'account_id');
 | ||
|         // 调用 Service 层查询
 | ||
|         return $this->adsInsightService::exportAccountsToExcel(
 | ||
|             $platformType,
 | ||
|             $options['jwtClaims']['merchant_id'],    // 店铺
 | ||
|             $store_id,
 | ||
|             $accountIds,   // 客户 ID 数组
 | ||
|             $page,          // 页码
 | ||
|             $pageSize,      // 每页数量
 | ||
|             $keyword,       // 关键字
 | ||
|             $startDate,     // 开始日期
 | ||
|             $endDate,        // 结束日期
 | ||
|             $requireSpend
 | ||
|         );
 | ||
|     }
 | ||
| 
 | ||
|     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;  // 关键字搜索
 | ||
|         $store_id     = $options['conditions']['store_id'] ?? null;
 | ||
| //        $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 获取广告账户
 | ||
|         $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType, 'store_id' => $store_id]);
 | ||
| 
 | ||
|         if (empty($accounts) || $store_id === null) {
 | ||
|             $data = [
 | ||
|                 'code' => 901,
 | ||
|                 'msg' => 'No data available for export.',
 | ||
|                 'data' => []
 | ||
|             ];
 | ||
|             return new Response(400, ['Content-Type' => 'application/json'], json_encode($data, JSON_UNESCAPED_UNICODE));
 | ||
| //            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,        // 结束日期
 | ||
|             $requireSpend
 | ||
|         );
 | ||
|     }
 | ||
| 
 | ||
|     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;  // 关键字搜索
 | ||
|         $store_id     = $options['conditions']['store_id'] ?? null;
 | ||
|         $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 获取广告账户
 | ||
|         $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType, 'store_id' => $store_id]);
 | ||
| 
 | ||
|         if (empty($accounts) || $store_id === null) {
 | ||
|             $data = [
 | ||
|                 'code' => 901,
 | ||
|                 'msg' => 'No data available for export.',
 | ||
|                 'data' => []
 | ||
|             ];
 | ||
|             return new Response(400, ['Content-Type' => 'application/json'], json_encode($data, JSON_UNESCAPED_UNICODE));
 | ||
| //            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,
 | ||
|             $requireSpend
 | ||
|         );
 | ||
|     }
 | ||
| 
 | ||
|     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;  // 关键字搜索
 | ||
|         $store_id     = $options['conditions']['store_id'] ?? null;
 | ||
|         $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 获取广告账户
 | ||
|         $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType, 'store_id' => $store_id]);
 | ||
| 
 | ||
|         if (empty($accounts) || $store_id === null) {
 | ||
|             $data = [
 | ||
|                 'code' => 901,
 | ||
|                 'msg' => 'No data available for export.',
 | ||
|                 'data' => []
 | ||
|             ];
 | ||
|             return new Response(400, ['Content-Type' => 'application/json'], json_encode($data, JSON_UNESCAPED_UNICODE));
 | ||
| //            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,
 | ||
|             $requireSpend
 | ||
|         );
 | ||
|     }
 | ||
| 
 | ||
|     public function listAdsets(Request $request)
 | ||
|     {
 | ||
|         $options              = $request->all();
 | ||
|         $options['jwtClaims'] = $request->jwtClaims;
 | ||
|         $options['rpc']       = $request->rpc ?? false;
 | ||
| 
 | ||
| 
 | ||
|         // 获取请求参数
 | ||
|         $page         = $options['pageNo'] ?? 1;  // 页码
 | ||
|         $pageSize     = $options['pageSize'] ?? 1000;  // 每页数量
 | ||
|         $keyword      = $options['conditions']['keyword'] ?? '';  // 关键字搜索
 | ||
|         $platformType = $options['conditions']['platformType'] ?? 0;  // 平台类型
 | ||
|         $store_id     = $options['conditions']['store_id'] ?? null;
 | ||
|         $status       = $options['conditions']['status'] ?? 0;  // 平台类型
 | ||
|         $startDate    = $options['conditions']['startDate'] ?? null;  // 开始日期
 | ||
|         $endDate      = $options['conditions']['endDate'] ?? null;  // 结束日期
 | ||
|         $requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
 | ||
| //        $dateRange    = 'Last Week';  // 默认日期范围
 | ||
| 
 | ||
|         if ($options['rpc']) {
 | ||
|             //$options['conditions']['startDate']由2025-02-14处理成纯整数
 | ||
|             $options['conditions']['startDate'] = (int)str_replace('-', '', $options['conditions']['startDate']);
 | ||
|             $options['conditions']['endDate']   = (int)str_replace('-', '', $options['conditions']['endDate']);
 | ||
|             $options['jwtClaims']               = $options['conditions']['user'];
 | ||
|             $store_id                           = $options['jwtClaims']['store_id'] ?? null;
 | ||
|         }
 | ||
| 
 | ||
|         // 根据 platformType 获取广告账户
 | ||
|         $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType, 'store_id' => $store_id]);
 | ||
| 
 | ||
|         if (empty($accounts) || $store_id === null) {
 | ||
|             if ($options['rpc']) {
 | ||
|                 return [];
 | ||
|             }
 | ||
|             return $this->successResponse(['data' => []], $request);
 | ||
|         }
 | ||
| 
 | ||
|         // 获取客户ID数组
 | ||
|         $accountIds = array_unique(array_column($accounts, 'account_id'));
 | ||
| //        dump($accountIds);
 | ||
|         // 调用 Service 层查询广告组列表
 | ||
|         $result = $this->adsInsightService::getAdsetList(
 | ||
|             $platformType,
 | ||
|             $accountIds,   // 客户 ID 数组
 | ||
|             $page,          // 页码
 | ||
|             $pageSize,      // 每页数量
 | ||
|             $keyword,       // 关键字
 | ||
|             $startDate,     // 开始日期
 | ||
|             $endDate,        // 结束日期
 | ||
|             $status,
 | ||
|             $requireSpend
 | ||
|         );
 | ||
| 
 | ||
|         if ($options['rpc']) {
 | ||
|             return $result['statistics'];
 | ||
|         }
 | ||
| 
 | ||
|         // 返回结果
 | ||
|         return $this->successResponse($result, $request);
 | ||
|     }
 | ||
| 
 | ||
|     public function listCreatives(Request $request)
 | ||
|     {
 | ||
|         $options              = $request->all();
 | ||
|         $options['jwtClaims'] = $request->jwtClaims;
 | ||
|         $options['rpc']       = $request->rpc ?? false;
 | ||
| 
 | ||
| 
 | ||
|         // 获取请求参数
 | ||
|         $page         = $options['pageNo'] ?? 1;  // 页码
 | ||
|         $pageSize     = $options['pageSize'] ?? 1000;  // 每页数量
 | ||
|         $keyword      = $options['conditions']['keyword'] ?? '';  // 关键字搜索
 | ||
|         $platformType = $options['conditions']['platformType'] ?? 0;  // 平台类型
 | ||
|         $store_id     = $options['conditions']['store_id'] ?? null;
 | ||
|         $startDate    = $options['conditions']['startDate'] ?? null;  // 开始日期
 | ||
|         $endDate      = $options['conditions']['endDate'] ?? null;  // 结束日期
 | ||
|         $requireSpend = (bool)($options['conditions']['requireSpend'] ?? false);
 | ||
| 
 | ||
| //        $dateRange    = 'Last Week';  // 默认日期范围
 | ||
|         if ($options['rpc']) {
 | ||
|             //$options['conditions']['startDate']由2025-02-14处理成纯整数
 | ||
|             $options['conditions']['startDate'] = (int)str_replace('-', '', $options['conditions']['startDate']);
 | ||
|             $options['conditions']['endDate']   = (int)str_replace('-', '', $options['conditions']['endDate']);
 | ||
|             $options['jwtClaims']               = $options['conditions']['user'];
 | ||
|             $store_id                           = $options['jwtClaims']['store_id'] ?? null;
 | ||
|         }
 | ||
| 
 | ||
|         // 根据 platformType 获取广告账户
 | ||
|         $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType, 'store_id' => $store_id]);
 | ||
| 
 | ||
|         if (empty($accounts) || $store_id === null) {
 | ||
|             if ($options['rpc']) {
 | ||
|                 return [];
 | ||
|             }
 | ||
|             return $this->successResponse(['data' => []], $request);
 | ||
|         }
 | ||
| 
 | ||
|         // 获取客户ID数组
 | ||
|         $accountIds = array_unique(array_column($accounts, 'account_id'));
 | ||
| //        dump($accountIds);
 | ||
|         // 调用 Service 层查询广告组列表
 | ||
|         $result = $this->adsInsightService::getCreativeInsightData(
 | ||
|             $platformType,
 | ||
|             $accountIds,   // 客户 ID 数组
 | ||
|             $page,          // 页码
 | ||
|             $pageSize,      // 每页数量
 | ||
|             $keyword,       // 关键字
 | ||
|             $startDate,     // 开始日期
 | ||
|             $endDate,        // 结束日期
 | ||
|             $requireSpend
 | ||
|         );
 | ||
|         if ($options['rpc']) {
 | ||
|             return $result['statistics'];
 | ||
|         }
 | ||
| 
 | ||
|         // 返回结果
 | ||
|         return $this->successResponse($result, $request);
 | ||
|     }
 | ||
| 
 | ||
|     public function listLandingUrls(Request $request)
 | ||
|     {
 | ||
|         $options              = $request->all();
 | ||
|         $options['jwtClaims'] = $request->jwtClaims;
 | ||
| 
 | ||
|         // 获取请求参数
 | ||
|         $page         = $options['pageNo'] ?? 1;  // 页码
 | ||
|         $pageSize     = $options['pageSize'] ?? 1000;  // 每页数量
 | ||
|         $keyword      = $options['conditions']['keyword'] ?? '';  // 关键字搜索
 | ||
|         $platformType = $options['conditions']['platformType'] ?? 0;  // 平台类型
 | ||
|         $store_id     = $options['conditions']['store_id'] ?? null;
 | ||
|         $startDate    = $options['conditions']['startDate'] ?? null;  // 开始日期
 | ||
|         $endDate      = $options['conditions']['endDate'] ?? null;  // 结束日期
 | ||
| //        $dateRange    = 'Last Week';  // 默认日期范围
 | ||
| 
 | ||
|         // 根据 platformType 获取广告账户
 | ||
|         $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType, 'store_id' => $store_id]);
 | ||
| 
 | ||
|         if (empty($accounts) || $store_id === null) {
 | ||
|             return $this->successResponse(['data' => []], $request);
 | ||
|         }
 | ||
| 
 | ||
|         // 获取客户ID数组
 | ||
|         $accountIds = array_unique(array_column($accounts, 'account_id'));
 | ||
| //        dump($accountIds);
 | ||
|         // 调用 Service 层查询广告组列表
 | ||
|         $result = $this->landingUrlInsightService::getLandingUrlInsightData(
 | ||
|             $platformType,
 | ||
|             $accountIds,   // 客户 ID 数组
 | ||
|             $page,          // 页码
 | ||
|             $pageSize,      // 每页数量
 | ||
|             $keyword,       // 关键字
 | ||
|             $startDate,     // 开始日期
 | ||
|             $endDate        // 结束日期
 | ||
|         );
 | ||
| 
 | ||
|         // 返回结果
 | ||
|         return $this->successResponse($result, $request);
 | ||
|     }
 | ||
| 
 | ||
|     public function listCharts(Request $request)
 | ||
|     {
 | ||
|         $options              = $request->all();
 | ||
|         $options['jwtClaims'] = $request->jwtClaims;
 | ||
| 
 | ||
|         // 获取请求参数
 | ||
|         $cycle        = $options['conditions']['cycle'] ?? 3;  // 页码
 | ||
|         $platformType = $options['conditions']['platformType'] ?? 0;  // 平台类型
 | ||
|         $store_id     = $options['conditions']['store_id'] ?? null;
 | ||
|         $startDate    = $options['conditions']['startDate'] ?? null;  // 开始日期
 | ||
|         $endDate      = $options['conditions']['endDate'] ?? null;  // 结束日期
 | ||
| //        $dateRange    = 'Last Week';  // 默认日期范围
 | ||
| 
 | ||
|         // 根据 platformType 获取广告账户
 | ||
|         $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'platform' => $platformType, 'store_id' => $store_id]);
 | ||
| 
 | ||
|         if (empty($accounts) || $store_id === null) {
 | ||
|             return $this->successResponse(['data' => []], $request);
 | ||
|         }
 | ||
| 
 | ||
|         // 获取客户ID数组
 | ||
|         $accountIds = array_unique(array_column($accounts, 'account_id'));
 | ||
| //        dump($accountIds);
 | ||
|         // 调用 Service 层查询广告组列表
 | ||
|         $result = $this->adsInsightService->getAdCycleInsight(
 | ||
|             $platformType,
 | ||
|             $accountIds,   // 客户 ID 数组
 | ||
|             $cycle,          // 页码
 | ||
|             $startDate,     // 开始日期
 | ||
|             $endDate        // 结束日期
 | ||
|         );
 | ||
| 
 | ||
|         // 返回结果
 | ||
|         return $this->successResponse($result, $request);
 | ||
|     }
 | ||
| 
 | ||
|     public function listCards(Request $request)
 | ||
|     {
 | ||
|         $options              = $request->all();
 | ||
|         $options['jwtClaims'] = $request->jwtClaims;
 | ||
|         $options['rpc']       = $request->rpc ?? false;
 | ||
| 
 | ||
| 
 | ||
|         // 获取请求参数
 | ||
|         $cycle    = $options['conditions']['dateType'] ?? 'today'; //默认today || today yesterday、month、year 、custom
 | ||
|         $store_id = $options['conditions']['store_id'] ?? null;
 | ||
| //        $platformType = $options['conditions']['platformType'] ?? 0;  // 平台类型
 | ||
|         $startDate = $options['conditions']['startDate'] ?? null;  // 开始日期
 | ||
|         $endDate   = $options['conditions']['endDate'] ?? null;  // 结束日期
 | ||
| //        $dateRange    = 'Last Week';  // 默认日期范围
 | ||
|         if ($options['rpc']) {
 | ||
|             $options['jwtClaims'] = $options['conditions']['user'];
 | ||
|             $store_id             = $options['jwtClaims']['store_id'] ?? null;
 | ||
|         }
 | ||
|         $accounts = $this->bpsAdAccountService->getAllAdAccounts(['merchant_id' => $options['jwtClaims']['merchant_id'], 'store_id' => $store_id]);
 | ||
| 
 | ||
| 
 | ||
|         if (empty($accounts) || $store_id === null) {
 | ||
|             return $this->successResponse(['data' => new \stdClass()], $request);
 | ||
|         }
 | ||
| 
 | ||
|         // 获取客户ID数组
 | ||
|         $accountIds = array_unique(array_column($accounts, 'account_id'));
 | ||
| //        dump($accountIds);
 | ||
|         // 调用 Service 层查询广告组列表
 | ||
|         $result = $this->adsDashboardService->getAdCycleCard(
 | ||
|             $accountIds,   // 客户 ID 数组
 | ||
|             $cycle,          // 页码
 | ||
|             $startDate,     // 开始日期
 | ||
|             $endDate        // 结束日期
 | ||
|         );
 | ||
|         if ($options['rpc']) {
 | ||
|             return $result['data'];
 | ||
|         }
 | ||
| 
 | ||
|         // 返回结果
 | ||
|         return $this->successResponse($result, $request);
 | ||
|     }
 | ||
| 
 | ||
| 
 | ||
|     // 可以加入一些公共方法
 | ||
|     protected function successResponse($data, Request $request): Response
 | ||
|     {
 | ||
|         $responseData = [
 | ||
|             'code' => 0,
 | ||
|             'msg' => 'ok',
 | ||
|             'data' => $data,
 | ||
|         ];
 | ||
| 
 | ||
| 
 | ||
|         if (is_array($data) && empty($data)) {
 | ||
|             $responseData['data'] = new \stdClass();
 | ||
|         }
 | ||
| 
 | ||
|         // 如果有新 token,添加到 header
 | ||
|         if ($request->jwtNewToken) {
 | ||
|             return new Response(200, [
 | ||
|                 'Content-Type' => 'application/json',
 | ||
|                 'X-New-Token' => $request->jwtNewToken,
 | ||
|             ], json_encode($responseData, JSON_UNESCAPED_UNICODE));
 | ||
|         }
 | ||
| 
 | ||
|         return Json($responseData);
 | ||
|     }
 | ||
| 
 | ||
| 
 | ||
|     protected
 | ||
|     function errorResponse($code, $message, $data = []): Response
 | ||
|     {
 | ||
|         return Json([
 | ||
|             'code' => $code,
 | ||
|             'msg' => $message ?: 'error',
 | ||
|             'data' => $data
 | ||
|         ]);
 | ||
|     }
 | ||
| 
 | ||
| }
 | 
