<?php

namespace app\model;

use think\Model;

class Permission extends Model
{
    protected $name = 'permissions';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    // 权限枚举
    const ALLOW_NO = 0;
    const ALLOW_YES = 1;

    // 数据模式枚举
    const MODE_FILTER = 'filter';
    const MODE_CUSTOM = 'custom';
    const MODE_FULL = 'full';

    /**
     * 关联API密钥
     */
    public function apiKey()
    {
        return $this->belongsTo(ApiKey::class, 'api_key_id');
    }

    /**
     * 检查是否允许访问接口
     */
    public function canAccess($endpoint)
    {
        if ($this->isFullData()) {
            return true;
        }
        if ($endpoint === 'chuku') {
            return $this->allow_chuku == self::ALLOW_YES;
        } elseif ($endpoint === 'ruku') {
            return $this->allow_ruku == self::ALLOW_YES;
        }
        return false;
    }

    /**
     * 检查是否允许全量数据
     */
    public function isFullData()
    {
        return $this->allow_full_data == self::ALLOW_YES;
    }
}