Files
syops/backend/app/models/shipping_customer.py
T
hugo d396371020 feat(shipping_notice): 支持客户 ODM 白名单与整单 ODM 选择收敛导出
- 客户主数据新增 odm_ids 白名单字段(含 Alembic 迁移),表单与管理端支持维护
- 导出库存命中行按客户白名单与整单 ODM 裁决收敛,新增 notice_odm_choice_service
- 工作台新增 odm-options / odm-choice 接口,投影行级候选并持久化用户选择
- 修复复核工作台跨 Run 切换时接收解析上下文残留
- 同步四语言 i18n、测试用例、funclist 与 shipping-notice 技术文档
2026-09-16 19:50:12 +08:00

264 lines
13 KiB
Python

"""
backend/app/models/shipping_customer.py
用途:货代标准客户、客户识别证据与业务编号流水模型。
依赖:models.base.Base、users、shipping_notices。
逻辑:客户主数据提供稳定身份;发货通知自持履约事实;流水表供编号服务原子递增。
"""
from __future__ import annotations
from datetime import date, datetime
from typing import List, Optional
from sqlalchemy import Boolean, CheckConstraint, Date, ForeignKey, Index, Integer, JSON, String, Text, UniqueConstraint, text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.models.base import Base
class ShippingCustomer(Base):
"""货代标准客户主数据;简码由管理员维护并用于业务编号前缀。"""
__tablename__ = 'shipping_customers'
__table_args__ = (
CheckConstraint(
"length(short_code) BETWEEN 2 AND 6 "
"AND short_code = upper(short_code) "
"AND short_code NOT GLOB '*[^A-Z]*'",
name='ck_shipping_customers_short_code_shape',
).ddl_if(dialect='sqlite'),
CheckConstraint(
"short_code ~ '^[A-Z]{2,6}$'",
name='ck_shipping_customers_short_code_shape',
).ddl_if(dialect='postgresql'),
Index('ix_shipping_customers_status_name', 'status', 'standard_name'),
)
id: Mapped[int] = mapped_column(primary_key=True)
standard_name: Mapped[str] = mapped_column(String(200), nullable=False, index=True)
short_code: Mapped[str] = mapped_column(String(6), nullable=False, unique=True)
status: Mapped[str] = mapped_column(String(32), nullable=False, default='active', server_default='active')
merged_into_customer_id: Mapped[Optional[int]] = mapped_column(
ForeignKey('shipping_customers.id'),
nullable=True,
index=True,
)
remark: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
# 客户可用的 ODM ID 清单;为空表示不限定。生成发货 Excel 时用于收敛库存命中行。
odm_ids: Mapped[Optional[list]] = mapped_column(JSON, nullable=True)
created_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
updated_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
identifiers: Mapped[List['ShippingCustomerIdentifier']] = relationship(
'ShippingCustomerIdentifier',
back_populates='customer',
cascade='all, delete-orphan',
)
merged_into_customer: Mapped[Optional['ShippingCustomer']] = relationship(
'ShippingCustomer',
remote_side='ShippingCustomer.id',
back_populates='merged_source_customers',
)
external_refs: Mapped[List['ShippingCustomerExternalRef']] = relationship(
'ShippingCustomerExternalRef', back_populates='customer', cascade='all, delete-orphan',
)
sites: Mapped[List['ShippingCustomerSite']] = relationship(
'ShippingCustomerSite', back_populates='customer', cascade='all, delete-orphan',
)
merged_source_customers: Mapped[List['ShippingCustomer']] = relationship(
'ShippingCustomer',
back_populates='merged_into_customer',
)
class ShippingCustomerIdentifier(Base):
"""标准客户的可审核识别证据;同一别名可属于多个客户以表达歧义。"""
__tablename__ = 'shipping_customer_identifiers'
__table_args__ = (
UniqueConstraint(
'customer_id',
'identifier_type',
'normalized_value',
name='uq_shipping_customer_identifiers_customer_type_value',
),
Index(
'ix_shipping_customer_identifiers_lookup',
'identifier_type',
'normalized_value',
'status',
),
)
id: Mapped[int] = mapped_column(primary_key=True)
customer_id: Mapped[int] = mapped_column(ForeignKey('shipping_customers.id'), nullable=False, index=True)
identifier_type: Mapped[str] = mapped_column(String(32), nullable=False)
raw_value: Mapped[str] = mapped_column(String(500), nullable=False)
normalized_value: Mapped[str] = mapped_column(String(500), nullable=False)
auto_match_allowed: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=text('false'))
status: Mapped[str] = mapped_column(String(32), nullable=False, default='pending', server_default='pending')
source: Mapped[str] = mapped_column(String(32), nullable=False, default='manual', server_default='manual')
remark: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
confirmed_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
confirmed_at: Mapped[Optional[datetime]] = mapped_column(nullable=True)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
customer: Mapped['ShippingCustomer'] = relationship('ShippingCustomer', back_populates='identifiers')
class ShippingCustomerExternalRef(Base):
"""客户在期初文件或外部系统中的稳定来源键,用于重复导入幂等。"""
__tablename__ = 'shipping_customer_external_refs'
__table_args__ = (
UniqueConstraint(
'source_system',
'legacy_customer_key',
name='uq_shipping_customer_external_refs_source_key',
),
)
id: Mapped[int] = mapped_column(primary_key=True)
customer_id: Mapped[int] = mapped_column(ForeignKey('shipping_customers.id'), nullable=False, index=True)
source_system: Mapped[str] = mapped_column(String(64), nullable=False)
legacy_customer_key: Mapped[str] = mapped_column(String(128), nullable=False)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
customer: Mapped['ShippingCustomer'] = relationship('ShippingCustomer', back_populates='external_refs')
class ShippingCustomerSite(Base):
"""标准客户下的送货/收货站点;Ship-to 匹配指向站点而不是复制一条账款客户。"""
__tablename__ = 'shipping_customer_sites'
__table_args__ = (
UniqueConstraint('customer_id', 'site_code', name='uq_shipping_customer_sites_customer_code'),
Index('ix_shipping_customer_sites_status_name', 'status', 'site_name'),
)
id: Mapped[int] = mapped_column(primary_key=True)
customer_id: Mapped[int] = mapped_column(ForeignKey('shipping_customers.id'), nullable=False, index=True)
site_name: Mapped[str] = mapped_column(String(200), nullable=False)
site_code: Mapped[str] = mapped_column(String(64), nullable=False)
address: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
city: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
country: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
status: Mapped[str] = mapped_column(String(32), nullable=False, default='active', server_default='active')
remark: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
created_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
updated_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
customer: Mapped['ShippingCustomer'] = relationship('ShippingCustomer', back_populates='sites')
identifiers: Mapped[List['ShippingCustomerSiteIdentifier']] = relationship(
'ShippingCustomerSiteIdentifier', back_populates='site', cascade='all, delete-orphan',
)
class ShippingCustomerSiteIdentifier(Base):
"""送货站点的名称、外部站点编码或地址别名,用于 Ship-to 确定性匹配。"""
__tablename__ = 'shipping_customer_site_identifiers'
__table_args__ = (
UniqueConstraint(
'site_id', 'identifier_type', 'source_system', 'normalized_value',
name='uq_shipping_customer_site_identifiers_value',
),
Index(
'ix_shipping_customer_site_identifiers_lookup',
'identifier_type', 'source_system', 'normalized_value', 'status',
),
)
id: Mapped[int] = mapped_column(primary_key=True)
site_id: Mapped[int] = mapped_column(ForeignKey('shipping_customer_sites.id'), nullable=False, index=True)
identifier_type: Mapped[str] = mapped_column(String(32), nullable=False)
source_system: Mapped[str] = mapped_column(String(64), nullable=False, default='', server_default='')
raw_value: Mapped[str] = mapped_column(String(500), nullable=False)
normalized_value: Mapped[str] = mapped_column(String(500), nullable=False)
status: Mapped[str] = mapped_column(String(32), nullable=False, default='approved', server_default='approved')
remark: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
site: Mapped['ShippingCustomerSite'] = relationship('ShippingCustomerSite', back_populates='identifiers')
class ShippingBusinessSequence(Base):
"""按业务类型、客户和 Asia/Shanghai 业务日期保存当前流水值。"""
__tablename__ = 'shipping_business_sequences'
__table_args__ = (
CheckConstraint(
'current_value BETWEEN 0 AND 99',
name='ck_shipping_business_sequences_current_value',
),
UniqueConstraint(
'business_type',
'customer_id',
'business_date',
name='uq_shipping_business_sequences_type_customer_date',
),
)
id: Mapped[int] = mapped_column(primary_key=True)
business_type: Mapped[str] = mapped_column(
String(32),
nullable=False,
default='shipping_notice',
server_default='shipping_notice',
)
customer_id: Mapped[int] = mapped_column(ForeignKey('shipping_customers.id'), nullable=False, index=True)
business_date: Mapped[date] = mapped_column(Date, nullable=False, index=True)
current_value: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default='0')
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
class ShippingCustomerLifecycleEvent(Base):
"""客户生命周期审计;只保存客户快照,不能反向阻断受控删除。"""
__tablename__ = 'shipping_customer_lifecycle_events'
__table_args__ = (
Index('ix_shipping_customer_lifecycle_events_customer_created', 'customer_id_snapshot', 'created_at'),
Index('ix_shipping_customer_lifecycle_events_event_created', 'event_type', 'created_at'),
)
id: Mapped[int] = mapped_column(primary_key=True)
event_type: Mapped[str] = mapped_column(String(32), nullable=False)
customer_id_snapshot: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
customer_name_snapshot: Mapped[Optional[str]] = mapped_column(String(200), nullable=True)
customer_short_code_snapshot: Mapped[Optional[str]] = mapped_column(String(6), nullable=True)
actor_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
reason: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
detail_json: Mapped[dict] = mapped_column(JSON, nullable=False, default=dict)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
class ShippingCustomerMerge(Base):
"""客户身份归并审计;仅存快照,不给历史审计链增加客户外键依赖。"""
__tablename__ = 'shipping_customer_merges'
__table_args__ = (
UniqueConstraint('source_customer_id_snapshot', name='uq_shipping_customer_merges_source_snapshot'),
Index('ix_shipping_customer_merges_target_created', 'target_customer_id_snapshot', 'created_at'),
)
id: Mapped[int] = mapped_column(primary_key=True)
source_customer_id_snapshot: Mapped[int] = mapped_column(Integer, nullable=False)
source_name_snapshot: Mapped[str] = mapped_column(String(200), nullable=False)
source_short_code_snapshot: Mapped[str] = mapped_column(String(6), nullable=False)
target_customer_id_snapshot: Mapped[int] = mapped_column(Integer, nullable=False)
target_name_snapshot: Mapped[str] = mapped_column(String(200), nullable=False)
target_short_code_snapshot: Mapped[str] = mapped_column(String(6), nullable=False)
actor_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
reason: Mapped[str] = mapped_column(Text, nullable=False)
detail_json: Mapped[dict] = mapped_column(JSON, nullable=False, default=dict)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)