This commit is contained in:
aaron 2025-02-13 12:37:16 +08:00
parent ff318a8dfd
commit 155a1ba164
2 changed files with 20 additions and 13 deletions

View File

@ -105,17 +105,23 @@ async def get_merchant(
):
"""获取商家详情"""
# 构建基础查询
merchant = db.query(
query = db.query(
MerchantDB,
UserDB.phone.label('user_phone'),
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.nickname.label('user_nickname')
)
# 如果提供了经纬度,添加距离计算
if longitude is not None and latitude is not None:
query = query.add_columns(
text("ST_Distance_Sphere(point(merchants.longitude, merchants.latitude), "
"point(:lon, :lat)) as distance").params(lon=longitude, lat=latitude)
)
else:
query = query.add_columns(text("NULL as distance"))
# 完成查询
merchant = query.join(
UserDB,
MerchantDB.user_id == UserDB.userid
).filter(
@ -126,12 +132,12 @@ async def get_merchant(
return error_response(code=404, message="商家不存在")
# 构建返回数据
merchant_info = MerchantInfo.model_validate(merchant.MerchantDB)
merchant_info = MerchantInfo.model_validate(merchant[0])
merchant_data = merchant_info.model_dump()
merchant_data.update({
'user_phone': merchant.user_phone,
'user_nickname': merchant.user_nickname,
'distance': round(merchant.distance) if merchant.distance else None
'user_phone': merchant[1],
'user_nickname': merchant[2],
'distance': round(merchant[3]) if merchant[3] else None
})
return success_response(data=merchant_data)

View File

@ -68,6 +68,7 @@ class MerchantInfo(BaseModel):
address: str
longitude: float
latitude: float
distance: Optional[int] = None # 距离(米)
phone: str
pay_gift_points_rate: float
pay_share_rate: float