ImportTaskJob.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\LibraryBook;
  4. use App\Models\LibraryCategory;
  5. use App\Models\LibraryTask;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Support\Facades\Storage;
  14. class ImportTaskJob implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. protected $tid = 0;
  18. /**
  19. * Create a new job instance.
  20. *
  21. * @return void
  22. */
  23. public function __construct($tid)
  24. {
  25. $this->tid = $tid;
  26. }
  27. /**
  28. * Execute the job.
  29. *
  30. * @return void
  31. */
  32. public function handle()
  33. {
  34. $this->import();
  35. }
  36. /**
  37. * 导入数据
  38. * @return false|void
  39. */
  40. public function import()
  41. {
  42. $task = LibraryTask::query()->find($this->tid);
  43. $task->status = LibraryTask::STATUS_IN;
  44. $task->save();
  45. $path = Storage::disk(config('admin.upload.disk'))->path($task->path);
  46. if (!file_exists($path)) {
  47. $task->status = LibraryTask::STATUS_FAIL;
  48. $task->message = '找不到文件';
  49. $task->save();
  50. Log::error('*************找不到文件***************');
  51. Log::error($path);
  52. return false;
  53. }
  54. try {
  55. $config = ['path' => ''];
  56. $excel = new \Vtiful\Kernel\Excel($config);
  57. $excel->openFile($path)
  58. ->openSheet()
  59. ->setSkipRows(1);
  60. //处理数据
  61. $data = [];
  62. Log::error('*************开始读取数据*****************');
  63. while (($row = $excel->nextRow()) !== NULL) {
  64. $nums = count($row);
  65. if ($nums == 6) {
  66. list(
  67. $xh,
  68. $type_name,
  69. $source,
  70. $name,
  71. $link,
  72. $cover_path,
  73. ) = $row;
  74. $type = array_search($type_name, LibraryCategory::resourceTypeMaps);
  75. if (!$type) {
  76. Log::error("*************类型不对{$type_name}*****************");
  77. continue;
  78. }
  79. $data[] = [
  80. 'xh' => $xh,
  81. 'type' => $type,
  82. 'source' => $source,
  83. 'name' => $name,
  84. 'link' => $link,
  85. 'cover_path' => $cover_path,
  86. ];
  87. }
  88. }
  89. Log::error("数据读取完成");
  90. $n = count($data);
  91. Log::error("开始写入数据:共{$n}条");
  92. //数据写入
  93. DB::beginTransaction();
  94. $total_nums = 0;
  95. foreach ($data as $i => $item) {
  96. $book = LibraryBook::query()->where('category_id', $task->category_id)->where('name', $item['name'])->where('resource_type', $item['type'])->first();
  97. if ($book) {
  98. $book->fill([
  99. 'category_id' => $task->category_id,
  100. 'name' => $item['name'],
  101. 'resource_type' => $item['type'],
  102. 'source' => $item['source'],
  103. 'body' => $item['link'],
  104. 'cover_path' => $item['cover_path']]
  105. );
  106. $book->save();
  107. continue;
  108. }
  109. LibraryBook::query()->create([
  110. 'name' => $item['name'],
  111. 'category_id' => $task->category_id,
  112. 'resource_type' => $item['type'],
  113. 'source' => $item['source'],
  114. 'body' => $item['link'],
  115. 'cover_path' => $item['cover_path'],
  116. 'task_id' => $task->id,
  117. ]);
  118. $total_nums++;
  119. }
  120. DB::commit();
  121. Log::error("-----------完成---------------");
  122. $task->status = LibraryTask::STATUS_SUCCESS;
  123. $task->message = "共读取{$n}条,共导入{$total_nums}条。";
  124. $task->save();
  125. } catch (\Exception $exception) {
  126. DB::rollBack();
  127. $task->status = LibraryTask::STATUS_FAIL;
  128. $task->message = $exception->getMessage();
  129. $task->save();
  130. Log::error($exception);
  131. }
  132. }
  133. }