no message
This commit is contained in:
parent
24354b9373
commit
64e2f4a339
@ -14,6 +14,8 @@ from app.models.database import get_db
|
|||||||
from app.api.deps import get_admin_user
|
from app.api.deps import get_admin_user
|
||||||
from app.models.user import UserDB
|
from app.models.user import UserDB
|
||||||
from app.core.response import success_response, error_response, ResponseModel
|
from app.core.response import success_response, error_response, ResponseModel
|
||||||
|
from app.models.merchant_pay_order import MerchantPayOrderDB
|
||||||
|
from sqlalchemy.sql import func
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@ -182,18 +184,36 @@ async def list_merchants(
|
|||||||
if longitude is not None and latitude is not None:
|
if longitude is not None and latitude is not None:
|
||||||
query = query.add_columns(
|
query = query.add_columns(
|
||||||
text("ST_Distance_Sphere(point(longitude, latitude), point(:lon, :lat)) as distance")
|
text("ST_Distance_Sphere(point(longitude, latitude), point(:lon, :lat)) as distance")
|
||||||
).params(lon=longitude, lat=latitude).order_by(text("distance"))
|
).params(lon=longitude, lat=latitude)
|
||||||
|
# 默认按距离排序
|
||||||
|
query = query.order_by(text("distance"))
|
||||||
else:
|
else:
|
||||||
|
# 如果没有经纬度,则按创建时间排序
|
||||||
query = query.order_by(MerchantDB.create_time.desc())
|
query = query.order_by(MerchantDB.create_time.desc())
|
||||||
|
|
||||||
|
# 添加一个空的距离列,保持返回结构一致
|
||||||
|
query = query.add_columns(text("NULL as distance"))
|
||||||
|
|
||||||
merchants = query.offset(skip).limit(limit).all()
|
merchants = query.offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
# 获取商家在线买单数量
|
||||||
|
merchant_ids = [m[0].id for m in merchants]
|
||||||
|
pay_order_counts = dict(
|
||||||
|
db.query(
|
||||||
|
MerchantPayOrderDB.merchant_id,
|
||||||
|
func.count(MerchantPayOrderDB.id).label('count')
|
||||||
|
).filter(
|
||||||
|
MerchantPayOrderDB.merchant_id.in_(merchant_ids)
|
||||||
|
).group_by(MerchantPayOrderDB.merchant_id).all()
|
||||||
|
)
|
||||||
|
|
||||||
# 处理返回结果
|
# 处理返回结果
|
||||||
merchant_list = [{
|
merchant_list = [{
|
||||||
**MerchantInfo.model_validate(m[0]).model_dump(),
|
**MerchantInfo.model_validate(m[0]).model_dump(),
|
||||||
"category_name": m[1],
|
"category_name": m[1],
|
||||||
"user_phone": m[2],
|
"user_phone": m[2],
|
||||||
"user_nickname": m[3],
|
"user_nickname": m[3],
|
||||||
|
"online_pay_count": pay_order_counts.get(m[0].id, 0),
|
||||||
"distance": round(m[4]) if longitude is not None and latitude is not None else None
|
"distance": round(m[4]) if longitude is not None and latitude is not None else None
|
||||||
} for m in merchants]
|
} for m in merchants]
|
||||||
|
|
||||||
|
|||||||
@ -76,6 +76,7 @@ class MerchantInfo(BaseModel):
|
|||||||
user_id: int
|
user_id: int
|
||||||
user_phone: Optional[str] = None
|
user_phone: Optional[str] = None
|
||||||
user_nickname: Optional[str] = None
|
user_nickname: Optional[str] = None
|
||||||
|
online_pay_count: Optional[int] = 0
|
||||||
name: str
|
name: str
|
||||||
business_hours: str
|
business_hours: str
|
||||||
address: str
|
address: str
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user