feat(shipping-notice): 增强订单抽取功能,添加进度事件和诊断运行管理,优化数据模型和访问控制

This commit is contained in:
hugo committed 2026-07-21 23:17:29 +08:00
1 parent ce26d6d0c4
commit c4095b08da
175 files changed
+13064 -1814

No files matched your search

+51 -16
View File
@@ -21,6 +21,33 @@ from app.services.shipping_notice.reservation_id import (
looks_like_reservation_id,
normalize_reservation_id,
)
from app.services.shipping_notice.order_schema.table_contract import compile_table_header_contract
def _table_schema_definition() -> dict:
"""构造本文件表格样本使用的显式动态 Schema,禁止依赖生产硬编码别名。"""
aliases = {
'line_no': ['Line No'], 'part_number': ['Part Number', 'MPN', 'Vendor Part Number'],
'qty': ['Qty', 'Quantity', 'Order Qty', 'Allocated QTY'], 'unit_price': ['Unit Price'],
'amount': ['Amount'], 'currency': ['Currency'],
'reservation_id': ['Reservation ID', 'Reservation Order', 'Reservation N.r', 'External reservation ID'],
'required_date': ['ETA'], 'customer_part_number': ['Allocated HHPN'],
'aws_part_number': ['Allocated APN', 'Material', 'AWS Sku'],
'inventory_source_hint': ['source type'], 'po_number': ['PO'],
}
decimal_fields = {'qty', 'unit_price', 'amount'}
return {'fields': [{
'field_id': f'test-{code}', 'field_code': code,
'scope': 'header' if code == 'po_number' else 'line',
'value_type': 'decimal' if code in decimal_fields else 'date' if code == 'required_date' else 'string',
'enabled': True, 'display_name': code, 'aliases': values, 'normalizer_key': 'trim',
'validator_keys': ['positive_decimal'] if code == 'qty' else ['reservation_id'] if code == 'reservation_id' else [],
} for code, values in aliases.items()]}
def _table_contract():
"""编译测试共享的冻结表头契约。"""
return compile_table_header_contract(_table_schema_definition())
def _minimal_pdf_bytes(marker: str) -> bytes:
@@ -146,7 +173,7 @@ S-ENC-FOBU260702-286\t2000485916\tVMI-FRA01\tTEMP-S000218922\t504-006043-001\t30
S-BRA-FOBU260702-187\t2000485915\tVMI-FRA01\tTEMP-S000196207\t504-006076-001\t60\tD4-AR1200115A1\tFRA01\t4510039228
S-ENCFOX260705BG977W\t1000148820\tDBS DUB\tTEMP-S000218922\t504-006043-001\t1\tD4-A08AR12000001\tDUB\t4510039229
"""
rows = build_email_order_index(body)
rows = build_email_order_index(body, schema_definition=_table_schema_definition())
assert len(rows) == 3
assert rows[0]['po_number'] == '4510039227'
assert rows[0]['reservation_id'] == '2000485916'
@@ -1247,7 +1274,9 @@ def test_external_reservation_id_corrects_invalid_llm_candidate_with_column_evid
}
]
adjudicated, issues, status = adjudicate_notice_lines(lines, raw_text=raw_text)
adjudicated, issues, status = adjudicate_notice_lines(
lines, raw_text=raw_text, schema_definition=_table_schema_definition(),
)
assert status == "ready"
assert adjudicated[0]["reservation_id"] == "3000092917"
@@ -1280,7 +1309,9 @@ def test_flattened_text_does_not_guess_external_reservation_id():
}
]
adjudicated, issues, status = adjudicate_notice_lines(lines, raw_text=raw_text)
adjudicated, issues, status = adjudicate_notice_lines(
lines, raw_text=raw_text, schema_definition=_table_schema_definition(),
)
assert status == "needs_review"
assert "reservation_id" not in adjudicated[0]
@@ -1293,28 +1324,30 @@ def test_adjudication_supports_non_numeric_reservation_templates():
raw_text = "Part Number\tReservation Order\tQty\nA001\tAWS-311\t2"
lines = [{"part_number": "A001", "reservation_id": "", "qty": 2, "uom": "EA"}]
adjudicated, issues, status = adjudicate_notice_lines(lines, raw_text=raw_text)
adjudicated, issues, status = adjudicate_notice_lines(
lines, raw_text=raw_text, schema_definition=_table_schema_definition(),
)
assert status == "ready"
assert adjudicated[0]["reservation_id"] == "AWS-311"
assert issues[0]["reason"] == "value_corrected"
def test_wistron_po_table_with_pr_number_in_document():
def test_wistron_po_table_does_not_broadcast_document_reservation_to_every_line():
text = """
Vendor: HOXIN
PR Number: ADU_S-ENCWIW260528126265@1000140351
Line No\tMaterial\tVendor Part Number\tDescription\tQuantity\tETA\tUnit Price\tAmount
10\t504-010213-001\tB3-A08AR11000001\tENCLOSURE\t9.000 PCS\t2026/06/15\t1,607.72 USD\t14,469.48 USD
"""
lines = parse_notice_table_text(text)
lines = parse_notice_table_text(text, contract=_table_contract())
assert len(lines) == 1
assert lines[0]["part_number"] == "B3-A08AR11000001"
assert lines[0]["line_no"] == "10"
assert lines[0]["qty"] == 9.0
assert lines[0]["unit_price"] == 1607.72
assert lines[0]["amount"] == 14469.48
assert lines[0]["reservation_id"] == "1000140351"
assert "reservation_id" not in lines[0]
assert lines[0]["required_date"] == "2026-06-15"
assert lines[0]["aws_part_number"] == "504-010213-001"
assert lines[0]["currency"] == "USD"
@@ -1331,15 +1364,15 @@ def test_parse_notice_table_numeric_reservation_id():
Part Number\tReservation ID\tQty\tUnit Price\tAmount
10-40594-001\t1000140351\t2\t15.5\t31.0
"""
lines = parse_notice_table_text(text)
lines = parse_notice_table_text(text, contract=_table_contract())
assert lines[0]["reservation_id"] == "1000140351"
def test_infer_reservation_id_without_header():
def test_does_not_infer_reservation_id_without_configured_header():
headers = ["Part Number", "Ref", "Qty"]
rows = [["10-40594-001", 1000140351, 2]]
lines = parse_notice_table_rows(headers, rows)
assert lines[0]["reservation_id"] == "1000140351"
lines = parse_notice_table_rows(headers, rows, contract=_table_contract())
assert "reservation_id" not in lines[0]
def test_parse_notice_table_with_all_fields():
@@ -1348,7 +1381,7 @@ Part Number\tReservation ID\tQty\tUnit Price\tAmount
10-40594-001\tAWS-311\t2\t15.5\t31.0
10-40594-002\tORD-3002\t1\t20\t20
"""
lines = parse_notice_table_text(text)
lines = parse_notice_table_text(text, contract=_table_contract())
assert len(lines) == 2
assert lines[0]["part_number"] == "10-40594-001"
assert lines[0]["reservation_id"] == "AWS-311"
@@ -1375,7 +1408,6 @@ def test_normalize_notice_line_aliases():
"line_no": "10",
"part_number": "A-001",
"qty": 3.0,
"uom": "EA",
"reservation_id": "COM-99",
"unit_price": 12.5,
"amount": 37.5,
@@ -1412,6 +1444,7 @@ def test_parse_notice_table_maps_three_part_number_roles():
lines = parse_notice_table_rows(
["MPN", "Allocated HHPN", "Allocated APN", "Allocated QTY"],
[["D4-A08AR12000002", "TEMP-S000218929", "504-006055-001", 10]],
contract=_table_contract(),
)
assert lines == [{
@@ -1419,7 +1452,6 @@ def test_parse_notice_table_maps_three_part_number_roles():
"customer_part_number": "TEMP-S000218929",
"aws_part_number": "504-006055-001",
"qty": 10.0,
"uom": "EA",
}]
@@ -1428,6 +1460,7 @@ def test_parse_notice_table_rejects_aws_only_as_internal_part_number():
assert parse_notice_table_rows(
["AWS Sku", "Reservation ID", "Qty"],
[["504-006055-001", "3000092917", 10]],
contract=_table_contract(),
) == []
@@ -1500,7 +1533,7 @@ Shipping Notice
Part Number\tReservation Order\tOrder Qty\tUnit Price\tLine Amount
10-40594-001\tORD-3002\t4\t5\t20
"""
parsed, confidence, _ = _heuristic_extract(text)
parsed, confidence, _ = _heuristic_extract(text, schema_definition=_table_schema_definition())
assert len(parsed["lines"]) == 1
assert parsed["lines"][0]["part_number"] == "10-40594-001"
assert parsed["lines"][0]["reservation_id"] == "ORD-3002"
@@ -1530,7 +1563,9 @@ def test_extract_notice_fields_structured_over_heuristic():
def test_extract_notice_fields_heuristic_fallback():
"""无 structured 时走后端启发式兜底。"""
text = "Part Number\tReservation Order\tOrder Qty\n10-40594-001\tORD-3002\t4"
parsed, _, warnings = extract_notice_fields(raw_text=text)
parsed, _, warnings = extract_notice_fields(
raw_text=text, schema_definition=_table_schema_definition(),
)
assert parsed["parse_source"] == "heuristic"
assert parsed["lines"][0]["part_number"] == "10-40594-001"
assert "parse_fallback_heuristic" in warnings