CampaignsUpdate.php 1.89 KB
<?php

namespace App\Console\Commands;

use App\Models\Pivots\DictionaryCampaign;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Carbon\Carbon;
use Illuminate\Console\Command;

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

    /**
     * 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()
    {
        $tokens = Tokens::whereHas('dictionaryCampaignsEnabledForExternalUpdatedNeedUpdatedForNotReserveUpdate.campaign')
            ->with('dictionaryCampaignsEnabledForExternalUpdatedNeedUpdatedForNotReserveUpdate.campaign')
            ->where('type', '!=', Tokens::MAIN)
            ->get();

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

            foreach (array_chunk($token->dictionaryCampaignsEnabledForExternalUpdatedNeedUpdatedForNotReserveUpdate->pluck('id')->toArray(), 1000) as $items){
                DictionaryCampaign::whereIn('id', $items)
                    ->update([
                        'reserve_update_at' => Carbon::now(),
                    ]);
            }

            $factory->getRequest('Campaigns', 'update')
                ->call([
                    'dictionaryCampaigns' => $token->dictionaryCampaignsEnabledForExternalUpdatedNeedUpdatedForNotReserveUpdate,
                ]);
        }

        return 0;
    }
}