ResetData.php 4.65 KB
<?php

namespace App\Console\Commands;

use App\Models\Ad_employer;
use App\Models\Ad_jobs;
use App\Models\ad_response;
use App\Models\Answer;
use App\Models\Chat;
use App\Models\Contacts;
use App\Models\Dop_info;
use App\Models\Employer;
use App\Models\EmployerAutoliftOption;
use App\Models\employers_main;
use App\Models\Flot;
use App\Models\Group_user;
use App\Models\Group_works;
use App\Models\Like_vacancy;
use App\Models\Like_worker;
use App\Models\Media;
use App\Models\Message;
use App\Models\MessagesRequests;
use App\Models\PrevCompany;
use App\Models\ResponseWork;
use App\Models\Static_ad;
use App\Models\Static_worker;
use App\Models\Title_worker;
use App\Models\User;
use App\Models\WorkerAutoliftOption;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $users = User::query()
            ->whereHas('workers')
            ->where('is_bd', 0)
            ->where('created_at', '<', '2024-11-30 00:00:00')
            ->whereNotIn('email', [
                'vkontakte@rekamore.su',
                '2anketa@rekamore.su'
            ])
            ->get();

        foreach ($users as $user) {
            Answer::query()->where(['user_id' => $user->id])->delete();

            Chat::query()->where(['user_id' => $user->id])->delete();
            Chat::query()->where(['to_user_id' => $user->id])->delete();
            Message::query()->where(['user_id' => $user->id])->delete();
            Message::query()->where(['to_user_id' => $user->id])->delete();
            MessagesRequests::query()->where(['user_id' => $user->id])->delete();

            Group_user::query()->where(['user_id' => $user->id])->get()->map(function ($groupUser) {
                Group_works::query()->where(['group_user_id' => $groupUser->id])->delete();
                $groupUser->delete();
            });

            Like_vacancy::query()->where(['user_id' => (string)$user->id])->delete();
            Like_worker::query()->where(['user_id' => (string)$user->id])->delete();
            Media::query()->where(['user_id' => $user->id])->delete();
            Static_worker::query()->where(['user_id' => $user->id])->delete();

//            $user->employers()->get()->map(function ($employer) {
//                Answer::query()->where(['employer_id' => $employer->id])->delete();
//
//                Ad_employer::query()->where(['employer_id' => $employer->id])->get()->map(function ($adEmployer) {
//                    Ad_jobs::query()->where(['ad_employer_id' => $adEmployer->id])->delete();
//                    ad_response::query()->where(['ad_employer_id' => $adEmployer->id])->delete();
//                    Static_ad::query()->where(['ad_employer_id' => $adEmployer->id])->delete();
//                    Like_vacancy::query()->where(['code_record' => $adEmployer->id])->delete();
//
//                    $adEmployer->delete();
//                });
//
//                Contacts::query()->where(['employer_id' => $employer->id])->delete();
//                employers_main::query()->where(['employer_id' => $employer->id])->delete();
//                EmployerAutoliftOption::query()->where(['employer_id' => $employer->id])->delete();
//
//                Flot::query()->where(['employer_id' => $employer->id])->delete();
//
//                $employer->delete();
//            });

            $user->workers()->get()->map(function ($worker) {
                Dop_info::query()->where(['worker_id' => $worker->id])->delete();
                PrevCompany::query()->where(['worker_id' => $worker->id])->delete();
                ResponseWork::query()->where(['worker_id' => $worker->id])->delete();
                Like_worker::query()->where(['code_record' => $worker->id])->delete();
                Title_worker::query()->where(['worker_id' => $worker->id])->delete();
                WorkerAutoliftOption::query()->where(['worker_id' => $worker->id])->delete();
                $worker->delete();
            });

            $user->delete();

        }

        User::query()->where('password', 1234567890)->get()->map(function ($userBd) {
            $password = $userBd->password;

            $userBd->update([
                'password' => Hash::make($password),
                'pubpassword' => base64_encode($password),
            ]);
        });

        return Command::SUCCESS;
    }
}