<?php

namespace app\common\api;

use app\common\model\TalentCondition;

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

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

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

    public static function edit($params) {
        $data["type"] = $params["type"];
        $data["talentLevel"] = $params["talentLevel"];
        $data["talentLevelCat"] = $params["talentLevelCat"];
        $data["companyIds"] = $params["companyIds"];
        $data["bindFileTypes"] = $params["bindFileTypes"];
        $data["name"] = $params["name"];
        $data["active"] = $params["active"];
        $data["isSalary"] = $params["isSalary"];
        $relation = [];
        foreach ($params["relation"] as $_companyId => $_relation) {
            $relation[] = $_companyId . ":" . $_relation;
        }
        $data["companyWithFileType"] = implode(";", $relation);
        if ($params["id"]) {
            $data["id"] = $params["id"];
            $data["updateUser"] = session("user")["uid"];
            $data["updateTime"] = date("Y-m-d H:i:s");
            return TalentCondition::update($data);
        } else {
            $data["createUser"] = session("user")["uid"];
            $data["createTime"] = date("Y-m-d H:i:s");
            return TalentCondition::insert($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"]];
          } */
        $where[] = ["type", "=", session("user")["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];
    }

}