123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- /*
- * This file is part of the Jiannei/lumen-api-starter.
- *
- * (c) Jiannei <longjian.huang@foxmail.com>
- *
- * 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\Navigation\Organization;
- use App\Repositories\Models\User\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;
- }
- }
|