BidModifiersUpdate.php 3.11 KB
<?php

namespace App\Console\Commands;

use App\Models\Pivots\GoalBidModifier;
use App\Models\Tokens;
use App\Service\Requests\Direct\SetBidModifiers;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

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

    /**
     * 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::allowedLimit()->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')
                ->whereNotNull('goal_bid_modifiers.external_id')
                ->whereNotNull('goal_bid_modifiers.external_id')
                ->whereNotNull('goal_bid_modifiers.updated_need')
                ->whereNull('goal_bid_modifiers.reserve_update_at')
                ->select([
                    'goal_bid_modifiers.external_id as external_id',
                    DB::raw(
                        'CASE' .
                        ' WHEN bid_modifiers.mobile_adjustment IS NOT NULL AND JSON_VALID(bid_modifiers.mobile_adjustment) THEN bid_modifiers.mobile_adjustment->"$.BidModifier"' .
                        ' WHEN bid_modifiers.desktop_adjustment IS NOT NULL AND JSON_VALID(bid_modifiers.desktop_adjustment) THEN bid_modifiers.desktop_adjustment->"$.BidModifier"' .
                        ' WHEN bid_modifiers.demographics_adjustment IS NOT NULL AND JSON_VALID(bid_modifiers.demographics_adjustment) THEN bid_modifiers.demographics_adjustment->"$.BidModifier"' .
                        ' WHEN bid_modifiers.retargeting_adjustment IS NOT NULL AND JSON_VALID(bid_modifiers.retargeting_adjustment) THEN bid_modifiers.retargeting_adjustment->"$.BidModifier"' .
                        ' END as bidModifierValue'
                    )
                ])
                ->get();

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

            foreach (array_chunk($goalBidModifiers->pluck('external_id')->toArray(), 100) as $items) {
                GoalBidModifier::whereIn('external_id', $items)
                    ->update([
                        'reserve_update_at' => Carbon::now(),
                    ]);
            }


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

        return 0;
    }
}