diff --git a/app/api/endpoints/order.py b/app/api/endpoints/order.py index 7eed272..bdbd5ab 100644 --- a/app/api/endpoints/order.py +++ b/app/api/endpoints/order.py @@ -20,7 +20,7 @@ from app.api.deps import get_current_user, get_deliveryman_user, get_admin_user from app.models.user import UserDB,UserRole from app.core.response import success_response, error_response, ResponseModel from app.models.coupon import UserCouponDB, CouponStatus -from app.models.point_product_order import PointProductOrderDB, PointProductOrderInfo +from app.models.point_product_order import PointProductOrderDB, PointProductOrderInfo, PointProductOrderStatus from datetime import datetime, timezone from app.core.config import settings from app.models.address import AddressDB @@ -35,6 +35,7 @@ from sqlalchemy import func from app.core.mpclient import mp_client from datetime import timedelta from app.core.imageprocessor import process_image, ImageFormat +from app.core.point_manager import PointManager router = APIRouter() @@ -604,21 +605,37 @@ async def cancel_order( if coupon: coupon.status = CouponStatus.UNUSED + # 检查子订单是否全部取消 + sub_orders = db.query(PointProductOrderDB).filter( + PointProductOrderDB.delivery_order_id == order.orderid + ).all() + + if sub_orders: + for sub_order in sub_orders: + sub_order.status = PointProductOrderStatus.CANCELLED + #返还积分 + point_manager = PointManager(db) + point_manager.add_points( + user_id=current_user.userid, + points=sub_order.order_point_amount, + description=f"兑换订单取消返还", + order_id=order.orderid + ) + + # 如果使用了积分,返还积分 if order.point_discount_amount > 0: # 返还积分 return_points = int(order.point_discount_amount * settings.POINT_RATIO) - current_user.points += return_points - # 记录积分变动 - point_record = PointRecordDB( + point_manager = PointManager(db) + point_manager.add_points( user_id=current_user.userid, points=return_points, - type=PointRecordType.CONSUME_RETURN, - order_id=order.orderid, - description=f"订单取消返还积分" + description=f"配送订单取消返还", + order_id=order.orderid ) - db.add(point_record) + db.commit() # 发送模板消息 @@ -769,26 +786,36 @@ async def deliveryman_cancel_order( if coupon: coupon.status = CouponStatus.UNUSED + # 检查子订单是否全部取消 + sub_orders = db.query(PointProductOrderDB).filter( + PointProductOrderDB.delivery_order_id == order.orderid, + PointProductOrderDB.status != PointProductOrderStatus.CANCELLED + ).all() + + if sub_orders: + for sub_order in sub_orders: + sub_order.status = PointProductOrderStatus.CANCELLED + #返还积分 + point_manager = PointManager(db) + point_manager.add_points( + user_id=order.userid, + points=sub_order.order_point_amount, + description=f"兑换订单取消返还", + order_id=order.orderid + ) + # 如果使用了积分,返还积分 if order.point_discount_amount > 0: # 返还积分 return_points = int(order.point_discount_amount * settings.POINT_RATIO) - - order_user = db.query(UserDB).filter( - UserDB.userid == order.userid - ).first() - order_user.points += return_points - - # 记录积分变动 - point_record = PointRecordDB( - user_id=order_user.userid, + point_manager = PointManager(db) + point_manager.add_points( + user_id=order.userid, points=return_points, - type=PointRecordType.CONSUME_RETURN, - order_id=order.orderid, - description=f"订单取消返还积分" + description=f"配送订单取消返还", + order_id=order.orderid ) - db.add(point_record) db.commit() @@ -919,6 +946,11 @@ async def receive_order( if order.deliveryman_user_id is not None: return error_response(code=400, message="订单已被其他配送员接单") + # 检查子订单是否全部处理 + sub_orders = db.query(PointProductOrderDB).filter( + PointProductOrderDB.delivery_order_id == order.orderid + ).all() + try: # 更新订单状态和配送员ID order.status = OrderStatus.RECEIVED @@ -978,6 +1010,15 @@ async def pickup_order( if order.status != OrderStatus.RECEIVED: return error_response(code=400, message="只有已接单的订单才能标记为已取货") + # 检查子订单是否全部处理 + sub_orders = db.query(PointProductOrderDB).filter( + PointProductOrderDB.delivery_order_id == order.orderid + ).all() + + for sub_order in sub_orders: + if sub_order.status != PointProductOrderStatus.PENDING: + return error_response(code=400, message="请先处理兑换商品子订单") + try: # 更新订单状态为配送中 order.status = OrderStatus.DELIVERING