mirror of
https://git.oceanpay.cc/danial/kami_apple_exchage.git
synced 2025-12-18 22:29:09 +00:00
- 新增 CODEBUDDY.md、GEMINI.md、GEMINI_CN.md 等项目文档 - 更新 Dockerfile 和其他配置文件 - 优化部分代码结构,如 orders.py、tasks.py 等 - 新增 .dockerignore 文件
83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
"""
|
|
订单相关模型
|
|
"""
|
|
|
|
import enum
|
|
from typing import Any, TYPE_CHECKING
|
|
from sqlalchemy import String, Text, ForeignKey, Enum, Index
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from .base import BaseModel
|
|
|
|
if TYPE_CHECKING:
|
|
from .user_data import UserData
|
|
from .giftcards import GiftCards
|
|
from .links import Links
|
|
|
|
|
|
class OrderResultStatus(enum.Enum):
|
|
"""订单处理结果状态"""
|
|
|
|
PENDING = "pending" # 待处理
|
|
PROCESSING = "processing" # 处理中
|
|
SUCCESS = "success" # 成功
|
|
FAILURE = "failure" # 失败
|
|
|
|
|
|
class Orders(BaseModel):
|
|
"""订单处理结果表"""
|
|
|
|
__tablename__ = "orders"
|
|
__table_args__ = (
|
|
Index("ix_orders_status", "status"),
|
|
Index("ix_orders_user_data_id", "user_data_id"),
|
|
)
|
|
|
|
status: Mapped[OrderResultStatus] = mapped_column(
|
|
Enum(OrderResultStatus),
|
|
default=OrderResultStatus.PENDING,
|
|
nullable=False,
|
|
comment="订单状态",
|
|
)
|
|
final_order_url: Mapped[str | None] = mapped_column(
|
|
Text, nullable=True, comment="最终订单URL"
|
|
)
|
|
failure_reason: Mapped[str | None] = mapped_column(
|
|
Text, nullable=True, comment="失败原因"
|
|
)
|
|
|
|
# 外键关系
|
|
user_data_id: Mapped[str] = mapped_column(
|
|
ForeignKey("user_data.id"), nullable=False, comment="用户数据ID"
|
|
)
|
|
links_id: Mapped[str] = mapped_column(
|
|
ForeignKey("links.id"), nullable=False, comment="链接ID"
|
|
)
|
|
|
|
# 关系映射
|
|
user_data: Mapped["UserData"] = relationship("UserData", back_populates="orders")
|
|
links: Mapped["Links"] = relationship("Links", back_populates="orders")
|
|
gift_cards: Mapped["list[GiftCards]"] = relationship(
|
|
"GiftCards", back_populates="order", cascade="all, delete-orphan"
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<Orders(id={self.id}, status={self.status.value}, order_number={self.id})>"
|
|
|
|
@property
|
|
def is_completed(self) -> bool:
|
|
"""是否已完成(成功或失败)"""
|
|
return self.status in [OrderResultStatus.SUCCESS, OrderResultStatus.FAILURE]
|
|
|
|
@property
|
|
def is_processing(self) -> bool:
|
|
"""是否正在处理"""
|
|
return self.status == OrderResultStatus.PROCESSING
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
"""转换为字典,包含状态信息"""
|
|
data = super().to_dict()
|
|
data["status"] = self.status.value
|
|
data["is_completed"] = self.is_completed
|
|
data["is_processing"] = self.is_processing
|
|
return data
|