<?php

namespace App\Console\Commands;

use App\Facades\UploadService;
use App\Models\ServiceUser;
use Illuminate\Console\Command;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class MakeWorkUserQrcode extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'qrcode:bindWork';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        ServiceUser::whereNull('bind_work_qrcode')->chunk(100, function ($users){
            foreach ($users as $user) {
                $url = config('app.url').'/api/h5/user/work/auth?service_user_id='.$user->id;
                $name = time().uniqid().'work_qrcode.png';
                $path = storage_path("qrcode/$name");
                QrCode::format('png')->margin(1)->size(300)->generate($url, $path);
                $pic = '';
                if (is_file($path)) {
                    $pic = UploadService::uploadFile($path);
                    unlink($path);
                }
                ServiceUser::where('id', $user->id)->update(['bind_work_qrcode'=>$pic]);
            }
        });
        return Command::SUCCESS;
    }
}
