feat(shipping-notice): 新增临时库存管理功能,支持创建会话、获取分析结果及字段映射建议

This commit is contained in:
hugo committed 2026-07-18 11:41:02 +08:00
1 parent 46eb63f5bb
commit e9bdd81f1a
320 files changed
+28361 -10487

No files matched your search

+6 -6
View File
@@ -2,7 +2,7 @@
backend/app/services/schedule/job_options.py
用途:平台任务参数 schema 的动态选项来源(options_source → 统一 label/value 列表)。
依赖:models.Agent、shipping_notice.inventory_sources。
依赖:models.Agent、shipping_notice.inventory_source_repository。
逻辑:api/schedule/platform_tasks.py options 接口与 job_params 选项范围校验调用;
前端只认识 options_source 字符串,不感知数据来自哪张表/哪个配置文件。
"""
@@ -32,8 +32,8 @@ def _agent_apps_options() -> list[dict[str, Any]]:
def _shipping_inventory_sources_options() -> list[dict[str, Any]]:
"""启用中的货代库存源(value=source_code,来自 backend/config/shipping_inventory_sources.yaml)。"""
from app.services.shipping_notice.inventory_sources import list_inventory_sources
"""启用中的货代库存源(value=source_code,来自数据库主数据)。"""
from app.services.shipping_notice.inventory_source_repository import list_database_inventory_sources
return [
{
@@ -41,11 +41,11 @@ def _shipping_inventory_sources_options() -> list[dict[str, Any]]:
"value": source.get("source_code"),
"description": (
f"{source.get('connector_type')} · "
f"{source.get('site_id') or '-'} · "
f"{source.get('warehouse_id')}"
f"{source.get('warehouse_resolution_mode')} · "
f"{source.get('fixed_warehouse_id') or '-'}"
),
}
for source in list_inventory_sources()
for source in list_database_inventory_sources()
]
@@ -36,14 +36,14 @@ _EMPTY_ARCHIVE_SUMMARY: dict[str, Any] = {
def get_inventory_archive_dir() -> str:
"""返回库存 RPA 原始 ZIP 归档根目录(自动创建)。"""
archive_dir = Path(
current_app.config.get("FREIGHT_RPA_INVENTORY_ARCHIVE_DIR")
current_app.config.get("SHIPPING_NOTICE_RPA_INVENTORY_ARCHIVE_DIR")
if current_app
else ""
)
if not str(archive_dir):
from app.core.config.settings import Config
archive_dir = Path(Config.FREIGHT_RPA_INVENTORY_ARCHIVE_DIR)
archive_dir = Path(Config.SHIPPING_NOTICE_RPA_INVENTORY_ARCHIVE_DIR)
archive_dir.mkdir(parents=True, exist_ok=True)
return str(archive_dir)
@@ -195,7 +195,7 @@ def _is_allowed_inventory_archive_path(zip_path: Path) -> bool:
"""
校验归档 ZIP 路径是否安全可下载。
核心逻辑:文件存在、后缀为 .zip、真实路径必须在 FREIGHT_RPA_INVENTORY_ARCHIVE_DIR 内。
核心逻辑:文件存在、后缀为 .zip、真实路径必须在 SHIPPING_NOTICE_RPA_INVENTORY_ARCHIVE_DIR 内。
"""
resolved = zip_path.resolve()
if resolved.suffix.lower() != ".zip":
@@ -216,7 +216,7 @@ def resolve_inventory_raw_zip_download(run: ScheduleRun) -> tuple[Path, str]:
解析 run 已归档的原始库存 ZIP 路径供下载接口 send_file。
核心逻辑:读取 summary.raw_inventory_zip_path,校验文件存在、后缀为 .zip、
且真实路径位于 FREIGHT_RPA_INVENTORY_ARCHIVE_DIR 内;任一不满足则 404。
且真实路径位于 SHIPPING_NOTICE_RPA_INVENTORY_ARCHIVE_DIR 内;任一不满足则 404。
Returns:
tuple[Path, str]: 归档 ZIP 绝对路径与建议下载文件名。
@@ -28,7 +28,6 @@ FinalizeScheduleRunResult = dict[str, Any]
_INVENTORY_RPA_HEADER_ORDER = [
"source_code",
"site_id",
"warehouse_id",
"display_name",
"inventory_file_no",
@@ -38,7 +37,6 @@ _INVENTORY_RPA_HEADER_ORDER = [
]
_INVENTORY_RPA_HEADER_LABELS = {
"source_code": "库存源",
"site_id": "站点",
"warehouse_id": "仓库",
"display_name": "名称",
"inventory_file_no": "库存快照号",
@@ -145,7 +143,6 @@ def _build_finalize_details(
base = pending_by_key.get(key, {})
details.append({
"source_code": code or item.get("source_code"),
"site_id": item.get("site_id") or base.get("site_id"),
"warehouse_id": wh or base.get("warehouse_id"),
"display_name": base.get("display_name"),
"inventory_file_no": item.get("inventory_file_no"),
@@ -163,7 +160,6 @@ def _build_finalize_details(
base = pending_by_key.get(key, {})
details.append({
"source_code": code or item.get("source_code"),
"site_id": item.get("site_id") or base.get("site_id"),
"warehouse_id": wh or base.get("warehouse_id"),
"display_name": base.get("display_name"),
"inventory_file_no": "",
@@ -181,7 +177,6 @@ def _build_finalize_details(
base = pending_by_key.get(key, {})
details.append({
"source_code": code or item.get("source_code"),
"site_id": item.get("site_id") or base.get("site_id"),
"warehouse_id": wh or base.get("warehouse_id"),
"display_name": base.get("display_name"),
"inventory_file_no": "",
@@ -194,7 +189,6 @@ def _build_finalize_details(
continue
details.append({
"source_code": base.get("source_code"),
"site_id": base.get("site_id"),
"warehouse_id": base.get("warehouse_id"),
"display_name": base.get("display_name"),
"inventory_file_no": "",
@@ -1,8 +1,8 @@
"""
backend/app/services/schedule/shipping_inventory_rpa_job.py
用途:排程触发货代库存 RPA 批量导出(各 Warehouse 一次登录多导出)。
依赖:inventory_sources、rpa.write_inventory_export_batch_trigger、ScheduleTask/ScheduleRun。
用途:排程触发货代库存 RPA 批量导出(每个 Inventory Source 一次执行)。
依赖:inventory_source_repository、rpa.write_inventory_export_batch_trigger、ScheduleTask/ScheduleRun。
逻辑:platform_api_path=shipping-inventory-rpa 时由 job_registry 调用;
写 trigger 后返回 awaiting_rpa_callback;终态由 shipping_inventory_rpa_finalize 在 RPA 回调时写入。
"""
@@ -17,7 +17,7 @@ from app import db
from app.core.errors import ServiceUnavailableError
from app.models import ScheduleRun, ScheduleTask
from app.services.schedule.shipping_inventory_rpa_finalize import AWAITING_RPA_CALLBACK_STATUS
from app.services.shipping_notice.inventory_sources import build_rpa_inventory_source, list_inventory_sources
from app.services.shipping_notice.inventory_source_repository import list_database_inventory_sources
from app.services.shipping_notice.rpa import write_inventory_export_batch_trigger
from app.services.shipping_notice.rpa_worker_heartbeat import RPA_WORKER_OFFLINE_MESSAGE
@@ -53,34 +53,15 @@ def _normalize_filter_tokens(raw: Any) -> set[str]:
return {str(item).strip().upper() for item in values if str(item).strip()}
def _source_identity_tokens(source: dict[str, Any]) -> set[str]:
"""收集库存源可用于匹配的标识集合(source_code / site_id / warehouse_id / match_aliases)。"""
tokens: set[str] = set()
for key in ("source_code", "site_id", "warehouse_id"):
value = str(source.get(key) or "").strip().upper()
if value:
tokens.add(value)
for alias in source.get("match_aliases") or []:
value = str(alias or "").strip().upper()
if value:
tokens.add(value)
return tokens
def _extract_wanted_tokens(body_template: dict[str, Any]) -> set[str]:
"""
从 api_body_template 提取筛选 token。
推荐键:source_codes;兼容:source_code / site_ids / site_id / warehouse_ids / warehouse_id / match_aliases。
仅支持 source_codes/source_code,排程目标必须是 Inventory Source。
"""
wanted: set[str] = set()
for key in (
"source_codes",
"source_code",
"site_ids",
"site_id",
"warehouse_ids",
"warehouse_id",
"match_aliases",
):
if key in body_template:
wanted |= _normalize_filter_tokens(body_template.get(key))
@@ -107,18 +88,18 @@ def _resolve_filter_payload(task: ScheduleTask) -> dict[str, Any] | None:
def _filter_sources(task: ScheduleTask) -> list[dict[str, Any]]:
"""
读取 enabled 库存源;job_params / api_body_template 可限定子集。
核心逻辑:无筛选则导出全部启用源;有筛选时按 source_code / site_id / warehouse_id / match_aliases 命中。
核心逻辑:无筛选则导出全部启用 Source;有筛选时严格按 source_code 命中。
"""
enabled = list_inventory_sources()
enabled = list_database_inventory_sources()
body_template = _resolve_filter_payload(task)
if body_template:
wanted = _extract_wanted_tokens(body_template)
if wanted:
enabled = [
item for item in enabled
if _source_identity_tokens(item) & wanted
if str(item.get('source_code') or '').strip().upper() in wanted
]
return [source for source in enabled if build_rpa_inventory_source(source.get("source_code"))]
return enabled
def run_shipping_inventory_rpa_schedule(task: ScheduleTask, run: ScheduleRun) -> ScheduleExecuteResult:
@@ -183,8 +164,7 @@ def run_shipping_inventory_rpa_schedule(task: ScheduleTask, run: ScheduleRun) ->
details = [
{
"source_code": item.get("source_code"),
"site_id": item.get("site_id"),
"warehouse_id": item.get("warehouse_id"),
"warehouse_id": item.get("fixed_warehouse_id"),
"display_name": item.get("display_name"),
}
for item in sources