feat(shipping-order): 迭代货代订单提取与履约全流程功能

添加订单字段自动标注配置,默认仅关键字段参与自动框选
完善字段规范化逻辑,支持更多日期格式解析
添加订单候选人工采纳、驳回的完整流程,包含API、前端交互与状态管理
完善盲测功能,支持订单级字段真值校验,调整盲测集唯一性约束
加固库存校验逻辑,防止重复预约与数量不一致问题
优化坐标匹配逻辑,支持表格外的锚点定位
新增上传结果未知的状态处理,完善任务状态机
更新多语言文案与文档说明,补充功能细节
修复订单合并、重解析的边缘情况问题
补充相关测试用例,完善质量保障
This commit is contained in:
hugo committed 2026-07-27 20:48:40 +08:00
1 parent 11079910ec
commit 90f77634d6
76 files changed
+5056 -338

No files matched your search

@@ -23,6 +23,26 @@ from app.services.shipping_notice.intake_service import can_force_create_shippin
_ACTIONS = frozenset({'open_existing', 'create_revision', 'force_create'})
def ensure_candidate_fulfillment_ready(candidate: ShippingNoticeCandidate) -> None:
"""统一履约门禁:只有已接受、当前且无阻断问题的 Candidate 才能建单。"""
if str(candidate.candidate_status or '') != 'accepted':
raise ConflictError(
code='SHIPPING_NOTICE.CANDIDATE_ACCEPTANCE_REQUIRED',
message='订单候选尚未被接受,不能进入履约',
)
if not candidate.selected:
raise ConflictError(code='SHIPPING_NOTICE.CANDIDATE_NOT_SELECTED', message='订单候选未选中')
if not candidate.is_current_version:
raise ConflictError(code='SHIPPING_NOTICE.CANDIDATE_SUPERSEDED', message='订单候选已被重新识别版本替代')
if candidate.consumed_task_id is not None:
raise ConflictError(code='SHIPPING_NOTICE.CANDIDATE_ALREADY_CONSUMED', message='订单候选已被消费')
if str(candidate.validation_status or '') != 'ready' or candidate.conflicts_json:
raise ConflictError(
code='SHIPPING_NOTICE.CANDIDATE_REVIEW_REQUIRED',
message='订单候选仍有字段冲突或未完成审核',
)
def _candidate_for_user(candidate_no: str, user_id: int, *, lock: bool = False) -> tuple[ShippingNoticeCandidate, ShippingNoticeIntake, ShippingNoticeDocument]:
"""按候选编号和用户加载 Candidate、Intake、Document;写路径可加行锁。"""
query = (
@@ -97,7 +117,8 @@ def update_candidate(*, user_id: int, candidate_no: str, selected: bool | None =
if selected is not None:
if not isinstance(selected, bool):
raise ValidationError(code='VALIDATION.INVALID_PARAM', message='selected 必须为布尔值')
candidate.selected = selected
# Selection is retained for legacy batch APIs but may not bypass acceptance.
candidate.selected = selected if candidate.candidate_status == 'accepted' else False
if business_summary is not None:
if not isinstance(business_summary, dict):
raise ValidationError(code='VALIDATION.INVALID_PARAM', message='business_summary 必须为对象')
@@ -126,6 +147,66 @@ def update_candidate(*, user_id: int, candidate_no: str, selected: bool | None =
return _serialize_candidate_brief(candidate)
def _append_candidate_decision_history(candidate: ShippingNoticeCandidate, *, action: str, user_id: int, reason: str | None = None) -> None:
"""记录人工采纳或驳回事件,便于审核回放。"""
structured = dict(candidate.structured_result_json or {})
history = list(structured.get('manual_review_history') or [])
history.append({
'action': action,
'actor_user_id': int(user_id),
'reason': str(reason or '').strip() or None,
'decided_at': datetime.utcnow().isoformat(),
})
structured['manual_review_history'] = history
candidate.structured_result_json = structured
def promote_candidate(*, user_id: int, candidate_no: str) -> dict[str, Any]:
"""人工采纳 Dify 兜底 Candidate;重复采纳请求保持幂等。"""
candidate, intake, document = _candidate_for_user(candidate_no, user_id, lock=True)
if candidate.candidate_status == 'accepted':
return _serialize_candidate_brief(candidate)
if candidate.candidate_status != 'provisional':
raise ConflictError(code='SHIPPING_NOTICE.CANDIDATE_DECISION_FINAL', message='订单候选已被驳回或过期')
if str(candidate.extraction_source or '') not in {'dify_workflow', 'manual'}:
raise ConflictError(code='SHIPPING_NOTICE.CANDIDATE_PROMOTION_FORBIDDEN', message='仅 Dify 兜底候选允许人工采纳')
if candidate.consumed_task_id is not None or not candidate.is_current_version:
raise ConflictError(code='SHIPPING_NOTICE.CANDIDATE_ALREADY_CONSUMED', message='订单候选不可再采纳')
_refresh_candidate(candidate, intake, document)
if candidate.validation_status != 'ready' or candidate.conflicts_json or candidate.duplicate_status not in {'none', 'unchecked'}:
raise ConflictError(code='SHIPPING_NOTICE.CANDIDATE_REVIEW_REQUIRED', message='请先完成字段和重复审核')
candidate.candidate_status = 'accepted'
candidate.acceptance_mode = 'manual'
candidate.accepted_at = datetime.utcnow()
candidate.accepted_by_id = int(user_id)
candidate.acceptance_reason = 'dify_candidate_promoted'
candidate.acceptance_policy_version = 'manual-review-v1'
candidate.selected = True
_append_candidate_decision_history(candidate, action='promoted', user_id=user_id)
db.session.commit()
return _serialize_candidate_brief(candidate)
def reject_candidate(*, user_id: int, candidate_no: str, reason: str | None = None) -> dict[str, Any]:
"""人工驳回未消费 Candidate,并保留审计原因。"""
candidate, intake, _document = _candidate_for_user(candidate_no, user_id, lock=True)
if candidate.candidate_status == 'rejected':
return _serialize_candidate_brief(candidate)
if candidate.candidate_status != 'provisional' or candidate.consumed_task_id is not None or not candidate.is_current_version:
raise ConflictError(code='SHIPPING_NOTICE.CANDIDATE_DECISION_FINAL', message='订单候选不可再驳回')
candidate.candidate_status = 'rejected'
candidate.selected = False
candidate.acceptance_mode = None
candidate.accepted_at = None
candidate.accepted_by_id = None
candidate.acceptance_reason = str(reason or '').strip() or 'manual_rejected'
candidate.acceptance_policy_version = 'manual-review-v1'
candidate.updated_at = datetime.utcnow()
_append_candidate_decision_history(candidate, action='rejected', user_id=user_id, reason=reason)
db.session.commit()
return _serialize_candidate_brief(candidate)
def refresh_candidate_duplicate(*, user_id: int, candidate_no: str) -> dict[str, Any]:
"""刷新单个 Candidate 的重复检测结果并清除陈旧决策。"""
candidate, intake, document = _candidate_for_user(candidate_no, user_id, lock=True)