deliveryman-api/app/models/merchant_product.py
2025-01-06 11:56:11 +08:00

95 lines
3.5 KiB
Python

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 MerchantProductCategoryDB(Base):
__tablename__ = "merchant_product_categories"
id = Column(Integer, primary_key=True, autoincrement=True)
merchant_id = Column(Integer, ForeignKey("merchants.id", ondelete="CASCADE"), index=True)
name = Column(String(50), nullable=False)
sort = Column(Integer, nullable=False, default=0)
create_time = Column(DateTime(timezone=True), server_default=func.now())
update_time = Column(DateTime(timezone=True), onupdate=func.now())
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)
category_id = Column(Integer, ForeignKey("merchant_product_categories.id"), 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 ProductCategoryCreate(BaseModel):
merchant_id: int
name: str = Field(..., max_length=50)
sort: int = Field(0, ge=0)
class ProductCategoryUpdate(BaseModel):
name: Optional[str] = Field(None, max_length=50)
sort: Optional[int] = Field(None, ge=0)
class ProductCategoryInfo(BaseModel):
id: int
merchant_id: int
name: str
sort: int
create_time: datetime
update_time: Optional[datetime]
class Config:
from_attributes = True
# Pydantic 模型
class MerchantProductCreate(BaseModel):
merchant_id: int
name: str = Field(..., max_length=100)
category_id: int
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)
category_id: Optional[int] = None
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
category_id: int
category_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