BidModifiersAdd.php 3.74 KB
<?php

namespace App\Console\Commands;

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

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

        foreach ($tokens as $token) {
            $goalBidModifiers = DB::table('goal_bid_modifiers')
                ->join('bid_modifiers', 'goal_bid_modifiers.bid_modifier_id', '=', 'bid_modifiers.id')
                ->whereNull('bid_modifiers.deleted_at')
                ->whereNull('goal_bid_modifiers.external_id')
                ->whereNull('goal_bid_modifiers.reserve_create_at')
                ->whereNotNull('goal_bid_modifiers.dictionary_campaign_external_id')
                ->where(function (Builder $query) {
                    $query->whereNull('goal_bid_modifiers.goal_ad_group_id')
                        ->orWhere(function (Builder $query) {
                            $query->whereNotNull('goal_bid_modifiers.goal_ad_group_id')
                                ->whereNotNull('goal_bid_modifiers.goal_ad_group_external_id');
                        });
                })
                ->whereIn('goal_bid_modifiers.dictionary_campaign_id', $token->dictionaryCampaignsEnabledForExternalSynchronized->pluck('id'))
                ->groupBy([
                    'goal_bid_modifiers.dictionary_campaign_external_id',
                    'goal_bid_modifiers.goal_ad_group_external_id',
                    'bid_modifiers.mobile_adjustment',
                    'bid_modifiers.desktop_adjustment',
                ])
                ->select([
                    DB::raw('json_arrayagg(bid_modifiers.id) as ids'),
                    'goal_bid_modifiers.dictionary_campaign_external_id as dictionary_campaign_external_id',
                    'goal_bid_modifiers.goal_ad_group_external_id as goal_ad_group_external_id',
                    'bid_modifiers.mobile_adjustment as mobile_adjustment',
                    'bid_modifiers.desktop_adjustment as desktop_adjustment',
                    DB::raw('json_arrayagg(bid_modifiers.demographics_adjustment) as demographics_adjustments'),
                    DB::raw('json_arrayagg(bid_modifiers.retargeting_adjustment) as retargeting_adjustments'),
                ])
                ->get();

            foreach (array_chunk($goalBidModifiers->pluck('ids')->map(function ($ids) {
                return json_decode($ids);
            })->collapse()->toArray(), 1000) as $items) {
                GoalBidModifier::whereIn('id', $items)
                    ->update([
                        'reserve_create_at' => Carbon::now(),
                    ]);
            }


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

        return 0;
    }
}