72 lines
2.3 KiB
Python
72 lines
2.3 KiB
Python
from datetime import datetime
|
|
from typing import Optional, List
|
|
from sqlalchemy import Column, String, Integer, Float, DateTime, ForeignKey
|
|
from sqlalchemy.sql import func
|
|
from pydantic import BaseModel, Field
|
|
from .database import Base
|
|
|
|
# 数据库模型
|
|
class ShippingOrderDB(Base):
|
|
__tablename__ = "shipping_orders"
|
|
|
|
orderid = Column(String(32), primary_key=True)
|
|
userid = Column(Integer, ForeignKey("users.userid"), index=True)
|
|
addressid = Column(Integer, ForeignKey("delivery_addresses.id"), index=True)
|
|
package_count = Column(Integer, nullable=False)
|
|
original_amount = Column(Float, nullable=False)
|
|
coupon_discount_amount = Column(Float, default=0)
|
|
coupon_id = Column(Integer, ForeignKey("user_coupons.id"), nullable=True)
|
|
final_amount = Column(Float, nullable=False)
|
|
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
class ShippingOrderPackageDB(Base):
|
|
__tablename__ = "shipping_order_packages"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
orderid = Column(String(32), ForeignKey("shipping_orders.orderid"), index=True)
|
|
station_id = Column(Integer, ForeignKey("stations.id"), index=True)
|
|
pickup_codes = Column(String(100), nullable=False)
|
|
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
# Pydantic 模型
|
|
class OrderPackage(BaseModel):
|
|
station_id: int
|
|
pickup_codes: str = Field(..., max_length=100)
|
|
|
|
class OrderCreate(BaseModel):
|
|
addressid: int
|
|
package_count: int = Field(..., gt=0)
|
|
original_amount: float = Field(..., ge=0)
|
|
coupon_id: Optional[int] = None
|
|
packages: List[OrderPackage]
|
|
|
|
class OrderInfo(BaseModel):
|
|
orderid: str
|
|
userid: int
|
|
addressid: int
|
|
package_count: int
|
|
original_amount: float
|
|
coupon_discount_amount: float
|
|
coupon_id: Optional[int]
|
|
final_amount: float
|
|
create_time: datetime
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
class OrderPackageInfo(BaseModel):
|
|
id: int
|
|
orderid: str
|
|
station_id: int
|
|
pickup_codes: str
|
|
create_time: datetime
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
def generate_order_id() -> str:
|
|
"""生成订单号:日期+时间戳"""
|
|
now = datetime.now()
|
|
date_str = now.strftime('%Y%m%d')
|
|
timestamp = int(now.timestamp() * 1000)
|
|
return f"{date_str}{timestamp}" |