<?php

namespace app\common\api;

use app\common\model\TalentLog;

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

    public static function getList($type, $mainId, $active = 1) {
        $where[] = ["type", "=", $type];
        $where[] = ["mainId", "=", $mainId];
        $where[] = ["active", "=", $active];
        return $list = TalentLog::where($where)->order("createTime desc")->select()->toArray();
    }

    public static function getLastLog($mainId, $type, $companyId = 0) {
        $where = [];
        $where[] = ["mainId", "=", $mainId];
        $where[] = ["type", "=", $type];
        $where[] = ["typeFileId", "null"];
        if ($companyId) {
            $where[] = ["companyId", "=", $companyId];
        }
        $last_log = TalentLog::where($where)->order("createTime desc")->findOrEmpty()->toArray();
        return $last_log;
    }

    public static function getListLogByTime($id, $time) {
        $where = [];
        $where[] = ["mainId", "=", $id];
        $where[] = ["createTime", ">=", $time];
        $list = TalentLog::where($where)->order("createTime desc")->select()->toArray();
        return $list;
    }

    public static function write($type, $mainId, $state = [], $description = "", $active = 0, $fileType = null) {
        $user = session("user");
        $last_log = self::getLastLog($mainId, $type);
        $log["last_state"] = $last_log["state"] ?: 0;
        $log["id"] = getStringId();
        if (is_array($state)) {
            $log["state"] = $state[0];
            $log["new_state"] = $state[1];
        } else {
            $log["state"] = $log["new_state"] = $state;
        }
        $log["type"] = $type;
        $log["mainId"] = $mainId;
        $log["typeFileId"] = $fileType;
        $log["companyId"] = $user["companyId"];
        $log["active"] = $active;
        $log["description"] = $description;
        $log["createUser"] = sprintf("%s(%s)", $user["account"], $user["companyName"] ?: $user["rolename"]);
        $log["createTime"] = date("Y-m-d H:i:s");
        return TalentLog::create($log);
    }

    public static function rewrite($id, $state = [], $description = "", $active = 0) {
        $user = session("user");
        if (is_array($state)) {
            $log["state"] = $state[0];
            $log["new_state"] = $state[1];
        } else {
            $log["state"] = $log["new_state"] = $state;
        }
        $log["id"] = $id;
        $log["companyId"] = $user["companyId"];
        $log["active"] = $active;
        $log["description"] = $description;
        $log["updateUser"] = sprintf("%s(%s)", $user["account"], $user["companyName"] ?: $user["rolename"]);
        $log["updateTime"] = date("Y-m-d H:i:s");
        return TalentLog::update($log);
    }

    public static function setActive($id, $value) {
        $user = session("user");
        $data["id"] = $id;
        $data["active"] = $value;
        $data["updateUser"] = sprintf("%s(%s)", $user["account"], $user["rolename"]);
        $data["updateTime"] = date("Y-m-d H:i:s");
        return TalentLog::update($data);
    }

}
