VCardsLoad.php 1.6 KB
<?php

namespace App\Console\Commands;

use App\Models\Tokens;
use App\Service\Requests\Direct\GetVCards;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

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

    /**
     * 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
     * @throws \Exception
     */
    public function handle()
    {
        $token = Tokens::where('type', Tokens::MAIN)->first();

        if (!$token) {
            throw new \Exception('Не найден токен блин');
        }

        $v_card_external_ids = DB::table('advertisements')
            ->leftJoin('v_cards', 'v_cards.external_id', '=', 'advertisements.v_card_external_id')
            ->whereNotNull('advertisements.v_card_external_id')
            ->whereNull('v_cards.id')
            ->groupBy('advertisements.v_card_external_id')
            ->pluck('advertisements.v_card_external_id');

        if (!$v_card_external_ids->count()) {
            return 0;
        }

        $request = new GetVCards();
        $request->setToken($token)
            ->call([
                'Ids' => $v_card_external_ids->toArray(),
            ]);

        return 0;
    }
}