diff --git a/app/event/GoogleAdsAssetRelations.php b/app/event/GoogleAdsAssetRelations.php index c8ac136..9264c60 100644 --- a/app/event/GoogleAdsAssetRelations.php +++ b/app/event/GoogleAdsAssetRelations.php @@ -26,6 +26,7 @@ class GoogleAdsAssetRelations const queue = 'googleads:assetrelations:queue'; const video_queue = 'googleads:assetrelations:video_queue'; + //2025-2-11作废,改用syncDateRangeAssetRelations public function syncAssetRelations($options = []) { $queue = self::queue; @@ -41,6 +42,41 @@ class GoogleAdsAssetRelations 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 @@ -49,7 +85,7 @@ class GoogleAdsAssetRelations { if ($customer['login_customer_id'] > 0) { $googleAdsAssetRelationService = new GoogleAdsAssetRelationService($customer['customer_id']); - $resourceName = $googleAdsAssetRelationService->runListAssetRelations($customer['customer_id']); + $resourceName = $googleAdsAssetRelationService->runListAssetRelations($customer['customer_id'],$customer['date']); } // return $this->successResponse(['ads_list' => $resourceName]); } diff --git a/app/event/GoogleAdsDateDatas.php b/app/event/GoogleAdsDateDatas.php index 18758ee..232039b 100644 --- a/app/event/GoogleAdsDateDatas.php +++ b/app/event/GoogleAdsDateDatas.php @@ -27,6 +27,56 @@ class GoogleAdsDateDatas const queue = 'googleads:datedatas:queue'; const task = 'googleads:datedatas:task'; + //同步某段时间成效(默认新绑定用户触发7天历史数据) + public function syncDateRangeDatas($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; + + // 获取客户数据 + $customers = $this->googleOAuthService->getGoogleAdCustomers($options); + + // 遍历每一天的日期 + $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(1, 10)); // 发送到 Redis 队列 + } + } + } + //同步消息到同步任务表 +// $queueTask = self::task; +// $taskInfo = [ +// 'ecommerce' => 1001, +// 'ecommerce_shop_id' => $options['date'], +// 'task_type' => 'InsightsMerge', +// 'remain' => 1 +// ]; +// Client::send($queueTask, $taskInfo, 30); + + + return self::event . ' send queue ok'; + } + + //同步某天成效 2025-2-11作废,改用syncDateRangeDatas public function syncDateDatas($options = []) { $queue = self::queue; diff --git a/app/process/UpdateGoogleAdsTask.php b/app/process/UpdateGoogleAdsTask.php index 7ac0706..9e94382 100644 --- a/app/process/UpdateGoogleAdsTask.php +++ b/app/process/UpdateGoogleAdsTask.php @@ -25,41 +25,54 @@ class UpdateGoogleAdsTask // 每15分钟执行一次 - new Crontab('10 15 */2 * * *', function () { - - $dayBeforeYesterdayStart = date('Y-m-d', strtotime('-2 day')); - dump($dayBeforeYesterdayStart . '更新' . GoogleAdsDateDatas::event . '开始'); - Event::emit(GoogleAdsDateDatas::event, ['date' => $dayBeforeYesterdayStart]); + new Crontab('* * */11 * * *', function () { + $data = []; + $data['endDate'] = date('Y-m-d'); // 获取今天的日期 + $data['startDate'] = date('Y-m-d', strtotime('-7 days')); // 获取7天前的日期 + dump($data['startDate'] . '到' . $data['endDate'] . '' . '更新' . GoogleAdsDateDatas::event . '开始'); + Event::emit(GoogleAdsDateDatas::event, $data); } ); // 每15分钟执行一次 - new Crontab('20 14 */2 * * *', function () { - $yesterdayStart = date('Y-m-d', strtotime('-1 day')); - dump($yesterdayStart . '更新' . GoogleAdsDateDatas::event . '开始'); - Event::emit(GoogleAdsDateDatas::event, ['date' => $yesterdayStart]); +// new Crontab('10 15 */12 * * *', function () { +// +// $dayBeforeYesterdayStart = date('Y-m-d', strtotime('-2 day')); +// dump($dayBeforeYesterdayStart . '更新' . GoogleAdsDateDatas::event . '开始'); +// Event::emit(GoogleAdsDateDatas::event, ['date' => $dayBeforeYesterdayStart]); +// +// } +// ); + + // 每15分钟执行一次 +// new Crontab('20 14 */12 * * *', function () { +// $yesterdayStart = date('Y-m-d', strtotime('-1 day')); +// dump($yesterdayStart . '更新' . GoogleAdsDateDatas::event . '开始'); +// Event::emit(GoogleAdsDateDatas::event, ['date' => $yesterdayStart]); +// } +// ); + + // 每15分钟执行一次 +// new Crontab('30 13 */12 * * *', function () { +// //获取今天的 0 点的YYYY - MM - DD格式 +// $todayStart = date('Y-m-d', strtotime('0 day')); +// dump($todayStart . '更新' . GoogleAdsDateDatas::event . '开始'); +// Event::emit(GoogleAdsDateDatas::event, ['date' => $todayStart]); +// } +// ); + // 每15分钟执行一次 + new Crontab('* * */14 * * *', function () { + $data = []; + $data['endDate'] = date('Y-m-d'); // 获取今天的日期 + $data['startDate'] = date('Y-m-d', strtotime('-7 days')); // 获取7天前的日期 + dump($data['startDate'] . '到' . $data['endDate'] . '' . '更新' . GoogleAdsAssetRelations::event . '开始'); + Event::emit(GoogleAdsAssetRelations::event, $data); } ); // 每15分钟执行一次 - new Crontab('30 13 */2 * * *', function () { - //获取今天的 0 点的YYYY - MM - DD格式 - $todayStart = date('Y-m-d', strtotime('0 day')); - dump($todayStart . '更新' . GoogleAdsDateDatas::event . '开始'); - Event::emit(GoogleAdsDateDatas::event, ['date' => $todayStart]); - } - ); - // 每15分钟执行一次 - new Crontab('15 12 */2 * * *', function () { - - dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsAssetRelations::event . '开始'); - Event::emit(GoogleAdsAssetRelations::event, []); - } - ); - - // 每15分钟执行一次 - new Crontab('30 * */2 * * *', function () { + new Crontab('30 * */12 * * *', function () { dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsGroups::event . '开始'); Event::emit(GoogleAdsGroups::event, []); @@ -69,26 +82,26 @@ class UpdateGoogleAdsTask ); // 每15分钟执行一次 - new Crontab('5 * */2 * * *', function () { + new Crontab('5 * */12 * * *', function () { dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsAds::event . '开始'); Event::emit(GoogleAdsAds::event, []); } ); // 每15分钟执行一次 - new Crontab('25 12 */3 * * *', function () { + new Crontab('* * */12 * * *', function () { dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsCreatives::event . '开始'); Event::emit(GoogleAdsCreatives::event, []); } ); - new Crontab('25 11 */2 * * *', function () { + new Crontab('25 11 */12 * * *', function () { dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsAssets::event . '开始'); Event::emit(GoogleAdsAssets::event, []); } ); - new Crontab('16 */14 * * * *', function () { + new Crontab('16 */1 * * * *', function () { dump(date('Y-m-d H:i:s') . '更新' . GoogleAdsCustomers::event . '开始'); Event::emit(GoogleAdsCustomers::event, []); } diff --git a/app/queue/redis/GoogleAdsCustomerInitQueue.php b/app/queue/redis/GoogleAdsCustomerInitQueue.php index 35fa086..c73735f 100644 --- a/app/queue/redis/GoogleAdsCustomerInitQueue.php +++ b/app/queue/redis/GoogleAdsCustomerInitQueue.php @@ -28,7 +28,6 @@ class GoogleAdsCustomerInitQueue implements Consumer //新绑定的客户,立即同步广告系列 Event::emit(GoogleAdsCampaigns::event, $data); - //新绑定的客户,立即同步广告组 Event::emit(GoogleAdsGroups::event, $data); //新绑定的客户,立即同步广告 @@ -36,16 +35,19 @@ class GoogleAdsCustomerInitQueue implements Consumer //新绑定的客户,立即同步素材库 Event::emit(GoogleAdsAssets::event, $data); //新绑定的客户,立即同步素材关系绑定 - Event::emit(GoogleAdsAssetRelations::event, $data); - //新绑定的客户,立即同步素材关系绑定 Event::emit(GoogleAdsCreatives::event, $data); - //新绑定的客户,立即同步最近3天的广告报表 - $data['date'] = date('Y-m-d', strtotime('-2 day')); - Event::emit(GoogleAdsDateDatas::event, $data); - $data['date'] = date('Y-m-d', strtotime('-1 day')); - Event::emit(GoogleAdsDateDatas::event, $data); - $data['date'] = date('Y-m-d', strtotime('0 day')); + //新绑定的客户,立即同步最近7天的广告报表 +// $data['date'] = date('Y-m-d', strtotime('-2 day')); +// Event::emit(GoogleAdsDateDatas::event, $data); +// $data['date'] = date('Y-m-d', strtotime('-1 day')); +// Event::emit(GoogleAdsDateDatas::event, $data); +// $data['date'] = date('Y-m-d', strtotime('0 day')); +// Event::emit(GoogleAdsDateDatas::event, $data); + $data['endDate'] = date('Y-m-d'); // 获取今天的日期 + $data['startDate'] = date('Y-m-d', strtotime('-7 days')); // 获取7天前的日期 Event::emit(GoogleAdsDateDatas::event, $data); + //新绑定的客户,立即同步最近7天素材-广告关系绑定 + Event::emit(GoogleAdsAssetRelations::event, $data); } diff --git a/app/service/GoogleAdsAssetRelationService.php b/app/service/GoogleAdsAssetRelationService.php index 411d127..71ab2dd 100644 --- a/app/service/GoogleAdsAssetRelationService.php +++ b/app/service/GoogleAdsAssetRelationService.php @@ -75,11 +75,11 @@ class GoogleAdsAssetRelationService extends BaseService * @return mixed * @throws ApiException */ - public function runListAssetRelations(int $customerId): array + public function runListAssetRelations(int $customerId, $date): array { // dump($customerId); // Creates a single shared budget to be used by the campaigns added below. - $assetsResourceName = self::getAssetRelations($customerId); + $assetsResourceName = self::getAssetRelations($customerId,$date); if (is_array($assetsResourceName) && count($assetsResourceName) > 0) { // dump($assetsResourceName); @@ -115,6 +115,7 @@ class GoogleAdsAssetRelationService extends BaseService */ public static function saveAssetRelations($assetsResourceName) { +// dump($assetsResourceName); $tableName = 'bps_google_ads_asset_relations'; $tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'public' . $tableName; foreach ($assetsResourceName as $data) { @@ -134,9 +135,8 @@ class GoogleAdsAssetRelationService extends BaseService * @param int $customerId the customer ID */ - public static function getAssetRelations(int $customerId) + public static function getAssetRelations(int $customerId, $date = '2024-12-19') { - $date = date('Y-m-d'); // 调用私有方法提取 year, month, season $dateDetails = self::extractDateDetails($date); @@ -185,6 +185,7 @@ class GoogleAdsAssetRelationService extends BaseService } } +// dump($result); return $result; // dump('Google Ads Asset synchronization completed.'); @@ -197,7 +198,8 @@ class GoogleAdsAssetRelationService extends BaseService public static function getVideoAssetRelations(int $customerId) { - $date = date('Y-m-d'); +// $date = date('Y-m-d'); + $date = '2025-02-07'; // 调用私有方法提取 year, month, season $dateDetails = self::extractDateDetails($date); diff --git a/config/event.php b/config/event.php index 14af5a0..63fd9c0 100644 --- a/config/event.php +++ b/config/event.php @@ -54,7 +54,8 @@ return [ [GoogleAdsCreatives::class, 'getCreatives'], ], GoogleAdsAssetRelations::event => [ - [GoogleAdsAssetRelations::class, 'syncAssetRelations'], +// [GoogleAdsAssetRelations::class, 'syncAssetRelations'], //2025-2-11作废,改用syncDateRangeAssetRelations + [GoogleAdsAssetRelations::class, 'syncDateRangeAssetRelations'], ], GoogleAdsAssetRelations::queue => [ [GoogleAdsAssetRelations::class, 'getAssetRelations'], @@ -64,7 +65,8 @@ return [ [GoogleAdsAssetRelations::class, 'getVideoAssetRelations'], ], GoogleAdsDateDatas::event => [ - [GoogleAdsDateDatas::class, 'syncDateDatas'], +// [GoogleAdsDateDatas::class, 'syncDateDatas'],//2025-2-11作废,syncDateRangeDatas + [GoogleAdsDateDatas::class, 'syncDateRangeDatas'], ], GoogleAdsDateDatas::queue => [ [GoogleAdsDateDatas::class, 'getDateDatas'],