<?php

namespace App\Jobs;

use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class CleanData implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $phone;

    public function __construct($phone)
    {
        //
        $this->phone = $phone;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        $phone = $this->phone;
        $cnt = User::query()->where('type', User::OWNER)->where('phone', $phone)->count();
        if ($cnt > 1) {
            $exclude = User::query()->where('type', User::OWNER)->where('phone', $phone)->first();
            if ($exclude) {
                User::query()->where('type', User::OWNER)
                    ->where('phone', $phone)
                    ->where('id', '<>', $exclude->id)
                    ->delete();
            }
        }
    }
}
