上传页、按钮和成功判断改由连接器固定。Crane 分配中只排一次 10 分钟状态回查,手动刷新不改到期时间。 Co-authored-by: Cursor <cursoragent@cursor.com>
459 lines
29 KiB
Python
459 lines
29 KiB
Python
"""
|
||
backend/app/models/freight_rpa_config.py
|
||
|
||
用途:定义货代库存源、RPA Profile、在手/入站/出站报表配置、通用路由规则及外部系统仓库映射主数据。
|
||
依赖:models.base.Base、SQLAlchemy ORM;凭据密文由 freight_rpa.common.credential_crypto 读写。
|
||
逻辑:库存运行链路以 FreightRpaSource.source_code 为业务主键;RPA Profile 仅提供登录连接配置;
|
||
在手报表路径/查询条件/解析方案绑定自 s18 起归 FreightRpaOnhandReportConfig(站点行不再存这三列),
|
||
入站/出站配置分别挂 inbound_report_config / outbound_report_config 一对一关系;
|
||
外部系统映射仅供后端内部复用,不承载调拨决策。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import datetime
|
||
from typing import Any, Optional
|
||
|
||
from sqlalchemy import Boolean, CheckConstraint, DateTime, ForeignKey, Index, Integer, JSON, String, Text, UniqueConstraint, text
|
||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||
|
||
from app.models.base import Base
|
||
|
||
|
||
class FreightRpaOnhandField(Base):
|
||
"""用户维护的库存业务字段目录;程序仅依据字段 ID 和业务用途运行。"""
|
||
|
||
__tablename__ = 'freight_rpa_onhand_fields'
|
||
__table_args__ = (
|
||
UniqueConstraint('field_code', name='uq_freight_rpa_onhand_fields_code'),
|
||
UniqueConstraint('display_name', name='uq_freight_rpa_onhand_fields_display_name'),
|
||
CheckConstraint("status IN ('active', 'retired')", name='ck_freight_rpa_onhand_fields_status'),
|
||
CheckConstraint("value_type IN ('text', 'integer', 'decimal', 'date')", name='ck_freight_rpa_onhand_fields_value_type'),
|
||
CheckConstraint(
|
||
"usage_type IN ('match_identity', 'inventory_quantity', 'warehouse_identity', 'general')",
|
||
name='ck_freight_rpa_onhand_fields_usage_type',
|
||
),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
# Internal stable code supports runtime contracts; it is deliberately not exposed in the business UI.
|
||
field_code: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
display_name: Mapped[str] = mapped_column(String(128), nullable=False)
|
||
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||
value_type: Mapped[str] = mapped_column(String(16), nullable=False, default='text', server_default='text')
|
||
usage_type: Mapped[str] = mapped_column(String(32), nullable=False, default='general', server_default='general')
|
||
status: Mapped[str] = mapped_column(String(16), nullable=False, default='active', server_default='active')
|
||
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=100, server_default='100')
|
||
created_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
updated_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
|
||
|
||
|
||
class FreightRpaProfile(Base):
|
||
"""货代库存登录连接配置;凭据字段始终保存密文。"""
|
||
|
||
__tablename__ = 'freight_rpa_profiles'
|
||
__table_args__ = (
|
||
UniqueConstraint('profile_code', name='uq_freight_rpa_profiles_profile_code'),
|
||
CheckConstraint("connector_type IN ('dbs', 'crane')", name='ck_freight_rpa_profiles_connector_type'),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
profile_code: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
display_name: Mapped[str] = mapped_column(String(128), nullable=False)
|
||
connector_type: Mapped[str] = mapped_column(String(32), nullable=False)
|
||
login_url: Mapped[str] = mapped_column(String(500), nullable=False)
|
||
username_ciphertext: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||
password_ciphertext: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||
login_config_json: Mapped[dict[str, Any]] = mapped_column(
|
||
JSON, default=lambda: {'auth_mode': 'password'}, nullable=False,
|
||
)
|
||
# 连接级“发货 Excel 自动上传”开关。只保存 {enabled}。
|
||
# 上传页、文件控件、提交和成功判断在连接器代码里,由 login_url 的主机拼出入口。
|
||
# 该配置变更不递增 config_version(见 admin 服务)。
|
||
upload_config_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
||
enabled: Mapped[bool] = mapped_column(default=True, server_default=text('true'), nullable=False)
|
||
config_version: Mapped[int] = mapped_column(Integer, default=1, server_default=text('1'), nullable=False)
|
||
credential_updated_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||
created_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
updated_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
|
||
|
||
sources: Mapped[list['FreightRpaSource']] = relationship(back_populates='rpa_profile')
|
||
|
||
|
||
class FreightRpaSource(Base):
|
||
"""库存业务来源;固定仓或库存行动态解析 Warehouse。"""
|
||
|
||
__tablename__ = 'freight_rpa_sources'
|
||
__table_args__ = (
|
||
UniqueConstraint('source_code', name='uq_freight_rpa_sources_source_code'),
|
||
CheckConstraint("connector_type IN ('dbs', 'crane')", name='ck_freight_rpa_sources_connector_type'),
|
||
CheckConstraint(
|
||
"(warehouse_resolution_mode = 'fixed' AND fixed_warehouse_id IS NOT NULL) "
|
||
"OR (warehouse_resolution_mode = 'inventory_match' AND fixed_warehouse_id IS NULL)",
|
||
name='ck_freight_rpa_sources_warehouse_mode',
|
||
),
|
||
CheckConstraint(
|
||
"enabled = false OR excel_template_id IS NOT NULL",
|
||
name='ck_freight_rpa_sources_enabled_template',
|
||
),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
source_code: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
display_name: Mapped[str] = mapped_column(String(128), nullable=False)
|
||
connector_type: Mapped[str] = mapped_column(String(32), nullable=False)
|
||
rpa_profile_id: Mapped[int] = mapped_column(ForeignKey('freight_rpa_profiles.id'), nullable=False, index=True)
|
||
excel_template_id: Mapped[Optional[int]] = mapped_column(ForeignKey('shipping_excel_templates.id'), nullable=True, index=True)
|
||
warehouse_resolution_mode: Mapped[str] = mapped_column(String(32), nullable=False)
|
||
fixed_warehouse_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
# 业务报表定义自 s18 起搬到 FreightRpaOnhandReportConfig(路径、查询条件、解析方案绑定);
|
||
# CHECK 不能跨表,模板绑定仍留在站点行的启用约束上,解析方案存在性由保存站点的服务校验。
|
||
snapshot_ttl_seconds: Mapped[int] = mapped_column(Integer, default=86400, server_default=text('86400'), nullable=False)
|
||
enabled: Mapped[bool] = mapped_column(default=True, server_default=text('true'), nullable=False)
|
||
config_version: Mapped[int] = mapped_column(Integer, default=1, server_default=text('1'), nullable=False)
|
||
created_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
updated_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
|
||
|
||
rpa_profile: Mapped[FreightRpaProfile] = relationship(back_populates='sources')
|
||
excel_template: Mapped[Optional['ShippingExcelTemplate']] = relationship(back_populates='sources')
|
||
onhand_report_config: Mapped[Optional['FreightRpaOnhandReportConfig']] = relationship(
|
||
back_populates='source', uselist=False, cascade='all, delete-orphan',
|
||
)
|
||
external_mappings: Mapped[list['FreightRpaOnhandExternalMapping']] = relationship(
|
||
back_populates='source', cascade='all, delete-orphan'
|
||
)
|
||
inbound_report_config: Mapped[Optional['FreightRpaInboundReportConfig']] = relationship(
|
||
back_populates='source', uselist=False, cascade='all, delete-orphan',
|
||
)
|
||
outbound_report_config: Mapped[Optional['FreightRpaOutboundReportConfig']] = relationship(
|
||
back_populates='source', uselist=False, cascade='all, delete-orphan',
|
||
)
|
||
|
||
|
||
class FreightRpaOnhandReportConfig(Base):
|
||
"""库存站点的一对一在手报表配置;报表路径与查询条件归 RPA 域。
|
||
|
||
解析方案绑定随本表保存(purpose='onhand')。站点启用要求「本配置存在且
|
||
parsing_profile_id 非空」,PostgreSQL CHECK 不能跨表,该校验由保存站点的服务执行。
|
||
"""
|
||
|
||
__tablename__ = 'freight_rpa_onhand_report_configs'
|
||
__table_args__ = (
|
||
UniqueConstraint('source_id', name='uq_freight_rpa_onhand_report_configs_source'),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
# 站点删除时配置行级联删除(与迁移端 fk_freight_rpa_onhand_report_configs_source 的 ondelete 一致)。
|
||
source_id: Mapped[int] = mapped_column(
|
||
ForeignKey('freight_rpa_sources.id', ondelete='CASCADE'), nullable=False, index=True,
|
||
)
|
||
# 在手报表文件解析规则(purpose='onhand');空则站点不得启用(服务层校验)。
|
||
parsing_profile_id: Mapped[Optional[int]] = mapped_column(
|
||
ForeignKey('freight_rpa_parsing_profiles.id'), nullable=True, index=True,
|
||
)
|
||
# 业务报表定义属于配置行;Driver 的 selector、等待与下载实现不进入数据库配置。
|
||
report_path_json: Mapped[dict[str, Any]] = mapped_column(
|
||
JSON, default=dict, server_default=text("'{}'"), nullable=False,
|
||
)
|
||
# 与入站同构:查询条件原样下发 Worker;空条件表示页面默认。
|
||
query_parameters_json: Mapped[list[dict[str, Any]]] = mapped_column(
|
||
JSON, default=list, server_default=text("'[]'"), nullable=False,
|
||
)
|
||
config_version: Mapped[int] = mapped_column(Integer, default=1, server_default=text('1'), nullable=False)
|
||
created_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
updated_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
|
||
|
||
source: Mapped['FreightRpaSource'] = relationship(back_populates='onhand_report_config')
|
||
parsing_profile: Mapped[Optional['FreightRpaParsingProfile']] = relationship(back_populates='sources')
|
||
|
||
|
||
class FreightRpaSourceParsingBindingAudit(Base):
|
||
"""库存站点解析方案绑定的只追加审计;保存绑定前后快照以支持上线追溯。"""
|
||
|
||
__tablename__ = 'freight_rpa_onhand_source_parsing_binding_audits'
|
||
__table_args__ = (
|
||
Index('ix_inventory_source_parsing_binding_audit_source_created', 'source_id', 'created_at'),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
source_id: Mapped[int] = mapped_column(ForeignKey('freight_rpa_sources.id'), nullable=False)
|
||
previous_parsing_profile_id: Mapped[Optional[int]] = mapped_column(
|
||
ForeignKey('freight_rpa_parsing_profiles.id'), nullable=True,
|
||
)
|
||
parsing_profile_id: Mapped[int] = mapped_column(ForeignKey('freight_rpa_parsing_profiles.id'), nullable=False)
|
||
operation: Mapped[str] = mapped_column(String(32), nullable=False)
|
||
reason: Mapped[str] = mapped_column(String(500), nullable=False)
|
||
actor_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
source_config_version_before: Mapped[int] = mapped_column(Integer, nullable=False)
|
||
source_config_version_after: Mapped[int] = mapped_column(Integer, nullable=False)
|
||
before_snapshot_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
after_snapshot_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
|
||
|
||
class FreightRpaParsingProfile(Base):
|
||
"""可跨源复用的通用文件解析规则(在手/入站/出站按 purpose 区分)。
|
||
|
||
表 freight_rpa_parsing_profiles 按 purpose 服务在手/入站/出站。
|
||
purpose='onhand':在手库存文件,端点用 FreightRpaOnhandField 库存字段目录;
|
||
purpose='inbound':入站文件,端点用定死领域字段
|
||
(MOVEMENT_FIELDS,doc_no/line_no/.../movement_date),
|
||
不依赖库存字段目录。校验逻辑按 purpose 分支。
|
||
绑定入口:在手配置 FreightRpaOnhandReportConfig.parsing_profile_id /
|
||
入站配置 FreightRpaInboundReportConfig.parsing_profile_id(后者必须 purpose='inbound');
|
||
purpose='outbound':出站文件,结构就位(白名单放行,暂无运行时消费方)。
|
||
"""
|
||
|
||
__tablename__ = 'freight_rpa_parsing_profiles'
|
||
__table_args__ = (
|
||
UniqueConstraint('profile_code', 'version', name='uq_inventory_parsing_profiles_code_version'),
|
||
Index('ix_inv_parse_profile_exact_fp', 'exact_structure_fingerprint'),
|
||
Index('ix_inv_parse_profile_compat_fp', 'compatibility_fingerprint'),
|
||
CheckConstraint(
|
||
"purpose IN ('onhand', 'inbound', 'outbound')",
|
||
name='ck_freight_rpa_parsing_profiles_purpose',
|
||
),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
purpose: Mapped[str] = mapped_column(String(16), nullable=False, default='onhand', server_default='onhand')
|
||
profile_code: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
display_name: Mapped[str] = mapped_column(String(128), nullable=False)
|
||
version: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default=text('1'))
|
||
status: Mapped[str] = mapped_column(String(16), nullable=False, default='draft', server_default='draft')
|
||
file_format: Mapped[str] = mapped_column(String(16), nullable=False, default='xlsx', server_default='xlsx')
|
||
definition_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
checksum: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
structure_definition_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
exact_structure_fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
compatibility_fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
fingerprint_version: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
|
||
# Draft and published definitions keep their analysed sample evidence without retaining the full uploaded workbook.
|
||
sample_analysis_id: Mapped[Optional[int]] = mapped_column(
|
||
ForeignKey('freight_rpa_onhand_structure_analyses.id'), nullable=True, index=True,
|
||
)
|
||
# Preserve every confirmed evidence switch so a draft remains explainable after samples change.
|
||
sample_evidence_history_json: Mapped[list[dict[str, Any]]] = mapped_column(
|
||
JSON, nullable=False, default=list, server_default=text("'[]'"),
|
||
)
|
||
created_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
published_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
published_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||
|
||
# s18 起站点行不再绑定解析方案;在手配置行(FreightRpaOnhandReportConfig)是 onhand 侧的唯一挂点。
|
||
sources: Mapped[list['FreightRpaOnhandReportConfig']] = relationship(back_populates='parsing_profile')
|
||
sample_analysis: Mapped[Optional['FreightRpaOnhandStructureAnalysis']] = relationship(
|
||
foreign_keys=[sample_analysis_id],
|
||
)
|
||
|
||
|
||
class FreightRpaOnhandStructureAnalysis(Base):
|
||
"""库存文件结构分析记录;保存脱敏结构证据,不承载正式库存快照。"""
|
||
|
||
__tablename__ = 'freight_rpa_onhand_structure_analyses'
|
||
__table_args__ = (
|
||
Index('ix_inv_structure_analysis_file_hash', 'file_checksum'),
|
||
Index('ix_inv_structure_analysis_created', 'created_at'),
|
||
Index('ix_inv_structure_analysis_exact_fp', 'exact_structure_fingerprint'),
|
||
Index('ix_inv_structure_analysis_compat_fp', 'compatibility_fingerprint'),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
source_kind: Mapped[str] = mapped_column(String(32), nullable=False, default='sample_upload', server_default='sample_upload')
|
||
source_file_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||
# Only temporary conversation sessions retain a short-lived source file for confirmation.
|
||
storage_path: Mapped[Optional[str]] = mapped_column(String(500), nullable=True)
|
||
file_checksum: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
file_format: Mapped[str] = mapped_column(String(16), nullable=False)
|
||
analysis_status: Mapped[str] = mapped_column(String(32), nullable=False, default='ready', server_default='ready')
|
||
structure_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
suggested_mapping_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
validation_summary_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
sample_rows_json: Mapped[list[list[Any]]] = mapped_column(JSON, nullable=False, default=list)
|
||
warning_json: Mapped[list[str]] = mapped_column(JSON, nullable=False, default=list)
|
||
exact_structure_fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
compatibility_fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
match_result_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
created_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
|
||
|
||
class FreightRpaOnhandConfigurationTestRun(Base):
|
||
"""库存站点配置测试的异步 RPA 运行;锁定站点和连接配置版本且不进入生产库存池。"""
|
||
|
||
__tablename__ = 'freight_rpa_onhand_configuration_test_runs'
|
||
__table_args__ = (
|
||
UniqueConstraint('run_no', name='uq_inventory_configuration_test_runs_run_no'),
|
||
# PostgreSQL 标识符上限 63。index=True 会生成 65 字节的自动名,故用显式短名。
|
||
Index('ix_fr_onhand_cfg_test_runs_validation_batch_id', 'validation_batch_id'),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
run_no: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
validation_batch_id: Mapped[Optional[int]] = mapped_column(
|
||
ForeignKey('freight_rpa_onhand_validation_batches.id'), nullable=True,
|
||
)
|
||
rpa_batch_run_no: Mapped[Optional[str]] = mapped_column(String(64), nullable=True, index=True)
|
||
source_id: Mapped[int] = mapped_column(ForeignKey('freight_rpa_sources.id'), nullable=False, index=True)
|
||
source_config_version: Mapped[int] = mapped_column(Integer, nullable=False)
|
||
rpa_profile_config_version: Mapped[int] = mapped_column(Integer, nullable=False)
|
||
status: Mapped[str] = mapped_column(String(32), nullable=False, default='awaiting_rpa', server_default='awaiting_rpa')
|
||
stage: Mapped[str] = mapped_column(String(32), nullable=False, default='waiting_worker', server_default='waiting_worker')
|
||
analysis_id: Mapped[Optional[int]] = mapped_column(ForeignKey('freight_rpa_onhand_structure_analyses.id'), nullable=True)
|
||
result_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False, default=dict)
|
||
error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||
created_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
completed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||
|
||
|
||
class FreightRpaOnhandValidationBatch(Base):
|
||
"""库存站点批量验证的聚合运行;逐站点结果仍由 ConfigurationTestRun 保存。"""
|
||
|
||
__tablename__ = 'freight_rpa_onhand_validation_batches'
|
||
__table_args__ = (UniqueConstraint('batch_no', name='uq_inventory_validation_batches_batch_no'),)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
batch_no: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
status: Mapped[str] = mapped_column(String(32), nullable=False, default='queued', server_default='queued')
|
||
total_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text('0'))
|
||
success_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text('0'))
|
||
empty_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text('0'))
|
||
failed_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text('0'))
|
||
stale_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text('0'))
|
||
created_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
retry_of_batch_id: Mapped[Optional[int]] = mapped_column(
|
||
ForeignKey('freight_rpa_onhand_validation_batches.id'), nullable=True,
|
||
)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
completed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||
|
||
|
||
class FreightRpaOnhandRoutingRule(Base):
|
||
"""可版本化的库存源路由决策表;仅产出受控 Source 候选。"""
|
||
|
||
__tablename__ = 'freight_rpa_onhand_routing_rules'
|
||
__table_args__ = (
|
||
UniqueConstraint('rule_code', 'version', name='uq_freight_rpa_onhand_routing_rules_code_version'),
|
||
CheckConstraint("scope IN ('task', 'line')", name='ck_freight_rpa_onhand_routing_rules_scope'),
|
||
CheckConstraint("match_mode IN ('all', 'any')", name='ck_freight_rpa_onhand_routing_rules_match_mode'),
|
||
CheckConstraint("status IN ('draft', 'published', 'retired')", name='ck_freight_rpa_onhand_routing_rules_status'),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
rule_code: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
rule_name: Mapped[str] = mapped_column(String(128), nullable=False)
|
||
scope: Mapped[str] = mapped_column(String(16), nullable=False)
|
||
priority: Mapped[int] = mapped_column(Integer, default=0, server_default=text('0'), nullable=False)
|
||
match_mode: Mapped[str] = mapped_column(String(16), default='all', server_default=text("'all'"), nullable=False)
|
||
status: Mapped[str] = mapped_column(String(16), default='draft', server_default=text("'draft'"), nullable=False)
|
||
action_type: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
action_payload_json: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict, nullable=False)
|
||
version: Mapped[int] = mapped_column(Integer, default=1, server_default=text('1'), nullable=False)
|
||
effective_from: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||
effective_to: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||
created_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
updated_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
|
||
|
||
conditions: Mapped[list['FreightRpaOnhandRoutingRuleCondition']] = relationship(
|
||
back_populates='rule', cascade='all, delete-orphan', order_by='FreightRpaOnhandRoutingRuleCondition.sequence'
|
||
)
|
||
|
||
|
||
class FreightRpaOnhandRoutingRuleCondition(Base):
|
||
"""库存源路由条件;仅引用已发布订单 Schema 的稳定 field_id。"""
|
||
|
||
__tablename__ = 'freight_rpa_onhand_routing_rule_conditions'
|
||
__table_args__ = (
|
||
UniqueConstraint('rule_id', 'sequence', name='uq_freight_rpa_onhand_routing_rule_conditions_sequence'),
|
||
CheckConstraint(
|
||
"source_kind = 'order_field' AND order_field_id IS NOT NULL",
|
||
name='ck_inventory_routing_condition_source',
|
||
),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
rule_id: Mapped[int] = mapped_column(ForeignKey('freight_rpa_onhand_routing_rules.id'), nullable=False, index=True)
|
||
condition_group: Mapped[int] = mapped_column(Integer, default=1, server_default=text('1'), nullable=False)
|
||
source_kind: Mapped[str] = mapped_column(String(16), nullable=False, default='order_field')
|
||
order_field_id: Mapped[Optional[str]] = mapped_column(ForeignKey('shipping_order_fields.id'), nullable=True)
|
||
operator: Mapped[str] = mapped_column(String(32), nullable=False)
|
||
expected_value_json: Mapped[Any] = mapped_column(JSON, nullable=True)
|
||
sequence: Mapped[int] = mapped_column(Integer, nullable=False)
|
||
|
||
rule: Mapped[FreightRpaOnhandRoutingRule] = relationship(back_populates='conditions')
|
||
|
||
|
||
class FreightRpaOnhandRoutingRuleVersion(Base):
|
||
"""已发布路由规则的不可变定义快照,供任务审计回放使用。"""
|
||
|
||
__tablename__ = 'freight_rpa_onhand_routing_rule_versions'
|
||
__table_args__ = (UniqueConstraint('rule_id', 'version', name='uq_freight_rpa_onhand_routing_rule_versions_rule_version'),)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
rule_id: Mapped[int] = mapped_column(ForeignKey('freight_rpa_onhand_routing_rules.id'), nullable=False, index=True)
|
||
version: Mapped[int] = mapped_column(Integer, nullable=False)
|
||
definition_json: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False)
|
||
published_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
published_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
|
||
|
||
class FreightRpaOnhandExternalMapping(Base):
|
||
"""库存源到外部系统仓库的映射。
|
||
|
||
freight_warehouse_id(货代仓库)为可选维度:inventory_match 源用它匹配收料文件
|
||
「仓库」列(空值即「空库位」默认映射行,下方两个部分唯一索引保证同源最多
|
||
一条启用的精确/默认映射);fixed 源仅一条启用映射直接取用。
|
||
in_transit_warehouse_code(在途仓编码)是在途调拨拨出库位的来源
|
||
(in_transit_transfer DefaultRouteResolver),保存端必填,库内空串仅为存量行兼容。
|
||
"""
|
||
|
||
__tablename__ = 'freight_rpa_onhand_external_mappings'
|
||
__table_args__ = (
|
||
Index(
|
||
'uq_shipping_inv_ext_map_src_default_enabled',
|
||
'source_id',
|
||
unique=True,
|
||
postgresql_where=text('freight_warehouse_id IS NULL AND enabled = true'),
|
||
sqlite_where=text('freight_warehouse_id IS NULL AND enabled = 1'),
|
||
),
|
||
Index(
|
||
'uq_shipping_inv_ext_map_src_wh_enabled',
|
||
'source_id',
|
||
'freight_warehouse_id',
|
||
unique=True,
|
||
postgresql_where=text('freight_warehouse_id IS NOT NULL AND enabled = true'),
|
||
sqlite_where=text('freight_warehouse_id IS NOT NULL AND enabled = 1'),
|
||
),
|
||
)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
source_id: Mapped[int] = mapped_column(ForeignKey('freight_rpa_sources.id'), nullable=False, index=True)
|
||
freight_warehouse_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
external_system_code: Mapped[str] = mapped_column(String(32), nullable=False)
|
||
external_organization_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
external_site_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||
external_warehouse_code: Mapped[str] = mapped_column(String(64), nullable=False)
|
||
external_warehouse_name: Mapped[str] = mapped_column(String(128), nullable=False)
|
||
# 在途仓编码:在途调拨拨出库位来源(DefaultRouteResolver);保存端必填。
|
||
in_transit_warehouse_code: Mapped[str] = mapped_column(String(64), nullable=False, server_default='', default='')
|
||
enabled: Mapped[bool] = mapped_column(default=True, server_default=text('true'), nullable=False)
|
||
version: Mapped[int] = mapped_column(Integer, default=1, server_default=text('1'), nullable=False)
|
||
created_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
updated_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
|
||
|
||
source: Mapped[FreightRpaSource] = relationship(back_populates='external_mappings')
|