<?php

namespace app\common\api;

use app\common\model\TalentCondition;

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

    public static function getList($lv, $type) {
        $where[] = ["talentLevel", "=", $lv];
        $where[] = ["type", "=", $type];
        $where[] = ["active", "=", 1];
        $where[] = ["delete", "=", 0];
        return TalentCondition::where($where)->select()->toArray();
    }

    public static function getOne($id) {
        return TalentCondition::findOrEmpty($id)->toArray();
    }

    public static function edit($params) {
        if ($params["id"]) {
            $data["id"] = $params["id"];
        }
        $data["type"] = $params["type"];
        $data["talentLevel"] = $params["talentLevel"];
        $data["companyIds"] = $params["companyIds"];
        $data["name"] = $params["name"];
        $data["active"] = $params["active"];
        return TalentCondition::update($data);
    }

    public static function delete($id) {
        $data["id"] = $id;
        $data["delete"] = 1;
        $data["updateUser"] = session("user")["uid"];
        $data["updateTime"] = date("Y-m-d H:i:s");
        return TalentCondition::update($data);
    }

    public static function getListByCondition($params) {
        $order = $params["order"] ?: "desc";
        $offset = $params["offset"] ?: 0;
        $limit = $params["limit"] ?: 10;
        $talent_arrange_kvs = DictApi::selectByParentCode("talent_arrange");
        $companys = \app\common\model\Company::column("name", "id");
        $where[] = ["delete", "=", 0];
        if ($params["name"]) {
            $where[] = ["name", "like", "%" . $params["name"] . "%"];
        }
        if ($params["type"]) {
            $where[] = ["type", "=", $params["type"]];
        }
        if ($params["talentLevel"]) {
            $where[] = ["talentLevel", "=", $params["talentLevel"]];
        }
        if ($params["active"]) {
            $where[] = ["active", "=", $params["active"]];
        }
        $count = TalentCondition::where($where)->count();
        $list = TalentCondition::where($where)->limit($offset, $limit)->order("createTime " . $order)->select()->toArray();
        foreach ($list as &$item) {
            $item["talentLevel"] = $talent_arrange_kvs[$item["talentLevel"]];
            $company_ids = array_filter(explode(",", $item["companyIds"]));
            $tmp_companys = [];
            for ($i = 0; $i < count($company_ids); $i++) {
                $tmp_companys[] = $companys[$company_ids[$i]];
            }
            $item["companyNames"] = implode("，", $tmp_companys);
        }unset($item);
        return ["total" => $count, "rows" => $list];
    }

}
