CampaignsCheckUpdatedChildren.php 2.55 KB
<?php

namespace App\Console\Commands;

use App\Models\Campaigns;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Illuminate\Console\Command;

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

    /**
     * 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()) {

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

            $campaign_ids_group = Campaigns::forUpdatedChildren()->pluck('external_id')->chunk(3000)->toArray();

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

                $factory->getRequest('Changes', 'check')->call([
                    'CampaignIds' => $campaign_ids,
                    'FieldNames' => [
                        'AdGroupIds', 'AdIds',
                    ],
                ]);
            }

        }


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

        foreach ($tokens as $token) {

            $campaign_ids_group = $token->dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren
                ->pluck('external_id')->chunk(3000)->toArray();

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

                $factory->getRequest('Changes', 'check')->call([
                    'CampaignIds' => $campaign_ids,
                    'FieldNames' => [
                        'AdGroupIds', 'AdIds',
                    ],
                ]);
            }
        }


        return 0;
    }
}