<?php

namespace app\enterprise\api;

use app\enterprise\model\Talent;
use app\common\api\DictApi;
use app\common\api\TalentConditionApi;
use app\common\api\TalentLogApi;
use app\common\api\TalentState;

/**
 * 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, TalentState::FST_SAVE, TalentState::BASE_REVERIFY_PASS, TalentState::SCND_SAVE]))
            return false;
        return true;
    }

    static public function chkIsOwner($id, $uid) {
        $info = self::getOne($id);
        if ($info["enterprise_id"] != $uid)
            return null;
        return $info;
    }

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

    public static function getList($request, $step = 0) {
        $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"));
        $type = session("user")["type"];
        $where = [];
        $where[] = ["delete", "=", 0];
        if (session("user")["usertype"] == 2) {
            $where[] = ["enterprise_id", "=", session("user")["uid"]];
        }
        //$where[] = ["isImport", "=", $request->param("import") ?: 0];
        if ($step) {
            switch ($step) {
                case 1:
                    $where[] = ["checkState", "in", [TalentState::FST_SAVE, TalentState::BASE_VERIFY_PASS, TalentState::BASE_REVERIFY_REJECT, TalentState::BASE_REVERIFY_FAIL, TalentState::BASE_REJECT, TalentState::FST_SUBMIT, TalentState::BASE_VERIFY_FAIL]];
                    switch ($checkState) {
                        case -1:
                            $where[] = ["checkState", "in", [TalentState::BASE_VERIFY_FAIL, TalentState::BASE_REVERIFY_FAIL]];
                            break;
                        case 1:
                            $where[] = ["checkState", "=", TalentState::FST_SAVE];
                            break;
                        case 2:
                            $where[] = ["checkState", "in", [TalentState::BASE_REJECT, TalentState::BASE_REVERIFY_REJECT]];
                            break;
                        case 3:
                            $where[] = ["checkState", "in", [TalentState::FST_SUBMIT, TalentState::BASE_VERIFY_PASS]];
                            break;
                    }
                    break;
                case 2:
                    $where[] = ["checkState", "not in", [TalentState::FST_SAVE, TalentState::BASE_VERIFY_PASS, TalentState::BASE_REVERIFY_REJECT, TalentState::BASE_REVERIFY_FAIL, TalentState::BASE_REJECT, TalentState::FST_SUBMIT, TalentState::BASE_VERIFY_FAIL]];
                    switch ($checkState) {
                        case -1:
                            $where[] = ["checkState", "in", [TalentState::FST_VERIFY_FAIL, TalentState::REVERIFY_FAIL, TalentState::ZX_FAIL, TalentState::ANNOUNCED_REVERIFY_FAIL, TalentState::PUBLISH_FAIL]];
                            break;
                        case 1:
                            $where[] = ["checkState", "=", TalentState::SCND_SAVE];
                            break;
                        case 2:
                            $where[] = ["checkState", "in", [TalentState::FST_VERIFY_REJECT, TalentState::DEPT_VERIFY_REJECT, TalentState::REVERIFY_REJECT]];
                            break;
                        case 3:
                            $where[] = ["checkState", "in", [TalentState::SCND_SUBMIT, TalentState::FST_VERIFY_PASS, TalentState::DEPT_VERIFY_PASS, TalentState::REVERIFY_PASS, TalentState::ZX_PASS,
                                    TalentState::ANNOUNCED_REVERIFY_PASS, TalentState::PUBLISH_PASS]];
                            break;
                        case 4:
                            $where[] = ["checkState", "=", TalentState::CERTIFICATED];
                            break;
                    }
            }
            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];
            }
            $count = Talent::where($where)->count();
            $list = Talent::where($where)->limit($offset, $limit)->order("createTime " . $order)->select()->toArray();
            $talentTagList = DictApi::selectByParentCode("enterprise_tag"); //单位标签
            $talentArangeList = DictApi::selectByParentCode("talent_arrange"); //人才层次
            $industries = DictApi::selectByParentCode("industry_field"); //产业
            $enterprise = \app\common\model\Enterprise::find(session("user")["uid"]);
            foreach ($list as $key => $item) {
                $condition = TalentConditionApi::getOne($item["talent_condition"]);
                $list[$key]["talentArrangeName"] = isset($talentArangeList[$item["talent_arrange"]]) ? $talentArangeList[$item["talent_arrange"]] : "";
                $list[$key]["identifyConditionText"] = $condition["name"];
                if (!$item["isImport"]) {
                    $list[$key]["companyIds"] = $condition["companyIds"];
                }
                $list[$key]["type"] = $enterprise["type"];
                $list[$key]["enterpriseName"] = $enterprise["name"];
                $list[$key]["industryName"] = $industries[$enterprise["industryFieldNew"]];
                $list[$key]["enterpriseTagName"] = $talentTagList[$enterprise["enterpriseTag"]];
                $last_log = TalentLogApi::getLastLog($item["id"], 1, 0, ["active", "=", 1]);
                $list[$key]["real_state"] = $last_log["state"];
                $list[$key]["last_state"] = $last_log["last_state"];
            }
            return ["total" => $count, "rows" => $list];
        }
    }

}
