123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?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\Base;
- use App\Jobs\Job;
- use App\Repositories\Models\Base\Admin;
- use App\Repositories\Models\Base\Department;
- use App\Repositories\Models\School\Student;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- class ImportAdminJob extends Job
- {
- public $timeout = 1200;
- private $path = '';
- private $disk = '';
- /**
- * Create a new job instance.
- */
- public function __construct($disk, $path)
- {
- $this->disk = $disk;
- $this->path = $path;
- }
- /**
- * 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) {
- Log::error("数据:" . php2js($row));
- list(
- $account,
- $name,
- $mobile,
- $email,
- // $sex_index,
- $department_name,
- $role_name,
- ) = $row;
- // $sex = 0;
- // switch ($sex_index) {
- // case "男":
- // case 'man':
- // $sex = 1;
- // break;
- // case '女':
- // case 'women':
- // $sex = 2;
- // break;
- // }
- if (!empty($account)) {
- if (!Admin::query()->where('username', $account)->exists()) {
- $department_id = 0;
- if (!empty($department_name)) {
- $department = Department::query()->firstOrCreate(['name' => $department_name], []);
- $department_id = $department->id;
- }
- $data = [
- 'name' => $name,
- 'username' => $account,
- 'mobile' => $mobile,
- 'password' => Hash::make($account),
- 'email' => $email,
- // 'sex' => $sex,
- 'department_id' => $department_id,
- ];
- $model = Admin::query()->create($data);
- if (in_array($role_name, ['管理员', '外系教师', '本系教师', '实验室维护员', '院系审核员'])) {
- $role = false;
- switch ($role_name) {
- case '管理员';
- $role = 'admin';
- break;
- case '外系教师';
- $role = 'outside_teacher';
- break;
- case '本系教师';
- $role = 'teacher';
- break;
- case '实验室维护员';
- $role = 'maintain';
- break;
- case '院系审核员';
- $role = 'faculty_administrator';
- break;
- }
- if ($role) $model->syncRoles([$role]);
- }
- }
- }
- }
- return true;
- }
- }
|