CampaignsAdd.php 1.74 KB
<?php

namespace App\Console\Commands;

use App\Models\Pivots\DictionaryCampaign;
use App\Models\Tokens;
use App\Service\Requests\Direct\AddCampaigns;
use Carbon\Carbon;
use Illuminate\Console\Command;

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

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


        foreach ($tokens as $token) {
            foreach (array_chunk($token->dictionaryCampaignsEnabledForNotExternalForNotReserveCreate->pluck('id')->toArray(), 100) as $items) {
                DictionaryCampaign::whereIn('id', $items)
                    ->update([
                        'reserve_create_at' => Carbon::now(),
                    ]);
            }

            $request = new AddCampaigns();
            $request->setToken($token)
                ->call([
                    'dictionaryCampaigns' => $token->dictionaryCampaignsEnabledForNotExternalForNotReserveCreate,
                ]);
        }

        return 0;
    }
}