diff --git a/app/event/GoogleAdsCustomers.php b/app/event/GoogleAdsCustomers.php index 56a4c01..def98f6 100644 --- a/app/event/GoogleAdsCustomers.php +++ b/app/event/GoogleAdsCustomers.php @@ -34,6 +34,7 @@ class GoogleAdsCustomers private $googleAdsAccountService; const event = 'googleads:customers:event'; + const last_sync = 'googleads:customers:last_sync'; const newbind_event = 'googleads:customers:newbind:event'; const add_queue = 'googleads:customers:add:queue'; const init_queue = 'googleads:customers:init:queue'; @@ -147,6 +148,27 @@ class GoogleAdsCustomers } + + //更新某个广告账号的最后同步时间 + public function updateLastSyncTime($options) + { + if (!$options) { + dump('not found new third user'); + return; + } + $merchant_id = $options['merchant_id']; + $account_id = $options['account_id']; + + try { + // 更新某个广告账号的最后同步时间 + $this->googleOAuthService->updateAccountLastSyncTime($merchant_id, $account_id); + } catch (\Exception $e) { + // 记录错误日志并跳过当前循环 + Log::error("updateLastSyncTime Error processing account_id {$account_id}: " . $e->getMessage()); + } + + } + //每分钟定时轮询缓存是否有新的广告账号需要绑定-荣洁set缓存 public function newBindAccount() { diff --git a/app/queue/redis/GoogleAdsCustomerInitQueue.php b/app/queue/redis/GoogleAdsCustomerInitQueue.php index 04036a9..63aab7f 100644 --- a/app/queue/redis/GoogleAdsCustomerInitQueue.php +++ b/app/queue/redis/GoogleAdsCustomerInitQueue.php @@ -49,6 +49,8 @@ class GoogleAdsCustomerInitQueue implements Consumer //新绑定的客户,立即同步最近7天素材-广告关系绑定 Event::emit(GoogleAdsAssetRelations::event, $data); + Event::emit(GoogleAdsCustomers::last_sync, $data); + } diff --git a/app/queue/redis/GoogleAdsDateDataQueue.php b/app/queue/redis/GoogleAdsDateDataQueue.php index 43a1d2a..8e527ac 100644 --- a/app/queue/redis/GoogleAdsDateDataQueue.php +++ b/app/queue/redis/GoogleAdsDateDataQueue.php @@ -2,6 +2,7 @@ namespace app\queue\redis; +use app\event\GoogleAdsCustomers; use app\event\GoogleAdsDateDatas; use Webman\Event\Event; use Webman\RedisQueue\Consumer; @@ -19,6 +20,7 @@ class GoogleAdsDateDataQueue implements Consumer { // dump($this->queue.' consumed',$data); Event::emit(GoogleAdsDateDatas::queue, $data); + Event::emit(GoogleAdsCustomers::last_sync, $data); } } \ No newline at end of file diff --git a/app/service/BpsAdAccountService.php b/app/service/BpsAdAccountService.php index b14d596..4f1557b 100644 --- a/app/service/BpsAdAccountService.php +++ b/app/service/BpsAdAccountService.php @@ -60,7 +60,7 @@ class BpsAdAccountService $query->where('bamr.account_id', $account_id); } // 选择需要的字段 - $customers = $query->field('bamr.account_id, bamr.refresh_token, bamr.ext_info->>\'login_customer_id\' as login_customer_id') + $customers = $query->field('bamr.merchant_id,bamr.account_id, bamr.refresh_token, bamr.ext_info->>\'login_customer_id\' as login_customer_id') ->select(); // 执行查询 } else { // 获取符合条件的客户ID数组 diff --git a/app/service/GoogleOAuthService.php b/app/service/GoogleOAuthService.php index 051feb9..c7a51db 100644 --- a/app/service/GoogleOAuthService.php +++ b/app/service/GoogleOAuthService.php @@ -283,6 +283,27 @@ class GoogleOAuthService } } + + public function updateAccountLastSyncTime($merchantId, $accountId): void + { + $tableName = 'bps_ads_merchant_relation'; + $tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'bps' . $tableName; + + //更新last_sync_time 赋值当前时间戳 + $data = [ + 'merchant_id' => $merchantId, + 'account_id' => $accountId, + 'last_sync_time' => time() + ]; + $sql = " + UPDATE {$tableName} + SET last_sync_time = :last_sync_time + where paltform = 2 and merchant_id = :merchant_id and account_id = :account_id + "; + ThinkDb::execute($sql, $data); + } + + public function updateThirdUserDefault($third_user_id, $is_default = 't'): void { $tableName = 'bps_third_user'; diff --git a/config/event.php b/config/event.php index ca47464..a2593ef 100644 --- a/config/event.php +++ b/config/event.php @@ -81,6 +81,9 @@ return [ GoogleAdsCustomers::newbind_event => [ [GoogleAdsCustomers::class, 'newBindAccount'], ], + GoogleAdsCustomers::last_sync => [ + [GoogleAdsCustomers::class, 'updateLastSyncTime'], + ], GoogleAdsCustomers::add_queue => [ [GoogleAdsCustomers::class, 'addRootCustomersNew'], ],