From ef3814283d9c6abb0dd3ca54d39069638193a959 Mon Sep 17 00:00:00 2001 From: aaron <> Date: Thu, 23 Jan 2025 15:14:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=AE=B6=E5=88=97=E8=A1=A8=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=95=86=E5=93=81=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/endpoints/merchant.py | 25 ++++++++++++++++++++++++- app/models/merchant_order.py | 1 - app/models/merchant_pay_order.py | 1 - 3 files changed, 24 insertions(+), 3 deletions(-) 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