feat(shipping-notice): 新增通用解析能力,优化性能并清理遗留废弃代码

This commit is contained in:
hugo committed 2026-08-23 00:27:18 +08:00
1 parent 935bf26fa6
commit 7cfd5d167e
230 files changed
+9337 -10989

No files matched your search

@@ -426,13 +426,11 @@ def reconcile_and_progress(
expected_case_version: int | None = None,
source_content_hash: str | None = None,
is_admin: bool = False,
legacy_job_id: int | None = None,
replay_mode: bool = False,
) -> ReconcileProgressResult:
"""
统一对账恢复总入口(Q6 reconcile-and-progress API 委托本函数)。
【AI 触发时机】:用户点击「处理重复任务」走自动化恢复、或历史 Job 928/929 的
受权限控制的管理入口、或前端 duplicate_decision_required 裁决后调用。
【AI 触发时机】:用户点击「处理重复任务」走自动化恢复,或前端
duplicate_decision_required 裁决后调用。
核心执行顺序(Q11 短事务锁 + Q1 五级归属 + Q3 复用 Candidate 裁决 + Q2 写回绑定):
1. 加载 Case(FOR UPDATE 短锁);查可见性;构建 request_snapshot(参数一致性校验)
@@ -447,14 +445,9 @@ def reconcile_and_progress(
5. 写审计(fulfillment_audit + CaseEvent 幂等持久化)
6. commit 后触发 auto-progress post-commit 钩子
Phase 4-1 管理端扩展:
• legacy_job_id(int):历史 STP Job 928/929 锚点。管理员仅;必须真实、
必须 domain=shipping_notice、必须 status in {failed, dead}。
• replay_mode(bool):允许 legacy_job_id.reference_key != case_no。
默认 false(防跨 Case 错挂)。
• BRANCH F 硬隔离:先 db.session.rollback() 回滚 BRANCH B/C 半写 → 写 sensitive 审计
→ 归属异常统一抛 422 SHIPPING.RECONCILE_OWNERSHIP_ANOMALY,
否则返回 outcome=ownership_anomaly_rejected。
BRANCH F 硬隔离:先 db.session.rollback() 回滚 BRANCH B/C 半写 → 写 sensitive 审计
→ 归属异常统一抛 422 SHIPPING.RECONCILE_OWNERSHIP_ANOMALY,
否则返回 outcome=ownership_anomaly_rejected。
Args:
user_id: 操作人。
@@ -464,8 +457,6 @@ def reconcile_and_progress(
expected_case_version: 前端传来的草稿版本(过期抛 409 VERSION_CONFLICT)。
source_content_hash: 请求快照指纹(用于参数一致性比对)。
is_admin: 是否管理员(force_create 受权限控制)。
legacy_job_id: Phase 4 —— 历史 928/929 Job 锚点(管理员专用)。
replay_mode: Phase 4 —— 允许跨 case_no 重放 legacy_job(管理员专用)。
Returns:
ReconcileProgressResult(公开响应 to_public() 方法)。
@@ -493,61 +484,6 @@ def reconcile_and_progress(
details={'allowed': ['reuse_existing', 'create_revision', 'force_create', 'adjudicate', 'auto']},
)
# [Phase 4 PRE-CHECK-A] replay_mode / legacy_job_id → 必须 admin
if replay_mode and not is_admin:
raise ValidationError(
'replay_mode requires admin',
code='SHIPPING.RECONCILE_FORCE_CREATE_UNAUTHORIZED',
details={'legacy_job_id': legacy_job_id, 'replay_mode': replay_mode},
)
if legacy_job_id is not None and not is_admin:
raise ValidationError(
'legacy_job_id requires admin',
code='SHIPPING.RECONCILE_FORCE_CREATE_UNAUTHORIZED',
details={'legacy_job_id': legacy_job_id},
)
# [Phase 4 PRE-CHECK-B] legacy_job_id 真实性、失败态、domain、reference 对齐
_legacy_job = None
if legacy_job_id is not None:
from app.models import Job, JobType
_legacy_job = db.session.get(Job, int(legacy_job_id))
if _legacy_job is None:
raise ValidationError(
f'legacy_job_id not found: {legacy_job_id}',
code='SHIPPING.REVIEW_CASE_NOT_FOUND',
details={'legacy_job_id': legacy_job_id},
)
if str(_legacy_job.domain or '').strip() != 'shipping_notice':
raise ValidationError(
'legacy_job must be shipping_notice domain',
code='SHIPPING.RECONCILE_INVALID_USER_ACTION',
details={'legacy_job_id': legacy_job_id, 'domain': str(getattr(_legacy_job, 'domain', None) or '')},
)
if str(getattr(_legacy_job, 'job_type', None) or '') not in {
JobType.SHIPPING_STP_AUTO_CREATE.value,
str(JobType.SHIPPING_STP_AUTO_CREATE.value),
'shipping_stp_auto_create',
}:
# 放宽兼容:允许 SHIPPING_STP_AUTO_CREATE,其它 928/929 类型 fail-closed
pass
if str(_legacy_job.status or '').strip().lower() not in {'failed', 'dead'}:
raise ValidationError(
f'legacy_job must be failed/dead to replay (current status={_legacy_job.status})',
code='SHIPPING.RECONCILE_INVALID_USER_ACTION',
details={'legacy_job_id': legacy_job_id, 'legacy_status': str(_legacy_job.status or '')},
)
if str(_legacy_job.reference_key or '') != str(case_no).strip() and not replay_mode:
raise ValidationError(
'legacy_job.reference_key mismatches case_no; enable replay_mode to override',
code='SHIPPING.RECONCILE_IDEMPOTENT_CONFLICT',
details={
'legacy_job_id': legacy_job_id,
'legacy_reference_key': str(_legacy_job.reference_key or ''),
'case_no': str(case_no).strip(),
'replay_mode_required': True,
},
)
# [STEP 1] 加载 Case + 权限校验(短事务内 FOR UPDATE,Q11)
from app.services.shipping_notice.order_review_service import get_review_case_visible_to_user
@@ -579,12 +515,6 @@ def reconcile_and_progress(
'contract_version': RECOVERY_CONTRACT_VERSION,
'owner_user_id': int(case.owner_user_id),
'document_id': int(case.document_id),
# Phase 4:将 legacy_job 纳入幂等键与审计链,保证 replay/同 operation 不被 Q7 误回返
'legacy_job_id': (int(legacy_job_id) if legacy_job_id is not None else None),
'replay_mode': bool(replay_mode),
'legacy_job_reference_key': (
str(_legacy_job.reference_key or '') if _legacy_job is not None else None
),
}
# [STEP 2] Q7 幂等校验(返回同 operation_id 第一次结果或抛 409)
@@ -757,8 +687,6 @@ def reconcile_and_progress(
'match_level_steps': match_result.get('match_level_steps'),
'user_action': user_action,
'case_version': case_ver,
'legacy_job_id': (int(legacy_job_id) if legacy_job_id is not None else None),
'replay_mode': bool(replay_mode),
}
record_fulfillment_audit(
user_id=int(user_id) if user else None,