mirror of
https://git.oceanpay.cc/danial/kami_apple_exchage.git
synced 2025-12-18 22:29:09 +00:00
- 新增链接权重字段,支持1-100范围设置 - 修改轮询算法为基于权重的选择机制 - 更新链接API接口返回统一使用LinkInfo模型 - 添加更新链接权重的PATCH端点 - 调整链接仓库查询逻辑,只包含激活状态链接 - 迁移链接相关Pydantic模型到task模块统一管理 - 修改分页响应格式为通用PaginatedResponse包装 - 禁用OpenTelemetry监控配置
68 lines
1.9 KiB
Python
68 lines
1.9 KiB
Python
"""
|
|
链接相关模型
|
|
"""
|
|
|
|
from typing import TYPE_CHECKING, Any
|
|
from enum import Enum
|
|
|
|
from sqlalchemy import Boolean, Float, Index, Integer, String
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from .base import BaseModel
|
|
|
|
if TYPE_CHECKING:
|
|
from .orders import Orders
|
|
|
|
|
|
class LinkStatus(str, Enum):
|
|
"""链接状态枚举"""
|
|
|
|
ACTIVE = "active"
|
|
INACTIVE = "inactive"
|
|
|
|
|
|
class Links(BaseModel):
|
|
"""链接数据表"""
|
|
|
|
__tablename__ = "links"
|
|
__table_args__ = (
|
|
Index("ix_links_url", "url"),
|
|
Index("ix_links_amount", "amount"),
|
|
Index("ix_links_url_amount", "url", "amount"), # 组合索引
|
|
)
|
|
|
|
url: Mapped[str] = mapped_column(String(255), nullable=False, comment="链接地址")
|
|
amount: Mapped[float] = mapped_column(Float, nullable=False, comment="金额")
|
|
weight: Mapped[int] = mapped_column(Integer, default=1, nullable=False, comment="权重(1-100)")
|
|
status: Mapped[LinkStatus] = mapped_column(
|
|
default=LinkStatus.ACTIVE, comment="链接状态"
|
|
)
|
|
|
|
# 关系映射
|
|
orders: Mapped[list["Orders"]] = relationship("Orders", back_populates="links")
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<Links(id={self.id}, url={self.url}, amount={self.amount})>"
|
|
|
|
@property
|
|
def is_valid_amount(self) -> bool:
|
|
"""金额是否有效"""
|
|
return self.amount > 0
|
|
|
|
@property
|
|
def is_active(self) -> bool:
|
|
"""链接是否激活"""
|
|
return self.status == LinkStatus.ACTIVE
|
|
|
|
@property
|
|
def formatted_amount(self) -> str:
|
|
"""格式化金额显示"""
|
|
return f"${self.amount:.2f}"
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
"""转换为字典,包含格式化信息"""
|
|
data = super().to_dict()
|
|
data["is_valid_amount"] = self.is_valid_amount
|
|
data["formatted_amount"] = self.formatted_amount
|
|
return data
|