feat(shipping-notice): 实现滚动履约功能,优化发货通知流程与代码清理

清理废弃的ShippingCase相关代码与依赖,统一使用ShippingNotice直接关联客户数据。调整库存网关依赖规则,支持仅对库存满足或已人工放行的订单行生成发货Excel,实现滚动履约逻辑。
This commit is contained in:
hugo committed 2026-08-26 02:47:07 +08:00
1 parent 7cfd5d167e
commit 1e5aedb9ed
235 files changed
+11394 -9613

No files matched your search

@@ -2,7 +2,7 @@
backend/tests/test_shipping_customer_auto_resolution.py
用途:验证受控自动客户确认、降级开关、强规则歧义和编号后客户纠错。
依赖:客户主数据/证据、Candidate 影子结果、ShippingNotice/Case、customer_auto_resolution。
依赖:客户主数据/证据、Candidate 影子结果、ShippingNotice、customer_auto_resolution。
逻辑:构造真实 ORM 证据后直接调用领域服务,确认仅唯一已审核强规则可自动编号。
"""
@@ -14,7 +14,6 @@ from app import db
from app.core.errors import ConflictError, ValidationError
from app.models import (
FileAsset,
ShippingCase,
ShippingCustomer,
ShippingCustomerShadowResolution,
ShippingIdentityDefinition,
@@ -122,11 +121,10 @@ def _seed_candidate_task(*, sender: str = "buyer@wyc.example.com"):
candidate_key="document:1", validation_status="ready", selected=True,
structured_result_json={"business_summary": {"customer_name": "unstable name"}},
)
case = ShippingCase(case_no=f"AUTO-CASE-{user.id}", numbering_status="pending")
db.session.add_all([candidate, case])
db.session.add(candidate)
db.session.flush()
task = ShippingNotice(
task_no=f"SN-AUTO-{user.id}", shipping_case_id=case.id, source_candidate_id=candidate.id,
task_no=f"SN-AUTO-{user.id}", source_candidate_id=candidate.id,
user_id=user.id, source_type="email_attachment",
customer_resolution_status="pending",
)
@@ -216,8 +214,8 @@ def test_unique_approved_strong_rule_auto_confirms_and_numbers(app):
assert result["rule_version"] == AUTO_RESOLUTION_VERSION
assert task.customer_resolution_status == "confirmed"
assert task.customer_resolution_method == "auto_rule"
assert task.shipping_case.customer_id == customer.id
assert task.shipping_case.business_no.startswith("WYC")
assert task.customer_id == customer.id
assert task.business_no.startswith("WYC")
assert ShippingTaskEvent.query.filter_by(event_type="customer_auto_confirmed").count() == 1
event = ShippingTaskEvent.query.filter_by(event_type="customer_auto_confirmed").one()
assert event.detail_json['rule_set']['checksum']
@@ -240,13 +238,13 @@ def test_switch_off_and_unapproved_rule_fall_back_to_manual(app):
app.config["SHIPPING_NOTICE_AUTO_CUSTOMER_CONFIRM_ENABLED"] = False
disabled = try_auto_confirm_task_customer(task)
assert disabled["status"] == "disabled"
assert task.shipping_case.business_no is None
assert task.business_no is None
_allow_auto_confirmation(app)
unmatched = try_auto_confirm_task_customer(task)
assert unmatched["status"] == "unmatched"
assert task.customer_resolution_status == "pending"
assert task.shipping_case.business_no is None
assert task.business_no is None
def test_multiple_customers_matching_strong_evidence_is_ambiguous(app):
@@ -261,7 +259,7 @@ def test_multiple_customers_matching_strong_evidence_is_ambiguous(app):
assert result["status"] == "ambiguous"
assert len(result["candidate_customer_ids"]) == 2
assert task.customer_resolution_status == "pending"
assert task.shipping_case.business_no is None
assert task.business_no is None
def test_auto_confirmation_can_be_rolled_back_by_outer_transaction(app):
@@ -271,7 +269,6 @@ def test_auto_confirmation_can_be_rolled_back_by_outer_transaction(app):
_customer_with_identifier(code="WYC", value="buyer@wyc.example.com")
task = _seed_candidate_task()
task_id = task.id
case_id = task.shipping_case_id
result = try_auto_confirm_task_customer(task)
assert result["status"] == "auto_confirmed"
@@ -279,10 +276,9 @@ def test_auto_confirmation_can_be_rolled_back_by_outer_transaction(app):
db.session.expire_all()
persisted_task = db.session.get(ShippingNotice, task_id)
persisted_case = db.session.get(ShippingCase, case_id)
assert persisted_task.customer_resolution_status == "pending"
assert persisted_case.customer_id is None
assert persisted_case.business_no is None
assert persisted_task.customer_id is None
assert persisted_task.business_no is None
assert ShippingTaskEvent.query.filter_by(event_type="customer_auto_confirmed").count() == 0
@@ -300,7 +296,7 @@ def test_quality_gate_blocks_auto_confirmation_when_labeled_samples_are_insuffic
assert result["status"] == "insufficient_labeled_samples"
assert result["quality_gate"]["labeled_sample_count"] == 0
assert task.customer_resolution_status == "pending"
assert task.shipping_case.business_no is None
assert task.business_no is None
def test_business_evidence_never_auto_confirms(app):
@@ -405,61 +401,48 @@ def test_quality_gate_blocks_auto_confirmation_when_accuracy_is_insufficient(app
assert result["quality_gate"]["labeled_sample_count"] == 1
assert result["quality_gate"]["accuracy"] == 0
assert task.customer_resolution_status == "pending"
assert task.shipping_case.business_no is None
assert task.business_no is None
def test_numbered_customer_correction_voids_old_number_and_replaces_case(app):
"""客户误确认纠错必须保留原号作废,生成新客户编号并让原号查询跟随替代单。"""
def test_numbered_customer_correction_voids_old_notice_and_creates_replacement(app):
"""客户误确认纠错必须作废原 Notice,生成新客户编号并保持原号审计可查。"""
with app.app_context():
wrong = ShippingCustomer(standard_name="错误客户", short_code="OLD", status="active")
correct = ShippingCustomer(standard_name="正确客户", short_code="NEW", status="active")
case = ShippingCase(case_no="CASE-CORRECTION", numbering_status="pending")
db.session.add_all([wrong, correct, case])
db.session.add_all([wrong, correct])
db.session.flush()
first = ShippingNotice(
task_no="SN-CORRECTION-V1", shipping_case_id=case.id, document_version=1,
source_type="manual", customer_resolution_status="pending",
)
db.session.add(first)
db.session.flush()
latest = ShippingNotice(
task_no="SN-CORRECTION-V2", shipping_case_id=case.id, document_version=2,
supersedes_task_id=first.id, source_type="manual", upload_status="pending",
customer_resolution_status="pending", inventory_source_code="DSV",
task_no="SN-CORRECTION-V1", document_version=1,
source_type="manual", upload_status="pending", customer_resolution_status="pending", inventory_source_code="DSV",
inventory_source_status="user_confirmed", inventory_lines_json=[{"sku": "A", "quantity": 1}],
match_status="fulfillable", line_results_json=[{"sku": "A"}],
export_file_path="/tmp/old.xlsx", export_file_name="old.xlsx",
)
db.session.add(latest)
db.session.add(first)
db.session.commit()
confirm_task_customer(first, customer_id=wrong.id, actor_id=None)
old_case_id = case.id
old_business_no = case.business_no
old_business_no = first.business_no
result = correct_numbered_task_customer(
latest,
first,
customer_id=correct.id,
reason="自动识别客户错误",
actor_id=None,
)
old_case = db.session.get(ShippingCase, old_case_id)
replacement = db.session.get(ShippingCase, result["new_case_id"])
assert old_case.business_no == old_business_no
assert old_case.status == "voided"
assert old_case.numbering_status == "voided"
assert replacement.replaces_case_id == old_case.id
replacement = db.session.get(ShippingNotice, result["new_notice_id"])
assert first.business_no == old_business_no
assert first.lifecycle_status == "voided"
assert replacement.supersedes_task_id == first.id
assert replacement.business_no.startswith("NEW")
assert replacement.business_no != old_business_no
assert latest.shipping_case_id == replacement.id
assert first.shipping_case_id == replacement.id
assert latest.customer_resolution_method == "admin_correction"
assert latest.inventory_source_status is None
assert latest.inventory_lines_json is None
assert latest.export_file_path is None
assert latest.source_type == "manual"
assert get_task_by_ref(old_business_no).id == latest.id
assert get_task_by_ref(replacement.business_no).id == latest.id
assert replacement.customer_resolution_method == "admin_correction"
assert replacement.inventory_source_status is None
assert replacement.inventory_lines_json is None
assert replacement.export_file_path is None
assert replacement.source_type == "manual"
assert get_task_by_ref(old_business_no).id == first.id
assert get_task_by_ref(replacement.business_no).id == replacement.id
assert ShippingTaskEvent.query.filter_by(event_type="customer_correction_applied").count() == 2
metrics = get_auto_resolution_metrics()
assert metrics["corrected_task_count"] == 2
@@ -471,11 +454,10 @@ def test_numbered_customer_correction_requires_reason_and_blocks_uploaded_case(a
with app.app_context():
wrong = ShippingCustomer(standard_name="错误客户", short_code="BAD", status="active")
correct = ShippingCustomer(standard_name="正确客户", short_code="GOOD", status="active")
case = ShippingCase(case_no="CASE-CORRECTION-BLOCK", numbering_status="pending")
db.session.add_all([wrong, correct, case])
db.session.add_all([wrong, correct])
db.session.flush()
task = ShippingNotice(
task_no="SN-CORRECTION-BLOCK", shipping_case_id=case.id,
task_no="SN-CORRECTION-BLOCK",
source_type="manual", customer_resolution_status="pending",
)
db.session.add(task)
@@ -493,4 +475,4 @@ def test_numbered_customer_correction_requires_reason_and_blocks_uploaded_case(a
correct_numbered_task_customer(task, customer_id=correct.id, reason="客户错误", actor_id=None)
assert getattr(exc_info.value, "code", None) == "SHIPPING.CUSTOMER_CORRECTION_UPLOADED_BLOCKED"
db.session.rollback()
assert db.session.query(ShippingCase).count() == 1
assert db.session.query(ShippingNotice).count() == 1