This commit is contained in:
aaron 2025-03-28 14:44:48 +08:00
parent b06457f0aa
commit b82d5c5374
3 changed files with 49 additions and 7 deletions

View File

@ -215,8 +215,49 @@ async def get_user_orders(
"items": result "items": result
}) })
class MerchantCancelOrderRequest(BaseModel):
reason: str
@router.put("/{order_id}/merchant/cancel", response_model=ResponseModel)
async def merchant_cancel_order(
order_id: str,
request: MerchantCancelOrderRequest,
db: Session = Depends(get_db),
merchant_user: UserDB = Depends(get_merchant_user)
):
"""取消订单"""
merchant = db.query(MerchantDB).filter(
MerchantDB.user_id == merchant_user.userid
).first()
if not merchant:
return error_response(code=404, message="商家不存在")
order = db.query(MerchantOrderDB).filter(
MerchantOrderDB.order_id == order_id,
MerchantOrderDB.merchant_id == merchant.id
).first()
if not order:
return error_response(code=404, message="订单不存在")
try:
order.status = MerchantOrderStatus.REFUNDING
db.commit()
wechat = WeChatClient()
await wechat.apply_refund(
order_id=order.order_id,
total_amount=int(float(order.pay_amount) * 100) if not settings.DEBUG else 1, # 转换为分
reason="商家取消订单"
)
return success_response(data=MerchantOrderInfo.model_validate(order))
except Exception as e:
db.rollback()
return error_response(code=500, message=f"取消订单失败: {str(e)}")
@router.put("/{order_id}/user/cancel", response_model=ResponseModel) @router.put("/{order_id}/user/cancel", response_model=ResponseModel)
async def cancel_order( async def user_cancel_order(
order_id: str, order_id: str,
db: Session = Depends(get_db), db: Session = Depends(get_db),
current_user: UserDB = Depends(get_current_user) current_user: UserDB = Depends(get_current_user)
@ -269,7 +310,7 @@ async def complete_order(
if not order: if not order:
return error_response(code=404, message="订单不存在") return error_response(code=404, message="订单不存在")
if order.status not in [MerchantOrderStatus.DELIVERING]: if order.status not in [MerchantOrderStatus.DELIVERING, MerchantOrderStatus.PICKUP_READY]:
return error_response(code=400, message="订单状态不正确") return error_response(code=400, message="订单状态不正确")
try: try:

View File

@ -79,7 +79,7 @@ async def update_product(
@router.get("/list", response_model=ResponseModel) @router.get("/list", response_model=ResponseModel)
async def list_merchant_products( async def list_merchant_products(
merchant_id: Optional[int] = None, user_id: Optional[int] = None,
community_id: Optional[int] = None, community_id: Optional[int] = None,
skip: int = 0, skip: int = 0,
limit: int = 20, limit: int = 20,
@ -90,10 +90,11 @@ async def list_merchant_products(
query = db.query(MerchantProductDB).options( query = db.query(MerchantProductDB).options(
joinedload(MerchantProductDB.merchant) joinedload(MerchantProductDB.merchant)
) )
# 如果指定了商家ID添加筛选条件 if user_id:
if merchant_id: merchant = db.query(MerchantDB).filter(MerchantDB.user_id == user_id).first()
query = query.filter(MerchantProductDB.merchant_id == merchant_id) if merchant:
query = query.filter(MerchantProductDB.merchant_id == merchant.id)
if community_id: if community_id:
query = query.filter(MerchantProductDB.merchant.has(community_id=community_id)) query = query.filter(MerchantProductDB.merchant.has(community_id=community_id))

Binary file not shown.