DictionaryCampaignsSyncByCampaign.php 1.67 KB
<?php

namespace App\Console\Commands;

use App\Models\AdGroup;
use App\Models\Keyword;
use App\Models\Pivots\DictionaryCampaign;
use App\Models\Pivots\GoalAdGroup;
use App\Models\Pivots\GoalKeyword;
use Illuminate\Console\Command;

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

    /**
     * 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()
    {
        DictionaryCampaign::with([
            'campaign.groups',
        ])->synced()->needSynced()->get()->each(function (DictionaryCampaign $dictionaryCampaign) {

            $dictionaryCampaign->campaign->groups->each(function (AdGroup $adGroup) use ($dictionaryCampaign) {

                $goalAdGroup = GoalAdGroup::updateOrCreateByMain($adGroup, $dictionaryCampaign);

                $adGroup->keywords->each(function (Keyword $keyword) use ($goalAdGroup, $dictionaryCampaign) {
                    GoalKeyword::updateOrCreateByMain($keyword, $goalAdGroup, $dictionaryCampaign);
                });

            });

            $dictionaryCampaign->update([
                'synced_need' => null,
            ]);

        });

        return 0;
    }
}