<?php

namespace app\common\api;

use app\enterprise\model\TalentTypeChange as ttcModel;
use app\enterprise\model\Talent;
use app\common\api\TalentState;
use think\facade\Db;

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

    public static function getOne($id) {
        $info = ttcModel::find($id);
        if (strlen($info["talentId"]) == 19) {
            $where = [];
            $where[] = ["card_number", "=", $info["idCard"]];
            $where[] = ["delete", "=", 0];
            $where[] = ["checkState", "=", TalentState::CERTIFICATED];
            $talentInfo = Talent::where($where)->find();
            $info["talentId"] = $talentInfo["id"];
        }
        if (strlen($info["oldIdentifyCondition"]) == 19) {
            $newIdentifyCondition = self::getNewIdentifyConditionIdFromOldId($info["oldIdentifyCondition"]);
            $info["oldIdentifyCondition"] = $newIdentifyCondition["id"];
        }
        if (strlen($info["newIdentifyCondition"]) == 19) {
            $newIdentifyCondition = self::getNewIdentifyConditionIdFromOldId($info["newIdentifyCondition"]);
            $info["newIdentifyCondition"] = $newIdentifyCondition["id"];
        }
        return $info;
    }

    public static function getHospitalExamineList($request) {
        $user = session("user");

        $order = trim($request->param("order")) ?: "desc";
        $offset = trim($request->param("offset")) ?: 0;
        $limit = trim($request->param("limit")) ?: 10;
        $talentName = trim($request->param("talentName"));
        $idCard = trim($request->param("idCard"));
        $enterpriseId = trim($request->param("enterpriseName"));
        $oldTalentArrange = trim($request->param("oldTalentArrange"));
        $newTalentArrange = trim($request->param("newTalentArrange"));
        $checkState = trim($request->param("checkState"));
        $oldIdentifyCondition = trim($request->param("oldIdentifyCondition"));
        $newIdentifyCondition = trim($request->param("newIdentifyCondition"));
        $oldIdentifyMonth = trim($request->param("oldIdentifyMonth"));
        $newIdentifyMonth = trim($request->param("newIdentifyMonth"));
        $oldYear = trim($request->param("oldYear"));
        $newYear = trim($request->param("newYear"));
        $where = [];
        $where[] = ["ttc.delete", "=", 0];
        $where[] = ["ttc.type", "=", 5];
        $where[] = ["ttc.createTime", ">=", "2022-10-01 00:00:00"];
        $where[] = ["e.medicalCommunityId", "=", $user["medicalCommunityId"]];
        $where[] = ["e.isGeneral", "=", 2];
        if ($talentName) {
            $where[] = ["talentName", "like", "%" . $talentName . "%"];
        }
        if ($idCard) {
            $where[] = ["ttc.idCard", "like", "%" . $idCard . "%"];
        }
        if ($enterpriseId) {
            $where[] = ["enterpriseId", "=", $enterpriseId];
        }
        if ($oldTalentArrange) {
            $where[] = ["oldTalentArrange", "=", $oldTalentArrange];
        }
        if ($newTalentArrange) {
            $where[] = ["newTalentArrange", "=", $newTalentArrange];
        }
        if ($oldIdentifyCondition) {
            $where[] = ["oldIdentifyCondition", "=", $oldIdentifyCondition];
        }
        if ($newIdentifyCondition) {
            $where[] = ["newIdentifyCondition", "=", $newIdentifyCondition];
        }
        if ($checkState) {
            $where[] = ["ttc.checkState", "=", $checkState];
        }
        if ($oldIdentifyMonth) {
            $where[] = ["oldIdentifyMonth", "like", "%" . $oldIdentifyMonth . "%"];
        }
        if ($newIdentifyMonth) {
            $where[] = ["newIdentifyMonth", "like", "%" . $newIdentifyMonth . "%"];
        }
        if ($oldYear) {
            $where[] = ["oldYear", "like", "%" . $oldYear . "%"];
        }
        if ($newYear) {
            $where[] = ["newYear", "like", "%" . $newYear . "%"];
        }
        $count = ttcModel::alias("ttc")->leftJoin("un_enterprise e", "e.id=ttc.enterpriseId")->where($where)->count();
        $list = ttcModel::alias("ttc")->field("ttc.*")->leftJoin("un_enterprise e", "e.id=ttc.enterpriseId")->where($where)->limit($offset, $limit)->order("ttc.createTime " . $order)->select()->toArray();
        $talentArangeList = DictApi::selectByParentCode("talent_arrange"); //人才层次

        foreach ($list as $k => $v) {
            $list[$k]["oldTalentArrangeName"] = array_key_exists($v["oldTalentArrange"], $talentArangeList) ? $talentArangeList[$v["oldTalentArrange"]] : "";
            $list[$k]["newTalentArrangeName"] = array_key_exists($v["newTalentArrange"], $talentArangeList) ? $talentArangeList[$v["newTalentArrange"]] : "";
            if (strlen($v["oldIdentifyCondition"]) == 19) {
                $condition = Db::table("un_identify_condition")->find($v["oldIdentifyCondition"]);
                $list[$k]["oldIdentifyConditionCH"] = $condition["name"];
            } else if ($v["oldIdentifyCondition"]) {
                $condition = TalentConditionApi::getOne($v["oldIdentifyCondition"]);
                $list[$k]["oldIdentifyConditionCH"] = $condition["name"];
            }
            if (strlen($v["newIdentifyCondition"]) == 19) {
                $condition = Db::table("un_identify_condition")->find($v["newIdentifyCondition"]);
                $list[$k]["newIdentifyConditionCH"] = $condition["name"];
            } else if ($v["newIdentifyCondition"]) {
                $condition = TalentConditionApi::getOne($v["newIdentifyCondition"]);
                $list[$k]["newIdentifyConditionCH"] = $condition["name"];
            }
        }

        return ["total" => $count, "rows" => $list];
    }

    public static function getNewIdentifyConditionIdFromOldId($oldIdentifyConditionId) {
        $oldIdentifyCondition = Db::table("un_identify_condition")->find($oldIdentifyConditionId);
        $where = [];
        $where[] = ["type", "=", $oldIdentifyCondition["type"]];
        $where[] = ["name", "=", $oldIdentifyCondition["name"]];
        $newIdentifyCondtion = Db::table("new_talent_condition")->where($where)->find();
        return $newIdentifyCondtion;
    }

    private static function getSuperPrivsForDeptVerify() {
        $config = getJsonConfig("../sys_config.json", "super_privs_for_talent_type_change_dept_verify");
        return $config;
    }

    public static function chkUserInSuperDeptUsers() {
        $config = self::getSuperPrivsForDeptVerify();
        $companyId = session("user")["companyId"];
        $company = getCacheById("Company", $companyId);
        $account = session("user")["account"];
        return in_array($company["code"], $config["companys"]) || in_array($account, $config["users"]);
    }

}
