<?php

namespace app\enterprise\api;

use app\enterprise\model\Talent;
use app\common\api\DictApi;

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

    /**
     * 判断是否可以编辑
     * @param type $id
     * @return boolean
     */
    public static function checkIsEditable($id) {
        $info = Talent::findOrEmpty($id);
        if (!$info || !in_array($info["checkState"], [0, 1, 3, 5]))
            return false;
        return true;
    }

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

    public static function getList($request) {
        $order = trim($request->param("order")) ?: "desc";
        $offset = trim($request->param("offset")) ?: 0;
        $limit = trim($request->param("limit")) ?: 10;
        $name = trim($request->param("name"));
        $idCard = trim($request->param("card_number"));
        $sex = trim($request->param("sex"));
        $nation = trim($request->param("nation"));
        $nationality = trim($request->param("nationality"));
        $talentArrange = trim($request->param("talent_arrange"));
        $checkState = trim($request->param("checkState"));

        $where = [];
        $where[] = ["delete", "=", 0];
        if (session("user")["usertype"] == 2) {
            $where[] = ["enterprise_id", "=", session("user")["uid"]];
        }
        if ($name) {
            $where[] = ["name", "like", "%" . $name . "%"];
        }
        if ($idCard) {
            $where[] = ["card_number", "like", "%" . $idCard . "%"];
        }
        if ($sex) {
            $where[] = ["sex", $sex];
        }
        if ($nation) {
            $where[] = ["nation", $nation];
        }
        if ($nationality) {
            $where[] = ["nationality", $nationality];
        }
        if ($talentArrange) {
            $where[] = ["talent_arrange", $talentArrange];
        }
        if ($checkState) {
            switch ($checkState) {
                case -1:
                    $where[] = [["checkState", $checkState], ["isPublic", ">=", 5]];
                    break;
                case 1:
                    $where[] = [["checkState", $checkState]];
                    break;
                case 2:
                    $where[] = [["checkState", "in", "5,10"]];
                    break;
                case 3:
                    $where[] = [["checkState", "not in", "1,5,10"], ["isPublic", "<", 5]];
                    break;
                case 4:
                    $where[] = [["checkState", 35], ["isPublic", ">=", 5]];
                    break;
            }
        }
        if ($request->param("type") == 2) {
            $where[] = [["checkState", 35], ["isPublic", 6]];
        }
        $count = Talent::where($where)->count();
        $list = Talent::where($where)->limit($offset, $limit)->order("createTime " . $order)->select()->toArray();
        $talentTypeList = DictApi::selectByParentCode("enterprise_tag"); //人才标签
        $talentArangeList = DictApi::selectByParentCode("talent_arrange"); //人才层次
        //$talentTypeNameList = DictApi::selectByParentCode("un_talentLevel");//认定条件
        //DictApi::selectByParentCode($code);
        foreach ($list as $key => $item) {
            $list[$key]["talentArrangeName"] = isset($talentArangeList[$item["talent_arrange"]]) ? $talentArangeList[$item["talent_arrange"]] : "";
            $list[$key]["identifyConditionText"] = "";
        }
        return ["total" => $count, "rows" => $list];
    }

}
