ImportStudentJob.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Jobs;
  11. use App\Repositories\Models\User\Student;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Support\Facades\Storage;
  14. use Illuminate\Support\Str;
  15. class ImportStudentJob extends Job
  16. {
  17. public $timeout = 1200;
  18. private $path = '';
  19. private $disk = '';
  20. /**
  21. * Create a new job instance.
  22. */
  23. public function __construct($disk, $path)
  24. {
  25. $this->disk = $disk;
  26. $this->path = $path;
  27. }
  28. /**
  29. * Execute the job.
  30. */
  31. public function handle()
  32. {
  33. $config = ['path' => ''];
  34. $excel = new \Vtiful\Kernel\Excel($config);
  35. $excel->openFile(Storage::disk($this->disk)->path($this->path))
  36. ->openSheet()
  37. ->setSkipRows(1);
  38. while (($row = $excel->nextRow()) !== NULL) {
  39. $sex = 0;
  40. switch ($row[3]) {
  41. case "男":
  42. case 'man':
  43. $sex = 1;
  44. break;
  45. case '女':
  46. case 'women':
  47. $sex = 2;
  48. break;
  49. }
  50. if (!empty($row[1])) {
  51. if (!Student::query()->where('account', $row[2])->exists()) {
  52. $data = [
  53. 'turename' => $row[1],
  54. 'account' => $row[2],
  55. 'faculty' => $row[6],
  56. 'class' => $row[7],
  57. 'mobile' => $row[4],
  58. 'period' => $row[5],
  59. 'password' => Str::random(6),
  60. 'sex' => $sex,
  61. ];
  62. Log::error(arr2str($data));
  63. Student::query()->create($data);
  64. }
  65. }
  66. }
  67. return true;
  68. }
  69. }