From 2170e2e9cfdeb4093ab1021718a6106a1c0aca88 Mon Sep 17 00:00:00 2001 From: duxingchen Date: Wed, 12 Aug 2026 17:33:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=BD=AC=E4=BA=A4=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E4=B8=ADproduct=5Fsn=E5=A7=8B=E7=BB=88=E4=B8=BA=E7=A9=BA=20?= =?UTF-8?q?=E2=80=94=20product=E6=9F=A5=E8=AF=A2=E7=A7=BB=E5=88=B0?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E5=BE=AA=E7=8E=AF=E4=B9=8B=E5=89=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因: transfer_task中 product_result 查询在通知创建循环之后 通知内容 product_sn 引用未初始化的 product 变量 → 始终为空字符串 修复: 将 product 查询提前到 flush() 之后、通知循环之前 --- backend/app/services/task_service.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/backend/app/services/task_service.py b/backend/app/services/task_service.py index eaf2535..06d662b 100644 --- a/backend/app/services/task_service.py +++ b/backend/app/services/task_service.py @@ -816,6 +816,13 @@ async def transfer_task( # 批量 flush 以生成 ID await db.flush() + # 提前查询产品(通知需要 product_sn) + product_result = await db.execute( + select(Product).where(Product.id == task.product_id) + ) + product = product_result.scalar_one_or_none() + product_sn = product.serial_number if product else "" + for nt in created_tasks: await _create_task_log( db, nt.id, @@ -826,12 +833,6 @@ async def transfer_task( ) # 🔔 通知:新任务派发 if nt.assignee_id: - product_sn = "" - try: - if product: - product_sn = product.serial_number or "" - except Exception: - pass db.add(Notification( user_id=nt.assignee_id, title=f"🟢 新任务派发", @@ -841,10 +842,6 @@ async def transfer_task( )) # --- 更新 Product 的 current_location_id --- - product_result = await db.execute( - select(Product).where(Product.id == task.product_id) - ) - product = product_result.scalar_one_or_none() if product: if has_warehouse and not real_branches: product.current_location_id = VIRTUAL_WAREHOUSE