123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?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\School;
- use App\Jobs\Job;
- use App\Repositories\Enums\Base\TaskMakeStatusEnum;
- use App\Repositories\Models\Base\Department;
- use App\Repositories\Models\Base\Task;
- use App\Repositories\Models\School\Teacher;
- use Illuminate\Support\Facades\Storage;
- class ImportTeacherJob 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, Teacher::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['department_id'] = 0;
- if ($class_name) {
- $grade_id = Department::byNameGetId($class_name, true);
- $data['department_id'] = $grade_id;
- }
- $teacher = Teacher::query()->where('account', $account)->first();
- if ($teacher) {
- if (count($data)) {
- $teacher->fill($data);
- $teacher->save();
- $w_total++;
- }
- } else {
- $w_total++;
- Teacher::query()->create($data);
- }
- $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;
- }
- }
- }
|