ImportAdminJob.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\Enums\Base\TaskMakeStatusEnum;
  13. use App\Repositories\Models\Base\Admin;
  14. use App\Repositories\Models\Base\Department;
  15. use App\Repositories\Models\Base\Role;
  16. use App\Repositories\Models\Base\Task;
  17. use Illuminate\Support\Facades\Storage;
  18. class ImportAdminJob extends Job
  19. {
  20. public $timeout = 1200;
  21. private $path = '';
  22. private $disk = '';
  23. private $resource = '';
  24. private $login_admin_id = '';
  25. /**
  26. * Create a new job instance.
  27. */
  28. public function __construct($resource, $login_admin_id)
  29. {
  30. $this->resource = $resource;
  31. $this->disk = $resource['disk'];
  32. $this->path = $resource['path'];
  33. $this->login_admin_id = $login_admin_id;
  34. }
  35. /**
  36. * Execute the job.
  37. */
  38. public function handle()
  39. {
  40. $task = Task::importTask($this->resource, Admin::class, $this->login_admin_id);
  41. if (!Storage::disk($this->disk)->exists($this->path)) {
  42. $task->make_status = TaskMakeStatusEnum::FAIL;
  43. $task->result = '找不到文件';
  44. $task->save();
  45. return false;
  46. }
  47. try {
  48. $config = ['path' => ''];
  49. $excel = new \Vtiful\Kernel\Excel($config);
  50. $excel->openFile(Storage::disk($this->disk)->path($this->path))
  51. ->openSheet()
  52. ->setSkipRows(1);
  53. } catch (\Exception $exception) {
  54. $task->make_status = TaskMakeStatusEnum::FAIL;
  55. $task->result = '读取文件失败:' . $exception->getMessage();
  56. $task->save();
  57. return false;
  58. }
  59. $msg = '';
  60. try {
  61. //处理数据
  62. $total = 0;
  63. $r_total = 0;
  64. $w_total = 0;
  65. $errAccount = [];
  66. while (($row = $excel->nextRow()) !== NULL) {
  67. list(
  68. $name,
  69. $mobile,
  70. $email,
  71. $class_name,
  72. $role_name,
  73. ) = $row;
  74. if ($mobile && $mobile) {
  75. $total++;
  76. $account = rk($mobile);
  77. if ($mobile) $data['mobile'] = rk($mobile);
  78. if ($email) $data['email'] = rk($email);
  79. if ($name) $data['name'] = rk($name);
  80. $data['username'] = $account;
  81. $data['department_id'] = 0;
  82. if ($class_name) {
  83. $class_name = rk($class_name);
  84. $grade_id = Department::byNameGetId($class_name, true);
  85. $data['department_id'] = $grade_id;
  86. }
  87. $model = Admin::query()->where('account', $account)->first();
  88. if ($model) {
  89. if (count($data)) {
  90. $model->fill($data);
  91. $model->save();
  92. $w_total++;
  93. }
  94. } else {
  95. $model = Admin::query()->create($data);
  96. }
  97. if ($role_name) {
  98. $role_id = Role::query()->where('name', $role_name)->value('id') ?? 0;
  99. if ($role_id) $model->syncRoles([$role_id]);
  100. }
  101. $r_total++;
  102. }
  103. }
  104. if (count($errAccount)) $msg = "工号错误:" . arr2str($errAccount);
  105. $task->make_status = TaskMakeStatusEnum::OK;
  106. $task->result = "共{$total}条,读取{$r_total}条,导入{$w_total}条。{$msg}";
  107. $task->save();
  108. return true;
  109. } catch (\Exception $exception) {
  110. $task->make_status = TaskMakeStatusEnum::FAIL;
  111. $task->result = '异常错误:' . $exception->getMessage();
  112. $task->save();
  113. return false;
  114. }
  115. }
  116. }