74 lines
2.7 KiB
Python
74 lines
2.7 KiB
Python
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Enum, Float
|
|
from sqlalchemy.sql import func
|
|
from pydantic import BaseModel, Field
|
|
from typing import Optional
|
|
from datetime import datetime
|
|
from .database import Base
|
|
import enum
|
|
from app.core.utils import CommonUtils
|
|
|
|
class PointProductOrderStatus(str, enum.Enum):
|
|
PENDING = "PENDING" # 待确认
|
|
CONFIRMED = "CONFIRMED" # 已确认
|
|
CANCELLED = "CANCELLED" # 已取消
|
|
DELIVERED = "DELIVERED" # 已送达
|
|
|
|
@property
|
|
def status_text(self) -> str:
|
|
status_map = {
|
|
PointProductOrderStatus.PENDING: "待确认",
|
|
PointProductOrderStatus.CONFIRMED: "已确认",
|
|
PointProductOrderStatus.CANCELLED: "已取消",
|
|
PointProductOrderStatus.DELIVERED: "已送达"
|
|
}
|
|
return status_map.get(self, "未知状态")
|
|
|
|
class PointProductOrderDB(Base):
|
|
__tablename__ = "point_product_orders"
|
|
|
|
orderid = Column(String(32), primary_key=True)
|
|
user_id = Column(Integer, ForeignKey("users.userid"), index=True)
|
|
delivery_order_id = Column(String(32), ForeignKey("shipping_orders.orderid"), nullable=True)
|
|
product_id = Column(Integer, ForeignKey("point_products.id"))
|
|
product_name = Column(String(100), nullable=False)
|
|
product_image = Column(String(200), nullable=False)
|
|
product_description = Column(String(500), nullable=False)
|
|
product_point_amount = Column(Integer, nullable=False) # 单件商品所需积分
|
|
quantity = Column(Integer, nullable=False, default=1)
|
|
order_point_amount = Column(Integer, nullable=False) # 订单总积分
|
|
status = Column(Enum(PointProductOrderStatus), nullable=False, default=PointProductOrderStatus.PENDING)
|
|
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
|
update_time = Column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
@staticmethod
|
|
def generate_order_id() -> str:
|
|
"""生成订单号"""
|
|
return CommonUtils.generate_order_id("E") # E代表积分兑换商品订单
|
|
|
|
# Pydantic 模型
|
|
class PointProductOrderCreate(BaseModel):
|
|
product_id: int
|
|
quantity: int = Field(default=1, gt=0)
|
|
delivery_order_id: Optional[str] = None
|
|
|
|
class PointProductOrderUpdate(BaseModel):
|
|
status: PointProductOrderStatus
|
|
delivery_order_id: Optional[str] = None
|
|
|
|
class PointProductOrderInfo(BaseModel):
|
|
orderid: str
|
|
user_id: int
|
|
delivery_order_id: Optional[str]
|
|
product_id: int
|
|
product_name: str
|
|
product_image: str
|
|
product_description: str
|
|
product_point_amount: int
|
|
quantity: int
|
|
order_point_amount: int
|
|
status: PointProductOrderStatus
|
|
create_time: datetime
|
|
update_time: Optional[datetime]
|
|
|
|
class Config:
|
|
from_attributes = True |