<?php

namespace app\admin\controller;

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

/**
 * Description of TalentCondition
 *
 * @author sgq
 */
class TalentCondition extends AdminController {

    /**
     * @auth {{/identifyCondition}}
     * @return type
     */
    function index() {
        return view();
    }

    /**
     * @auth {{/identifyCondition/list}}
     * @return type
     */
    function list() {
        $result = TalentConditionApi::getListByCondition($this->request->param());
        return json($result);
    }

    /**
     * @auth {{/identifyCondition/add}}
     * @return type
     */
    function add() {
        if ($this->request->isPost()) {
            TalentConditionApi::edit($this->request->param());
            return json(["code" => 200, "msg" => "添加认定条件成功"]);
        }
        return view();
    }

    /**
     * @auth {{/identifyCondition/update}}
     * @return type
     */
    function edit() {
        if ($this->request->isPost()) {
            TalentConditionApi::edit($this->request->param());
            return json(["code" => 200, "msg" => "编辑认定条件成功"]);
        }
        $id = $this->request->param("id");
        $info = TalentConditionApi::getOne($id);
        return view("", ["info" => $info]);
    }

    /**
     * @auth {{/identifyCondition/delete}}
     * @return type
     */
    function delete() {
        $id = $this->request->param("id");
        TalentConditionApi::delete($id);
        return json(["code" => 200, "msg" => "删除成功"]);
    }

    /**
     * @auth {{/identifyCondition/import}}
     */
    function import() {
        ignore_user_abort(true);
        set_time_limit(0);
        if (!$this->request->file())
            return json(["msg" => "没有选择文件"]);
        $excel = $this->request->file("file");

        if (!isExcelFile($excel->getMime()))
            return json(["msg" => "不是正确的Excel文件"]);
        $mapping = [
            0 => "type",
            1 => "talentLevel",
            2 => "name",
            3 => "activeYear",
            4 => "description"
        ];

        $path = $excel->getRealPath();
        $datas = getExcelDatas($path);
        $datas = array_slice($datas, 1); //去标题
        $inserts = [];
        while ($row = array_shift($datas)) {
            $cols = count($row);
            $companyIds = [];
            $new = [];
            for ($i = 0; $i < $cols; $i++) {
                if ($i < count($mapping)) {
                    $new[$mapping[$i]] = $row[$i];
                } else {
                    $companyIds[] = $row[$i];
                }
            }
            $new["companyIds"] = $companyIds ? implode(",", $companyIds) : null;
            $new["createTime"] = date("Y-m-d H:i:s");
            $inserts[] = $new;
        }
        $chunks = array_chunk($inserts, 200);
        foreach ($chunks as $chunk) {
            Db::table("new_talent_condition")->insertAll($chunk);
        }
        $data = ["code" => 200, "msg" => "导入成功"];
        echo sprintf('<script>parent.IdentifyCondition.callBack(%s);</script>', json_encode($data));
    }

}
