<?php

namespace app\event;

use app\service\GoogleAdsCampaignService;
use app\service\GoogleOAuthService;
use Google\ApiCore\ApiException;
use support\Response;
use DI\Annotation\Inject;
use Webman\RedisQueue\Redis;

//use QL\QueryList;


class GoogleAdsDateDatas
{

    /**
     * @Inject
     * @var GoogleOAuthService
     */

    private $googleOAuthService;

    const event = 'googleads:datedatas:event';
    const queue = 'googleads:datedatas:queue';

    public function syncDateDatas($options = [])
    {
        $queue     = self::queue;
        $customers = $this->googleOAuthService->getGoogleAdCustomers($options);
        foreach ($customers as $customer) {
            if ($customer['login_customer_id'] > 0) {
                $customer['date'] = $options['date'];
                Redis::send($queue, $customer,rand(1,10));
            }
        }
        return self::event.' send queue ok';
    }

    /**
     * get date datas
     * @throws ApiException
     */
    public function getDateDatas($customer)
    {

        if ($customer['login_customer_id'] > 0 && ((isset($customer['manager']) && $customer['manager'] === false))) {
//                dump($customer);
            $googleAdsCampaignService = new googleAdsCampaignService($customer['customer_id']);
//                dump($customer['customer_id'],$customer, $options['date']);
            $googleAdsCampaignService->runListDateDatas($customer['customer_id'], $customer, $customer['date']);
        }

//        $this->googleAdsCampaignService->runListDateDatas($options['customer_id'], $options['date']);
//        return $this->successResponse(['data' => $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
        ]);
    }
}