348 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			348 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace app\controller;
 | |
| 
 | |
| use app\service\GoogleAdsCampaignService;
 | |
| use app\service\GoogleAdsGroupService;
 | |
| use app\service\GoogleAdsAdService;
 | |
| use app\service\GoogleAdsReportService;
 | |
| use support\Request;
 | |
| use support\Response;
 | |
| use DI\Annotation\Inject;
 | |
| 
 | |
| 
 | |
| class AdController
 | |
| {
 | |
| 
 | |
|     /**
 | |
|      * @Inject
 | |
|      * @var GoogleAdsReportService
 | |
|      */
 | |
|     private $googleAdsReportService;
 | |
| 
 | |
|     /**
 | |
|      * @Inject
 | |
|      * @var GoogleAdsGroupService
 | |
|      */
 | |
|     private $googleAdsGroupService;
 | |
| 
 | |
|     /**
 | |
|      * @Inject
 | |
|      * @var GoogleAdsAdService
 | |
|      */
 | |
|     private $googleAdsAdService;
 | |
| 
 | |
|     /**
 | |
|      * @Inject
 | |
|      * @var GoogleAdsCampaignService
 | |
|      */
 | |
|     private $googleAdsCampaignService;
 | |
| 
 | |
|     public function listAds(Request $request)
 | |
|     {
 | |
|         // 获取请求参数
 | |
|         $page      = $request->input('page', 1); // 页码
 | |
|         $pageSize  = $request->input('page_size', 20); // 每页数量
 | |
|         $keyword   = $request->input('keyword', ''); // 关键字搜索
 | |
|         $dateRange = $request->input('date_range', 'Last Week'); // 日期范围
 | |
| 
 | |
|         // 获取自定义日期范围
 | |
|         $startDate = $request->input('start_date', null); // 开始日期,默认为 null
 | |
|         $endDate   = $request->input('end_date', null); // 结束日期,默认为 null
 | |
| 
 | |
|         // 你可以进一步验证日期格式(可选)
 | |
| //        if ($startDate && !strtotime($startDate)) {
 | |
| //            return response()->json(['error' => 'Invalid start date format'], 400);
 | |
| //        }
 | |
| //        if ($endDate && !strtotime($endDate)) {
 | |
| //            return response()->json(['error' => 'Invalid end date format'], 400);
 | |
| //        }
 | |
| 
 | |
|         // 调用 Service 层查询
 | |
|         $result = $this->googleAdsReportService::getAdList($page, $pageSize, $keyword, $dateRange, $startDate, $endDate);
 | |
|         return $this->successResponse($result);
 | |
|     }
 | |
| 
 | |
|     public function listCampaigns(Request $request)
 | |
|     {
 | |
|         // 获取请求参数
 | |
|         $page      = $request->input('page', 1); // 页码
 | |
|         $pageSize  = $request->input('page_size', 20); // 每页数量
 | |
|         $keyword   = $request->input('keyword', ''); // 关键字搜索
 | |
|         $dateRange = $request->input('date_range', 'Last Week'); // 日期范围
 | |
| 
 | |
|         // 获取自定义日期范围
 | |
|         $startDate = $request->input('start_date', null); // 开始日期,默认为 null
 | |
|         $endDate   = $request->input('end_date', null); // 结束日期,默认为 null
 | |
| 
 | |
|         // 你可以进一步验证日期格式(可选)
 | |
| //        if ($startDate && !strtotime($startDate)) {
 | |
| //            return response()->json(['error' => 'Invalid start date format'], 400);
 | |
| //        }
 | |
| //        if ($endDate && !strtotime($endDate)) {
 | |
| //            return response()->json(['error' => 'Invalid end date format'], 400);
 | |
| //        }
 | |
| 
 | |
|         // 调用 Service 层查询
 | |
|         $result = $this->googleAdsReportService::getCampaignList($page, $pageSize, $keyword, $dateRange, $startDate, $endDate);
 | |
|         return $this->successResponse($result);
 | |
|     }
 | |
| 
 | |
|     public function exportAdsToExcel(Request $request)
 | |
|     {
 | |
|         $keyword   = $request->input('keyword', ''); // 获取关键字参数
 | |
|         $dateRange = $request->input('date_range', 'Last Week'); // 获取日期范围参数
 | |
| 
 | |
|         // 获取自定义日期范围
 | |
|         $startDate = $request->input('start_date', null); // 开始日期,默认为 null
 | |
|         $endDate   = $request->input('end_date', null); // 结束日期,默认为 null
 | |
| 
 | |
|         // 你可以进一步验证日期格式(可选)
 | |
| //        if ($startDate && !strtotime($startDate)) {
 | |
| //            return response()->json(['error' => 'Invalid start date format'], 400);
 | |
| //        }
 | |
| //        if ($endDate && !strtotime($endDate)) {
 | |
| //            return response()->json(['error' => 'Invalid end date format'], 400);
 | |
| //        }
 | |
| 
 | |
|         // 调用 service 层导出数据
 | |
|         return $this->googleAdsReportService::exportAdListToExcel($keyword, $dateRange, $startDate, $endDate);
 | |
|     }
 | |
| 
 | |
|     public function exportCampaignsToExcel(Request $request)
 | |
|     {
 | |
|         $keyword   = $request->input('keyword', ''); // 获取关键字参数
 | |
|         $dateRange = $request->input('date_range', 'Last Week'); // 获取日期范围参数
 | |
| 
 | |
|         // 获取自定义日期范围
 | |
|         $startDate = $request->input('start_date', null); // 开始日期,默认为 null
 | |
|         $endDate   = $request->input('end_date', null); // 结束日期,默认为 null
 | |
| 
 | |
|         // 你可以进一步验证日期格式(可选)
 | |
| //        if ($startDate && !strtotime($startDate)) {
 | |
| //            return response()->json(['error' => 'Invalid start date format'], 400);
 | |
| //        }
 | |
| //        if ($endDate && !strtotime($endDate)) {
 | |
| //            return response()->json(['error' => 'Invalid end date format'], 400);
 | |
| //        }
 | |
| 
 | |
|         // 调用 service 层导出数据
 | |
|         return $this->googleAdsReportService::exportCampaignsToExcel($keyword, $dateRange, $startDate, $endDate);
 | |
|     }
 | |
| 
 | |
|     public function exportGroupsToExcel(Request $request)
 | |
|     {
 | |
|         $keyword   = $request->input('keyword', ''); // 获取关键字参数
 | |
|         $dateRange = $request->input('date_range', 'Last Week'); // 获取日期范围参数
 | |
| 
 | |
|         // 获取自定义日期范围
 | |
|         $startDate = $request->input('start_date', null); // 开始日期,默认为 null
 | |
|         $endDate   = $request->input('end_date', null); // 结束日期,默认为 null
 | |
| 
 | |
|         // 你可以进一步验证日期格式(可选)
 | |
| //        if ($startDate && !strtotime($startDate)) {
 | |
| //            return response()->json(['error' => 'Invalid start date format'], 400);
 | |
| //        }
 | |
| //        if ($endDate && !strtotime($endDate)) {
 | |
| //            return response()->json(['error' => 'Invalid end date format'], 400);
 | |
| //        }
 | |
| 
 | |
|         // 调用 service 层导出数据
 | |
|         return $this->googleAdsReportService::exportAdGroupsToExcel($keyword, $dateRange, $startDate, $endDate);
 | |
|     }
 | |
| 
 | |
|     public function listGroups(Request $request)
 | |
|     {
 | |
|         // 获取请求参数
 | |
|         $page      = $request->input('page', 1); // 页码
 | |
|         $pageSize  = $request->input('page_size', 20); // 每页数量
 | |
|         $keyword   = $request->input('keyword', ''); // 关键字搜索
 | |
|         $dateRange = $request->input('date_range', 'Last Week'); // 日期范围
 | |
| 
 | |
|         // 获取自定义日期范围
 | |
|         $startDate = $request->input('start_date', null); // 开始日期,默认为 null
 | |
|         $endDate   = $request->input('end_date', null); // 结束日期,默认为 null
 | |
| 
 | |
|         // 你可以进一步验证日期格式(可选)
 | |
| //        if ($startDate && !strtotime($startDate)) {
 | |
| //            return response()->json(['error' => 'Invalid start date format'], 400);
 | |
| //        }
 | |
| //        if ($endDate && !strtotime($endDate)) {
 | |
| //            return response()->json(['error' => 'Invalid end date format'], 400);
 | |
| //        }
 | |
| 
 | |
|         // 调用 Service 层查询
 | |
|         $result = $this->googleAdsReportService::getAdGroupList($page, $pageSize, $keyword, $dateRange, $startDate, $endDate);
 | |
|         return $this->successResponse($result);
 | |
|     }
 | |
| 
 | |
| 
 | |
|     /**
 | |
|      * 获取广告系列的状态 备用
 | |
|      */
 | |
| //    public function getCampaignStatus(int $campaignId): Response
 | |
| //    {
 | |
| //        try {
 | |
| //            $status = $this->googleAdsCampaignService->getCampaignStatus($campaignId);
 | |
| //            return $this->successResponse(['status' => $status]);
 | |
| //        } catch (ValidateException $e) {
 | |
| //            return $this->errorResponse(400, $e->getMessage());
 | |
| //        }
 | |
| //    }
 | |
| 
 | |
|     /**
 | |
|      * 更新广告系列的状态
 | |
|      */
 | |
|     public function updateCampaignStatus(Request $request): Response
 | |
|     {
 | |
|         $campaignStatus = [
 | |
|             0,  // UNSPECIFIED
 | |
|             1,  // UNKNOWN
 | |
|             2,  // ENABLED
 | |
|             3,  // PAUSED
 | |
|             4,  // REMOVED
 | |
|         ];
 | |
|         $requestData    = $request->all(); // 获取请求数据
 | |
|         $customerId     = $requestData['customer_id'];
 | |
|         $campaignId     = $requestData['campaign_id'];
 | |
|         $status         = $requestData['status'];
 | |
|         if (!in_array($status, $campaignStatus)) {
 | |
|             return $this->errorResponse(101, 'status参数错误');
 | |
|         }
 | |
| //        try {
 | |
|         $reslut = $this->googleAdsCampaignService->updateCampaignStatus($customerId, $campaignId, $status);
 | |
|         if (!$reslut) {
 | |
|             return $this->errorResponse(101, 'Status update failed');
 | |
|         }
 | |
|         return $this->successResponse(['message' => 'Status updated successfully']);
 | |
| //        } catch (ValidateException $e) {
 | |
| //             return $this->errorResponse(400, $e->getMessage());
 | |
| //        }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 判断广告系列是否启用
 | |
|      */
 | |
| //    public function isEnabled(int $campaignId): Json
 | |
| //    {
 | |
| //        try {
 | |
| //            $isEnabled = $this->campaignService->isCampaignEnabled($campaignId);
 | |
| //            return json(['enabled' => $isEnabled], 200);
 | |
| //        } catch (ValidateException $e) {
 | |
| //            return json(['error' => $e->getMessage()], 400);
 | |
| //        }
 | |
| //    }
 | |
| 
 | |
|     /**
 | |
|      * 判断广告系列是否暂停
 | |
|      */
 | |
| //    public function isPaused(int $campaignId): Json
 | |
| //    {
 | |
| //        try {
 | |
| //            $isPaused = $this->campaignService->isCampaignPaused($campaignId);
 | |
| //            return json(['paused' => $isPaused], 200);
 | |
| //        } catch (ValidateException $e) {
 | |
| //            return json(['error' => $e->getMessage()], 400);
 | |
| //        }
 | |
| //    }
 | |
| 
 | |
|     /**
 | |
|      * 判断广告系列是否停止
 | |
|      */
 | |
| //    public function isStopped(int $campaignId): Json
 | |
| //    {
 | |
| //        try {
 | |
| //            $isStopped = $this->campaignService->isCampaignStopped($campaignId);
 | |
| //            return json(['stopped' => $isStopped], 200);
 | |
| //        } catch (ValidateException $e) {
 | |
| //            return json(['error' => $e->getMessage()], 400);
 | |
| //        }
 | |
| //    }
 | |
| //
 | |
| 
 | |
|     /**
 | |
|      * 更新广告组的状态
 | |
|      */
 | |
|     public function updateGroupStatus(Request $request): Response
 | |
|     {
 | |
|         $adGroupStatus = [
 | |
|             0,  // UNSPECIFIED
 | |
|             1,  // UNKNOWN
 | |
|             2,  // ENABLED
 | |
|             3,  // PAUSED
 | |
|             4   // REMOVED
 | |
|         ];
 | |
| 
 | |
|         $requestData = $request->all(); // 获取请求数据
 | |
|         $customerId  = $requestData['customer_id'];
 | |
|         $groupId     = $requestData['group_id'];
 | |
|         $status      = $requestData['status'];
 | |
|         //        $options['bid_micro_amount'] = $options['amount'] * 1000000 < 0 ? 0 : $options['amount'] * 1000000;
 | |
|         if (!in_array($status, $adGroupStatus)) {
 | |
|             return $this->errorResponse(101, 'status参数错误');
 | |
|         }
 | |
| //        try {
 | |
|         $result = $this->googleAdsGroupService->updateGroupStatus($customerId, $groupId, $status);
 | |
|         if (!$result) {
 | |
|             return $this->errorResponse(101, 'Status update failed');
 | |
|         }
 | |
|         return $this->successResponse(['message' => 'Status updated successfully']);
 | |
| //        } catch (ValidateException $e) {
 | |
| //             return $this->errorResponse(400, $e->getMessage());
 | |
| //        }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 更新广告的状态
 | |
|      */
 | |
|     public function updateAdStatus(Request $request): Response
 | |
|     {
 | |
|         $adStatus = [
 | |
|             0,  // UNSPECIFIED
 | |
|             1,  // UNKNOWN
 | |
|             2,  // ENABLED
 | |
|             3,  // PAUSED
 | |
|             4   // REMOVED
 | |
|         ];
 | |
| 
 | |
|         $requestData = $request->all(); // 获取请求数据
 | |
|         $customerId  = $requestData['customer_id'];
 | |
|         $groupId     = $requestData['group_id'];
 | |
|         $adId        = $requestData['ad_id'];
 | |
|         $status      = $requestData['status'];
 | |
|         if (!in_array($status, $adStatus)) {
 | |
|             return $this->errorResponse(101, 'status参数错误');
 | |
|         }
 | |
| //        try {
 | |
|         $result = $this->googleAdsAdService->updateAdStatus($customerId, $groupId, $adId, $status);
 | |
|         if (!$result) {
 | |
|             return $this->errorResponse(101, 'Status update failed');
 | |
|         }
 | |
|         return $this->successResponse(['message' => 'Status updated successfully']);
 | |
| //        } catch (ValidateException $e) {
 | |
| //             return $this->errorResponse(400, $e->getMessage());
 | |
| //        }
 | |
|     }
 | |
| 
 | |
| 
 | |
|     // 可以加入一些公共方法
 | |
|     protected function successResponse($data): Response
 | |
|     {
 | |
|         return Json([
 | |
|             'code' => 0,
 | |
|             'msg' => 'ok',
 | |
|             'data' => $data,
 | |
|         ]);
 | |
|     }
 | |
| 
 | |
|     protected function errorResponse($code, $message, $data = []): Response
 | |
|     {
 | |
|         return Json([
 | |
|             'code' => $code,
 | |
|             'msg' => $message ?: 'error',
 | |
|             'data' => $data
 | |
|         ]);
 | |
|     }
 | |
| 
 | |
| }
 |