123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- /*
- * This file is part of the Jiannei/lumen-api-starter.
- *
- * (c) Jiannei <longjian.huang@foxmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace App\Jobs;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Enums\Navigation\TypeEnum;
- use App\Repositories\Enums\Navigation\VisitTypeEnum;
- use App\Repositories\Models\Navigation\Category;
- use App\Repositories\Models\Navigation\Website;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Storage;
- class ImportWebsitesJob extends Job
- {
- // public $timeout = 24 * 60 * 60;
- private $path = '';
- private $disk = '';
- private $organization_id = 0;
- /**
- * Create a new job instance.
- */
- public function __construct($disk, $path, $organization_id = 0)
- {
- $this->disk = $disk;
- $this->path = $path;
- $this->organization_id = $organization_id;
- }
- /**
- * 确定任务应该超时的时间
- *
- * @return \DateTime
- */
- public function retryUntil()
- {
- return Carbon::now()->addHours(8);
- }
- /**
- * Execute the job.
- */
- public function handle()
- {
- $disk = $this->disk;
- $path = $this->path;
- $organization_id = $this->organization_id;
- $config = ['path' => ''];
- $excel = new \Vtiful\Kernel\Excel($config);
- $excel->openFile(Storage::disk($disk)->path($path))
- ->openSheet()
- ->setSkipRows(1);
- $i = 1;
- $header = [
- '序号',
- '一级分类',
- '二级分类',
- '网站名称',
- '网址',
- '网站简介',
- '网站图标地址',
- ];
- $rows = [];
- $categories = [];
- while (($row = $excel->nextRow()) !== NULL) {
- $rows[] = $row;
- list(
- $id,
- $category_one,
- $category_two,
- $name,
- $url,
- $des,
- $logo_url,
- ) = $row;
- $img_type = last(str2arr($logo_url, '.'));
- $filepath = null;
- if (!empty($logo_url)) {
- try {
- $client = new \GuzzleHttp\Client();
- $data = $client->request('get', $logo_url)->getBody()->getContents();
- $filepath = 'websites' . DIRECTORY_SEPARATOR . $name . '.' . $img_type;
- Storage::disk('public')->put($filepath, $data);
- } catch (\Exception $e) {
- $filepath = null;
- }
- }
- $one = Category::query()->firstOrCreate([
- 'name' => $category_one,
- 'pid' => 0,
- 'type' => TypeEnum::ORGANIZATION,
- 'organization_id' => $organization_id,
- ], [
- 'visit_type' => VisitTypeEnum::COMMON,
- 'status' => ModelStatusEnum::OK
- ]);
- $two = Category::query()->firstOrCreate([
- 'name' => $category_two,
- 'pid' => $one->id,
- 'type' => TypeEnum::ORGANIZATION,
- 'organization_id' => $organization_id,
- ], [
- 'visit_type' => VisitTypeEnum::COMMON,
- 'status' => ModelStatusEnum::OK
- ]);
- Website::query()->updateOrCreate([
- 'name' => $name,
- 'category_id' => $two->id,
- 'organization_id' => $organization_id,
- 'url' => $url,
- 'type' => TypeEnum::ORGANIZATION,
- 'status' => ModelStatusEnum::OK,
- ], [
- 'description' => $des,
- 'logo' => $filepath
- ]);
- // $this->line($name);
- // $this->line($logo_url);
- $i++;
- }
- //排序
- $categories = Category::query()->where('pid', 0)->where('organization_id', $organization_id)->orderBy('id')->pluck('id');
- $nums = count($categories);
- //一级
- foreach ($categories as $cid) {
- Category::query()->where('id', $cid)->update(['sort' => $nums]);
- $nums--;
- $ids = Category::query()->where('organization_id', $organization_id)->where('pid', $cid)->orderBy('id')->pluck('id');
- $nums_t = count($ids);
- foreach ($ids as $id) {
- Category::query()->where('id', $id)->update(['sort' => $nums_t]);
- $nums_t--;
- $ids_w = Website::query()->where('organization_id', $organization_id)->where('category_id', $id)->orderBy('id')->pluck('id');
- $nums_w = count($ids_w);
- foreach ($ids_w as $item) {
- Website::query()->where('id', $item)->update(['sort' => $nums_w]);
- $nums_w--;
- }
- }
- }
- return true;
- }
- }
|