AdvertisementsAdd.php 5.68 KB
<?php

namespace App\Console\Commands;

use App\Models\Pivots\GoalAdvertisement;
use App\Models\Tokens;
use App\Service\Requests\Direct\AddAds;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;

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

        foreach ($tokens as $token) {
            $goalAds = DB::table('goal_advertisements')
                ->join('advertisements', 'goal_advertisements.advertisement_id', '=', 'advertisements.id')
                ->leftJoin('goal_advertisement_goal_ad_extensions', 'goal_advertisements.id', '=', 'goal_advertisement_goal_ad_extensions.goal_advertisement_id')
                ->leftJoin('goal_ad_extensions', 'goal_advertisement_goal_ad_extensions.goal_ad_extension_id', '=', 'goal_ad_extensions.id')
                ->leftJoin('ad_images', 'advertisements.ad_image_hash', '=', 'ad_images.hash')
                ->leftJoin('goal_ad_images', function($join) use ($token) {
                    $join->on('goal_ad_images.ad_image_id', '=', 'ad_images.id')
                        ->where('goal_ad_images.token_id', '=', $token->id);
                })
                ->whereNotExists(function (Builder $query) {
                    $query->select(DB::raw(1))
                        ->from('goal_advertisement_goal_ad_extensions')
                        ->join('goal_ad_extensions', 'goal_advertisement_goal_ad_extensions.goal_ad_extension_id', '=', 'goal_ad_extensions.id')
                        ->whereNull('goal_ad_extensions.external_id')
                        ->whereColumn('goal_advertisements.id', 'goal_advertisement_goal_ad_extensions.goal_advertisement_id');
                })
                ->where(function (Builder $query) {
                    $query->whereNull('goal_advertisements.goal_v_card_id')
                        ->orWhere(function (Builder $query) {
                            $query->whereNotNull('goal_advertisements.goal_v_card_id')
                                ->whereNotNull('goal_advertisements.goal_v_card_external_id');
                        });
                })
                ->where(function (Builder $query) {
                    $query->whereNull('goal_advertisements.goal_sitelink_id')
                        ->orWhere(function (Builder $query) {
                            $query->whereNotNull('goal_advertisements.goal_sitelink_id')
                                ->whereNotNull('goal_advertisements.goal_sitelink_external_id');
                        });
                })
                ->whereNull('advertisements.deleted_at')
                ->whereNull('goal_advertisements.external_id')
                ->whereNull('goal_advertisements.reserve_create_at')
                ->whereNotNull('goal_advertisements.goal_ad_group_external_id')
                ->whereNotNull('goal_advertisements.dictionary_campaign_external_id')
                ->whereIn('goal_advertisements.dictionary_campaign_id', $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('id'))
                ->select([
                    'goal_advertisements.id as id',
                    'goal_advertisements.dictionary_campaign_id as dictionary_campaign_id',
                    'goal_advertisements.goal_ad_group_external_id as goal_ad_group_external_id',
                    'advertisements.title as title',
                    'advertisements.text as text',
                    'advertisements.mobile as mobile',
                    'advertisements.title2 as title2',
                    'advertisements.href as href',
                    'advertisements.display_url_path as display_url_path',
                    'goal_advertisements.goal_v_card_external_id as goal_v_card_external_id',
                    'goal_ad_images.hash as ad_image_hash',
                    'goal_advertisements.goal_sitelink_external_id as goal_sitelink_external_id',
                    DB::raw('JSON_ARRAYAGG(goal_ad_extensions.external_id) as ad_extension_ids'),
                    'advertisements.ad_extensions as ad_extensions',
                    'advertisements.video_extension as video_extension',
                    'advertisements.price_extension as price_extension',
                    'advertisements.turbo_page_id as turbo_page_id',
                    'advertisements.business_id as business_id',
                    'advertisements.prefer_v_card_over_business as prefer_v_card_over_business',
                ])
                ->groupBy('goal_advertisements.id')
                ->get();

            if (!$goalAds->count()) {
                continue;
            }

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

            $request = new AddAds();
            $request->setToken($token)
                ->call([
                    'goalAds' => $goalAds,
                ]);
        }

        return 0;
    }
}