<?php

namespace App\Console\Commands;

use App\Handlers\LoadExcelHandler;
use App\Trace;
use Illuminate\Console\Command;

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

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

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $data = (new LoadExcelHandler)->load(public_path('uploads') . '/禾美988-6w.xls');
        unset($data[0]);
        $insert = [];
        foreach ($data as $item) {
            $key = str_replace('http://code.hnhemeile.cn/key/', '', $item[3]);
            if ($key) {
                $insert[] = [
                    'key' => $item[0],
                    'group' => (int)$item[2],
                    'slug' => $key,
                    'gid' => 4,
                    'status' => 1,
                    'product_id' => 0,
                    'dealer_id' => 0
                ];
            }

            if (count($insert) >= 2000) {

                Trace::query()->insert($insert);
                $insert = [];
            }
        }
        $this->line("ok");
    }
}
