<?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];
        $whr[] = ["step", "=", 3];
        $whr[] = ["type", "=", $type];
        $whr[] = ["mainId", "=", $mainId];
        return $list = TalentLog::whereOr([$where, $whr])->order("createTime desc")->select()->toArray();
    }

    public static function getPassDepts($mainId, $fst_dept_check_time = null) {
        $dept_pass_state = TalentState::DEPT_VERIFY_PASS;
        $where[] = ["type", "=", 1];
        $where[] = ["mainId", "=", $mainId];
        $where[] = ["step", "=", 3];
        if ($fst_dept_check_time) {
            $where[] = ["createTime", ">=", $fst_dept_check_time];
        }
        $dept_logs = TalentLog::where($where)->order("createTime desc")->select();
        $check_depts = [];
        $pass_depts = [];
        foreach ($dept_logs as $dept_log) {
            if (!in_array($dept_log["companyId"], $check_depts)) {
                $check_depts[] = $dept_log["companyId"];
                if ($dept_log["new_state"] == $dept_pass_state && $dept_log["active"] == 1) {
                    $pass_depts[] = $dept_log["companyId"];
                }
            }
        }
        return array_unique($pass_depts);
    }

    public static function getUnCheckDepts($mainId) {
        $where[] = ["type", "=", 1];
        $where[] = ["mainId", "=", $mainId];
        $where[] = ["active", "=", 0];
        $where[] = ["step", "=", 3];
        //$where[] = ["new_state", "in", [TalentState::DEPT_VERIFY_PASS, TalentState::FST_VERIFY_PASS]];
        return TalentLog::where($where)->column("companyId");
    }

    public static function getFstLog($mainId, $type) {
        $where = [];
        $where[] = ["mainId", "=", $mainId];
        $where[] = ["type", "=", $type];
        $where[] = ["active", "=", 1];
        $where[] = ["typeFileId", "null"];
        $log = TalentLog::where($where)->order("createTime asc")->findOrEmpty()->toArray();
        return $log;
    }

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

    public static function getCompanyNewestCheckedLog($mainId, $companyId) {
        $where = [];
        $where[] = ["mainId", "=", $mainId];
        $where[] = ["step", "=", 3];
        $where[] = ["companyId", "=", $companyId];
        //$where[] = ["active", "=", 1];
        $one = TalentLog::where($where)->order("createTime desc")->findOrEmpty();
        return $one;
    }

    public static function getLogByCompanyId($mainId, $companyId, $fst_dept_check_time) {
        $where = [];
        $where[] = ["mainId", "=", $mainId];
        $where[] = ["companyId", "=", $companyId];
        $where[] = ["createTime", ">=", $fst_dept_check_time];
        $one = TalentLog::where($where)->findOrEmpty();
        return $one;
    }

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

    public static function writeDeptLogs($mainId, $companyIds, $state = []) {
        $user = session("user");
        $last_log = self::getLastLog($mainId, 1);
        for ($i = 0; $i < count($companyIds); $i++) {
            $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"] = 1;
            $log["mainId"] = $mainId;
            $log["companyId"] = $companyIds[$i];
            $log["description"] = "等待部门审核";
            $log["step"] = 3; //部门审核阶段
            $log["createUser"] = sprintf("%s(%s)", $user["account"], $user["companyName"] ?: $user["rolename"]);
            $log["createTime"] = date("Y-m-d H:i:s");
            TalentLog::create($log);
        }
    }

    public static function write($type, $mainId, $state = [], $description = "", $active = 0, $fileType = null, $fileId = 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];
            if ($state[2]) {
                $log["step"] = $state[2];
            }
        } else {
            $log["state"] = $log["new_state"] = $state;
        }
        $log["type"] = $type;
        $log["mainId"] = $mainId;
        $log["typeFileId"] = $fileType;
        $log["fileId"] = $fileId;
        $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["companyName"] ?: $user["rolename"]);
        $data["updateTime"] = date("Y-m-d H:i:s");
        return TalentLog::update($data);
    }

}