<?php

namespace app\common\api;

use app\enterprise\model\Talent;
use app\common\api\DictApi;
use app\admin\model\Enterprise;
use think\facade\Db;

/**
 * Description of VerifyApi
 *
 * @author sgq
 */
class VerifyApi {

    public static function getTalentInfoById($id) {
        $where = [];
        $where[] = ["id", "=", $id];
        $info = Talent::findOrEmpty($id)->toArray();
        if ($info) {
            if ($info["talent_type"]) {
                $info["talentTypeName"] = DictApi::selectByParentCode("talent_type")[$info["talent_type"]];
            }
            if ($info["nationality"]) {
                $info["nationalityName"] = DictApi::selectByParentCode("nationality")[$info["nationality"]];
            }
            if ($info["nation"]) {
                $info["nationName"] = DictApi::selectByParentCode("nation")[$info["nation"]];
            }
            if ($info["politics"]) {
                $info["politicsName"] = DictApi::selectByParentCode("politics")[$info["politics"]];
            }
            if ($info["province"]) {
                $info["provinceName"] = Db::table("un_common_location")->where("code", "=", $info["province"])->findOrEmpty()["name"];
            }
            if ($info["city"]) {
                $info["cityName"] = Db::table("un_common_location")->where("code", "=", $info["city"])->findOrEmpty()["name"];
            }
            if ($info["county"]) {
                $info["countyName"] = Db::table("un_common_location")->where("code", "=", $info["county"])->findOrEmpty()["name"];
            }
            $enterprise = Enterprise::findOrEmpty($info["enterprise_id"])->toArray();
            $info["enterpriseName"] = $enterprise["name"];
            if ($enterprise["street"]) {
                $info["street"] = $enterprise["street"];
                $info["streetName"] = DictApi::selectByParentCode("street")[$enterprise["street"]];
            }
            if ($enterprise["industryFieldNew"]) {
                $info["industryFieldName"] = DictApi::selectByParentCode("industry_field")[$enterprise["industryFieldNew"]];
            }
            if ($enterprise["enterpriseTag"]) {
                $info["enterpriseTagName"] = DictApi::selectByParentCode("enterprise_tag")[$enterprise["enterpriseTag"]];
            }
            if ($info["headimgurl"]) {
                $info["headimgurl"] = "/storage/" . $info["headimgurl"];
            }

            if ($info["talent_arrange"]) {
                $info["talentArrangeName"] = DictApi::selectByParentCode("talent_arrange")[$info["talent_arrange"]];
            }
            if ($info["import_way"]) {
                $info["importWayName"] = DictApi::selectByParentCode("import_way")[$info["import_way"]];
            }
            if ($info["source"]) {
                $info["sourceName"] = DictApi::selectByParentCode("source")[$info["source"]];
            }
            if ($info["highest_degree"]) {
                $info["highestDegreeName"] = DictApi::selectByParentCode("highest_degree")[$info["highest_degree"]];
            }
            if ($info["talent_condition"]) {
                $info["talentConditionName"] = \app\common\model\TalentCondition::findOrEmpty($info["talent_condition"])["name"];
            }
        }
        return $info;
    }

    public static function getOne($id) {
        return Talent::findOrEmpty($id);
    }

    public static function getList($request) {
        $where = [];
        $order = trim($request->param("order")) ?: "desc";
        $offset = trim($request->param("offset")) ?: 0;
        $limit = trim($request->param("limit")) ?: 10;

        $process = $request->param("process");
        switch ($process) {
            case 1:
                $where[] = ["ti.checkState", "=", 3];
                break;
            case 2:
                $where[] = ["ti.checkState", "=", 6];
                break;
            case 3:
                $where[] = ["ti.checkState", "=", 7];
                break;
            case 4:
                $where[] = ["ti.checkState", "=", 6];
                break;
        }
        $enterprise_tag_kvs = DictApi::selectByParentCode("enterprise_tag");
        $count = Talent::alias("ti")->leftJoin("new_enterprise e", "e.id=ti.enterprise_id")->where($where)->count();
        $list = Talent::alias("ti")->leftJoin("new_enterprise e", "e.id=ti.enterprise_id")
                ->where($where)->limit($offset, $limit)->order("ti.createTime " . $order)->field("ti.*,e.agentName,e.type as enterprise_type,enterpriseTag")->select()->toArray();
        foreach ($list as &$item) {
            $item["enterprise_name"] = $item["agentName"];
            $item["talent_type"] = $item["enterprise_type"] == 1 ? "晋江优秀人才" : "集成电路优秀人才";
            $item["enterprise_tag"] = $enterprise_tag_kvs[$item["enterpriseTag"]];
        }unset($item);
        return ["total" => $count, "rows" => $list];
    }

}
