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 文件
79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
"""
|
|
礼品卡相关模型
|
|
"""
|
|
|
|
import enum
|
|
from typing import TYPE_CHECKING, Any
|
|
from sqlalchemy import String, Text, ForeignKey, Enum, Index
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from .base import BaseModel
|
|
|
|
if TYPE_CHECKING:
|
|
from .orders import Orders
|
|
|
|
|
|
class GiftCardStatus(enum.Enum):
|
|
"""礼品卡状态"""
|
|
|
|
PENDING = "pending" # 待处理
|
|
SUCCESS = "success" # 成功
|
|
FAILURE = "failure" # 失败
|
|
|
|
|
|
class GiftCards(BaseModel):
|
|
"""礼品卡数据表"""
|
|
|
|
__tablename__ = "gift_cards"
|
|
__table_args__ = (
|
|
Index("ix_gift_cards_status", "status"),
|
|
Index("ix_gift_cards_order_id", "order_id"),
|
|
Index("ix_gift_cards_card_code", "card_code"),
|
|
)
|
|
|
|
card_code: Mapped[str] = mapped_column(
|
|
String(255), nullable=False, comment="礼品卡号码"
|
|
)
|
|
card_value: Mapped[float] = mapped_column(nullable=False, comment="卡片面额")
|
|
status: Mapped[GiftCardStatus] = mapped_column(
|
|
Enum(GiftCardStatus),
|
|
default=GiftCardStatus.PENDING,
|
|
nullable=False,
|
|
comment="礼品卡状态",
|
|
)
|
|
failure_reason: Mapped[str | None] = mapped_column(
|
|
Text, nullable=True, comment="失败原因"
|
|
)
|
|
order_id: Mapped[str] = mapped_column(
|
|
ForeignKey("orders.id"), nullable=False, comment="订单ID"
|
|
)
|
|
|
|
# 关系映射
|
|
order: Mapped["Orders"] = relationship("Orders", back_populates="gift_cards")
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<GiftCards(id={self.id}, code={self.card_code}, value={self.card_value}, status={self.status.value})>"
|
|
|
|
@property
|
|
def is_successful(self) -> bool:
|
|
"""是否处理成功"""
|
|
return self.status == GiftCardStatus.SUCCESS
|
|
|
|
@property
|
|
def is_failed(self) -> bool:
|
|
"""是否处理失败"""
|
|
return self.status == GiftCardStatus.FAILURE
|
|
|
|
@property
|
|
def is_pending(self) -> bool:
|
|
"""是否待处理"""
|
|
return self.status == GiftCardStatus.PENDING
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
"""转换为字典,包含状态信息"""
|
|
data = super().to_dict()
|
|
data["status"] = self.status.value
|
|
data["is_successful"] = self.is_successful
|
|
data["is_failed"] = self.is_failed
|
|
data["is_pending"] = self.is_pending
|
|
return data
|