ImportTeacherJob.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Base\Admin;
  12. use App\Repositories\Models\Mentor\Teacher;
  13. use Illuminate\Support\Facades\Log;
  14. use Illuminate\Support\Facades\Storage;
  15. class ImportTeacherJob 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. list(
  40. $account,
  41. $truename,
  42. $mobile,
  43. $sex_index,
  44. ) = $row;
  45. if (($account && $truename && $mobile)) {
  46. $sex = 0;
  47. switch ($sex_index) {
  48. case "男":
  49. case 'man':
  50. $sex = 1;
  51. break;
  52. case '女':
  53. case 'women':
  54. $sex = 2;
  55. break;
  56. }
  57. $data = [
  58. 'truename' => $truename,
  59. 'account' => $account,
  60. 'mobile' => $mobile,
  61. 'password' => $account,
  62. 'sex' => $sex,
  63. ];
  64. Log::warning($account);
  65. if (!Admin::query()->where('account', $account)->exists()) {
  66. Teacher::query()->create($data);
  67. }
  68. }
  69. }
  70. return true;
  71. }
  72. }