<?php

namespace App\Console\Commands;

use App\Good;
use App\Trace;
use Carbon\Carbon;
use Vinkla\Hashids\Facades\Hashids;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class GenerateKey extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generate:key {gid}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成key';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->line('开始生成码');

        $gid = $this->argument('gid');
        $good = Good::where('id', $gid)->first();
        $good->is_key = 'D';
        $good->save();

        Trace::where('gid', $gid)->where('slug', '=', null)->delete();
        $keyNum = Trace::where('gid', $gid)->count();

        $jishu = $good->group != 0 ? $good->group : $good->number;
        $leijia = 0;

        //生成key
        if (($num = ($good->number - $keyNum)) > 0) {
            $zNum = floor($num / 1000);
            $yNum = $num % 1000;
            $i = 0;

            while ($i < $zNum) {
                $data = [];
                for ($j = 0; $j < 1000; $j++) {
                    $key = $good->skey . $this->num_random($good->bit);

                    if ($good->slug_type) {
                        //数字
                        $slug = $this->num_random($good->slug_num);
                    } else {
                        //字符
                        $slug = str_random($good->slug_num);
                    }
                    $data[] = [
                        'gid' => $gid,
                        'key' => $key,
                        'group' => floor($leijia / $jishu),
                        'slug' => $good->data . $good->higher_key . $slug,
                        'created_at' => Carbon::now(),
                        'updated_at' => Carbon::now(),
                    ];
                    $leijia++;
                }
                Trace::insert($data);
                $i++;
            }

            for ($m = 0; $m < $yNum; $m++) {
                if ($good->slug_type) {
                    //数字
                    $slug = $this->num_random($good->slug_num);
                } else {
                    //字符
                    $slug = str_random($good->slug_num);
                }
                Trace::create([
                    'gid' => $gid,
                    'key' => $good->skey . $this->num_random($good->bit),
                    'group' => intval($leijia / $jishu),
                    'slug' => $good->data . $good->higher_key . $slug,
                ]);
                $leijia++;
            }

        }

        $keyNum = Trace::where('gid', $gid)->count();
        if ($keyNum < $good->number) {
            sleep(10);
            Artisan::call("generate:key", ['gid' => $good->id]);
        } else {
            $this->line('码生成完毕');
//            $ids = Trace::whereNull('slug')->pluck('id');
//            foreach ($ids as $id) {
//                Trace::where('id', $id)->update(['slug' => $this->num_random(21)]);
//            }
            $good->trace_count = $keyNum;
            $good->is_key = 'T';
            $good->save();
            Artisan::call("export:traces", ['gid' => $good->id]);

        }
    }


    private function num_random($len = 16)
    {
        $num = '';
        for ($i = 0; $i < $len; $i++) {
            $num .= rand(0, 9);
        }
        return $num;
    }
}
