<?php

namespace app\service;

use GuzzleHttp\Client;
use think\facade\Db as ThinkDb;
use app\model\ThirdUserAdvertiser;
use app\model\ThirdUser;

//use Webman\RedisQueue\Redis;
use Webman\RedisQueue\Client as QueueClient;
use app\event\GoogleAdsCustomers;

class BpsAdAccountService
{

    public function getCustomerList($third_user_id = 0)
    {
        $tableName = 'bps_third_user_advertiser';
        $tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'bps' . $tableName;

        $sql  = "SELECT * FROM {$tableName} WHERE doc_ = :third_user_id";
        $data = ['third_user_id' => $third_user_id];

        return ThinkDb::query($sql, $data);
    }

    /**
     * 批量获取全部Google广告账号数据
     *
     */
    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();  // 执行查询
        }


        // 如果没有找到符合条件的广告主,抛出异常
        if ($customers->isEmpty()) {
            return [];
//            throw new ApiException('No customers found for google third type');
        }

        // 转换为简单的数组(提取 advertiser_id)
        return $customers->toArray();

    }

    /**
     * 批量获取全部meta广告账号数据
     *
     */
    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广告账号数据
     *
     */
    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();

    }

    /**
     * 批量获取全部广告账号数据
     *
     */
    public function getAllAdAccounts($options = [])
    {
        // 获取符合条件的客户ID数组
        $customers = ThirdUserAdvertiser::alias('tua')
            ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id')  // 连接 bps_third_user 表
            ->where('tu.user_id', $options['uid'])  // 筛选 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();

    }

    // 获取 Facebook 第三方用户数据
    public function getMetaThirdUsers($options = [])
    {
        $uid = $options['uid'] ?? null;

        if ($uid) {
            // 根据 UID 获取符合条件的用户数据
            $users = ThirdUserAdvertiser::alias('tua')
                ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id')  // 连接 bps_third_user 表
                ->where('tu.third_type', 'facebook')  // 筛选 third_type 为 facebook 的记录
                ->where('tu.user_id', $uid)  // 筛选 user_id 的记录
                ->where('tu.access_token', '<>', '')  // 筛选 access_token 不为空的记录
                ->field('tu.id, tu.access_token as refresh_token')  // 获取相关字段
                ->select();
        } else {
            // 获取符合条件的用户数据(如果没有 UID)
            $users = ThirdUserAdvertiser::alias('tua')
                ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id')  // 连接 bps.bps_third_user 表
                ->where('tu.third_type', 'facebook')  // 筛选 third_type 为 facebook 的记录
                ->field('tu.id, tu.access_token as refresh_token')  // 获取相关字段
                ->select();
        }

        // 如果没有找到符合条件的用户,返回空数组
        if ($users->isEmpty()) {
            return [];
        }

        return $users->toArray();
    }


// 获取 Google 第三方用户数据
    public function getGoogleThirdUsers($options = [])
    {
        $uid = $options['uid'] ?? null;

        if ($uid) {
            // 获取符合条件的用户数据
            $users = 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.user_id', $uid)  // 筛选 user_id 的记录
                ->where('tu.access_token', '<>', '')  // 筛选 access_token 不为空的记录
                ->field('tu.id, tu.access_token as refresh_token')  // 获取相关字段
                ->select();
        } else {
            // 获取符合条件的用户数据
            $users = 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('tu.id, tu.access_token as refresh_token')  // 获取相关字段
                ->select();
        }

        // 如果没有找到符合条件的用户,返回空数组
        if ($users->isEmpty()) {
            return [];
        }

        return $users->toArray();
    }

// 获取 TikTok 第三方用户数据
    public function getTiktokThirdUsers($options = [])
    {
        $uid = $options['uid'] ?? null;

        if ($uid) {
//            $refreshToken = $options['refresh_token'];
            // 获取符合条件的用户数据
            $users = ThirdUserAdvertiser::alias('tua')
                ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id')  // 连接 bps_third_user 表
                ->where('tu.third_type', 'tiktok')  // 筛选 third_type 为 tiktok 的记录
                ->where('tu.user_id', $uid)  // 筛选 user_id 的记录
                ->where('tu.access_token', '<>', '')  // 筛选 access_token 不为空的记录
                ->field('tu.id, tu.access_token as refresh_token')  // 获取相关字段
                ->select();
        } else {
            // 获取符合条件的用户数据
            $users = ThirdUserAdvertiser::alias('tua')
                ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id')  // 连接 bps_third_user 表
                ->where('tu.third_type', 'tiktok')  // 筛选 third_type 为 tiktok 的记录
                ->field('tu.id, tu.access_token as refresh_token')  // 获取相关字段
                ->select();
        }

        // 如果没有找到符合条件的用户,返回空数组
        if ($users->isEmpty()) {
            return [];
        }

        return $users->toArray();
    }

// 获取所有平台的第三方用户数据
    public function getAllThirdUsers($options = [])
    {
        $userId = $options['uid'];  // 获取用户ID

        // 获取符合条件的用户数据
        $users = ThirdUserAdvertiser::alias('tua')
            ->join('bps.bps_third_user tu', 'tua.doc_ = tu.id')  // 连接 bps_third_user 表
            ->where('tu.user_id', $userId)  // 筛选 user_id 的记录
            ->where('tu.access_token', '<>', '')  // 筛选 access_token 不为空的记录
            ->field('tu.id, tu.third_type, tu.access_token as refresh_token')  // 获取相关字段
            ->select();

        // 如果没有找到符合条件的用户,返回空数组
        if ($users->isEmpty()) {
            return [];
        }
//dump($users->toArray());
        return $users->toArray();
    }


}