* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace App\Jobs\School; use App\Jobs\Job; use App\Repositories\Enums\Base\TaskMakeStatusEnum; use App\Repositories\Models\Base\Task; use App\Repositories\Models\School\Grade; use App\Repositories\Models\School\Student; use Illuminate\Support\Facades\Storage; class ImportStudentJob extends Job { public $timeout = 1200; private $path = ''; private $disk = ''; private $resource = ''; private $login_admin_id = ''; /** * Create a new job instance. */ public function __construct($resource, $login_admin_id) { $this->resource = $resource; $this->disk = $resource['disk']; $this->path = $resource['path']; $this->login_admin_id = $login_admin_id; } /** * Execute the job. */ public function handle() { $task = Task::importTask($this->resource, Student::class, $this->login_admin_id); if (!Storage::disk($this->disk)->exists($this->path)) { $task->make_status = TaskMakeStatusEnum::FAIL; $task->result = '找不到文件'; $task->save(); return false; } try { $config = ['path' => '']; $excel = new \Vtiful\Kernel\Excel($config); $excel->openFile(Storage::disk($this->disk)->path($this->path)) ->openSheet() ->setSkipRows(1); } catch (\Exception $exception) { $task->make_status = TaskMakeStatusEnum::FAIL; $task->result = '读取文件失败:' . $exception->getMessage(); $task->save(); return false; } $msg = []; try { //处理数据 $total = 0; $r_total = 0; $w_total = 0; while (($row = $excel->nextRow()) !== NULL) { if (count($row) < 6) continue; list( $account, $truename, $class_name, $mobile, $email, $sex_index, ) = $row; $account = rk($account); $truename = rk($truename); $class_name = rk($class_name); $mobile = rk($mobile); $email = rk($email); $sex_index = rk($sex_index); if (!$account) continue; if (!$class_name) continue; $total++; if ($account) $data['account'] = $account; if ($truename) $data['name'] = $truename; if ($mobile) $data['mobile'] = $mobile; if ($email) $data['email'] = $email; if ($sex_index) { $sex = 0; switch ($sex_index) { case "男": case 'man': $sex = 1; break; case '女': case 'women': $sex = 2; break; } $data['sex'] = $sex; } $data['grade_id'] = 0; if ($class_name) { $grade_id = Grade::byNameGetId($class_name, true); $data['grade_id'] = $grade_id; } $student = Student::query()->where('account', $account)->first(); if ($student) { if (count($data)) { $student->fill($data); $student->save(); $w_total++; } } else { Student::query()->create($data); $w_total++; } $r_total++; } $msg = arr2str($msg, ';'); $task->make_status = TaskMakeStatusEnum::OK; $task->result = "共{$total}条,读取{$r_total}条,导入{$w_total}条。{$msg}"; $task->save(); return true; } catch (\Exception $exception) { $task->make_status = TaskMakeStatusEnum::FAIL; $task->result = '异常错误:' . $exception->getMessage(); $task->save(); return false; } } }