update
This commit is contained in:
parent
ff318a8dfd
commit
155a1ba164
@ -105,17 +105,23 @@ async def get_merchant(
|
|||||||
):
|
):
|
||||||
"""获取商家详情"""
|
"""获取商家详情"""
|
||||||
# 构建基础查询
|
# 构建基础查询
|
||||||
merchant = db.query(
|
query = db.query(
|
||||||
MerchantDB,
|
MerchantDB,
|
||||||
UserDB.phone.label('user_phone'),
|
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,
|
if longitude is not None and latitude is not None:
|
||||||
lon=longitude
|
query = query.add_columns(
|
||||||
) if longitude and latitude else text("NULL as distance")
|
text("ST_Distance_Sphere(point(merchants.longitude, merchants.latitude), "
|
||||||
).join(
|
"point(:lon, :lat)) as distance").params(lon=longitude, lat=latitude)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
query = query.add_columns(text("NULL as distance"))
|
||||||
|
|
||||||
|
# 完成查询
|
||||||
|
merchant = query.join(
|
||||||
UserDB,
|
UserDB,
|
||||||
MerchantDB.user_id == UserDB.userid
|
MerchantDB.user_id == UserDB.userid
|
||||||
).filter(
|
).filter(
|
||||||
@ -126,12 +132,12 @@ async def get_merchant(
|
|||||||
return error_response(code=404, message="商家不存在")
|
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 = merchant_info.model_dump()
|
||||||
merchant_data.update({
|
merchant_data.update({
|
||||||
'user_phone': merchant.user_phone,
|
'user_phone': merchant[1],
|
||||||
'user_nickname': merchant.user_nickname,
|
'user_nickname': merchant[2],
|
||||||
'distance': round(merchant.distance) if merchant.distance else None
|
'distance': round(merchant[3]) if merchant[3] else None
|
||||||
})
|
})
|
||||||
|
|
||||||
return success_response(data=merchant_data)
|
return success_response(data=merchant_data)
|
||||||
|
|||||||
@ -68,6 +68,7 @@ class MerchantInfo(BaseModel):
|
|||||||
address: str
|
address: str
|
||||||
longitude: float
|
longitude: float
|
||||||
latitude: float
|
latitude: float
|
||||||
|
distance: Optional[int] = None # 距离(米)
|
||||||
phone: str
|
phone: str
|
||||||
pay_gift_points_rate: float
|
pay_gift_points_rate: float
|
||||||
pay_share_rate: float
|
pay_share_rate: float
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user