feat(shipping-notice): 新增货代客户管理功能,支持客户导入、识别证据管理与客户确认

This commit is contained in:
hugo committed 2026-07-16 08:16:19 +08:00
1 parent b595e157e9
commit 4fbaed2b4c
176 files changed
+14011 -994

No files matched your search

+176
View File
@@ -0,0 +1,176 @@
"""
backend/app/models/shipping_customer.py
用途:货代标准客户、客户识别证据、业务单聚合与业务编号流水模型。
依赖:models.base.Base、users、shipping_tasks。
逻辑:客户主数据提供稳定身份;ShippingCase 聚合任务修订链;流水表供后续编号服务原子递增。
"""
from __future__ import annotations
from datetime import date, datetime
from typing import List, Optional
from sqlalchemy import Boolean, CheckConstraint, Date, ForeignKey, Index, Integer, 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')
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)
identifiers: Mapped[List['ShippingCustomerIdentifier']] = relationship(
'ShippingCustomerIdentifier',
back_populates='customer',
cascade='all, delete-orphan',
)
cases: Mapped[List['ShippingCase']] = relationship('ShippingCase', back_populates='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')
class ShippingCase(Base):
"""一票发货业务聚合;多个线性任务修订版本共享同一业务单。"""
__tablename__ = 'shipping_cases'
__table_args__ = (
Index('ix_shipping_cases_customer_created', 'customer_id', 'created_at'),
Index('ix_shipping_cases_status_created', 'status', 'created_at'),
UniqueConstraint('replaces_case_id', name='uq_shipping_cases_replaces_case_id'),
)
id: Mapped[int] = mapped_column(primary_key=True)
case_no: Mapped[str] = mapped_column(String(64), nullable=False, unique=True)
customer_id: Mapped[Optional[int]] = mapped_column(ForeignKey('shipping_customers.id'), nullable=True, index=True)
business_no: Mapped[Optional[str]] = mapped_column(String(32), nullable=True, unique=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)
business_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
sequence_value: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
numbering_rule_version: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
numbering_status: Mapped[str] = mapped_column(String(32), nullable=False, default='pending', server_default='pending')
status: Mapped[str] = mapped_column(String(32), nullable=False, default='open', server_default='open')
replaces_case_id: Mapped[Optional[int]] = mapped_column(ForeignKey('shipping_cases.id'), nullable=True)
created_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[Optional['ShippingCustomer']] = relationship('ShippingCustomer', back_populates='cases')
tasks: Mapped[List['ShippingTask']] = relationship(
'ShippingTask',
back_populates='shipping_case',
lazy='selectin',
)
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)