webman_ad/app/service/BpsAdAccountService.php
2025-02-22 21:55:39 +08:00

319 lines
13 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\service;
use think\facade\Db as ThinkDb;
use app\model\ThirdUserAdvertiser;
//作废2025-2-13
use app\model\BpsAdsMerchantRelation;
//use Webman\RedisQueue\Redis;
use Webman\RedisQueue\Client as QueueClient;
use app\event\GoogleAdsCustomers;
class BpsAdAccountService
{
/**
* 批量获取全部Google广告账号数据
* 作废2025-2-13
*/
// public function getGoogleAdAccounts($options = [])
// {
// if (!empty($options)) {
// $refreshToken = $options['refresh_token'];
// // 获取符合条件的客户ID数组
// $customers = ThirdUserAdvertiser::alias('tua')
// ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
// ->where('tu.third_type', 'google') // 筛选 third_type 为 google 的记录
// ->where('tu.access_token', $refreshToken) // 筛选 third_type 为 google 的记录
// ->field('tua.advertiser_id as account_id,tua.google_login_customer_id as login_customer_id,tua.google_test_account as test_account,tua.google_manager as manager, tu.access_token as refresh_token') // 获取 advertiser_id 字段
// ->select(); // 执行查询
// } else {
// // 获取符合条件的客户ID数组
// $customers = ThirdUserAdvertiser::alias('tua')
// ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
// ->where('tu.third_type', 'google') // 筛选 third_type 为 google 的记录
// ->field('tua.advertiser_id as account_id,tua.google_login_customer_id as login_customer_id,tua.google_test_account as test_account,tua.google_manager as manager, tu.access_token as refresh_token') // 获取 advertiser_id 字段
// ->select(); // 执行查询
// }
// }
/**
* 批量获取全部Google广告账号数据
*作废2025-2-13
*/
public function getGoogleAdAccounts($options = [])
{
if (!empty($options)) {
$merchant_id = $options['merchant_id'];
$account_id = $options['account_id'] ?? null; // 从 $options 中获取 account_id如果没有则默认为 null
$query = BpsAdsMerchantRelation::alias('bamr')
->where('bamr.platform', 2)
->where('bamr.merchant_id', $merchant_id)
->where('bamr.is_unbind', 'f')
->where('bamr.is_del', 'f');
// 如果传入了 account_id则增加 account_id 的过滤条件
if (!empty($account_id)) {
$query->where('bamr.account_id', $account_id);
}
// 选择需要的字段
$customers = $query->field('bamr.account_id, bamr.refresh_token, COALESCE(CAST(bamr.ext_info->>\'login_customer_id\' AS INT), 0) as login_customer_id')
->select(); // 执行查询
} else {
// 获取符合条件的客户ID数组
$customers = BpsAdsMerchantRelation::alias('bamr')
->where('bamr.platform', 2)
->where('bamr.is_unbind', 'f')
->where('bamr.is_del', 'f')
// ->field('bamr.account_id, bamr.refresh_token')
->field('bamr.account_id, bamr.refresh_token, bamr.ext_info->>\'login_customer_id\' as login_customer_id') // 提取 ext_info 中的 login_customer_id
->select(); // 执行查询
}
// 如果没有找到符合条件的广告主,抛出异常
if ($customers->isEmpty()) {
return [];
// throw new ApiException('No customers found for google third type');
}
// 转换为简单的数组(提取 advertiser_id
return $customers->toArray();
}
/**
* 批量获取全部meta广告账号数据
* 作废2025-2-13
*/
// public function getMetaAdAccounts($options = [])
// {
// if (!empty($options)) {
// $refreshToken = $options['refresh_token'];
// // 获取符合条件的客户ID数组
// $customers = ThirdUserAdvertiser::alias('tua')
// ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
// ->where('tu.third_type', 'facebook') // 筛选 third_type 为 google 的记录
// ->where('tu.access_token', $refreshToken) // 筛选 third_type 为 google 的记录
// ->field('tua.advertiser_id as account_id, tu.access_token as refresh_token') // 获取 advertiser_id 字段
// ->select(); // 执行查询
// } else {
// // 获取符合条件的客户ID数组
// $customers = ThirdUserAdvertiser::alias('tua')
// ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
// ->where('tu.third_type', 'facebook') // 筛选 third_type 为 google 的记录
// ->field('tua.advertiser_id as account_id, tu.access_token as refresh_token') // 获取 advertiser_id 字段
// ->select(); // 执行查询
// }
//
//
// // 如果没有找到符合条件的广告主,抛出异常
// if ($customers->isEmpty()) {
// return [];
//// throw new ApiException('No customers found for google third type');
// }
//
// // 转换为简单的数组(提取 advertiser_id
// return $customers->toArray();
//
// }
/**
* 批量获取全部meta广告账号数据
*作废2025-2-13
*/
public function getMetaAdAccounts($options = [])
{
if (!empty($options)) {
$merchant_id = $options['merchant_id'];
// 获取符合条件的客户ID数组
$customers = BpsAdsMerchantRelation::alias('bamr')
->where('bamr.platform', 1)
->where('bamr.merchant_id', $merchant_id)
->field('bamr.account_id, bamr.access_token as refresh_token')
->select(); // 执行查询
} else {
// 获取符合条件的客户ID数组
$customers = BpsAdsMerchantRelation::alias('bamr')
->where('bamr.platform', 1)
->where('bamr.is_unbind', 'f')
->where('bamr.is_del', 'f')
->field('bamr.account_id, bamr.access_token as refresh_token')
->select(); // 执行查询
}
// 如果没有找到符合条件的广告主,抛出异常
if ($customers->isEmpty()) {
return [];
// throw new ApiException('No customers found for google third type');
}
// 转换为简单的数组(提取 advertiser_id
return $customers->toArray();
}
/**
* 批量获取全部meta广告账号数据
* 作废2025-2-13
*/
// public function getTiktokAdAccounts($options = [])
// {
// if (!empty($options)) {
// $refreshToken = $options['refresh_token'];
// // 获取符合条件的客户ID数组
// $customers = ThirdUserAdvertiser::alias('tua')
// ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
// ->where('tu.third_type', 'tiktok') // 筛选 third_type 为 google 的记录
// ->where('tu.access_token', $refreshToken) // 筛选 third_type 为 google 的记录
// ->field('tua.advertiser_id as account_id, tu.access_token as refresh_token') // 获取 advertiser_id 字段
// ->select(); // 执行查询
// } else {
// // 获取符合条件的客户ID数组
// $customers = ThirdUserAdvertiser::alias('tua')
// ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
// ->where('tu.third_type', 'tiktok') // 筛选 third_type 为 google 的记录
// ->field('tua.advertiser_id as account_id, tu.access_token as refresh_token') // 获取 advertiser_id 字段
// ->select(); // 执行查询
// }
//
//
// // 如果没有找到符合条件的广告主,抛出异常
// if ($customers->isEmpty()) {
// return [];
//// throw new ApiException('No customers found for google third type');
// }
//
// // 转换为简单的数组(提取 advertiser_id
// return $customers->toArray();
//
// }
/**
* 批量获取全部tiktok广告账号数据
*作废2025-2-13
*/
public function getTiktokAdAccounts($options = [])
{
if (!empty($options)) {
// 获取符合条件的客户ID数组
$merchant_id = $options['merchant_id'];
$customers = BpsAdsMerchantRelation::alias('bamr')
->where('bamr.platform', 3)
->where('bamr.merchant_id', $merchant_id)
->where('bamr.is_unbind', 'f')
->where('bamr.is_del', 'f')
->field('bamr.account_id, bamr.refresh_token')
->select(); // 执行查询
} else {
// 获取符合条件的客户ID数组
$customers = BpsAdsMerchantRelation::alias('bamr')
->where('bamr.platform', 3)
->where('bamr.is_unbind', 'f')
->where('bamr.is_del', 'f')
->field('bamr.account_id, bamr.access_token as refresh_token')
->select(); // 执行查询
}
// 如果没有找到符合条件的广告主,抛出异常
if ($customers->isEmpty()) {
return [];
// throw new ApiException('No customers found for google third type');
}
// 转换为简单的数组(提取 advertiser_id
return $customers->toArray();
}
/**
* 批量获取全部广告账号数据
* 作废2025-2-13
*/
// public function getAllAdAccounts($options = [])
// {
// // 获取符合条件的客户ID数组
// $customers = ThirdUserAdvertiser::alias('tua')
// ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id') // 连接 bps_third_user 表
// ->where('tu.merchant_id', $options['merchant_id']) // 筛选 third_type 为 google 的记录
// ->where('tu.access_token', '<>', '') // 筛选 access_token 不为空的记录
// ->field('tua.advertiser_id as account_id, tu.access_token as refresh_token') // 获取 advertiser_id 字段
// ->order('tu.third_type', 'asc') // 获取 advertiser_id 字段
// ->select(); // 执行查询
//
//
// // 如果没有找到符合条件的广告主,抛出异常
// if ($customers->isEmpty()) {
// return [];
//// throw new ApiException('No customers found for google third type');
// }
//
// // 转换为简单的数组(提取 advertiser_id
// return $customers->toArray();
//
// }
/**
* 批量获取全部广告账号数据
* TODO 增加fields入参
*/
public function getAllAdAccounts($options = [])
{
$merchant_id = $options['merchant_id'];
$platform = $options['platform'] ?? 0;
if ($platform > 0) {
// 获取某广告平台符合条件的客户ID数组
$customers = BpsAdsMerchantRelation::alias('bamr')
->where('bamr.platform', $platform)
->where('bamr.merchant_id', $merchant_id)
->where('bamr.is_unbind', 'f')
->where('bamr.is_del', 'f')
->field('bamr.account_id, bamr.access_token as refresh_token')
->select(); // 执行查询
} else {
// 获取全部广告平台符合条件的客户ID数组
$customers = BpsAdsMerchantRelation::alias('bamr')
->where('bamr.merchant_id', $merchant_id)
->where('bamr.is_unbind', 'f')
->where('bamr.is_del', 'f')
->field('bamr.account_id, bamr.access_token as refresh_token')
->select(); // 执行查询
}
// 如果没有找到符合条件的广告主,抛出异常
if ($customers->isEmpty()) {
return [];
// throw new ApiException('No customers found for google third type');
}
// 转换为简单的数组(提取 advertiser_id
return $customers->toArray();
}
// 获取所有平台的第三方用户数据
public function getAllThirdUsers($options = [])
{
// $userId = $options['uid']; // 获取用户ID
$merchant_id = $options['merchant_id']; // 获取用户ID
$customers = BpsAdsMerchantRelation::alias('bamr')
->where('bamr.merchant_id', $merchant_id)
->where('bamr.is_unbind', 'f')
->where('bamr.is_del', 'f')
->field('bamr.account_id, bamr.access_token as refresh_token')
->select(); // 执行查询
// 如果没有找到符合条件的用户,返回空数组
if ($customers->isEmpty()) {
return [];
}
//dump($customers->toArray());
return $customers->toArray();
}
}