KeywordsAdd.php 3.01 KB
<?php

namespace App\Console\Commands;

use App\Models\Pivots\GoalKeyword;
use App\Models\Tokens;
use App\Service\Requests\Direct\AddKeywords;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Support\Facades\DB;

class KeywordsAdd extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'keywords: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('dictionaryCampaignsEnabledForExternalSynchronized.goalKeywordsForNotExternal.keyword')
            ->where('type', '!=', Tokens::MAIN)
            ->get();

        foreach ($tokens as $token) {

            $token->load([
                'dictionaryCampaignsEnabledForExternalSynchronized' => function (HasManyThrough $query) {
                    return $query->has('goalKeywordsForNotExternal.keyword');
                },
            ]);

            $goalKeywords = DB::table('goal_keywords')
                ->join('keywords', 'goal_keywords.keyword_id', '=', 'keywords.id')
                ->whereNull('keywords.deleted_at')
                ->whereNull('goal_keywords.external_id')
                ->whereNull('goal_keywords.reserve_create_at')
                ->whereNotNull('goal_keywords.goal_ad_group_external_id')
                ->whereNotNull('goal_keywords.dictionary_campaign_external_id')
                ->whereIn('goal_keywords.dictionary_campaign_id', $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('id'))
                ->select([
                    'goal_keywords.id as id',
                    'goal_keywords.dictionary_campaign_id as dictionary_campaign_id',
                    'goal_keywords.goal_ad_group_external_id as goal_ad_group_external_id',
                    'keywords.keyword as keyword',
                    'keywords.bid as bid',
                    'keywords.context_bid as context_bid',
                    'keywords.user_param_1 as user_param_1',
                    'keywords.user_param_2 as user_param_2',
                ])
                ->get();

            foreach (array_chunk($goalKeywords->pluck('id')->toArray(), 1000) as $items) {
                GoalKeyword::whereIn('id', $items)
                    ->update([
                        'reserve_create_at' => Carbon::now(),
                    ]);
            }

            $factory = new AddKeywords();
            $factory->setToken($token)
                ->call([
                    'goalKeywords' => $goalKeywords,
                ]);
        }

        return 0;
    }
}