feat(shipping-notice): 收敛复核阶段同源投影与 OT 后履约推进

统一 Case/Progress 阶段 label_key、修复 Job 成功未推进 OT 的可重试失败投影与写路径,并落地 OT 后行级匹配/波次履约与版面诊断开关。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-08-07 13:11:14 +08:00
1 parent 74270adc22
commit fd6a23f870
100 files changed
+12138 -1017

No files matched your search

@@ -134,6 +134,9 @@ def _register_builtin_platform_jobs() -> None:
from app.services.schedule.shipping_audit_retention_job import run_shipping_audit_retention_schedule
from app.services.schedule.shipping_inventory_rpa_job import run_shipping_inventory_rpa_schedule
from app.services.schedule.shipping_order_extraction_progress_cleanup_job import run_shipping_order_extraction_progress_cleanup_schedule
from app.services.schedule.shipping_post_ot_auto_fulfill_compensation_job import (
run_shipping_post_ot_auto_fulfill_compensation_schedule,
)
register_platform_job(PlatformJobDefinition(
key="shipping-order-extraction-progress-cleanup",
@@ -144,6 +147,19 @@ def _register_builtin_platform_jobs() -> None:
params_schema=({"key": "limit", "type": "number", "label": "单批上限", "required": False, "default": 1000},),
defaults={"limit": 1000},
))
register_platform_job(PlatformJobDefinition(
key="shipping-post-ot-auto-fulfill-compensation",
label="OT 后行级自动履约补偿",
handler=run_shipping_post_ot_auto_fulfill_compensation_schedule,
description=(
"每 15 分钟扫描正式 OT 上尚未入波次且匹配 ready 的行,"
"复用 coordinate_post_ot_auto_fulfill(trigger=compensation)。"
"SHIPPING_NOTICE_LINE_AUTO_FULFILL_ENABLED=false 时空跑。"
),
category="shipping",
params_schema=({"key": "limit", "type": "number", "label": "单批 Case 上限", "required": False, "default": 50},),
defaults={"limit": 50},
))
register_platform_job(PlatformJobDefinition(
key="shipping-audit-retention",
label="货代审计到期清理与哈希校验",
@@ -0,0 +1,138 @@
"""
services/schedule/shipping_post_ot_auto_fulfill_compensation_job.py
用途:OT 后行级自动履约补偿轮询(每 15 分钟)——补齐 matching/ot_created 事件丢失。
依赖:line_auto_fulfill_coordinator、ShippingOrderReviewCase、list_eligible_post_ot_line_keys。
逻辑:platform_api_path=shipping-post-ot-auto-fulfill-compensation;flag 关闭时 no-op;
仅扫描有正式 OT 且存在 eligible 行的 Case,复用 coordinate_post_ot_auto_fulfill(trigger=compensation)。
"""
from __future__ import annotations
from typing import Any
from app.models import ScheduleRun, ScheduleTask, ShippingOrderReviewCase
from app.services.shipping_notice.line_auto_fulfill_coordinator import (
coordinate_post_ot_auto_fulfill,
enqueue_post_ot_auto_fulfill_job,
)
from app.services.shipping_notice.line_inventory_match_projection import line_auto_fulfill_enabled
from app.services.shipping_notice.post_ot_fulfillment_wave_service import (
list_eligible_post_ot_line_keys,
)
ScheduleExecuteResult = tuple[int, int, int, int, list[dict[str, Any]], list[str], list[dict[str, Any]]]
def run_post_ot_auto_fulfill_compensation(*, limit: int = 50) -> dict[str, Any]:
"""
扫描可补偿的正式 OT Case 并入队/协调自动履约。
Args:
limit: 单批最多处理的 Case 数。
Returns:
scanned / enqueued / coordinated / skipped / details。
"""
if not line_auto_fulfill_enabled():
return {
'scanned': 0,
'enqueued': 0,
'coordinated': 0,
'skipped': 0,
'reason': 'line_auto_fulfill_disabled',
'details': [],
}
safe_limit = min(200, max(1, int(limit or 50)))
cases = (
ShippingOrderReviewCase.query
.filter(ShippingOrderReviewCase.confirmed_task_id.isnot(None))
.order_by(ShippingOrderReviewCase.id.desc())
.limit(safe_limit * 3)
.all()
)
scanned = 0
enqueued = 0
coordinated = 0
skipped = 0
details: list[dict[str, Any]] = []
for case in cases:
if scanned >= safe_limit:
break
scanned += 1
eligible = list_eligible_post_ot_line_keys(case)
if not eligible:
skipped += 1
continue
task_no = str(getattr(case, 'confirmed_task_no', None) or '').strip()
if not task_no and case.confirmed_task_id is not None:
from app import db
from app.models import ShippingNotice
task = db.session.get(ShippingNotice, int(case.confirmed_task_id))
task_no = str(getattr(task, 'task_no', '') or '').strip() if task else ''
if not task_no:
skipped += 1
details.append({'case_no': case.case_no, 'status': 'skipped', 'reason': 'missing_task_no'})
continue
job_id = enqueue_post_ot_auto_fulfill_job(
task_no=task_no,
trigger='compensation',
commit=True,
)
if job_id is not None:
enqueued += 1
details.append({
'case_no': case.case_no,
'task_no': task_no,
'status': 'enqueued',
'job_id': int(job_id),
'eligible_count': len(eligible),
})
continue
# 入队失败时同步协调一次(仍受幂等键保护)
result = coordinate_post_ot_auto_fulfill(case=case, trigger='compensation')
coordinated += 1
details.append({
'case_no': case.case_no,
'task_no': task_no,
'status': str(result.get('status') or 'coordinated'),
'eligible_count': len(eligible),
})
return {
'scanned': scanned,
'enqueued': enqueued,
'coordinated': coordinated,
'skipped': skipped,
'details': details[:50],
}
def run_shipping_post_ot_auto_fulfill_compensation_schedule(
task: ScheduleTask,
run: ScheduleRun,
) -> ScheduleExecuteResult:
"""
平台排程入口:每 15 分钟补偿扫描。
Returns:
ScheduleExecuteResult 七元组。
"""
_ = run
params = getattr(task, 'job_params', None) or {}
result = run_post_ot_auto_fulfill_compensation(limit=int(params.get('limit') or 50))
enqueued = int(result.get('enqueued') or 0)
coordinated = int(result.get('coordinated') or 0)
skipped = int(result.get('skipped') or 0)
scanned = int(result.get('scanned') or 0)
success = enqueued + coordinated
details = list(result.get('details') or [])
warnings: list[str] = []
if result.get('reason') == 'line_auto_fulfill_disabled':
warnings.append('line_auto_fulfill_disabled')
return success, 0, skipped, scanned, details, warnings, []