from datetime import datetime from typing import Optional from sqlalchemy import String, DateTime, ForeignKey, Text, Integer from sqlalchemy.orm import Mapped, mapped_column from app.common.enums import PermissionFilterStrategy from app.core.base_model import ModelMixin, TenantMixin class TenantApiKeyModel(ModelMixin, TenantMixin): """ 租户API Key模型 """ __tablename__ = "sys_tenant_api_key" __permission_strategy__ = PermissionFilterStrategy.ENTERPRISE_BASED # 核心字段 api_key: Mapped[str] = mapped_column( String(256), nullable=False, unique=True, comment="API Key", index=True, ) api_secret: Mapped[str] = mapped_column( String(256), nullable=False, comment="API Secret", ) expired_at: Mapped[Optional[datetime]] = mapped_column( DateTime, default=None, nullable=True, comment="过期时间", index=True, ) last_used_at: Mapped[Optional[datetime]] = mapped_column( DateTime, default=None, nullable=True, comment="最后使用时间", ) # 关联字段 tenant_id: Mapped[int] = mapped_column( Integer, ForeignKey("sys_tenant.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False, index=True, comment="租户ID", ) return_url: Mapped[str] = mapped_column( String(512), nullable=True, comment="同步返回地址" ) class TenantApiLogModel(ModelMixin, TenantMixin): """ 租户API调用日志模型 """ __tablename__ = "sys_tenant_api_log" # 核心字段 api_key_id: Mapped[int] = mapped_column( Integer, nullable=True, index=True, comment="API Key ID", ) endpoint: Mapped[str] = mapped_column( String(256), nullable=False, comment="调用的接口", index=True, ) method: Mapped[str] = mapped_column( String(10), nullable=False, comment="请求方法", ) request_ip: Mapped[str] = mapped_column( String(50), nullable=False, comment="请求IP", index=True, ) request_data: Mapped[Optional[str]] = mapped_column( Text, default=None, nullable=True, comment="请求数据(脱敏存储)", ) response_code: Mapped[int] = mapped_column( Integer, nullable=False, comment="响应码", index=True, ) response_time: Mapped[float] = mapped_column( Integer, nullable=False, comment="响应时间(毫秒)", )