ImportAdminJob.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\Base;
  11. use App\Jobs\Job;
  12. use App\Repositories\Models\Base\Admin;
  13. use App\Repositories\Models\Base\Department;
  14. use App\Repositories\Models\School\Student;
  15. use Carbon\Carbon;
  16. use Illuminate\Support\Facades\Hash;
  17. use Illuminate\Support\Facades\Log;
  18. use Illuminate\Support\Facades\Storage;
  19. class ImportAdminJob extends Job
  20. {
  21. public $timeout = 1200;
  22. private $path = '';
  23. private $disk = '';
  24. /**
  25. * Create a new job instance.
  26. */
  27. public function __construct($disk, $path)
  28. {
  29. $this->disk = $disk;
  30. $this->path = $path;
  31. }
  32. /**
  33. * Execute the job.
  34. */
  35. public function handle()
  36. {
  37. $config = ['path' => ''];
  38. $excel = new \Vtiful\Kernel\Excel($config);
  39. $excel->openFile(Storage::disk($this->disk)->path($this->path))
  40. ->openSheet()
  41. ->setSkipRows(1);
  42. while (($row = $excel->nextRow()) !== NULL) {
  43. Log::error("数据:" . php2js($row));
  44. list(
  45. $account,
  46. $name,
  47. $mobile,
  48. $email,
  49. // $sex_index,
  50. $department_name,
  51. $role_name,
  52. ) = $row;
  53. // $sex = 0;
  54. // switch ($sex_index) {
  55. // case "男":
  56. // case 'man':
  57. // $sex = 1;
  58. // break;
  59. // case '女':
  60. // case 'women':
  61. // $sex = 2;
  62. // break;
  63. // }
  64. if (!empty($account)) {
  65. if (!Admin::query()->where('username', $account)->exists()) {
  66. $department_id = 0;
  67. if (!empty($department_name)) {
  68. $department = Department::query()->firstOrCreate(['name' => $department_name], []);
  69. $department_id = $department->id;
  70. }
  71. $data = [
  72. 'name' => $name,
  73. 'username' => $account,
  74. 'mobile' => $mobile,
  75. 'password' => Hash::make($account),
  76. 'email' => $email,
  77. // 'sex' => $sex,
  78. 'department_id' => $department_id,
  79. ];
  80. $model = Admin::query()->create($data);
  81. if (in_array($role_name, ['管理员', '外系教师', '本系教师', '实验室维护员', '院系审核员'])) {
  82. $role = false;
  83. switch ($role_name) {
  84. case '管理员';
  85. $role = 'admin';
  86. break;
  87. case '外系教师';
  88. $role = 'outside_teacher';
  89. break;
  90. case '本系教师';
  91. $role = 'teacher';
  92. break;
  93. case '实验室维护员';
  94. $role = 'maintain';
  95. break;
  96. case '院系审核员';
  97. $role = 'faculty_administrator';
  98. break;
  99. }
  100. if ($role) $model->syncRoles([$role]);
  101. }
  102. }
  103. }
  104. }
  105. return true;
  106. }
  107. }