修复数量增减及前后票连续追问

This commit is contained in:
2026-11-06 13:35:02 +08:00
parent bcfccc7013
commit 5fa6471929
14 changed files with 696 additions and 38 deletions
+11 -5
View File
@@ -23,7 +23,7 @@ FIELD_LABELS = {'inbound':'入仓码', 'number':'订单号', 'shipment':'Shipmen
'weight':'申报毛重', 'volume':'申报体积', 'role':'订单层级',
'parent':'父订单号', 'updated':'来源更新时间'}
IDENTIFIER = re.compile(r'(?<![A-Za-z0-9_-])[A-Za-z0-9][A-Za-z0-9_-]{5,63}(?![A-Za-z0-9_-])')
ORDER_DETAIL = r'状态|进度|轨迹|异常|到哪|开船|到港|清关|提柜|还柜|签收|预计|什么时候|何时|多久|重量|毛重|体积|件数|箱数|目的地|目的国|目的仓|仓库|父订单|运费|费用|价格|几件|几箱|公斤|千克|多少钱'
ORDER_DETAIL = r'状态|进度|轨迹|异常|到哪|开船|到港|清关|提柜|还柜|签收|预计|什么时候|何时|多久|重量|毛重|多重|重多少|体积|件数|箱数|目的地|目的国|目的仓|仓库|父订单|运费|费用|价格|几件|几箱|公斤|千克|多少钱'
class OrderDataUnavailable(Exception):
@@ -138,7 +138,7 @@ def tenant_filter(tenant_id=None, tenant_ids=None):
return (' AND tenant_id=?', [str(tenant)]) if tenant else ('', [])
def lookup_order(reference, tenant_id=None, tenant_ids=None):
def lookup_order(reference, tenant_id=None, tenant_ids=None, *, exact_order=False):
"""Exact, bounded lookup; scope comes from server configuration, never model SQL."""
from .db import connect
@@ -149,8 +149,9 @@ def lookup_order(reference, tenant_id=None, tenant_ids=None):
try:
connection = connect()
connection.row_factory = sqlite3.Row
query = 'SELECT * FROM order_test_data WHERE (order_no=? OR inbound_code=? OR shipment_id=?)' + scope[0]
values = [reference] * 3 + scope[1]
condition='order_no=?' if exact_order else '(order_no=? OR inbound_code=? OR shipment_id=?)'
query = 'SELECT * FROM order_test_data WHERE ' + condition + scope[0]
values = [reference] * (1 if exact_order else 3) + scope[1]
return [dict(row) for row in connection.execute(query+' ORDER BY id LIMIT 2', values)]
except sqlite3.Error:
raise OrderDataUnavailable from None
@@ -332,6 +333,7 @@ def is_order_followup(message):
def answer_query(message, conv_id):
references = extract_reference(message)
exact_order = False
tenants = customer_tenant_ids()
from .order_explanations import general_explanation, detail_question, explain_order
if not references:
@@ -345,6 +347,7 @@ def answer_query(message, conv_id):
if followup is not None:
if '_order_reference' not in followup:return followup
references=[followup['_order_reference']]
exact_order=True
count_intent = parse_count_query(message) if not references else None
if count_intent is not None:
return answer_count_query(count_intent,tenants,conv_id)
@@ -377,6 +380,7 @@ def answer_query(message, conv_id):
previous = json.loads(content[8:])
if previous.get('kind') == 'order':
reference = previous['no']
exact_order=True
break
except (ValueError, KeyError, TypeError):
pass
@@ -399,7 +403,9 @@ def answer_query(message, conv_id):
if not tenants:
return order_login_response()
try:
matches = lookup_order(reference,tenant_id=tenants[0]) if len(tenants) == 1 else lookup_order(reference,tenant_ids=tenants)
scope={'tenant_id':tenants[0]} if len(tenants)==1 else {'tenant_ids':tenants}
if exact_order:scope['exact_order']=True
matches = lookup_order(reference,**scope)
except OrderDataUnavailable:
return {'t':'订单信息暂时无法读取,请稍后重试。'}
if not matches: