feat(shipping-notice): 收敛复核状态隔离、STP 重试幂等与 XFA 合成预览标注

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-08-07 02:58:28 +08:00
1 parent 84a881b554
commit 74270adc22
149 files changed
+20111 -3871

No files matched your search

@@ -3,10 +3,12 @@ backend/app/api/admin/shipping_order_document_profiles.py
用途:提供订单文档模板 Profile 的管理员版本治理 API。
依赖:admin blueprint、require_admin、order_document_profile_service、
order_document_profile_visual_binding_service、order_profile_unified_migration。
逻辑:路由只转换请求和操作者;版本、Schema、字段、统一迁移与发布门禁全部由服务层执行。
order_document_profile_preview_service、order_document_profile_xfa_preview_service、
order_document_profile_xfa_automark_service、order_document_profile_visual_binding_service、
order_profile_unified_migration。
逻辑:路由只转换请求和操作者;版本、Schema、字段、XFA 合成预览、OCR 自动标记、
统一迁移与发布门禁由服务层执行。
"""
from __future__ import annotations
import io
@@ -23,6 +25,8 @@ from app.services.shipping_notice import order_document_profile_diff as diff_ser
from app.services.shipping_notice import order_document_profile_creation_service as creation_service
from app.services.shipping_notice import order_document_profile_lease_service as lease_service
from app.services.shipping_notice import order_document_profile_preview_service as preview_service
from app.services.shipping_notice import order_document_profile_xfa_preview_service as xfa_preview_service
from app.services.shipping_notice import order_document_profile_xfa_automark_service as xfa_automark_service
from app.services.shipping_notice import order_document_profile_revision_service as revision_service
from app.services.shipping_notice import order_document_profile_suggestion_service as suggestion_service
from app.services.shipping_notice import order_document_profile_service as service
@@ -798,6 +802,62 @@ def preview_order_document_profile_sample_xfa_value(document_id: int):
))
@admin_bp.route(
'/shipping-notice/order-document-profile-samples/<int:document_id>/xfa/preview',
methods=['POST'],
)
@require_admin
def request_order_document_profile_sample_xfa_synthetic_preview(document_id: int):
"""
生成或复用绑定冻结 Analysis 的 XFA 合成预览,返回身份与页元数据(不含大图)。
核心逻辑:委托 xfa_preview_service;可选 body.force=true 强制重生成。
"""
data = request.get_json(silent=True) or {}
return _private_ok(xfa_preview_service.request_xfa_synthetic_preview(
document_id,
actor_id=_actor_id(),
force=bool(data.get('force')),
))
@admin_bp.route(
'/shipping-notice/order-document-profile-samples/<int:document_id>/xfa/preview/pages',
methods=['GET'],
)
@require_admin
def list_order_document_profile_sample_xfa_synthetic_preview_pages(document_id: int):
"""列出冻结 Analysis 绑定的最新 ready 合成预览页并签发短时 token。"""
return _private_ok(xfa_preview_service.list_xfa_synthetic_preview_pages(
document_id,
actor_id=_actor_id(),
))
@admin_bp.route(
'/shipping-notice/order-document-profile-samples/<int:document_id>/xfa/preview/pages/<int:page_no>',
methods=['GET'],
)
@require_admin
def get_order_document_profile_sample_xfa_synthetic_preview_page(document_id: int, page_no: int):
"""
消费一次性授权并返回合成预览单页 PNG。
核心逻辑:与既有 preview-pages 一致——query preview_token、private no-store。
"""
image = xfa_preview_service.render_xfa_synthetic_preview_page(
document_id,
page_no=page_no,
token=str(request.args.get('preview_token') or ''),
actor_id=_actor_id(),
)
response = send_file(io.BytesIO(image), mimetype='image/png', as_attachment=False, max_age=0)
response.headers['Cache-Control'] = 'private, no-store, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['X-Content-Type-Options'] = 'nosniff'
return response
@admin_bp.route(
'/shipping-notice/order-document-profile-samples/<int:document_id>/xfa/split-rules/preview',
methods=['POST'],
@@ -948,7 +1008,7 @@ def suggest_order_document_profile_xfa_visual_bindings(profile_id: int):
)
@require_admin
def confirm_order_document_profile_xfa_visual_binding(profile_id: int, binding_id: str):
"""管理员确认视觉绑定;写 before/after 审计。"""
"""管理员确认视觉绑定;须携带合成预览 checksum / render_mode;写 before/after 审计。"""
data = request.get_json(silent=True) or {}
ctx = _write_context(data)
visual = data.get('visual_binding') if isinstance(data.get('visual_binding'), dict) else {}
@@ -962,6 +1022,8 @@ def confirm_order_document_profile_xfa_visual_binding(profile_id: int, binding_i
lease_token=ctx.get('lease_token'),
operation_id=ctx.get('operation_id'),
request_id=getattr(g, 'request_id', None),
preview_render_checksum=str(data.get('preview_render_checksum') or '').strip() or None,
render_mode=str(data.get('render_mode') or '').strip() or None,
))
@@ -986,6 +1048,97 @@ def reject_order_document_profile_xfa_visual_binding(profile_id: int, binding_id
))
@admin_bp.route(
'/shipping-notice/order-document-profiles/<int:profile_id>/xfa/automark',
methods=['POST'],
)
@require_admin
def enqueue_order_document_profile_xfa_ocr_automark(profile_id: int):
"""
入队 XFA OCR 自动标记异步 Job。
核心逻辑:body.document_id 必填;可选 force 强制重生成合成预览后入队。
"""
data = request.get_json(silent=True) or {}
document_id = data.get('document_id')
if document_id in (None, ''):
raise ValidationError(
'document_id is required',
code='SHIPPING.ORDER_DOCUMENT_PROFILE_INVALID',
)
page_nos = data.get('page_nos') if isinstance(data.get('page_nos'), list) else None
return _private_ok(xfa_automark_service.enqueue_xfa_ocr_automark(
profile_id,
document_id=int(document_id),
actor_id=_actor_id(),
force=bool(data.get('force')),
page_nos=[int(item) for item in (page_nos or [])],
))
@admin_bp.route(
'/shipping-notice/order-document-profiles/<int:profile_id>/xfa/automark/jobs/<int:job_id>',
methods=['GET'],
)
@require_admin
def get_order_document_profile_xfa_ocr_automark_job(profile_id: int, job_id: int):
"""轮询 OCR 自动标记 Job 状态与建议列表。"""
return _private_ok(xfa_automark_service.get_xfa_ocr_automark_job(
job_id,
profile_id=profile_id,
))
@admin_bp.route(
'/shipping-notice/order-document-profiles/<int:profile_id>/xfa/automark/suggestions/<string:suggestion_id>/accept',
methods=['POST'],
)
@require_admin
def accept_order_document_profile_xfa_automark_suggestion(profile_id: int, suggestion_id: str):
"""
接受 OCR 自动标记建议。
核心逻辑:high→confirmed(须 checksum);medium/low→suggested;mismatch 阻断。
"""
data = request.get_json(silent=True) or {}
ctx = _write_context(data)
job_id = data.get('job_id')
return _private_ok(xfa_automark_service.accept_xfa_automark_suggestion(
profile_id,
suggestion_id,
actor_id=_actor_id(),
browser_session_id=ctx.get('browser_session_id'),
lease_token=ctx.get('lease_token'),
operation_id=ctx.get('operation_id'),
request_id=getattr(g, 'request_id', None),
job_id=int(job_id) if job_id not in (None, '') else None,
preview_render_checksum=str(data.get('preview_render_checksum') or '').strip() or None,
))
@admin_bp.route(
'/shipping-notice/order-document-profiles/<int:profile_id>/xfa/automark/suggestions/<string:suggestion_id>/reject',
methods=['POST'],
)
@require_admin
def reject_order_document_profile_xfa_automark_suggestion(profile_id: int, suggestion_id: str):
"""驳回 OCR 自动标记建议并减少 unresolved mismatch 计数。"""
data = request.get_json(silent=True) or {}
ctx = _write_context(data)
job_id = data.get('job_id')
return _private_ok(xfa_automark_service.reject_xfa_automark_suggestion(
profile_id,
suggestion_id,
actor_id=_actor_id(),
browser_session_id=ctx.get('browser_session_id'),
lease_token=ctx.get('lease_token'),
operation_id=ctx.get('operation_id'),
request_id=getattr(g, 'request_id', None),
reason_code=str(data.get('reason_code') or '').strip() or None,
job_id=int(job_id) if job_id not in (None, '') else None,
))
@admin_bp.route(
'/shipping-notice/order-document-profiles/<int:profile_id>/unified-migration/dry-run',
methods=['POST'],
+69 -4
View File
@@ -15,7 +15,7 @@ import uuid
from io import BytesIO
from typing import Any
from flask import Blueprint, g, request, send_file
from flask import Blueprint, current_app, g, request, send_file
from app import db
from app.core.auth.jwt import get_current_user
@@ -28,6 +28,7 @@ from app.services.shipping_notice.constants import (
CP_EXPORT_GENERATED,
CP_NOTICE_TEXT_EXTRACTION_FAILED,
)
from app.services.shipping_notice.processing_ownership import assert_notice_task_ownership
from app.services.shipping_notice.query_service import (
EXPORT_MIME_TYPE,
count_my_todos,
@@ -89,6 +90,35 @@ def _ok(payload: Any, *, status: int = 200):
return ok(payload, request_id=getattr(g, 'request_id', None), status=status)
def _assert_fulfillment_task_ownership(task: ShippingNotice, user_id: int) -> None:
"""
履约写接口:若请求携带期望 OT/Case/Run,校验与正式 Notice 归属一致。
核心逻辑:委托 assert_notice_task_ownership;未携带期望字段时放行(兼容旧客户端)。
Args:
task: 已鉴权的 ShippingNotice。
user_id: 当前用户 id。
Raises:
ConflictError: 归属不匹配。
"""
data = request.get_json(silent=True) or {}
assert_notice_task_ownership(
task,
expected_order_task_no=(
str(data.get('expected_order_task_no') or data.get('order_task_no') or '').strip() or None
),
expected_review_case_no=(
str(data.get('expected_review_case_no') or data.get('case_no') or '').strip() or None
),
expected_run_no=(
str(data.get('expected_run_no') or data.get('run_no') or '').strip() or None
),
actor_id=user_id,
)
@workspace_shipping_notice_bp.route('/shipping-tasks', methods=['GET'])
def workspace_shipping_tasks():
"""分页列出当前用户可见的发货任务,可按批次与处理状态过滤。"""
@@ -950,6 +980,7 @@ def workspace_match_shipping_inventory(task_no: str):
task = get_task_for_user(task_no, user.id)
if not task:
raise NotFoundError('Not found')
_assert_fulfillment_task_ownership(task, user.id)
from app.services.shipping_notice import tools as shipping_tools
from app.services.shipping_notice.state_machine import action_buttons_for_checkpoint, human_options_for_checkpoint
matched = shipping_tools.match_inventory({
@@ -993,6 +1024,7 @@ def workspace_generate_shipping_excel(task_no: str):
task = get_task_for_user(task_no, user.id)
if not task:
raise NotFoundError('Not found')
_assert_fulfillment_task_ownership(task, user.id)
from app.services.shipping_notice import tools as shipping_tools
result = shipping_tools.generate_excel({'task_id': task.task_no})
if isinstance(result, dict) and result.get('status') == 'ok':
@@ -1015,6 +1047,7 @@ def workspace_mark_manual_uploaded(task_no: str):
task = get_task_for_user(task_no, user.id)
if not task:
raise NotFoundError('Not found')
_assert_fulfillment_task_ownership(task, user.id)
from app.services.shipping_notice import tools as shipping_tools
data = request.get_json(silent=True) or {}
payload = {
@@ -1033,8 +1066,16 @@ def workspace_mark_manual_uploaded(task_no: str):
@workspace_shipping_notice_bp.route('/shipping-tasks/<task_no>/retry-upload', methods=['POST'])
def workspace_retry_shipping_upload(task_no: str):
"""首期禁用货代 RPA 重试上传;引导用户改走人工凭据登记。"""
_require_user()
"""
货代上传重试入口;首期若全局禁用 RPA 则返回明确错误码。
核心逻辑:先做归属校验;若平台禁用 RPA 再抛 ValidationError,避免跳过归属门禁。
"""
user = _require_user()
task = get_task_for_user(task_no, user.id)
if not task:
raise NotFoundError('Not found')
_assert_fulfillment_task_ownership(task, user.id)
raise ValidationError(
'首期不支持货代 RPA 上传重试。请下载 Excel,在货代系统人工上传后提交截图/PDF 凭据。',
code='SHIPPING.FREIGHT_RPA_UPLOAD_DISABLED',
@@ -1378,7 +1419,11 @@ def workspace_get_shipping_order_review_case(case_no: str):
from app.services.shipping_notice.order_review_projection import serialize_review_case
from app.services.shipping_notice.order_review_service import get_review_case_visible_to_user
payload = serialize_review_case(get_review_case_visible_to_user(case_no, user.id, is_admin=is_admin))
# 只读:禁止在 GET 中入队 STP(入队仅抽取收尾 / resume / 显式 retry POST)。
payload = serialize_review_case(
get_review_case_visible_to_user(case_no, user.id, is_admin=is_admin),
is_admin=is_admin,
)
record_access_audit(
action='view_order_review_case',
aggregate_type='order_review_case',
@@ -1390,6 +1435,26 @@ def workspace_get_shipping_order_review_case(case_no: str):
return _ok(payload)
@workspace_shipping_notice_bp.route(
'/shipping-notice/order-review-cases/<case_no>/stp-auto-create/retry',
methods=['POST'],
)
def workspace_retry_shipping_order_stp_auto_create(case_no: str):
"""
显式重试 STP 自动建单入队(failed / manual_fallback)。
核心逻辑:委托 retry_stp_auto_create_for_case;成功返回 queued/reused+job_id;
失败抛结构化业务错误,禁止伪装成功。
"""
user = _require_user()
from app.services.shipping_notice.order_stp_auto_create_service import (
retry_stp_auto_create_for_case,
)
result = retry_stp_auto_create_for_case(user_id=int(user.id), case_no=str(case_no))
return _ok(result)
@workspace_shipping_notice_bp.route(
'/shipping-notice/order-review-cases/<case_no>/access/version-history',
methods=['POST'],