deliveryman-api/app/api/endpoints/merchant.py

158 lines
4.9 KiB
Python

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from typing import List, Optional, Dict, Any
from sqlalchemy import text
from app.models.merchant import (
MerchantDB,
MerchantCreate,
MerchantUpdate,
MerchantInfo,
MerchantImageDB
)
from app.models.merchant_category import MerchantCategoryDB
from app.models.database import get_db
from app.api.deps import get_admin_user
from app.models.user import UserDB
from app.core.response import success_response, error_response, ResponseModel
router = APIRouter()
@router.post("", response_model=ResponseModel)
async def create_merchant(
merchant: MerchantCreate,
db: Session = Depends(get_db),
admin: UserDB = Depends(get_admin_user)
):
"""创建商家(管理员)"""
# 创建商家基本信息
merchant_data = merchant.model_dump(exclude={'images'})
db_merchant = MerchantDB(**merchant_data)
db.add(db_merchant)
try:
db.flush() # 获取商家ID
# 创建商家图片
for image in merchant.images:
db_image = MerchantImageDB(
merchant_id=db_merchant.id,
image_url=image.image_url,
sort=image.sort
)
db.add(db_image)
db.commit()
db.refresh(db_merchant)
return success_response(data=MerchantInfo.model_validate(db_merchant))
except Exception as e:
db.rollback()
return error_response(code=500, message=f"创建失败: {str(e)}")
@router.put("/{merchant_id}", response_model=ResponseModel)
async def update_merchant(
merchant_id: int,
merchant: MerchantUpdate,
db: Session = Depends(get_db),
admin: UserDB = Depends(get_admin_user)
):
"""更新商家信息(管理员)"""
db_merchant = db.query(MerchantDB).filter(
MerchantDB.id == merchant_id
).first()
if not db_merchant:
return error_response(code=404, message="商家不存在")
# 更新基本信息
update_data = merchant.model_dump(exclude={'images'}, exclude_unset=True)
for key, value in update_data.items():
setattr(db_merchant, key, value)
# 如果更新了图片
if merchant.images is not None:
# 删除原有图片
db.query(MerchantImageDB).filter(
MerchantImageDB.merchant_id == merchant_id
).delete()
# 添加新图片
for image in merchant.images:
db_image = MerchantImageDB(
merchant_id=merchant_id,
image_url=image.image_url,
sort=image.sort
)
db.add(db_image)
try:
db.commit()
db.refresh(db_merchant)
return success_response(data=MerchantInfo.model_validate(db_merchant))
except Exception as e:
db.rollback()
return error_response(code=500, message=f"更新失败: {str(e)}")
@router.get("/{merchant_id}", response_model=ResponseModel)
async def get_merchant(
merchant_id: int,
db: Session = Depends(get_db)
):
"""获取商家详情"""
merchant = db.query(MerchantDB).filter(
MerchantDB.id == merchant_id
).first()
if not merchant:
return error_response(code=404, message="商家不存在")
return success_response(data=MerchantInfo.model_validate(merchant))
@router.get("", response_model=ResponseModel)
async def list_merchants(
longitude: Optional[float] = None,
latitude: Optional[float] = None,
category_id: Optional[int] = None,
skip: int = 0,
limit: int = 20,
db: Session = Depends(get_db)
):
"""获取商家列表,支持经纬度排序和分类过滤"""
query = db.query(
MerchantDB,
MerchantCategoryDB.name.label('category_name')
).outerjoin(
MerchantCategoryDB,
MerchantDB.category_id == MerchantCategoryDB.id
)
# 添加分类过滤
if category_id is not None:
query = query.filter(MerchantDB.category_id == category_id)
# 根据经纬度排序
if longitude is not None and latitude is not None:
query = query.add_columns(
text("ST_Distance_Sphere(point(longitude, latitude), point(:lon, :lat)) as distance")
).params(lon=longitude, lat=latitude).order_by(text("distance"))
else:
query = query.order_by(MerchantDB.create_time.desc())
merchants = query.offset(skip).limit(limit).all()
# 处理返回结果
merchant_list = [{
**MerchantInfo.model_validate(m[0]).model_dump(),
"category_name": m[1],
"distance": round(m[2]) if longitude is not None and latitude is not None else None
} for m in merchants]
# 获取总数(需要考虑分类过滤)
total_query = db.query(MerchantDB)
if category_id is not None:
total_query = total_query.filter(MerchantDB.category_id == category_id)
total = total_query.count()
return success_response(data={
"total": total,
"items": merchant_list
})