增加商品的上下架功能。
This commit is contained in:
parent
84ba861814
commit
9cc593b305
@ -1,6 +1,6 @@
|
|||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, Depends
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from typing import List
|
from typing import List, Optional
|
||||||
from app.models.merchant_product import (
|
from app.models.merchant_product import (
|
||||||
MerchantProductDB,
|
MerchantProductDB,
|
||||||
MerchantProductCreate,
|
MerchantProductCreate,
|
||||||
@ -69,23 +69,45 @@ async def update_product(
|
|||||||
db.rollback()
|
db.rollback()
|
||||||
return error_response(code=500, message=f"更新失败: {str(e)}")
|
return error_response(code=500, message=f"更新失败: {str(e)}")
|
||||||
|
|
||||||
@router.get("/merchant/{merchant_id}", response_model=ResponseModel)
|
@router.get("/list", response_model=ResponseModel)
|
||||||
async def list_merchant_products(
|
async def list_merchant_products(
|
||||||
merchant_id: int,
|
merchant_id: Optional[int] = None,
|
||||||
skip: int = 0,
|
skip: int = 0,
|
||||||
limit: int = 20,
|
limit: int = 20,
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
"""获取商家的产品列表"""
|
"""获取商品列表"""
|
||||||
products = db.query(MerchantProductDB).filter(
|
# 联表查询商家信息
|
||||||
MerchantProductDB.merchant_id == merchant_id
|
query = db.query(
|
||||||
).order_by(
|
MerchantProductDB,
|
||||||
|
MerchantDB.name.label('merchant_name')
|
||||||
|
).join(
|
||||||
|
MerchantDB,
|
||||||
|
MerchantProductDB.merchant_id == MerchantDB.id
|
||||||
|
)
|
||||||
|
|
||||||
|
# 如果指定了商家ID,添加筛选条件
|
||||||
|
if merchant_id:
|
||||||
|
query = query.filter(MerchantProductDB.merchant_id == merchant_id)
|
||||||
|
|
||||||
|
total = query.count()
|
||||||
|
results = query.order_by(
|
||||||
MerchantProductDB.create_time.desc()
|
MerchantProductDB.create_time.desc()
|
||||||
).offset(skip).limit(limit).all()
|
).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
return success_response(data=[
|
# 处理返回数据
|
||||||
MerchantProductInfo.model_validate(p) for p in products
|
products = []
|
||||||
])
|
for product, merchant_name in results:
|
||||||
|
product_info = MerchantProductInfo.model_validate(product)
|
||||||
|
products.append({
|
||||||
|
**product_info.model_dump(),
|
||||||
|
"merchant_name": merchant_name
|
||||||
|
})
|
||||||
|
|
||||||
|
return success_response(data={
|
||||||
|
"total": total,
|
||||||
|
"items": products
|
||||||
|
})
|
||||||
|
|
||||||
@router.get("/{product_id}", response_model=ResponseModel)
|
@router.get("/{product_id}", response_model=ResponseModel)
|
||||||
async def get_product(
|
async def get_product(
|
||||||
|
|||||||
@ -1,12 +1,17 @@
|
|||||||
from sqlalchemy import Column, String, Integer, Float, DateTime, ForeignKey
|
from sqlalchemy import Column, String, Integer, Float, DateTime, ForeignKey, Enum
|
||||||
from sqlalchemy.dialects.mysql import DECIMAL
|
from sqlalchemy.dialects.mysql import DECIMAL
|
||||||
from sqlalchemy.sql import func
|
from sqlalchemy.sql import func
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from .database import Base
|
from .database import Base
|
||||||
|
import enum
|
||||||
|
|
||||||
|
|
||||||
|
class ProductStatus(str, enum.Enum):
|
||||||
|
LISTING = "LISTING" # 上架
|
||||||
|
UNLISTING = "UNLISTING" # 下架
|
||||||
|
|
||||||
class MerchantProductDB(Base):
|
class MerchantProductDB(Base):
|
||||||
__tablename__ = "merchant_products"
|
__tablename__ = "merchant_products"
|
||||||
|
|
||||||
@ -21,6 +26,7 @@ class MerchantProductDB(Base):
|
|||||||
max_deduct_points = Column(DECIMAL(10,2), default=0) # 最高可抵扣积分
|
max_deduct_points = Column(DECIMAL(10,2), default=0) # 最高可抵扣积分
|
||||||
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
||||||
update_time = Column(DateTime(timezone=True), onupdate=func.now())
|
update_time = Column(DateTime(timezone=True), onupdate=func.now())
|
||||||
|
status = Column(Enum(ProductStatus), nullable=False, default=ProductStatus.UNLISTING)
|
||||||
|
|
||||||
|
|
||||||
# Pydantic 模型
|
# Pydantic 模型
|
||||||
@ -33,6 +39,7 @@ class MerchantProductCreate(BaseModel):
|
|||||||
settlement_amount: float = Field(..., gt=0)
|
settlement_amount: float = Field(..., gt=0)
|
||||||
tags: str = Field("", max_length=200)
|
tags: str = Field("", max_length=200)
|
||||||
max_deduct_points: float = Field(0.0, ge=0)
|
max_deduct_points: float = Field(0.0, ge=0)
|
||||||
|
status: ProductStatus = ProductStatus.UNLISTING
|
||||||
|
|
||||||
class MerchantProductUpdate(BaseModel):
|
class MerchantProductUpdate(BaseModel):
|
||||||
name: Optional[str] = Field(None, max_length=100)
|
name: Optional[str] = Field(None, max_length=100)
|
||||||
@ -42,10 +49,12 @@ class MerchantProductUpdate(BaseModel):
|
|||||||
settlement_amount: Optional[float] = Field(None, gt=0)
|
settlement_amount: Optional[float] = Field(None, gt=0)
|
||||||
tags: Optional[str] = Field(None, max_length=200)
|
tags: Optional[str] = Field(None, max_length=200)
|
||||||
max_deduct_points: Optional[float] = Field(None, ge=0)
|
max_deduct_points: Optional[float] = Field(None, ge=0)
|
||||||
|
status: Optional[ProductStatus] = None
|
||||||
|
|
||||||
class MerchantProductInfo(BaseModel):
|
class MerchantProductInfo(BaseModel):
|
||||||
id: int
|
id: int
|
||||||
merchant_id: int
|
merchant_id: int
|
||||||
|
merchant_name: Optional[str] = None # 添加商家名称字段
|
||||||
name: str
|
name: str
|
||||||
image_url: str
|
image_url: str
|
||||||
product_price: float
|
product_price: float
|
||||||
@ -55,6 +64,7 @@ class MerchantProductInfo(BaseModel):
|
|||||||
max_deduct_points: float
|
max_deduct_points: float
|
||||||
create_time: datetime
|
create_time: datetime
|
||||||
update_time: Optional[datetime]
|
update_time: Optional[datetime]
|
||||||
|
status: ProductStatus
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
from_attributes = True
|
from_attributes = True
|
||||||
Loading…
Reference in New Issue
Block a user