ImportStudentJob.php 4.5 KB

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