diff --git a/app/api/endpoints/order.py b/app/api/endpoints/order.py index 39e7740..ac0ea51 100644 --- a/app/api/endpoints/order.py +++ b/app/api/endpoints/order.py @@ -259,6 +259,32 @@ async def pre_order( logging.exception(f"预下单失败: {str(e)}") return error_response(code=500, message=f"预下单失败: {str(e)}") +async def order_created_callback(order: OrderInfo, db: Session): + """订单创建成功回调""" + + # 发送企业微信消息 + wecom_bot = WecomBot() + wecom_bot.send_order_notification(db, order, OrderStatus.CREATED) + + current_user = db.query(UserDB).filter(UserDB.userid == order.userid).first() + + # 发送公众号消息 + if current_user.mp_openid: + data={ + "character_string13": order.orderid, + "time4": order.create_time.strftime("%Y-%m-%d %H:%M:%S"), + "thing10": order.address_customer_name, + "thing15": f"{order.address_community_name} {order.address_community_building_name} {order.address_detail}", + "thing9": order.status.status_text + } + sent_order_status_change_message(db, order, current_user.mp_openid, settings.DELIVERY_ORDER_CREATED_TEMPLATE_ID) + + # 添加到新订单队列 + redis_client.push_new_order_to_queue(order.address_community_id, order.orderid, db) + + # 添加到今日选中的配送时段的 Redis 消息队列 + redis_client.push_order_to_community_period_queue(order.delivery_date, order.address_community_id, order.time_period_id, order.orderid) + @router.post("", response_model=ResponseModel) async def create_order( background_tasks: BackgroundTasks, @@ -326,7 +352,7 @@ async def create_order( more_station_price=price_info.more_station_price, coupon_id=coupon_id, final_amount=price_info.final_amount, - status=OrderStatus.CREATED, + status=OrderStatus.UNPAID if price_info.final_amount > 0 else OrderStatus.CREATED, delivery_method=order.delivery_method, delivery_date=order.delivery_date, is_first_order=is_first_order @@ -408,61 +434,16 @@ async def create_order( db.commit() db.refresh(db_order) - # 查询包裹信息 - packages = db.query(ShippingOrderPackageDB).filter( - ShippingOrderPackageDB.orderid == orderid - ).all() - - # 发送企业微信消息 - wecom_bot = WecomBot() - order_info = OrderInfo.model_validate(db_order) - background_tasks.add_task( - wecom_bot.send_order_notification, - db, - order_info, - OrderStatus.CREATED - ) - - #发送订单创建成功的公众号消息 - if current_user.mp_openid: - data={ - "character_string13": db_order.orderid, - "time4": db_order.create_time.strftime("%Y-%m-%d %H:%M:%S"), - "thing10": db_order.address_customer_name, - "thing15": f"{db_order.address_community_name} {db_order.address_community_building_name} {db_order.address_detail}", - "thing9": db_order.status.status_text - } - - background_tasks.add_task( - sent_order_status_change_message, - openid=current_user.mp_openid, - template_id=settings.DELIVERY_ORDER_CREATED_TEMPLATE_ID, - data=data, - orderid=db_order.orderid - ) - - # 添加到新订单队列 - if db_order.address_community_id: - background_tasks.add_task( - redis_client.push_new_order_to_queue, - db_order.address_community_id, - db_order.orderid, - db - ) - - # 今日订单加入今日选中的配送时段的 Redis 消息队列 - background_tasks.add_task( - redis_client.push_order_to_community_period_queue, - db_order.delivery_date, - order.community_time_period_id, - db_order.orderid - ) + # 如不需要支付,直接创建订单成功 + if db_order.status == OrderStatus.CREATED: + background_tasks.add_task(order_created_callback, OrderInfo.model_validate(db_order), db) + return success_response( message="订单创建成功", data={ "order": OrderInfo.model_validate(db_order), - "packages": [OrderPackageInfo.model_validate(p) for p in packages], + # "packages": [OrderPackageInfo.model_validate(p) for p in packages], "delivery_time": format_delivery_time(db_order.delivery_date, db_order.time_period_name), "success_text" : f"预计 {format_delivery_time(db_order.delivery_date, db_order.time_period_name)} 送达,请注意查收" } diff --git a/app/api/endpoints/wechat.py b/app/api/endpoints/wechat.py index b4957de..33b6a2a 100644 --- a/app/api/endpoints/wechat.py +++ b/app/api/endpoints/wechat.py @@ -24,7 +24,9 @@ from app.core.point_manager import PointRecordType from app.core.account import AccountManager import logging from app.core.security import get_password_hash - +from app.api.endpoints.order import order_created_callback +from app.models.order import OrderInfo +from fastapi import BackgroundTasks router = APIRouter() class PhoneNumberRequest(BaseModel): @@ -182,6 +184,7 @@ async def create_payment( @router.post("/payment/notify") async def payment_notify( + background_tasks: BackgroundTasks, request: Request, db: Session = Depends(get_db) ): @@ -223,11 +226,13 @@ async def payment_notify( if trade_state_desc == "支付成功": # 更新订单状态 order.pay_status = True - order.status = OrderStatus.COMPLETED # 支付成功后状态为已完成 + order.status = OrderStatus.CREATED # 支付成功后状态为已创建 order.completed_time = datetime.now() order.transaction_id = transaction_id order.pay_time = datetime.fromisoformat(success_time.replace('Z', '+00:00')) - + + # 回调订单创建成功 + order_created_callback(OrderInfo.model_validate(order), db) elif out_trade_no.startswith("M"): # 商家商品订单 # 查询商户订单 diff --git a/app/core/redis_client.py b/app/core/redis_client.py index 23049b2..d71542d 100644 --- a/app/core/redis_client.py +++ b/app/core/redis_client.py @@ -38,13 +38,13 @@ class RedisClient: return self.client - def push_order_to_community_period_queue(self, delivery_date: date,community_time_period_id: int, order_id: str) -> bool: + def push_order_to_community_period_queue(self, delivery_date: date,community_id: int, time_period_id: int, order_id: str) -> bool: """ 添加新订单到今日队列 """ try: today_date_str = delivery_date.strftime("%Y-%m-%d") - key = f"community_period:{community_time_period_id}:today_orders:{today_date_str}" + key = f"community_period:{community_id}:{time_period_id}:today_orders:{today_date_str}" self.client.lpush(key, order_id) # 设置过期时间为24小时 diff --git a/jobs.sqlite b/jobs.sqlite index ef5c313..53aeaad 100644 Binary files a/jobs.sqlite and b/jobs.sqlite differ