商家列表接口,添加商品返回。

This commit is contained in:
aaron 2025-01-23 15:14:22 +08:00
parent ef14928e96
commit ef3814283d
3 changed files with 24 additions and 3 deletions

View File

@ -15,7 +15,8 @@ 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 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() router = APIRouter()
@ -207,6 +208,27 @@ async def list_merchants(
).group_by(MerchantPayOrderDB.merchant_id).all() ).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 = [{ merchant_list = [{
**MerchantInfo.model_validate(m[0]).model_dump(), **MerchantInfo.model_validate(m[0]).model_dump(),
@ -214,6 +236,7 @@ async def list_merchants(
"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), "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 "distance": round(m[4]) if longitude is not None and latitude is not None else None
} for m in merchants] } for m in merchants]

View File

@ -53,7 +53,6 @@ class MerchantOrderInfo(BaseModel):
product_image: Optional[str] = None product_image: Optional[str] = None
product_price: Optional[float] = None product_price: Optional[float] = None
merchant_name: Optional[str] = None merchant_name: Optional[str] = None
merchant_logo: Optional[str] = None
merchant_address: Optional[str] = None merchant_address: Optional[str] = None
class Config: class Config:

View File

@ -42,7 +42,6 @@ class MerchantPayOrderInfo(BaseModel):
update_time: Optional[datetime] update_time: Optional[datetime]
pay_time: Optional[datetime] pay_time: Optional[datetime]
merchant_name: Optional[str] = None merchant_name: Optional[str] = None
merchant_logo: Optional[str] = None
class Config: class Config:
from_attributes = True from_attributes = True