CampaignsCheckUpdatedChildrenAdGroups.php 2.51 KB
<?php

namespace App\Console\Commands;

use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Campaigns;
use App\Models\Pivots\DictionaryCampaign;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Direct\CheckDictionaries;
use App\Service\Direct\GetCampaigns;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Support\Facades\Bus;

class CampaignsCheckUpdatedChildrenAdGroups extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'campaigns:checkUpdatedChildrenAdGroups';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Проверка наличия изменения в дочерних элементах';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        if (!Campaigns::forUpdatedChildren()->exists()) {
            return;
        }

        $token = Tokens::where('type', Tokens::MAIN)->first();
        if (!$token) {
            throw new \Exception('Не найден токен блин');
        }

        $factory = APIRequest::getInstance(API::YANDEX);
        $factory->setToken($token);

        $factory->getRequest('Changes', 'check')->call([
            'CampaignIds' => Campaigns::forUpdatedChildren()->pluck('external_id')->all(),
            'FieldNames' => [
                'AdGroupIds',
            ],
            'Timestamp' => $token->check_changes_ad_group_at->toIso8601ZuluString(),
        ]);

        $tokens = Tokens::has('dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren')
            ->with('dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren')
            ->where('type', '!=', Tokens::MAIN)
            ->get();

        foreach ($tokens as $token) {
            $factory = APIRequest::getInstance(API::YANDEX);
            $factory->setToken($token);

            $factory->getRequest('Changes', 'check')->call([
                'CampaignIds' => $token->dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren
                    ->pluck('external_id')->all(),
                'FieldNames' => [
                    'AdGroupIds',
                ],
            ]);
        }

        return 0;
    }
}