feat(shipping-notice): 添加识别定义和规则集管理功能,支持客户和站点的可配置识别规则
This commit is contained in:
1 parent
88408490d7
commit
34d489a2d1
167 files changed
+10365
-7978
No files matched your search
@@ -1,16 +1,15 @@
|
||||
"""
|
||||
backend/app/models/shipping_task_line.py
|
||||
|
||||
用途:规范订单行(ShippingTaskLine)与字段冲突审计(ShippingTaskFieldConflict)模型。
|
||||
用途:订单字段冲突审计(ShippingTaskFieldConflict)模型。
|
||||
依赖:models.base.Base、shipping_tasks、users。
|
||||
逻辑:Task.lines_json 在迁移窗口内仍为兼容快照;规范表由阶段四双写填充,阶段六再切读。
|
||||
逻辑:冲突按动态订单 Extraction 的行 position 定位,不保留固定订单行投影。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from typing import TYPE_CHECKING, List, Optional
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import (
|
||||
CheckConstraint,
|
||||
@@ -18,95 +17,14 @@ from sqlalchemy import (
|
||||
Index,
|
||||
Integer,
|
||||
JSON,
|
||||
Numeric,
|
||||
String,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
text,
|
||||
)
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.shipping import ShippingTask
|
||||
|
||||
|
||||
class ShippingTaskLine(Base):
|
||||
"""
|
||||
任务当前订单行投影;稳定主键 + (task_id, position) 唯一。
|
||||
|
||||
核心逻辑:Candidate 是不可变抽取审计,本表是任务可变真相源;未知历史字段进 extra_json。
|
||||
"""
|
||||
|
||||
__tablename__ = 'shipping_task_lines'
|
||||
__table_args__ = (
|
||||
UniqueConstraint('task_id', 'position', name='uq_shipping_task_lines_task_position'),
|
||||
CheckConstraint('qty > 0', name='ck_shipping_task_lines_qty_positive'),
|
||||
CheckConstraint(
|
||||
"part_number NOT LIKE '%/%'",
|
||||
name='ck_shipping_task_lines_part_number_atomic',
|
||||
),
|
||||
CheckConstraint(
|
||||
"customer_part_number IS NULL OR customer_part_number NOT LIKE '%/%'",
|
||||
name='ck_shipping_task_lines_customer_part_number_atomic',
|
||||
),
|
||||
CheckConstraint(
|
||||
"aws_part_number IS NULL OR aws_part_number NOT LIKE '%/%'",
|
||||
name='ck_shipping_task_lines_aws_part_number_atomic',
|
||||
),
|
||||
Index('ix_shipping_task_lines_task_id', 'task_id'),
|
||||
Index('ix_shipping_task_lines_reservation_id', 'reservation_id'),
|
||||
Index('ix_shipping_task_lines_part_number', 'part_number'),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
task_id: Mapped[int] = mapped_column(
|
||||
ForeignKey('shipping_tasks.id', ondelete='CASCADE'),
|
||||
nullable=False,
|
||||
)
|
||||
position: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
part_number: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
qty: Mapped[Decimal] = mapped_column(Numeric(18, 6), nullable=False)
|
||||
uom: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
|
||||
reservation_id: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
||||
inventory_source_hint: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
||||
line_no: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||||
required_date: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
|
||||
customer_part_number: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
||||
aws_part_number: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
||||
po_number: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
||||
allocation_id: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
||||
unit_price: Mapped[Optional[Decimal]] = mapped_column(Numeric(18, 6), nullable=True)
|
||||
amount: Mapped[Optional[Decimal]] = mapped_column(Numeric(18, 6), nullable=True)
|
||||
currency: Mapped[Optional[str]] = mapped_column(String(16), nullable=True)
|
||||
remark: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
extra_json: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
|
||||
field_sources_json: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
|
||||
evidence_json: Mapped[Optional[list]] = mapped_column(JSON, nullable=True)
|
||||
schema_version: Mapped[str] = mapped_column(
|
||||
String(64),
|
||||
nullable=False,
|
||||
default='shipping-task-line-v2',
|
||||
server_default=text("'shipping-task-line-v2'"),
|
||||
)
|
||||
line_version: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
nullable=False,
|
||||
default=1,
|
||||
server_default=text('1'),
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
|
||||
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
task: Mapped[ShippingTask] = relationship('ShippingTask', back_populates='order_lines')
|
||||
field_conflicts: Mapped[List[ShippingTaskFieldConflict]] = relationship(
|
||||
'ShippingTaskFieldConflict',
|
||||
back_populates='line',
|
||||
cascade='save-update, merge',
|
||||
)
|
||||
|
||||
|
||||
class ShippingTaskFieldConflict(Base):
|
||||
"""
|
||||
订单字段级冲突审计;未解决冲突将在后续阶段阻断下游流程。
|
||||
@@ -121,25 +39,13 @@ class ShippingTaskFieldConflict(Base):
|
||||
name='ck_shipping_task_field_conflicts_status',
|
||||
),
|
||||
Index('ix_shipping_task_field_conflicts_task_status', 'task_id', 'status'),
|
||||
Index('ix_shipping_task_field_conflicts_line_id', 'line_id'),
|
||||
# NULL 在唯一索引中互不冲突,因此拆成「有行」与「无行」两条部分唯一索引。
|
||||
Index(
|
||||
'uq_shipping_task_field_conflicts_open_line_field',
|
||||
'task_id',
|
||||
'line_id',
|
||||
'field_name',
|
||||
unique=True,
|
||||
postgresql_where=text("status = 'open' AND line_id IS NOT NULL"),
|
||||
sqlite_where=text("status = 'open' AND line_id IS NOT NULL"),
|
||||
),
|
||||
Index(
|
||||
'uq_shipping_task_field_conflicts_open_task_field',
|
||||
'task_id',
|
||||
'field_name',
|
||||
unique=True,
|
||||
# 仅任务级冲突(无行/无 position);行级冲突走 position 唯一索引。
|
||||
postgresql_where=text("status = 'open' AND line_id IS NULL AND position IS NULL"),
|
||||
sqlite_where=text("status = 'open' AND line_id IS NULL AND position IS NULL"),
|
||||
postgresql_where=text("status = 'open' AND position IS NULL"),
|
||||
sqlite_where=text("status = 'open' AND position IS NULL"),
|
||||
),
|
||||
Index(
|
||||
'uq_shipping_task_field_conflicts_open_position_field',
|
||||
@@ -158,11 +64,6 @@ class ShippingTaskFieldConflict(Base):
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
line_id: Mapped[Optional[int]] = mapped_column(
|
||||
# SET NULL:订单行双写重建主键时不得 CASCADE 抹掉冲突审计。
|
||||
ForeignKey('shipping_task_lines.id', ondelete='SET NULL'),
|
||||
nullable=True,
|
||||
)
|
||||
position: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
|
||||
field_name: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
existing_value: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
@@ -191,7 +92,3 @@ class ShippingTaskFieldConflict(Base):
|
||||
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
task: Mapped[ShippingTask] = relationship('ShippingTask', back_populates='field_conflicts')
|
||||
line: Mapped[Optional[ShippingTaskLine]] = relationship(
|
||||
'ShippingTaskLine',
|
||||
back_populates='field_conflicts',
|
||||
)
|
||||
Reference in new issue
Block a user