1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?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\Models\Base\Admin;
- use App\Repositories\Models\School\Student;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Storage;
- class ImportStudentJob 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) {
- list(
- $account,
- $name,
- $mobile,
- $email,
- $sex_index,
- $class,
- $admission_date,
- ) = $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()) {
- $data = [
- 'name' => $name,
- 'account' => $account,
- 'class' => $class,
- 'mobile' => $mobile,
- // 'password' => Str::random(6),
- 'password' => $account,
- 'email' => $email,
- 'sex' => $sex,
- ];
- if (empty($admission_date)) {
- $t = Carbon::parse($admission_date);
- $data['admission_date'] = $t->toDateString();
- $data['year'] = $t->format('Y');
- }
- Student::query()->create($data);
- }
- }
- }
- return true;
- }
- }
|