deliveryman-api/app/models/merchant_product.py
aaron 0005967a13 修改优化图片
修复积分列表功能
2025-02-07 22:13:38 +09:00

83 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from sqlalchemy import Column, String, Integer, Float, DateTime, ForeignKey, Enum, Boolean
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
import enum
from app.core.utils import CommonUtils
from app.core.imageprocessor import process_image, ImageFormat
class ProductStatus(str, enum.Enum):
LISTING = "LISTING" # 上架
UNLISTING = "UNLISTING" # 下架
class MerchantProductDB(Base):
__tablename__ = "merchant_products"
id = Column(Integer, primary_key=True, autoincrement=True)
merchant_id = Column(Integer, ForeignKey("merchants.id"), nullable=False)
name = Column(String(100), nullable=False)
image_url = Column(String(500), nullable=False)
product_price = Column(Float, nullable=False) # 原价
sale_price = Column(DECIMAL(10,2), nullable=False) # 售价
settlement_amount = Column(DECIMAL(10,2), nullable=False) # 商家结算金额
tags = Column(String(200)) # 标签,逗号分隔
purchase_limit = Column(Integer, nullable=False, default=0) # 限购次数0表示不限购
gift_points_rate = Column(DECIMAL(4,2), nullable=False, default=0.00) # 购买赠送积分比例默认0%
promotion_text = Column(String(100)) # 促销文本
create_time = Column(DateTime(timezone=True), server_default=func.now())
update_time = Column(DateTime(timezone=True), onupdate=func.now())
status = Column(Enum(ProductStatus), nullable=False, default=ProductStatus.UNLISTING)
@property
def optimized_image_url(self):
return process_image(self.image_url).quality(80).thumbnail(width=450, height=450).format(ImageFormat.WEBP).build()
# 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)
purchase_limit: int = Field(0, ge=0) # 限购次数默认0表示不限购
status: ProductStatus = ProductStatus.UNLISTING
promotion_text: Optional[str] = Field(None, max_length=100) # 促销文本
gift_points_rate: Optional[float] = Field(10.00, ge=0, le=100) # 购买赠送积分比例
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)
purchase_limit: Optional[int] = Field(None, ge=0) # 限购次数,可选字段
status: Optional[ProductStatus] = None
promotion_text: Optional[str] = Field(None, max_length=100) # 促销文本
gift_points_rate: Optional[float] = Field(None, ge=0, le=100) # 购买赠送积分比例
class MerchantProductInfo(BaseModel):
id: int
merchant_id: int
merchant_name: Optional[str] = None # 添加商家名称字段
name: str
image_url: str
optimized_image_url: Optional[str] = None
product_price: float
sale_price: float
settlement_amount: float
tags: str
purchase_limit: int # 限购次数
gift_points_rate: float
promotion_text: Optional[str] = None # 促销文本
create_time: datetime
update_time: Optional[datetime]
status: ProductStatus
class Config:
from_attributes = True