feat(shipping-notice): 新增库存快照管理与RPA批量导出功能

This commit is contained in:
hugo committed 2026-07-08 08:59:08 +08:00
1 parent fc694a8f43
commit d54f51ddc3
48 files changed
+4970 -543

No files matched your search

@@ -276,6 +276,9 @@ def resolve_schedule_executor(task: ScheduleTask) -> ScheduleExecutor:
if dst == "faq_hot_aggregate":
from app.services.schedule.faq_hot_aggregate_job import run_faq_hot_aggregate_schedule
return run_faq_hot_aggregate_schedule
if dst == "shipping_inventory_rpa":
from app.services.schedule.shipping_inventory_rpa_job import run_shipping_inventory_rpa_schedule
return run_shipping_inventory_rpa_schedule
if dst in ("database", "external_api"):
return _run_generic_check_job
return _run_common_executor
@@ -0,0 +1,104 @@
"""
backend/app/services/schedule/shipping_inventory_rpa_job.py
用途:排程触发货代库存 RPA 批量导出(各 Warehouse 一次登录多导出)。
依赖:inventory_sources、rpa.write_inventory_export_batch_trigger、ScheduleTask/ScheduleRun。
逻辑:data_source_type=shipping_inventory_rpa 时由 resolve_schedule_executor 调用。
"""
from __future__ import annotations
import json
from datetime import datetime
from typing import Any
from app import db
from app.models import ScheduleRun, ScheduleTask
from app.services.shipping_notice.inventory_sources import build_rpa_inventory_source, list_inventory_sources
from app.services.shipping_notice.rpa import write_inventory_export_batch_trigger
ScheduleExecuteResult = tuple[int, int, int, int, list[dict[str, Any]], list[str], list[dict[str, Any]]]
def _parse_api_body_template(task: ScheduleTask) -> dict[str, Any] | None:
"""解析 ScheduleTaskConfig.api_body_template(Text JSON)。"""
cfg = getattr(task, "config", None)
raw = getattr(cfg, "api_body_template", None) if cfg else None
if isinstance(raw, dict):
return raw
if isinstance(raw, str) and raw.strip():
try:
parsed = json.loads(raw)
return parsed if isinstance(parsed, dict) else None
except json.JSONDecodeError:
return None
return None
def _filter_sources(task: ScheduleTask) -> list[dict[str, Any]]:
"""读取 enabled 库存源;api_body_template 可限定 source_code 子集。"""
enabled = list_inventory_sources()
body_template = _parse_api_body_template(task)
if body_template:
codes = body_template.get("source_codes") or body_template.get("source_code")
if isinstance(codes, str):
codes = [codes]
if isinstance(codes, list):
wanted = {str(code).strip().upper() for code in codes if str(code).strip()}
enabled = [item for item in enabled if str(item.get("source_code") or "").upper() in wanted]
return [source for source in enabled if build_rpa_inventory_source(source.get("source_code"))]
def run_shipping_inventory_rpa_schedule(task: ScheduleTask, run: ScheduleRun) -> ScheduleExecuteResult:
"""
写入批量库存 RPA trigger,由 worker 异步导出并在 callback 入库。
核心逻辑:不在排程内同步等待 RPA;ScheduleRun 记录 trigger 写入结果。
"""
sources = _filter_sources(task)
step_logs: list[dict[str, Any]] = [{
"step": "shipping_inventory_rpa",
"status": "started",
"detail": {"source_count": len(sources)},
}]
if not sources:
message = "no enabled inventory sources"
step_logs.append({"step": "shipping_inventory_rpa", "status": "skipped", "detail": {"message": message}})
return 0, 0, 0, 1, [], [message], step_logs
run_no = run.run_no or f"RUN{datetime.utcnow().strftime('%Y%m%d%H%M%S')}"
trigger_path = write_inventory_export_batch_trigger(
run_no,
sources,
payload={
"trigger_type": "scheduled",
"schedule_run_id": run.id,
"schedule_task_id": task.id,
},
)
if not trigger_path:
message = "inventory RPA trigger dir not configured"
step_logs.append({"step": "shipping_inventory_rpa", "status": "failed", "detail": {"message": message}})
return len(sources), 0, 0, len(sources), [], [message], step_logs
run.api_request_json = {
"run_no": run_no,
"trigger_path": trigger_path,
"source_codes": [item.get("source_code") for item in sources],
}
db.session.commit()
step_logs.append({
"step": "shipping_inventory_rpa",
"status": "triggered",
"detail": {"trigger_path": trigger_path, "run_no": run_no},
})
details = [
{
"source_code": item.get("source_code"),
"warehouse_id": item.get("warehouse_id"),
"display_name": item.get("display_name"),
}
for item in sources
]
return len(sources), len(sources), 0, 0, details, [], step_logs