135 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			135 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | ||
| 
 | ||
| namespace app\event;
 | ||
| 
 | ||
| use app\service\BpsAdAccountService;
 | ||
| use app\service\GoogleAdsAssetRelationService;
 | ||
| use app\service\GoogleOAuthService;
 | ||
| use Google\ApiCore\ApiException;
 | ||
| use support\Response;
 | ||
| use DI\Annotation\Inject;
 | ||
| 
 | ||
| //use QL\QueryList;
 | ||
| use Webman\RedisQueue\Redis;
 | ||
| 
 | ||
| 
 | ||
| class GoogleAdsAssetRelations
 | ||
| {
 | ||
|     /**
 | ||
|      * @Inject
 | ||
|      * @var GoogleOAuthService
 | ||
|      */
 | ||
| 
 | ||
|     private $googleOAuthService;
 | ||
| 
 | ||
|     /**
 | ||
|      * @Inject
 | ||
|      * @var BpsAdAccountService
 | ||
|      */
 | ||
| 
 | ||
|     private $bpsAdAccountService;
 | ||
| 
 | ||
| 
 | ||
|     const event = 'googleads:assetrelations:event';
 | ||
|     const queue = 'googleads:assetrelations:queue';
 | ||
|     const video_queue = 'googleads:assetrelations:video_queue';
 | ||
| 
 | ||
|     //废弃2025-2-13,改用syncDateRangeAssetRelations
 | ||
|     public function syncAssetRelations($options = [])
 | ||
|     {
 | ||
|         $queue       = self::queue;
 | ||
|         $video_queue = self::video_queue;
 | ||
| //        $customers = $this->googleOAuthService->getGoogleAdCustomers($options);
 | ||
|         $customers = $this->bpsAdAccountService->getGoogleAdAccounts($options);
 | ||
| //        dump($customers);
 | ||
|         foreach ($customers as $customer) {
 | ||
|             if ($customer['login_customer_id'] > 0) {
 | ||
|                 Redis::send($queue, $customer, rand(5, 15));
 | ||
|                 Redis::send($video_queue, $customer, rand(5, 15));
 | ||
|             }
 | ||
|         }
 | ||
|         return self::event . ' send queue ok';
 | ||
|     }
 | ||
| 
 | ||
|     public function syncDateRangeAssetRelations($options = [])
 | ||
|     {
 | ||
|         // 获取 startDate 和 endDate
 | ||
|         $startDate = $options['startDate'];
 | ||
|         $endDate   = $options['endDate'];
 | ||
| 
 | ||
|         // 验证日期格式
 | ||
|         if (!strtotime($startDate) || !strtotime($endDate)) {
 | ||
|             throw new \InvalidArgumentException('Invalid date format for startDate or endDate');
 | ||
|         }
 | ||
| 
 | ||
|         // 将日期转换为 DateTime 对象
 | ||
|         $startDate = new \DateTime($startDate);
 | ||
|         $endDate   = new \DateTime($endDate);
 | ||
| 
 | ||
|         $queue       = self::queue;
 | ||
|         $video_queue = self::video_queue;
 | ||
| //        $customers = $this->googleOAuthService->getGoogleAdCustomers($options);
 | ||
|         $customers = $this->bpsAdAccountService->getGoogleAdAccounts($options);
 | ||
| //        dump($customers);
 | ||
|         // 遍历每一天的日期
 | ||
|         $interval  = new \DateInterval('P1D'); // 1天间隔
 | ||
|         $dateRange = new \DatePeriod($startDate, $interval, $endDate->modify('+1 day')); // 包含 endDate
 | ||
|         foreach ($dateRange as $date) {
 | ||
|             $currentDate = $date->format('Y-m-d'); // 格式化日期为 Y-m-d            // 遍历每个客户
 | ||
|             foreach ($customers as $customer) {
 | ||
|                 if ($customer['login_customer_id'] > 0) {
 | ||
|                     $customer['date'] = $currentDate; // 设置当前日期
 | ||
|                     Redis::send($queue, $customer, rand(10, 25));
 | ||
|                     Redis::send($video_queue, $customer, rand(10, 25));
 | ||
|                 }
 | ||
|             }
 | ||
|         }
 | ||
|         return self::event . ' send queue ok';
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * get asset relations
 | ||
|      * @throws ApiException
 | ||
|      */
 | ||
|     public function getAssetRelations($customer)
 | ||
|     {
 | ||
|         if ($customer['login_customer_id'] > 0) {
 | ||
|             $googleAdsAssetRelationService = new GoogleAdsAssetRelationService($customer['account_id']);
 | ||
|             $resourceName                  = $googleAdsAssetRelationService->runListAssetRelations($customer['account_id'], $customer['date']);
 | ||
|         }
 | ||
| //        return $this->successResponse(['ads_list' => $resourceName]);
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * get asset relations
 | ||
|      * @throws ApiException
 | ||
|      */
 | ||
|     public function getVideoAssetRelations($customer)
 | ||
|     {
 | ||
|         $googleAdsAssetRelationService = new GoogleAdsAssetRelationService($customer['account_id']);
 | ||
|         $resourceName                  = $googleAdsAssetRelationService->runListVideoAssetRelations($customer['account_id']);
 | ||
| 
 | ||
| //        return $this->successResponse(['ads_list' => $resourceName]);
 | ||
|     }
 | ||
| 
 | ||
| 
 | ||
|     // 可以加入一些公共方法
 | ||
|     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
 | ||
|         ]);
 | ||
|     }
 | ||
| }
 | 
