ImportStudentJob.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\School;
  11. use App\Jobs\Job;
  12. use App\Repositories\Models\Base\Admin;
  13. use App\Repositories\Models\School\Student;
  14. use Carbon\Carbon;
  15. use Illuminate\Support\Facades\Storage;
  16. class ImportStudentJob extends Job
  17. {
  18. public $timeout = 1200;
  19. private $path = '';
  20. private $disk = '';
  21. /**
  22. * Create a new job instance.
  23. */
  24. public function __construct($disk, $path)
  25. {
  26. $this->disk = $disk;
  27. $this->path = $path;
  28. }
  29. /**
  30. * Execute the job.
  31. */
  32. public function handle()
  33. {
  34. $config = ['path' => ''];
  35. $excel = new \Vtiful\Kernel\Excel($config);
  36. $excel->openFile(Storage::disk($this->disk)->path($this->path))
  37. ->openSheet()
  38. ->setSkipRows(1);
  39. while (($row = $excel->nextRow()) !== NULL) {
  40. list(
  41. $account,
  42. $name,
  43. $mobile,
  44. $email,
  45. $sex_index,
  46. $class,
  47. $admission_date,
  48. ) = $row;
  49. $sex = 0;
  50. switch ($sex_index) {
  51. case "男":
  52. case 'man':
  53. $sex = 1;
  54. break;
  55. case '女':
  56. case 'women':
  57. $sex = 2;
  58. break;
  59. }
  60. if (!empty($account)) {
  61. if (!Admin::query()->where('username', $account)->exists()) {
  62. $data = [
  63. 'name' => $name,
  64. 'account' => $account,
  65. 'class' => $class,
  66. 'mobile' => $mobile,
  67. // 'password' => Str::random(6),
  68. 'password' => $account,
  69. 'email' => $email,
  70. 'sex' => $sex,
  71. ];
  72. if (empty($admission_date)) {
  73. $t = Carbon::parse($admission_date);
  74. $data['admission_date'] = $t->toDateString();
  75. $data['year'] = $t->format('Y');
  76. }
  77. Student::query()->create($data);
  78. }
  79. }
  80. }
  81. return true;
  82. }
  83. }