取消配送订单,要同时取消子订单。

This commit is contained in:
aaron 2025-02-20 18:26:25 +08:00
parent d8b0afa0ad
commit 683aa8e2c2

View File

@ -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.models.user import UserDB,UserRole
from app.core.response import success_response, error_response, ResponseModel from app.core.response import success_response, error_response, ResponseModel
from app.models.coupon import UserCouponDB, CouponStatus 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 datetime import datetime, timezone
from app.core.config import settings from app.core.config import settings
from app.models.address import AddressDB from app.models.address import AddressDB
@ -35,6 +35,7 @@ from sqlalchemy import func
from app.core.mpclient import mp_client from app.core.mpclient import mp_client
from datetime import timedelta from datetime import timedelta
from app.core.imageprocessor import process_image, ImageFormat from app.core.imageprocessor import process_image, ImageFormat
from app.core.point_manager import PointManager
router = APIRouter() router = APIRouter()
@ -604,21 +605,37 @@ async def cancel_order(
if coupon: if coupon:
coupon.status = CouponStatus.UNUSED 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: if order.point_discount_amount > 0:
# 返还积分 # 返还积分
return_points = int(order.point_discount_amount * settings.POINT_RATIO) return_points = int(order.point_discount_amount * settings.POINT_RATIO)
current_user.points += return_points
# 记录积分变动 point_manager = PointManager(db)
point_record = PointRecordDB( point_manager.add_points(
user_id=current_user.userid, user_id=current_user.userid,
points=return_points, points=return_points,
type=PointRecordType.CONSUME_RETURN, description=f"配送订单取消返还",
order_id=order.orderid, order_id=order.orderid
description=f"订单取消返还积分"
) )
db.add(point_record)
db.commit() db.commit()
# 发送模板消息 # 发送模板消息
@ -769,26 +786,36 @@ async def deliveryman_cancel_order(
if coupon: if coupon:
coupon.status = CouponStatus.UNUSED 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: if order.point_discount_amount > 0:
# 返还积分 # 返还积分
return_points = int(order.point_discount_amount * settings.POINT_RATIO) return_points = int(order.point_discount_amount * settings.POINT_RATIO)
order_user = db.query(UserDB).filter( point_manager = PointManager(db)
UserDB.userid == order.userid point_manager.add_points(
).first() user_id=order.userid,
order_user.points += return_points
# 记录积分变动
point_record = PointRecordDB(
user_id=order_user.userid,
points=return_points, points=return_points,
type=PointRecordType.CONSUME_RETURN, description=f"配送订单取消返还",
order_id=order.orderid, order_id=order.orderid
description=f"订单取消返还积分"
) )
db.add(point_record)
db.commit() db.commit()
@ -919,6 +946,11 @@ async def receive_order(
if order.deliveryman_user_id is not None: if order.deliveryman_user_id is not None:
return error_response(code=400, message="订单已被其他配送员接单") return error_response(code=400, message="订单已被其他配送员接单")
# 检查子订单是否全部处理
sub_orders = db.query(PointProductOrderDB).filter(
PointProductOrderDB.delivery_order_id == order.orderid
).all()
try: try:
# 更新订单状态和配送员ID # 更新订单状态和配送员ID
order.status = OrderStatus.RECEIVED order.status = OrderStatus.RECEIVED
@ -978,6 +1010,15 @@ async def pickup_order(
if order.status != OrderStatus.RECEIVED: if order.status != OrderStatus.RECEIVED:
return error_response(code=400, message="只有已接单的订单才能标记为已取货") 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: try:
# 更新订单状态为配送中 # 更新订单状态为配送中
order.status = OrderStatus.DELIVERING order.status = OrderStatus.DELIVERING