<?php

namespace App\Console\Commands;

use App\Import;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '数据导入到数据库中';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $info = Import::where('id', $this->argument('id'))->first();
        $category = collect(Import::$CATEGORY)->where('id', $info->category)->first();
        $fun = $category['function'] ?? 'import';
        $re = (new $category['model'])->{$fun}(unserialize($info['data']), $category);
        if ($re) {
            $info->is_import = 'T';
            $info->import_time = date('Y-m-d H:i:s');
            $info->save();
        }
    }
}
