From 9a92b4f29e9b767540b76a8c67286359ad29ae87 Mon Sep 17 00:00:00 2001 From: aaron <> Date: Wed, 12 Feb 2025 21:43:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=95=86=E5=AE=B6=20distance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/endpoints/merchant.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/app/api/endpoints/merchant.py b/app/api/endpoints/merchant.py index c19c2ec..69c8fb8 100644 --- a/app/api/endpoints/merchant.py +++ b/app/api/endpoints/merchant.py @@ -99,13 +99,22 @@ async def update_merchant( @router.get("/{merchant_id}", response_model=ResponseModel) async def get_merchant( merchant_id: int, + longitude: Optional[float] = None, + latitude: Optional[float] = None, db: Session = Depends(get_db) ): """获取商家详情""" + # 构建基础查询 merchant = db.query( MerchantDB, UserDB.phone.label('user_phone'), - UserDB.nickname.label('user_nickname') + UserDB.nickname.label('user_nickname'), + text("CASE WHEN :lat IS NOT NULL AND :lon IS NOT NULL THEN " + "ST_Distance_Sphere(point(longitude, latitude), point(:lon, :lat)) " + "ELSE NULL END as distance").params( + lat=latitude, + lon=longitude + ) if longitude and latitude else text("NULL as distance") ).join( UserDB, MerchantDB.user_id == UserDB.userid @@ -121,7 +130,8 @@ async def get_merchant( merchant_data = merchant_info.model_dump() merchant_data.update({ 'user_phone': merchant.user_phone, - 'user_nickname': merchant.user_nickname + 'user_nickname': merchant.user_nickname, + 'distance': round(merchant.distance) if merchant.distance else None }) return success_response(data=merchant_data) @@ -207,7 +217,8 @@ async def list_merchants( "product_name": product.name, "product_image": product.optimized_image_url, "product_price": float(product.sale_price), - "purchase_limit": product.purchase_limit + "purchase_limit": product.purchase_limit, + "promotion_text": product.promotion_text } # 处理返回结果