<?php

namespace app\admin\controller;

use app\admin\common\AdminController;
use think\facade\Db;

class DirectlyIdentify extends AdminController{

    public function index(){
        return view("", []);
    }

    public function list(){
        $list = Db::table("new_talent_zhiren")->order('createTime','desc')->select();
        return ["total" => count($list), "rows" => $list];
    }

    public function enterprise_import(){
        $response_obj = new \StdClass();
        if (!$this->request->file()){
            $response_obj->code = 500;
            $response_obj->msg = '没有选择文件';
            return \StrUtil::back($response_obj, "DirectlyIdentify.callBack");
        }

        $excel = $this->request->file("file");

//        if (!isExcelFile($excel->getMime())){
//            $response_obj->code = 500;
//            $response_obj->msg = '不是正确的Excel文件，该文件类型为：'.$excel->getMime();
//            return \StrUtil::back($response_obj, "DirectlyIdentify.callBack");
//        }

        $savename = \think\facade\Filesystem::disk("public")->putFile("uploadexcel", $excel);

        $zhiren_data = [
            'task_name' => date("Y-m-d",time()) . "-导入机构数据",
            'task_file' => $savename,
            'task_status' => -1,
            'createTime' => date("Y-m-d H:i:s",time()),
            'updateTime' => date("Y-m-d H:i:s",time())
        ];

        $id = Db::table("new_talent_zhiren")->insertGetId($zhiren_data);

        queue("app\job\Import", ["id" => $id,"type" => 1]);

        $response_obj = new \StdClass();
        $response_obj->code = 200;
        $response_obj->msg = '导入成功';
        return \StrUtil::back($response_obj, "DirectlyIdentify.callBack");
        //echo sprintf('<script>console.log(DirectlyIdentify);DirectlyIdentify.callBack(%s);</script>', json_encode($data));
    }

    public function talent_import(){
        $response_obj = new \StdClass();
        if (!$this->request->file()){
            $response_obj->code = 500;
            $response_obj->msg = '没有选择文件';
            return \StrUtil::back($response_obj, "DirectlyIdentify.callBack");
        }

        $excel = $this->request->file("file");
        $type = $this->request['type'];

//        if (!isExcelFile($excel->getMime())){
//            $response_obj->code = 500;
//            $response_obj->msg = '不是正确的Excel文件，该文件类型为：'.$excel->getMime();
//            return \StrUtil::back($response_obj, "DirectlyIdentify.callBack");
//        }

        $savename = \think\facade\Filesystem::disk("public")->putFile("uploadexcel", $excel);

        if($type == 2){
            //导入省级人才数据
            $zhiren_data = [
                'task_name' => date("Y-m-d",time()) . "-导入省级人才数据",
                'task_file' => $savename,
                'task_status' => -1,
                'createTime' => date("Y-m-d H:i:s",time()),
                'updateTime' => date("Y-m-d H:i:s",time())
            ];
            $id = Db::table("new_talent_zhiren")->insertGetId($zhiren_data);

            queue("app\job\Import", ["id" => $id,"type" => 2]);

        }else{
            //导入泉州人才数据
            $zhiren_data = [
                'task_name' => date("Y-m-d",time()) . "-导入泉州人才数据",
                'task_file' => $savename,
                'task_status' => -1,
                'createTime' => date("Y-m-d H:i:s",time()),
                'updateTime' => date("Y-m-d H:i:s",time())
            ];
            $id = Db::table("new_talent_zhiren")->insertGetId($zhiren_data);

            queue("app\job\Import", ["id" => $id,"type" => 3]);
        }

        $response_obj->code = 200;
        $response_obj->msg = '导入成功';
        return \StrUtil::back($response_obj, "DirectlyIdentify.callBack");
    }

    /**
     * 泉州人才信息导入
     */
    public function qz_talent_import(){
        $response_obj = new \StdClass();
        if (!$this->request->file()){
            $response_obj->code = 500;
            $response_obj->msg = '没有选择文件';
            return \StrUtil::back($response_obj, "DirectlyIdentify.callBack");
        }

        $excel = $this->request->file("file");
        $savename = \think\facade\Filesystem::disk("public")->putFile("uploadexcel", $excel);
        $zhiren_data = [
            'task_name' => date("Y-m-d",time()) . "-导入泉州人才数据",
            'task_file' => $savename,
            'task_status' => -1,
            'createTime' => date("Y-m-d H:i:s",time()),
            'updateTime' => date("Y-m-d H:i:s",time())
        ];
        $id = Db::table("new_talent_zhiren")->insertGetId($zhiren_data);

        queue("app\job\Import", ["id" => $id,"type" => 2]);

        $response_obj->code = 200;
        $response_obj->msg = '导入成功';
        return \StrUtil::back($response_obj, "DirectlyIdentify.callBack");

    }
}