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

+17
View File
@@ -28,6 +28,15 @@ from app.models.shipping import (
ShippingTaskEvent,
ShippingTaskInventoryLink,
)
from app.models.shipping_customer import (
ShippingBusinessSequence,
ShippingCase,
ShippingCustomer,
ShippingCustomerExternalRef,
ShippingCustomerIdentifier,
)
from app.models.shipping_customer_import import ShippingCustomerImportBatch, ShippingCustomerImportRow
from app.models.shipping_customer_resolution import ShippingCustomerShadowResolution
from app.models.training import TrainingRecord
from app.models.user import LoginAudit, User, UserDefaultAgent, UserFavoriteAgent, UserHomepageConfig
from app.models.wecom import WecomCallbackAudit, WecomNotifyAudit
@@ -66,6 +75,14 @@ __all__ = [
'ScheduleRun',
'ShippingBatch',
'ShippingTask',
'ShippingCustomer',
'ShippingCustomerIdentifier',
'ShippingCustomerExternalRef',
'ShippingCustomerImportBatch',
'ShippingCustomerImportRow',
'ShippingCase',
'ShippingBusinessSequence',
'ShippingCustomerShadowResolution',
'ShippingTaskEvent',
'ShippingInventoryFile',
'ShippingTaskInventoryLink',
+16 -1
View File
@@ -98,6 +98,11 @@ class ShippingTask(Base):
id: Mapped[int] = mapped_column(primary_key=True)
task_no: Mapped[str] = mapped_column(String(64), unique=True, index=True)
shipping_case_id: Mapped[Optional[int]] = mapped_column(
ForeignKey('shipping_cases.id'),
index=True,
nullable=True,
)
batch_id: Mapped[Optional[int]] = mapped_column(ForeignKey('shipping_batches.id'), index=True, nullable=True)
user_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), index=True, nullable=True)
conversation_id: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
@@ -142,13 +147,18 @@ class ShippingTask(Base):
raw_content: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
customer_name: Mapped[Optional[str]] = mapped_column(String(200), nullable=True)
customer_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
customer_resolution_status: Mapped[Optional[str]] = mapped_column(String(32), nullable=True, index=True)
customer_resolution_method: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
customer_identity_fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
customer_confirmed_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
customer_confirmed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
ship_to: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
destination_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
destination_city: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
destination_country: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
warehouse_hint: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
inventory_source_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
inventory_source_type: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
inventory_connector_type: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
inventory_site_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
inventory_warehouse_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
inventory_source_status: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
@@ -197,6 +207,11 @@ class ShippingTask(Base):
back_populates='tasks',
foreign_keys=[batch_id],
)
shipping_case: Mapped[Optional['ShippingCase']] = relationship(
'ShippingCase',
back_populates='tasks',
lazy='joined',
)
events: Mapped[List['ShippingTaskEvent']] = relationship(
'ShippingTaskEvent',
back_populates='task',
+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)
@@ -0,0 +1,86 @@
"""
backend/app/models/shipping_customer_import.py
用途:保存货代期初客户导入批次、原始文件与逐行预检/发布结果。
依赖:models.base.Base、users、shipping_customers。
逻辑:上传仅写暂存批次与行;管理员解决冲突后由发布服务单事务写入正式客户主数据。
"""
from __future__ import annotations
from datetime import datetime
from typing import List, Optional
from sqlalchemy import ForeignKey, Index, Integer, JSON, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.models.base import Base
class ShippingCustomerImportBatch(Base):
"""期初客户导入批次,保存文件哈希、原始内容、统计和发布审计。"""
__tablename__ = 'shipping_customer_import_batches'
__table_args__ = (
UniqueConstraint('file_hash', 'source_system', name='uq_shipping_customer_import_batches_hash_source'),
Index('ix_shipping_customer_import_batches_status_created', 'status', 'created_at'),
)
id: Mapped[int] = mapped_column(primary_key=True)
batch_no: Mapped[str] = mapped_column(String(64), nullable=False, unique=True)
file_name: Mapped[str] = mapped_column(String(255), nullable=False)
file_hash: Mapped[str] = mapped_column(String(64), nullable=False)
file_size: Mapped[int] = mapped_column(Integer, nullable=False)
source_system: Mapped[str] = mapped_column(String(64), nullable=False)
status: Mapped[str] = mapped_column(String(32), nullable=False, default='validated', server_default='validated')
total_rows: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default='0')
valid_rows: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default='0')
warning_rows: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default='0')
error_rows: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default='0')
published_rows: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default='0')
uploaded_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
published_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
published_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)
rows: Mapped[List['ShippingCustomerImportRow']] = relationship(
'ShippingCustomerImportRow',
back_populates='batch',
cascade='all, delete-orphan',
order_by='ShippingCustomerImportRow.row_order',
)
class ShippingCustomerImportRow(Base):
"""导入文件逐行暂存、校验、人工决定与正式发布结果。"""
__tablename__ = 'shipping_customer_import_rows'
__table_args__ = (
UniqueConstraint('batch_id', 'sheet_name', 'row_no', name='uq_shipping_customer_import_rows_location'),
Index('ix_shipping_customer_import_rows_batch_status', 'batch_id', 'validation_status'),
)
id: Mapped[int] = mapped_column(primary_key=True)
batch_id: Mapped[int] = mapped_column(ForeignKey('shipping_customer_import_batches.id'), nullable=False, index=True)
sheet_name: Mapped[str] = mapped_column(String(32), nullable=False)
row_no: Mapped[int] = mapped_column(Integer, nullable=False)
row_order: Mapped[int] = mapped_column(Integer, nullable=False)
row_type: Mapped[str] = mapped_column(String(32), nullable=False)
legacy_customer_key: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
raw_json: Mapped[dict] = mapped_column(JSON, nullable=False, default=dict)
normalized_json: Mapped[dict] = mapped_column(JSON, nullable=False, default=dict)
validation_status: Mapped[str] = mapped_column(String(32), nullable=False, default='valid')
errors_json: Mapped[list] = mapped_column(JSON, nullable=False, default=list)
warnings_json: Mapped[list] = mapped_column(JSON, nullable=False, default=list)
suggested_action: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
decision_action: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
target_customer_id: Mapped[Optional[int]] = mapped_column(ForeignKey('shipping_customers.id'), nullable=True)
decision_note: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
result_customer_id: Mapped[Optional[int]] = mapped_column(ForeignKey('shipping_customers.id'), nullable=True)
result_status: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
result_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
batch: Mapped['ShippingCustomerImportBatch'] = relationship('ShippingCustomerImportBatch', back_populates='rows')
@@ -0,0 +1,54 @@
"""
backend/app/models/shipping_customer_resolution.py
用途:保存订单候选的客户影子识别结果与人工标签。
依赖:ShippingNoticeCandidate、ShippingNoticeIntake、ShippingCustomer、User。
逻辑:订单候选抽取后幂等刷新本表;阶段 3 仅用于质量观测,禁止据此绑定业务单或生成编号。
"""
from __future__ import annotations
from datetime import datetime
from typing import Optional
from sqlalchemy import CheckConstraint, Float, ForeignKey, Index, JSON, String
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import Base
class ShippingCustomerShadowResolution(Base):
"""每个订单候选唯一一条影子识别快照,可追加管理员真值标签。"""
__tablename__ = 'shipping_customer_shadow_resolutions'
__table_args__ = (
CheckConstraint(
"status IN ('unique_candidate', 'ambiguous', 'unmatched', 'failed')",
name='ck_shipping_customer_shadow_status',
),
Index('ix_shipping_customer_shadow_status_created', 'status', 'created_at'),
Index('ix_shipping_customer_shadow_labeled_created', 'manual_customer_id', 'created_at'),
)
id: Mapped[int] = mapped_column(primary_key=True)
candidate_id: Mapped[int] = mapped_column(
ForeignKey('shipping_notice_candidates.id', ondelete='CASCADE'),
nullable=False,
unique=True,
)
intake_id: Mapped[int] = mapped_column(
ForeignKey('shipping_notice_intakes.id', ondelete='CASCADE'),
nullable=False,
index=True,
)
resolution_version: Mapped[str] = mapped_column(String(32), nullable=False)
status: Mapped[str] = mapped_column(String(32), nullable=False, index=True)
evidence_json: Mapped[list] = mapped_column(JSON, nullable=False, default=list)
candidate_matches_json: Mapped[list] = mapped_column(JSON, nullable=False, default=list)
top_customer_id: Mapped[Optional[int]] = mapped_column(ForeignKey('shipping_customers.id'), nullable=True)
top_score: Mapped[Optional[float]] = mapped_column(Float, nullable=True)
manual_customer_id: Mapped[Optional[int]] = mapped_column(ForeignKey('shipping_customers.id'), nullable=True)
labeled_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
labeled_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)