webman_ad/app/event/GoogleAdsAssetRelations.php

125 lines
4.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\event;
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;
const event = 'googleads:assetrelations:event';
const queue = 'googleads:assetrelations:queue';
const video_queue = 'googleads:assetrelations:video_queue';
//2025-2-11作废改用syncDateRangeAssetRelations
public function syncAssetRelations($options = [])
{
$queue = self::queue;
$video_queue = self::video_queue;
$customers = $this->googleOAuthService->getGoogleAdCustomers($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);
// 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['customer_id']);
$resourceName = $googleAdsAssetRelationService->runListAssetRelations($customer['customer_id'],$customer['date']);
}
// return $this->successResponse(['ads_list' => $resourceName]);
}
/**
* get asset relations
* @throws ApiException
*/
public function getVideoAssetRelations($customer)
{
$googleAdsAssetRelationService = new GoogleAdsAssetRelationService($customer['customer_id']);
$resourceName = $googleAdsAssetRelationService->runListVideoAssetRelations($customer['customer_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
]);
}
}