"""Local, editable order test data. This repository can later be replaced by MCP.""" import json import re import sqlite3 from datetime import datetime from .config import get_setting from .order_tracking import build_tracking from .visitor_service import customer_tenant_id FIELDS = ('source_order_id', 'tenant_id', 'order_no', 'inbound_code', 'shipment_id', 'status', 'country_code', 'transport_mode', 'warehouse_code', 'order_role', 'parent_order_no', 'package_count', 'gross_weight_kg', 'volume_cbm', 'history_json', 'tracking_json', 'data_note', 'source_updated_at', 'snapshot_at') STATUS_LABELS = {'DRAFT': '草稿', 'SUBMITTED': '已提交', 'APPROVED': '审核通过', 'ACCEPTED': '已接单', 'REJECTED': '审核驳回', 'PROCESSING': '履约中', 'COMPLETED': '头程订单已完成', 'CANCELLED': '已取消'} FIELD_LABELS = {'inbound':'入仓码', 'shipment':'Shipment ID', 'country':'目的国', 'warehouse':'目的仓', 'transport':'运输方式', 'packages':'件数', 'weight':'申报毛重', 'volume':'申报体积', 'role':'订单层级', 'parent':'父订单号', 'updated':'来源更新时间'} IDENTIFIER = re.compile(r'(? 1: return {'t':'请一次提供一个订单号、入仓码或 Shipment ID,避免查错订单。'} reference = references[0] if references else None if not reference and conv_id and is_order_followup(message): from .db import connect connection = connect() try: recent = connection.execute('SELECT role,content FROM messages WHERE conv_id=? ORDER BY id DESC LIMIT 12', (conv_id,)).fetchall() finally: connection.close() for role, content in recent: if role == 'user': earlier_references = extract_reference(content) if earlier_references: reference = earlier_references[0] if len(earlier_references) == 1 else None break if not is_order_followup(content): break if role == 'assistant' and content.startswith('__CARD__'): try: previous = json.loads(content[8:]) if previous.get('kind') == 'order': reference = previous['no'] break except (ValueError, KeyError, TypeError): pass if not reference: if re.search(r'查.*(?:订单|运单|物流轨迹)|订单状态|物流轨迹|shipment\s*id|(?:列出|搜索|所有|全部|多少).*(?:订单|入仓)|订单.*(?:多少|列表)', message, re.I): return {'t':ORDER_LOGIN_TEXT if tenant is None else '请提供订单号、入仓码或 Shipment ID。'} return None if tenant is None: return {'t': ORDER_LOGIN_TEXT} try: matches = lookup_order(reference, tenant_id=tenant) except OrderDataUnavailable: return {'t':'订单信息暂时无法读取,请稍后重试。'} if not matches: return {'t':f'未找到“{reference}”对应的订单。请核对订单号、入仓码或 Shipment ID,或联系人工客服。'} if len(matches) > 1: return {'t':'该标识匹配到多个订单,请提供该订单唯一的订单号或入仓码。'} if re.search(r'运费|费用|多少钱|价格', message): return {'t':'该订单未记录实际运费,请联系人工客服核实。'} return {'card': build_card(matches[0])}