ImportWebsitesJob.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\Enums\ModelStatusEnum;
  12. use App\Repositories\Enums\Navigation\TypeEnum;
  13. use App\Repositories\Enums\Navigation\VisitTypeEnum;
  14. use App\Repositories\Models\Navigation\Category;
  15. use App\Repositories\Models\Navigation\Website;
  16. use Carbon\Carbon;
  17. use Illuminate\Support\Facades\Storage;
  18. class ImportWebsitesJob extends Job
  19. {
  20. // public $timeout = 24 * 60 * 60;
  21. private $path = '';
  22. private $disk = '';
  23. private $organization_id = 0;
  24. /**
  25. * Create a new job instance.
  26. */
  27. public function __construct($disk, $path, $organization_id = 0)
  28. {
  29. $this->disk = $disk;
  30. $this->path = $path;
  31. $this->organization_id = $organization_id;
  32. }
  33. /**
  34. * 确定任务应该超时的时间
  35. *
  36. * @return \DateTime
  37. */
  38. public function retryUntil()
  39. {
  40. return Carbon::now()->addHours(8);
  41. }
  42. /**
  43. * Execute the job.
  44. */
  45. public function handle()
  46. {
  47. $disk = $this->disk;
  48. $path = $this->path;
  49. $organization_id = $this->organization_id;
  50. $config = ['path' => ''];
  51. $excel = new \Vtiful\Kernel\Excel($config);
  52. $excel->openFile(Storage::disk($disk)->path($path))
  53. ->openSheet()
  54. ->setSkipRows(1);
  55. $i = 1;
  56. $header = [
  57. '序号',
  58. '一级分类',
  59. '二级分类',
  60. '网站名称',
  61. '网址',
  62. '网站简介',
  63. '网站图标地址',
  64. ];
  65. $rows = [];
  66. $categories = [];
  67. while (($row = $excel->nextRow()) !== NULL) {
  68. $rows[] = $row;
  69. list(
  70. $id,
  71. $category_one,
  72. $category_two,
  73. $name,
  74. $url,
  75. $des,
  76. $logo_url,
  77. ) = $row;
  78. $img_type = last(str2arr($logo_url, '.'));
  79. $filepath = null;
  80. if (!empty($logo_url)) {
  81. try {
  82. $client = new \GuzzleHttp\Client();
  83. $data = $client->request('get', $logo_url)->getBody()->getContents();
  84. $filepath = 'websites' . DIRECTORY_SEPARATOR . $name . '.' . $img_type;
  85. Storage::disk('public')->put($filepath, $data);
  86. } catch (\Exception $e) {
  87. $filepath = null;
  88. }
  89. }
  90. $one = Category::query()->firstOrCreate([
  91. 'name' => $category_one,
  92. 'pid' => 0,
  93. 'type' => TypeEnum::ORGANIZATION,
  94. 'organization_id' => $organization_id,
  95. ], [
  96. 'visit_type' => VisitTypeEnum::COMMON,
  97. 'status' => ModelStatusEnum::OK
  98. ]);
  99. $two = Category::query()->firstOrCreate([
  100. 'name' => $category_two,
  101. 'pid' => $one->id,
  102. 'type' => TypeEnum::ORGANIZATION,
  103. 'organization_id' => $organization_id,
  104. ], [
  105. 'visit_type' => VisitTypeEnum::COMMON,
  106. 'status' => ModelStatusEnum::OK
  107. ]);
  108. Website::query()->updateOrCreate([
  109. 'name' => $name,
  110. 'category_id' => $two->id,
  111. 'organization_id' => $organization_id,
  112. 'url' => $url,
  113. 'type' => TypeEnum::ORGANIZATION,
  114. 'status' => ModelStatusEnum::OK,
  115. ], [
  116. 'description' => $des,
  117. 'logo' => $filepath
  118. ]);
  119. // $this->line($name);
  120. // $this->line($logo_url);
  121. $i++;
  122. }
  123. //排序
  124. $categories = Category::query()->where('pid', 0)->where('organization_id', $organization_id)->orderBy('id')->pluck('id');
  125. $nums = count($categories);
  126. //一级
  127. foreach ($categories as $cid) {
  128. Category::query()->where('id', $cid)->update(['sort' => $nums]);
  129. $nums--;
  130. $ids = Category::query()->where('organization_id', $organization_id)->where('pid', $cid)->orderBy('id')->pluck('id');
  131. $nums_t = count($ids);
  132. foreach ($ids as $id) {
  133. Category::query()->where('id', $id)->update(['sort' => $nums_t]);
  134. $nums_t--;
  135. $ids_w = Website::query()->where('organization_id', $organization_id)->where('category_id', $id)->orderBy('id')->pluck('id');
  136. $nums_w = count($ids_w);
  137. foreach ($ids_w as $item) {
  138. Website::query()->where('id', $item)->update(['sort' => $nums_w]);
  139. $nums_w--;
  140. }
  141. }
  142. }
  143. return true;
  144. }
  145. }