<?php

namespace app\models\chat;

use think\Model;

/**
 * 客服状态配置模型
 * Class ChatAdminStatus
 * @package app\models\chat
 */
class ChatAdminStatus extends Model
{
    protected $table = 'cg_chat_admin_status';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    /**
     * 获取客服配置
     */
    public static function getByAdminId($adminId)
    {
        $status = self::where('admin_id', $adminId)->find();
        return $status ? $status->toArray() : null;
    }

    /**
     * 获取或创建客服配置
     */
    public static function getOrCreate($adminId)
    {
        $status = self::getByAdminId($adminId);
        if ($status === null) {
            $data = [
                'admin_id' => $adminId,
                'max_sessions' => 10,
                'is_enabled' => 1,
                'create_time' => time(),
                'update_time' => time(),
            ];
            self::insert($data);
            $status = $data;
        }
        return $status;
    }

    /**
     * 更新最后在线时间
     */
    public static function updateLastOnlineTime($adminId)
    {
        self::where('admin_id', $adminId)->update([
            'last_online_time' => time(),
            'update_time' => time(),
        ]);
    }

    /**
     * 获取启用客服功能的管理员ID列表
     */
    public static function getEnabledAdminIds()
    {
        return self::where('is_enabled', 1)->column('admin_id');
    }
}
