diff --git a/app/api/endpoints/merchant.py b/app/api/endpoints/merchant.py index e6c3416..957cdfb 100644 --- a/app/api/endpoints/merchant.py +++ b/app/api/endpoints/merchant.py @@ -15,7 +15,8 @@ from app.api.deps import get_admin_user from app.models.user import UserDB from app.core.response import success_response, error_response, ResponseModel from app.models.merchant_pay_order import MerchantPayOrderDB -from sqlalchemy.sql import func +from sqlalchemy.sql import func, desc +from app.models.merchant_product import MerchantProductDB router = APIRouter() @@ -207,6 +208,27 @@ async def list_merchants( ).group_by(MerchantPayOrderDB.merchant_id).all() ) + # 获取商家最新或限购商品 + merchant_products = {} + for merchant_id in merchant_ids: + product = db.query(MerchantProductDB).filter( + MerchantProductDB.merchant_id == merchant_id, + MerchantProductDB.status == True # 只查询上架商品 + ).order_by( + # 优先选择有限购的商品,其次按创建时间倒序 + desc(MerchantProductDB.purchase_limit > 0), + desc(MerchantProductDB.create_time) + ).first() + + if product: + merchant_products[merchant_id] = { + "product_id": product.id, + "product_name": product.name, + "product_price": float(product.sale_price), + "product_image": product.image_url, + "purchase_limit": product.purchase_limit + } + # 处理返回结果 merchant_list = [{ **MerchantInfo.model_validate(m[0]).model_dump(), @@ -214,6 +236,7 @@ async def list_merchants( "user_phone": m[2], "user_nickname": m[3], "online_pay_count": pay_order_counts.get(m[0].id, 0), + "featured_product": merchant_products.get(m[0].id), "distance": round(m[4]) if longitude is not None and latitude is not None else None } for m in merchants] diff --git a/app/models/merchant_order.py b/app/models/merchant_order.py index c5d2c04..2fe96a8 100644 --- a/app/models/merchant_order.py +++ b/app/models/merchant_order.py @@ -53,7 +53,6 @@ class MerchantOrderInfo(BaseModel): product_image: Optional[str] = None product_price: Optional[float] = None merchant_name: Optional[str] = None - merchant_logo: Optional[str] = None merchant_address: Optional[str] = None class Config: diff --git a/app/models/merchant_pay_order.py b/app/models/merchant_pay_order.py index 6b91ee7..c485f7c 100644 --- a/app/models/merchant_pay_order.py +++ b/app/models/merchant_pay_order.py @@ -42,7 +42,6 @@ class MerchantPayOrderInfo(BaseModel): update_time: Optional[datetime] pay_time: Optional[datetime] merchant_name: Optional[str] = None - merchant_logo: Optional[str] = None class Config: from_attributes = True