<?php

namespace App\Console\Commands;

use App\Imports\Order\chatOrder;
use App\Imports\Order\nurseOrder;
use App\Imports\Order\phoneOrder;
use App\Imports\Order\serviceOrder;
use App\Imports\Order\vaccineOrder;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;
class ImportOrder extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'importOrder {type} {filepath}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单数据导入 importOrder {订单类型} {导入xls文件名}';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $type = $this->argument('type');
        $file_path = $this->argument('filepath');

        //路径相对于项目根目录即 /public
        if(empty($type) || empty($file_path)){
            $this->output->error('请输入完整参数');
        }
        //图文咨询订单导入
        switch ($type){
            case 'chat';
                $this->makeChat($file_path);
            break;
            case 'phone';
                $this->makePhone($file_path);
            break;
            case 'vaccine';
                $this->makeVaccine($file_path);
            break;
            case 'nurse';
                $this->makeNurse($file_path);
            break;
            case 'service';
                $this->makeService($file_path);
            break;
            default;
                $this->output->error('请输入正确参数');
            break;
        }
    }

    //图文订单
    public function makeChat($file_path)
    {
        $filePath = './public/import/'.$file_path.'.xls';
        if(!file_exists($filePath)){
            $this->output->error('文件不存在');
            exit;
        }
        Excel::import(new chatOrder(), $filePath);
        $this->output->success('Import successful');
    }

    //电话订单
    public function makePhone($file_path)
    {
        $filePath = './public/import/'.$file_path.'.xls';
        if(!file_exists($filePath)){
            $this->output->error('文件不存在');
            exit;
        }
        Excel::import(new phoneOrder(), $filePath);
        $this->output->success('Import successful');
    }

    //疫苗订单
    public function makeVaccine($file_path)
    {
        $filePath = './public/import/'.$file_path.'.xls';
        if(!file_exists($filePath)){
            $this->output->error('文件不存在');
            exit;
        }
        Excel::import(new vaccineOrder(), $filePath);
        $this->output->success('Import successful');
    }

    //儿保订单
    public function makeNurse($file_path)
    {
        $filePath = './public/import/'.$file_path.'.xls';
        if(!file_exists($filePath)){
            $this->output->error('文件不存在');
            exit;
        }
        Excel::import(new nurseOrder(), $filePath);
        $this->output->success('Import successful');
    }



    //服务包订单
    public function makeService($file_path)
    {
        $filePath = './public/import/'.$file_path.'.xls';
        if(!file_exists($filePath)){
            $this->output->error('文件不存在');
            exit;
        }
        Excel::import(new serviceOrder(), $filePath);
        $this->output->success('Import successful');
    }
}
