feat(freight-rpa): 库存/收料改名在手/入站,报表归属RPA,类名统一FreightRpa

把 shipping 侧 inventory/receipt 词根迁到 onhand/inbound,29 张表与
枚举/权限/前端/i18n 同步改名;在手与入站报表配置及文件改由 freight_rpa
持有,发货与调拨仅按 source_code 消费;ORM 类加 FreightRpa 前缀,
上传动作改为 freight_rpa_upload。含类名与上传动作改名规格文档。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-10-19 10:46:06 +08:00
1 parent ffc0d4a430
commit 39fe3c05f2
453 files changed
+17945 -8646

No files matched your search

@@ -1,10 +1,12 @@
"""Import legacy YAML inventory sources into the shipping configuration database.
"""
scripts/import_freight_rpa_sources.py
Purpose: migrate business report paths, query inputs, aliases, and warehouse policies from
``config/shipping_inventory_sources.yaml`` into the new source master data.
Dependencies: Flask app context, SQLAlchemy models, PyYAML.
Flow: reuses connections by platform and login URL, keeps all imported sources disabled, and
never imports YAML selectors or credentials because those are worker implementation/secrets.
用途:把期初 YAML 库存源导入 freight_rpa_sources / freight_rpa_profiles 主数据。
依赖:Flask app、SQLAlchemy、PyYAML、models.FreightRpaSource、models.FreightRpaProfile、
models.FreightRpaOnhandReportConfig(s18 起报表路径/查询条件归配置行)。
逻辑:由运维手工执行;读取 config/freight_rpa_sources.yaml,按平台与登录地址复用连接,
导入行一律保持停用,不导入 selector 与凭据;YAML 的报表路径与查询条件写入
FreightRpaOnhandReportConfig 配置行,解析方案绑定留空。运行时不再读这份 YAML。
"""
from __future__ import annotations
@@ -17,7 +19,7 @@ import yaml
from app import create_app
from app.extensions import db
from app.models import ShippingInventorySource, ShippingRpaProfile
from app.models import FreightRpaOnhandReportConfig, FreightRpaSource, FreightRpaProfile
CONNECTOR_TYPES = {"dsv360": "dbs", "crane_footprint": "crane"}
@@ -30,7 +32,9 @@ def _source_payload(raw: dict[str, Any]) -> dict[str, Any]:
raw: A source item from the legacy YAML document.
Returns:
Normalized source fields suitable for ``ShippingInventorySource``.
Normalized source fields for ``FreightRpaSource`` plus the
``report_path_json`` / ``query_parameters_json`` carried by
``FreightRpaOnhandReportConfig`` since s18.
"""
connector_type = CONNECTOR_TYPES.get(str(raw.get("connector_type") or "").strip().lower())
if connector_type is None:
@@ -64,32 +68,32 @@ def _source_payload(raw: dict[str, Any]) -> dict[str, Any]:
}
def _connection_for(source: dict[str, Any]) -> ShippingRpaProfile:
def _connection_for(source: dict[str, Any]) -> FreightRpaProfile:
"""Return a compatible connection, reusing configured credentials whenever possible."""
profile = ShippingRpaProfile.query.filter_by(
profile = FreightRpaProfile.query.filter_by(
connector_type=source["connector_type"], login_url=source["login_url"],
).order_by(ShippingRpaProfile.id).first()
).order_by(FreightRpaProfile.id).first()
if profile is not None and (profile.username_ciphertext or profile.password_ciphertext):
return profile
# Legacy YAML stores the portal root while a configured DBS connection can use a portal path.
# Prefer an existing credential-bearing platform connection instead of creating an unusable duplicate.
if source["connector_type"] == "dbs":
profile = ShippingRpaProfile.query.filter_by(connector_type="dbs").filter(
(ShippingRpaProfile.username_ciphertext.is_not(None)) | (ShippingRpaProfile.password_ciphertext.is_not(None))
).order_by(ShippingRpaProfile.credential_updated_at.desc(), ShippingRpaProfile.id).first()
profile = FreightRpaProfile.query.filter_by(connector_type="dbs").filter(
(FreightRpaProfile.username_ciphertext.is_not(None)) | (FreightRpaProfile.password_ciphertext.is_not(None))
).order_by(FreightRpaProfile.credential_updated_at.desc(), FreightRpaProfile.id).first()
if profile is not None:
return profile
if profile is not None:
return profile
# The historical Crane connection was initially stored as DBS before source master data existed.
if source["connector_type"] == "crane":
profile = ShippingRpaProfile.query.filter_by(profile_code="CRANE").first()
profile = FreightRpaProfile.query.filter_by(profile_code="CRANE").first()
if profile is not None:
profile.connector_type = "crane"
profile.login_url = source["login_url"]
return profile
code = "DBS_S360" if source["connector_type"] == "dbs" else "CRANE"
profile = ShippingRpaProfile(
profile = FreightRpaProfile(
profile_code=code,
display_name="DBS 平台" if source["connector_type"] == "dbs" else "Crane 平台",
connector_type=source["connector_type"],
@@ -118,9 +122,9 @@ def import_sources(path: Path) -> tuple[int, int]:
continue
data = _source_payload(raw)
profile = _connection_for(data)
source = ShippingInventorySource.query.filter_by(source_code=data["source_code"]).first()
source = FreightRpaSource.query.filter_by(source_code=data["source_code"]).first()
if source is None:
source = ShippingInventorySource(
source = FreightRpaSource(
source_code=data["source_code"], display_name=data["display_name"],
connector_type=data["connector_type"], rpa_profile=profile,
warehouse_resolution_mode=data["warehouse_resolution_mode"], enabled=False,
@@ -132,18 +136,24 @@ def import_sources(path: Path) -> tuple[int, int]:
source.display_name = data["display_name"]
source.connector_type = data["connector_type"]
source.rpa_profile = profile
source.parsing_profile_id = None
source.excel_template_id = None
source.warehouse_resolution_mode = data["warehouse_resolution_mode"]
source.fixed_warehouse_id = data["fixed_warehouse_id"]
source.report_path_json = data["report_path_json"]
source.query_parameters_json = data["query_parameters_json"]
# s18 起报表路径/查询条件归 FreightRpaOnhandReportConfig 配置行(无则插入);
# 解析方案绑定保持 None,与今日导入语义一致(启用前由管理员绑定)。
config = source.onhand_report_config
if config is None:
config = FreightRpaOnhandReportConfig(source=source)
db.session.add(config)
config.parsing_profile_id = None
config.report_path_json = data["report_path_json"]
config.query_parameters_json = data["query_parameters_json"]
source.snapshot_ttl_seconds = 86400
# Templates are not present in legacy YAML; activation requires published bindings.
source.enabled = False
db.session.commit()
# A previous run may have created this deterministic placeholder before a configured DBS connection was found.
placeholder = ShippingRpaProfile.query.filter_by(profile_code="DBS_S360").first()
placeholder = FreightRpaProfile.query.filter_by(profile_code="DBS_S360").first()
if placeholder and not placeholder.sources and not placeholder.username_ciphertext and not placeholder.password_ciphertext:
db.session.delete(placeholder)
db.session.commit()
@@ -152,13 +162,13 @@ def import_sources(path: Path) -> tuple[int, int]:
def main() -> None:
"""Run the controlled YAML import and print counts without exposing credentials."""
parser = argparse.ArgumentParser(description="Import legacy shipping inventory sources")
parser.add_argument("--file", type=Path, default=Path("config/shipping_inventory_sources.yaml"))
parser = argparse.ArgumentParser(description="Import legacy freight RPA sources")
parser.add_argument("--file", type=Path, default=Path("config/freight_rpa_sources.yaml"))
args = parser.parse_args()
app = create_app()
with app.app_context():
created, updated = import_sources(args.file)
print(f"Imported inventory sources: created={created}, updated={updated}")
print(f"Imported freight RPA sources: created={created}, updated={updated}")
if __name__ == "__main__":