* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace App\Jobs; use App\Repositories\Enums\ModelStatusEnum; use App\Repositories\Models\Base\User; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Storage; class ImportUserJob extends Job { public $timeout = 1200; private $path = ''; private $disk = ''; private $role = 0; /** * Create a new job instance. */ public function __construct($disk, $path, $role = 0) { $this->disk = $disk; $this->path = $path; $this->role = $role; } /** * Execute the job. */ public function handle() { $config = ['path' => '']; $excel = new \Vtiful\Kernel\Excel($config); $excel->openFile(Storage::disk($this->disk)->path($this->path)) ->openSheet() ->setSkipRows(1); while (($row = $excel->nextRow()) !== NULL) { list( $index, $mobile, $organization, $name, $sex_name, $password ) = $row; if ($mobile) { $mobile = trim($mobile); $organization = trim($organization); $organization_id = Organization::query()->where('name', $organization)->value('id'); if (!User::query()->where('username', $mobile)->exists() && $organization_id) { $data['username'] = $mobile; $data['mobile'] = $mobile; $data['name'] = $name; $data['organization_id'] = $organization_id; if (empty($name)) { $data['name'] = '圆圈导航-' . rand(1000, 9999); } if (isset($sex_name)) { $sex = 0; switch ($sex_name) { case "男": case 'man': $sex = 1; break; case '女': case 'women': $sex = 2; break; } $data['sex'] = $sex; } if (isset($password)) { $data['password'] = Hash::make(trim($password)); } else { $data['password'] = Hash::make($data['username']); } $data['status'] = ModelStatusEnum::OK; User::query()->create($data); } } } return true; } }