diff --git a/app/api/endpoints/merchant_order.py b/app/api/endpoints/merchant_order.py index 7bec9d9..f9a5c7b 100644 --- a/app/api/endpoints/merchant_order.py +++ b/app/api/endpoints/merchant_order.py @@ -215,8 +215,49 @@ async def get_user_orders( "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) -async def cancel_order( +async def user_cancel_order( order_id: str, db: Session = Depends(get_db), current_user: UserDB = Depends(get_current_user) @@ -269,7 +310,7 @@ async def complete_order( if not order: 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="订单状态不正确") try: diff --git a/app/api/endpoints/merchant_product.py b/app/api/endpoints/merchant_product.py index cf694b9..cc6bdc8 100644 --- a/app/api/endpoints/merchant_product.py +++ b/app/api/endpoints/merchant_product.py @@ -79,7 +79,7 @@ async def update_product( @router.get("/list", response_model=ResponseModel) async def list_merchant_products( - merchant_id: Optional[int] = None, + user_id: Optional[int] = None, community_id: Optional[int] = None, skip: int = 0, limit: int = 20, @@ -90,10 +90,11 @@ async def list_merchant_products( query = db.query(MerchantProductDB).options( joinedload(MerchantProductDB.merchant) ) - - # 如果指定了商家ID,添加筛选条件 - if merchant_id: - query = query.filter(MerchantProductDB.merchant_id == merchant_id) + + if user_id: + merchant = db.query(MerchantDB).filter(MerchantDB.user_id == user_id).first() + if merchant: + query = query.filter(MerchantProductDB.merchant_id == merchant.id) if community_id: query = query.filter(MerchantProductDB.merchant.has(community_id=community_id)) diff --git a/jobs.sqlite b/jobs.sqlite index 6fec867..ef5c313 100644 Binary files a/jobs.sqlite and b/jobs.sqlite differ