from sqlalchemy import Column, String, Integer, Float, DateTime, ForeignKey from sqlalchemy.dialects.mysql import DECIMAL from sqlalchemy.sql import func from pydantic import BaseModel, Field from typing import Optional, List from datetime import datetime from .database import Base class MerchantProductDB(Base): __tablename__ = "merchant_products" id = Column(Integer, primary_key=True, autoincrement=True) merchant_id = Column(Integer, ForeignKey("merchants.id", ondelete="CASCADE"), index=True) name = Column(String(100), nullable=False) image_url = Column(String(500), nullable=False) product_price = Column(Float, nullable=False) # 原价 sale_price = Column(Float, nullable=False) # 售价 settlement_amount = Column(DECIMAL(10,2), nullable=False) # 商家结算金额 tags = Column(String(200)) # 标签,逗号分隔 max_deduct_points = Column(DECIMAL(10,2), default=0) # 最高可抵扣积分 create_time = Column(DateTime(timezone=True), server_default=func.now()) update_time = Column(DateTime(timezone=True), onupdate=func.now()) # Pydantic 模型 class MerchantProductCreate(BaseModel): merchant_id: int name: str = Field(..., max_length=100) image_url: str = Field(..., max_length=500) product_price: float = Field(..., gt=0) sale_price: float = Field(..., gt=0) settlement_amount: float = Field(..., gt=0) tags: str = Field("", max_length=200) max_deduct_points: float = Field(0.0, ge=0) class MerchantProductUpdate(BaseModel): name: Optional[str] = Field(None, max_length=100) image_url: Optional[str] = Field(None, max_length=500) product_price: Optional[float] = Field(None, gt=0) sale_price: Optional[float] = Field(None, gt=0) settlement_amount: Optional[float] = Field(None, gt=0) tags: Optional[str] = Field(None, max_length=200) max_deduct_points: Optional[float] = Field(None, ge=0) class MerchantProductInfo(BaseModel): id: int merchant_id: int name: str image_url: str product_price: float sale_price: float settlement_amount: float tags: str max_deduct_points: float create_time: datetime update_time: Optional[datetime] class Config: from_attributes = True