ThumbProjectImage.php 1.6 KB
<?php

namespace App\Console\Commands\Project;

use App\Helper\Arr;
use App\Models\Product\Product;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class ThumbProjectImage extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'thumb_project_image {project_id}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '处理项目产品缩略图';

    public function handle()
    {
        $project_id = $this->argument('project_id');

        if ($project_id > 0) {
            //指定项目
            ProjectServer::useProject($project_id);

            Product::select(['id', 'thumb'])->chunk(100, function ($products) {
                foreach ($products as $product) {
                    $thumb = $product->thumb;
                    if (isset($thumb['url']) && $thumb['url']) {
                        $thumb['url'] = thumbImageByUrl($thumb['url']);
                        $product->thumb = Arr::a2s($thumb);
                        $product->save();
                    }
                }
            });

            DB::disconnect('custom_mysql');

            $this->output('project_id:' . $project_id . ' | success');
        } else {
            //TODO:所有项目
        }
    }


    /**
     * 输出处理日志
     * @param $message
     * @return bool
     */
    public function output($message)
    {
        echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
        return true;
    }
}