<?php

namespace app\model;

use think\Model;

class Client extends Model
{
    protected $name = 'clients';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';
    protected $append = ['api_keys'];

    // 客户类型枚举
    const TYPE_SUPPLIER = 'supplier';
    const TYPE_DISTRIBUTOR = 'distributor';
    const TYPE_HOSPITAL = 'hospital';
    const TYPE_OTHER = 'other';

    // 状态枚举
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;

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

    /**
     * 获取api_keys字段（下划线命名）
     */
    public function getApiKeysAttr()
    {
        return $this->apiKeys()->select()->toArray();
    }

    /**
     * 获取客户类型列表
     */
    public static function getClientTypes()
    {
        return [
            self::TYPE_SUPPLIER => '供应商',
            self::TYPE_DISTRIBUTOR => '分销商',
            self::TYPE_HOSPITAL => '医院',
            self::TYPE_OTHER => '其他',
        ];
    }
}