308 lines
10 KiB
Python
308 lines
10 KiB
Python
"""
|
|
电商订单API路由
|
|
"""
|
|
from typing import Any, List, Optional
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
from sqlalchemy import select, func, and_, or_
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from loguru import logger
|
|
|
|
from app.database import get_async_session
|
|
from app.models.order import Order
|
|
from app.schemas.order import (
|
|
OrderCreate,
|
|
OrderUpdate,
|
|
OrderResponse,
|
|
OrderListResponse,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=OrderListResponse)
|
|
async def list_orders(
|
|
page: int = Query(1, ge=1, description="页码"),
|
|
size: int = Query(10, ge=1, le=100, description="每页数量"),
|
|
order_id: str = Query(None, description="订单ID"),
|
|
streamer_id: str = Query(None, description="主播ID"),
|
|
ecommerce_platform: str = Query(None, description="电商平台"),
|
|
order_status: str = Query(None, description="订单状态"),
|
|
db: AsyncSession = Depends(get_async_session),
|
|
):
|
|
"""
|
|
获取电商订单列表(分页查询)
|
|
"""
|
|
logger.info(f"获取电商订单列表: page={page}, size={size}")
|
|
|
|
# 构建查询
|
|
query = select(Order)
|
|
|
|
# 添加过滤条件
|
|
conditions = []
|
|
if order_id:
|
|
conditions.append(Order.order_id.ilike(f"%{order_id}%"))
|
|
if streamer_id:
|
|
conditions.append(Order.streamer_id == streamer_id)
|
|
if ecommerce_platform:
|
|
conditions.append(Order.ecommerce_platform == ecommerce_platform)
|
|
if order_status:
|
|
conditions.append(Order.order_status == order_status)
|
|
|
|
if conditions:
|
|
query = query.where(and_(*conditions))
|
|
|
|
# 获取总数
|
|
count_query = select(func.count()).select_from(Order)
|
|
if conditions:
|
|
count_query = count_query.where(and_(*conditions))
|
|
|
|
total_result = await db.execute(count_query)
|
|
total = total_result.scalar()
|
|
|
|
# 分页
|
|
query = query.offset((page - 1) * size).limit(size)
|
|
|
|
# 执行查询
|
|
result = await db.execute(query)
|
|
records = result.scalars().all()
|
|
|
|
# 转换为响应格式
|
|
response_records = []
|
|
for order in records:
|
|
response_records.append({
|
|
"id": order.id,
|
|
"order_id": order.order_id,
|
|
"platform_order_no": order.platform_order_no,
|
|
"ecommerce_platform": order.ecommerce_platform,
|
|
"streamer_id": order.streamer_id,
|
|
"streamer_name": order.streamer_name,
|
|
"product_id": order.product_id,
|
|
"product_name": order.product_name,
|
|
"quantity": order.quantity,
|
|
"original_price": order.original_price,
|
|
"sale_price": order.sale_price,
|
|
"total_amount": order.total_amount,
|
|
"actual_payment": order.actual_payment,
|
|
"commission_ratio": order.commission_ratio,
|
|
"commission_amount": order.commission_amount,
|
|
"streamer_commission": order.streamer_commission,
|
|
"buyer_id": order.buyer_id,
|
|
"order_time": order.order_time.isoformat() if order.order_time else None,
|
|
"settlement_time": order.settlement_time.isoformat() if order.settlement_time else None,
|
|
"order_status": order.order_status,
|
|
"is_commission_settled": order.is_commission_settled,
|
|
"province": order.province,
|
|
"city": order.city,
|
|
})
|
|
|
|
return OrderListResponse(
|
|
records=response_records,
|
|
total=total,
|
|
page=page,
|
|
size=size,
|
|
)
|
|
|
|
|
|
@router.get("/{order_id}", response_model=OrderResponse)
|
|
async def get_order(order_id: str, db: AsyncSession = Depends(get_async_session)):
|
|
"""
|
|
根据ID获取电商订单详细信息
|
|
"""
|
|
logger.info(f"获取电商订单详情: {order_id}")
|
|
|
|
query = select(Order).where(Order.order_id == order_id)
|
|
result = await db.execute(query)
|
|
order = result.scalar_one_or_none()
|
|
|
|
if not order:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"电商订单不存在: {order_id}",
|
|
)
|
|
|
|
# 转换为响应格式
|
|
response = {
|
|
"id": order.id,
|
|
"order_id": order.order_id,
|
|
"platform_order_no": order.platform_order_no,
|
|
"ecommerce_platform": order.ecommerce_platform,
|
|
"streamer_id": order.streamer_id,
|
|
"streamer_name": order.streamer_name,
|
|
"product_id": order.product_id,
|
|
"product_name": order.product_name,
|
|
"quantity": order.quantity,
|
|
"original_price": order.original_price,
|
|
"sale_price": order.sale_price,
|
|
"total_amount": order.total_amount,
|
|
"actual_payment": order.actual_payment,
|
|
"commission_ratio": order.commission_ratio,
|
|
"commission_amount": order.commission_amount,
|
|
"streamer_commission": order.streamer_commission,
|
|
"buyer_id": order.buyer_id,
|
|
"order_time": order.order_time.isoformat() if order.order_time else None,
|
|
"settlement_time": order.settlement_time.isoformat() if order.settlement_time else None,
|
|
"order_status": order.order_status,
|
|
"is_commission_settled": order.is_commission_settled,
|
|
"province": order.province,
|
|
"city": order.city,
|
|
}
|
|
|
|
return response
|
|
|
|
|
|
@router.post("", response_model=OrderResponse, status_code=status.HTTP_201_CREATED)
|
|
async def create_order(order: OrderCreate, db: AsyncSession = Depends(get_async_session)):
|
|
"""
|
|
创建新的电商订单
|
|
"""
|
|
logger.info(f"创建电商订单: {order.platform_order_no}")
|
|
|
|
# 生成订单ID
|
|
query = select(func.count()).select_from(Order)
|
|
result = await db.execute(query)
|
|
count = result.scalar()
|
|
order_id = f"ORD{2024}{(count + 1):06d}"
|
|
|
|
# 创建新订单
|
|
new_order = Order(
|
|
order_id=order_id,
|
|
platform_order_no=order.platform_order_no,
|
|
ecommerce_platform=order.ecommerce_platform,
|
|
streamer_id=order.streamer_id,
|
|
streamer_name=order.streamer_name,
|
|
product_id=order.product_id,
|
|
product_name=order.product_name,
|
|
quantity=order.quantity,
|
|
original_price=order.original_price,
|
|
sale_price=order.sale_price,
|
|
total_amount=order.total_amount,
|
|
actual_payment=order.actual_payment,
|
|
commission_ratio=order.commission_ratio,
|
|
commission_amount=order.commission_amount,
|
|
streamer_commission=order.streamer_commission,
|
|
buyer_id=order.buyer_id,
|
|
order_time=order.order_time,
|
|
settlement_time=order.settlement_time,
|
|
order_status=order.order_status,
|
|
is_commission_settled=order.is_commission_settled,
|
|
province=order.province,
|
|
city=order.city,
|
|
)
|
|
|
|
db.add(new_order)
|
|
await db.commit()
|
|
await db.refresh(new_order)
|
|
|
|
# 转换为响应格式
|
|
response = {
|
|
"id": new_order.id,
|
|
"order_id": new_order.order_id,
|
|
"platform_order_no": new_order.platform_order_no,
|
|
"ecommerce_platform": new_order.ecommerce_platform,
|
|
"streamer_id": new_order.streamer_id,
|
|
"streamer_name": new_order.streamer_name,
|
|
"product_id": new_order.product_id,
|
|
"product_name": new_order.product_name,
|
|
"quantity": new_order.quantity,
|
|
"original_price": new_order.original_price,
|
|
"sale_price": new_order.sale_price,
|
|
"total_amount": new_order.total_amount,
|
|
"actual_payment": new_order.actual_payment,
|
|
"commission_ratio": new_order.commission_ratio,
|
|
"commission_amount": new_order.commission_amount,
|
|
"streamer_commission": new_order.streamer_commission,
|
|
"buyer_id": new_order.buyer_id,
|
|
"order_time": new_order.order_time.isoformat() if new_order.order_time else None,
|
|
"settlement_time": new_order.settlement_time.isoformat() if new_order.settlement_time else None,
|
|
"order_status": new_order.order_status,
|
|
"is_commission_settled": new_order.is_commission_settled,
|
|
"province": new_order.province,
|
|
"city": new_order.city,
|
|
}
|
|
|
|
return response
|
|
|
|
|
|
@router.put("/{order_id}", response_model=OrderResponse)
|
|
async def update_order(
|
|
order_id: str,
|
|
order_update: OrderUpdate,
|
|
db: AsyncSession = Depends(get_async_session),
|
|
):
|
|
"""
|
|
更新电商订单
|
|
"""
|
|
logger.info(f"更新电商订单: {order_id}")
|
|
|
|
query = select(Order).where(Order.order_id == order_id)
|
|
result = await db.execute(query)
|
|
order = result.scalar_one_or_none()
|
|
|
|
if not order:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"电商订单不存在: {order_id}",
|
|
)
|
|
|
|
# 更新字段
|
|
update_data = order_update.model_dump(exclude_unset=True)
|
|
for key, value in update_data.items():
|
|
if hasattr(order, key):
|
|
setattr(order, key, value)
|
|
|
|
await db.commit()
|
|
await db.refresh(order)
|
|
|
|
# 转换为响应格式
|
|
response = {
|
|
"id": order.id,
|
|
"order_id": order.order_id,
|
|
"platform_order_no": order.platform_order_no,
|
|
"ecommerce_platform": order.ecommerce_platform,
|
|
"streamer_id": order.streamer_id,
|
|
"streamer_name": order.streamer_name,
|
|
"product_id": order.product_id,
|
|
"product_name": order.product_name,
|
|
"quantity": order.quantity,
|
|
"original_price": order.original_price,
|
|
"sale_price": order.sale_price,
|
|
"total_amount": order.total_amount,
|
|
"actual_payment": order.actual_payment,
|
|
"commission_ratio": order.commission_ratio,
|
|
"commission_amount": order.commission_amount,
|
|
"streamer_commission": order.streamer_commission,
|
|
"buyer_id": order.buyer_id,
|
|
"order_time": order.order_time.isoformat() if order.order_time else None,
|
|
"settlement_time": order.settlement_time.isoformat() if order.settlement_time else None,
|
|
"order_status": order.order_status,
|
|
"is_commission_settled": order.is_commission_settled,
|
|
"province": order.province,
|
|
"city": order.city,
|
|
}
|
|
|
|
return response
|
|
|
|
|
|
@router.delete("/{order_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_order(order_id: str, db: AsyncSession = Depends(get_async_session)):
|
|
"""
|
|
删除电商订单(软删除)
|
|
"""
|
|
logger.info(f"删除电商订单: {order_id}")
|
|
|
|
query = select(Order).where(Order.order_id == order_id)
|
|
result = await db.execute(query)
|
|
order = result.scalar_one_or_none()
|
|
|
|
if not order:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"电商订单不存在: {order_id}",
|
|
)
|
|
|
|
# 软删除 - 设置为已取消状态
|
|
order.order_status = "cancelled"
|
|
await db.commit()
|
|
|
|
return None
|