feat(shipping-notice): 增强发货通知与审计功能,支持敏感数据控制与哈希链校验

- 更新发货通知工具 API,剥离不可信的操作人身份字段,确保审计安全
- 引入审计事件的哈希链校验机制,支持数据完整性验证
- 增强订单抽取与审计功能,支持敏感快照的访问控制
- 更新相关模型与服务,确保新功能的完整性与可追溯性
- 完善文档与代码注释,反映新的业务逻辑与审计要求
This commit is contained in:
hugo committed 2026-07-31 08:59:44 +08:00
1 parent 95cdfe66b0
commit d0fe084999
225 files changed
+25923 -6653

No files matched your search

+146 -16
View File
@@ -2,9 +2,9 @@
services/shipping_notice/ui_state.py
用途:发货通知任务/批次 UI 状态读模型,作为通知摘要、人工复核与任务工作台的统一状态来源。
依赖:models.ShippingTask/ShippingBatch、state_machine、query_service、constants。
依赖:models.ShippingTask/ShippingBatch、state_machine、query_service、constants、stp_progress_contract。
逻辑:从 ShippingTask DB 字段经状态机解析 checkpoint/checkpoint_kind/action_buttons,
再补充七步流程与可解释 summary(当前待办、暂停原因、下一自动步骤、操作入口);
再补充七步流程与可解释 summary(当前待办、暂停原因、下一自动步骤、操作入口、stp_facts);
批次 UI 仅作会话上传集合摘要,不作为履约写操作面。
"""
@@ -123,9 +123,9 @@ _PAUSE_REASON_BY_CHECKPOINT: dict[str, tuple[str, str]] = {
CP_NOTICE_TEXT_EXTRACTION_FAILED: ("notice_text_extraction_failed", "通知文本解析失败"),
CP_UPLOAD_FAILED: ("upload_failed", "货代上传失败"),
CP_UPLOAD_RESULT_UNKNOWN: ("upload_result_unknown", "货代上传结果未知"),
CP_WAITING_UPLOAD: ("waiting_upload", "等待货代上传结果"),
CP_EXPORT_READY: ("wait_manual_upload", "Excel 已生成,待人工上传并提交凭据"),
CP_EXPORT_GENERATED: ("wait_manual_upload", "Excel 已生成,待人工上传并提交凭据"),
CP_WAITING_UPLOAD: ("waiting_upload", "待人工上传登记"),
CP_EXPORT_READY: ("wait_manual_upload", "Excel 已生成,请下载后在货代平台上传,并在本系统登记凭据"),
CP_EXPORT_GENERATED: ("wait_manual_upload", "Excel 已生成,请下载后在货代平台上传,并在本系统登记凭据"),
}
@@ -206,13 +206,41 @@ def build_shipping_workflow_steps(task: ShippingTask, checkpoint: str | None) ->
def _action_buttons_for_task(task: ShippingTask) -> list[dict[str, str]]:
"""从状态机解析当前任务可用动作按钮,库存源确认场景补充 source_code。"""
"""
从状态机解析当前任务可用动作按钮;多源匹配阻断时前置「人工指定库存源」。
核心逻辑:库存源确认场景补充 source_code;shortage + multi_source/multi_file 冲突时
注入 confirm_inventory_source,使工作台可打开候选选择对话框。
"""
from app.services.shipping_notice.state_machine import ACTION_SPECS
from app.services.shipping_notice.stp_line_inventory_match import (
multi_source_recovery_source_codes,
needs_multi_source_manual_recovery,
)
ui = resolve_task_ui_state(task)
if ui.checkpoint == CP_INVENTORY_SOURCE_REVIEW:
return inventory_source_action_buttons(task)
if ui.checkpoint == RS_SHORTAGE_REVIEW and needs_multi_source_manual_recovery(task):
codes = multi_source_recovery_source_codes(task)
confirm_spec = dict(ACTION_SPECS.get('confirm_inventory_source') or {})
confirm_btn: dict[str, str] = {
'id': 'confirm_inventory_source',
'label': str(confirm_spec.get('label') or '人工指定库存源'),
'message': '请选择单一库存源后继续匹配',
'variant': str(confirm_spec.get('variant') or 'primary'),
}
if len(codes) == 1:
confirm_btn['source_code'] = codes[0]
confirm_btn['label'] = f"指定库存源:{codes[0]}"
confirm_btn['message'] = f"使用库存源 {codes[0]} 重新匹配"
else:
confirm_btn['label'] = '人工指定库存源'
# 保留重试/重匹配/驳回,但把指定源放在首位作为主恢复入口。
rest = [btn for btn in (ui.action_buttons or []) if str(btn.get('id') or '') != 'confirm_inventory_source']
return [confirm_btn, *rest]
return ui.action_buttons
_PARSE_STAGE_CHECKPOINTS = {
CP_PARSE_REVIEW,
CP_RSV_REQ,
@@ -319,6 +347,9 @@ def _entry_surface_for(checkpoint: str | None, kind: CheckpointKind) -> str:
Returns:
review_workbench | task_workbench | none
"""
# Excel 就绪虽为 wait_manual_upload,仍进任务工作台以便下载与登记凭据。
if str(checkpoint or "") in {CP_EXPORT_READY, CP_EXPORT_GENERATED}:
return "task_workbench"
if kind == CheckpointKind.COMPLETE:
return "none"
if str(checkpoint or "") in _REVIEW_ENTRY_CHECKPOINTS:
@@ -346,6 +377,77 @@ def _current_todo_label(checkpoint: str | None, action_buttons: list[dict[str, s
return label or None
def _build_stp_facts(task: ShippingTask) -> dict[str, Any] | None:
"""
投影 STP 履约事实供 Chat/工作台/待办同源消费。
核心逻辑:只读 task + step_payload;无 STP 痕迹时返回 None,避免普通任务噪声。
Args:
task: 发货任务。
Returns:
stp_facts 字典或 None。
"""
from app.services.shipping_notice.stp_inventory_freeze import task_frozen_inventory_source_codes
from app.services.shipping_notice.stp_line_inventory_match import multi_source_recovery_source_codes
from app.services.shipping_notice.stp_progress_contract import STP_FACTS_ALLOWED_KEYS
payload = task.step_payload_json if isinstance(task.step_payload_json, dict) else {}
status = str(task.inventory_source_status or '').strip()
method = str(task.inventory_source_method or '').strip()
candidates = payload.get('line_inventory_candidates') if isinstance(payload.get('line_inventory_candidates'), dict) else {}
resolutions_raw = payload.get('line_inventory_resolutions')
resolutions = [item for item in resolutions_raw if isinstance(item, dict)] if isinstance(resolutions_raw, list) else []
multi_block = bool(payload.get('multi_source_excel_block'))
frozen_codes = list(candidates.get('source_codes') or []) or task_frozen_inventory_source_codes(task)
has_stp = (
status in {'candidates_frozen', 'resolved'}
or method.startswith('stp')
or bool(frozen_codes)
or bool(resolutions)
or multi_block
or bool(payload.get('line_inventory_candidates'))
)
if not has_stp:
return None
counts: dict[str, int] = {}
compact_resolutions: list[dict[str, Any]] = []
for item in resolutions:
disposition = str(item.get('disposition') or 'unknown')
counts[disposition] = counts.get(disposition, 0) + 1
compact_resolutions.append({
'line_index': item.get('line_index'),
'line_key': item.get('line_key'),
'disposition': disposition,
'effective_reservation_id': item.get('effective_reservation_id'),
'part_number': item.get('part_number'),
'resolved_inventory_source_code': item.get('resolved_inventory_source_code'),
'inventory_file_no': item.get('inventory_file_no'),
'inventory_row_ref': item.get('inventory_row_ref'),
})
recovery_codes = multi_source_recovery_source_codes(task)
facts = {
'inventory_source_status': status or None,
'inventory_source_code': str(task.inventory_source_code or '').strip().upper() or None,
'inventory_source_method': method or None,
'frozen_candidate_codes': [str(code).strip().upper() for code in frozen_codes if str(code or '').strip()],
'resolution_counts': counts,
'line_resolutions': compact_resolutions,
'multi_source_excel_block': multi_block,
'resolved_ok_source_codes': [
str(code).strip().upper()
for code in (payload.get('resolved_ok_source_codes') or [])
if str(code or '').strip()
],
'recovery_source_codes': recovery_codes,
'match_status': str(task.match_status or '').strip() or None,
}
return {key: facts[key] for key in STP_FACTS_ALLOWED_KEYS if key in facts}
def _build_task_summary(
task: ShippingTask,
checkpoint: str | None,
@@ -357,7 +459,7 @@ def _build_task_summary(
输出可解释任务摘要,供通知卡、人工复核与任务工作台同源消费。
核心逻辑:保留 title/message/issues;补充 stage、checkpoint_kind、current_todo、
pause_reason、next_auto_step、event_at、entry_surface、allowed_actions。
pause_reason、next_auto_step、event_at、entry_surface、allowed_actions、stp_facts。
"""
title = user_facing_task_stage(task)
message_by_checkpoint: dict[str, str] = {
@@ -371,10 +473,10 @@ def _build_task_summary(
RS_SHORTAGE_REVIEW: "库存存在缺口,当前不能继续上传货代。",
CP_READY_MATCH: "库存已就绪,系统将自动执行匹配。",
CP_READY_EXPORT: "库存匹配已满足发货条件,系统将自动生成发货文件。",
CP_EXPORT_READY: "发货文件已生成,请下载并在货代系统上传后提交截图/PDF 凭据。",
CP_EXPORT_GENERATED: "发货文件已生成,请下载并在货代系统上传后提交截图/PDF 凭据。",
CP_WAITING_UPLOAD: "系统正在等待货代上传结果。",
CP_UPLOAD_FAILED: "货代上传失败,需要重试或手工确认。",
CP_EXPORT_READY: "Excel 已生成,请下载后在货代平台上传,并在本系统登记凭据。",
CP_EXPORT_GENERATED: "Excel 已生成,请下载后在货代平台上传,并在本系统登记凭据。",
CP_WAITING_UPLOAD: "待人工上传登记:请下载 Excel,在货代平台上传后于本系统登记凭据。",
CP_UPLOAD_FAILED: "货代上传失败,请下载 Excel 后人工处理。",
CP_REJECTED: "任务已驳回,无需继续处理。",
CP_UPLOADED: "任务已完成上传。",
CP_COMPLETED: "任务已关闭。",
@@ -383,21 +485,46 @@ def _build_task_summary(
pause = _PAUSE_REASON_BY_CHECKPOINT.get(str(checkpoint or ""))
pause_reason_code = pause[0] if pause else (task.pending_human_reason or None)
pause_reason = pause[1] if pause else (task.error_message or task.upload_error_message or None)
if checkpoint_kind == CheckpointKind.AUTO_CONTINUE:
message = message_by_checkpoint.get(str(checkpoint or ""), title)
stp_facts = _build_stp_facts(task)
current_todo_override: str | None = None
# [STEP] STP 多源 Excel 阻断:覆盖通用 shortage 文案,并提示可指定库存源。
if stp_facts and stp_facts.get('multi_source_excel_block'):
pause_reason_code = 'multi_source_excel_block'
pause_reason = '订单行命中多个不同库存源,首期无法生成单一发货 Excel,请拆单或人工指定库存源后继续'
message = pause_reason
title = '多库存源冲突,待人工处理'
issue_text = pause_reason
if issue_text not in issues:
issues = [*issues, issue_text]
current_todo_override = '请拆单或人工指定库存源后继续'
elif stp_facts and str(stp_facts.get('inventory_source_status') or '') == 'candidates_frozen' and checkpoint_kind == CheckpointKind.AUTO_CONTINUE:
message = '多候选库存源已冻结,系统正在准备库存并跨文件匹配。'
if checkpoint_kind == CheckpointKind.AUTO_CONTINUE and not (stp_facts and stp_facts.get('multi_source_excel_block')):
pause_reason = None
pause_reason_code = None
allowed_actions = [str(btn.get("id")) for btn in action_buttons if btn.get("id")]
event_at = None
if task.updated_at is not None:
event_at = task.updated_at.isoformat()
return {
summary: dict[str, Any] = {
"title": title,
"message": message_by_checkpoint.get(str(checkpoint or ""), title),
"message": message,
"error_message": task.error_message or task.upload_error_message,
"issues": issues,
"stage": title,
"checkpoint_kind": str(checkpoint_kind),
"current_todo": _current_todo_label(checkpoint, action_buttons),
"current_todo": (
current_todo_override
if current_todo_override is not None
else (
pause_reason
if stp_facts and stp_facts.get('multi_source_excel_block')
else _current_todo_label(checkpoint, action_buttons)
)
),
"pause_reason": pause_reason,
"pause_reason_code": pause_reason_code,
"next_auto_step": (
@@ -409,6 +536,9 @@ def _build_task_summary(
"entry_surface": _entry_surface_for(checkpoint, checkpoint_kind),
"allowed_actions": allowed_actions,
}
if stp_facts is not None:
summary['stp_facts'] = stp_facts
return summary
def build_task_ui_state(task: ShippingTask) -> dict[str, Any]: