merge main
This commit is contained in:
@@ -289,17 +289,36 @@ async def alipay_callback(request: Request, db: AsyncSession = Depends(get_db)):
|
||||
async def list_orders(
|
||||
page: int = Query(1, ge=1),
|
||||
page_size: int = Query(20, ge=1, le=100),
|
||||
status_filter: str | None = Query(None, description="按状态筛选: pending/paid/refunded/failed/cancelled"),
|
||||
start_date: str | None = Query(None, description="创建时间起始,格式 YYYY-MM-DD"),
|
||||
end_date: str | None = Query(None, description="创建时间结束,格式 YYYY-MM-DD"),
|
||||
invoice_mode: bool = Query(False, description="开票模式:仅返回已支付订单"),
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
from app.services.payment import _check_and_expire_order
|
||||
from datetime import datetime, timezone, timedelta
|
||||
|
||||
count_query = select(func.count(PaymentOrder.id)).where(PaymentOrder.user_id == current_user.id)
|
||||
# 构建筛选条件
|
||||
conditions = [PaymentOrder.user_id == current_user.id]
|
||||
if status_filter:
|
||||
conditions.append(PaymentOrder.status == status_filter)
|
||||
if invoice_mode:
|
||||
conditions.append(PaymentOrder.status == "paid")
|
||||
if start_date:
|
||||
start_dt = datetime.strptime(start_date, "%Y-%m-%d").replace(tzinfo=timezone.utc)
|
||||
conditions.append(PaymentOrder.created_at >= start_dt)
|
||||
if end_date:
|
||||
end_dt = (datetime.strptime(end_date, "%Y-%m-%d") + timedelta(days=1)).replace(tzinfo=timezone.utc)
|
||||
conditions.append(PaymentOrder.created_at < end_dt)
|
||||
|
||||
# 统计总数
|
||||
count_query = select(func.count(PaymentOrder.id)).where(*conditions)
|
||||
total = (await db.execute(count_query)).scalar() or 0
|
||||
|
||||
result = await db.execute(
|
||||
select(PaymentOrder)
|
||||
.where(PaymentOrder.user_id == current_user.id)
|
||||
.where(*conditions)
|
||||
.order_by(PaymentOrder.created_at.desc())
|
||||
.offset((page - 1) * page_size)
|
||||
.limit(page_size)
|
||||
@@ -308,7 +327,37 @@ async def list_orders(
|
||||
for o in orders:
|
||||
await _check_and_expire_order(db, o)
|
||||
|
||||
return {"items": [PaymentOrderOut.model_validate(o) for o in orders], "total": total}
|
||||
# 开票模式:附带订单占用状态
|
||||
items = []
|
||||
if invoice_mode:
|
||||
# 收集当前页订单ID
|
||||
order_ids = [o.id for o in orders]
|
||||
# 查询这些订单是否已被占用
|
||||
from app.models.invoice import Invoice, InvoiceOrder
|
||||
occupied_map: dict[str, str] = {}
|
||||
if order_ids:
|
||||
occ_result = await db.execute(
|
||||
select(InvoiceOrder.order_id, Invoice.invoice_no)
|
||||
.join(Invoice, InvoiceOrder.invoice_id == Invoice.id)
|
||||
.where(
|
||||
InvoiceOrder.order_id.in_(order_ids),
|
||||
Invoice.status.in_(["processing", "success"]),
|
||||
)
|
||||
)
|
||||
for row in occ_result.all():
|
||||
occupied_map[row.order_id] = row.invoice_no
|
||||
for o in orders:
|
||||
item = PaymentOrderOut.model_validate(o)
|
||||
item_dict = item.model_dump()
|
||||
item_dict["is_occupied"] = o.id in occupied_map
|
||||
item_dict["occupied_by"] = occupied_map.get(o.id)
|
||||
items.append(item_dict)
|
||||
else:
|
||||
for o in orders:
|
||||
item = PaymentOrderOut.model_validate(o)
|
||||
items.append(item.model_dump())
|
||||
|
||||
return {"items": items, "total": total}
|
||||
|
||||
|
||||
@router.get("/orders/{order_no}", response_model=PaymentOrderOut)
|
||||
|
||||
Reference in New Issue
Block a user