feat(shipping-notice): 实现滚动履约功能,优化发货通知流程与代码清理
清理废弃的ShippingCase相关代码与依赖,统一使用ShippingNotice直接关联客户数据。调整库存网关依赖规则,支持仅对库存满足或已人工放行的订单行生成发货Excel,实现滚动履约逻辑。
This commit is contained in:
1 parent
7cfd5d167e
commit
1e5aedb9ed
235 files changed
+11394
-9613
No files matched your search
@@ -0,0 +1,64 @@
|
||||
"""为 ShippingNotice 增加独立发货通知业务编号。
|
||||
|
||||
用途:支持同一 PO 多个发货通知单各自拥有 business_no。
|
||||
依赖:shipping_notices、shipping_cases、shipping_business_sequences。
|
||||
逻辑:新增可空字段并从旧 Case 回填仅能唯一映射的历史 Notice;无法唯一映射的记录保留空值等待业务迁移。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = 'hn7o8p9q0r1s'
|
||||
down_revision = 'hm6n7o8p9q0r'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""新增 Notice 编号字段并安全回填一对一历史映射。"""
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
columns = {item['name'] for item in inspector.get_columns('shipping_notices')}
|
||||
with op.batch_alter_table('shipping_notices') as batch:
|
||||
if 'business_no' not in columns:
|
||||
batch.add_column(sa.Column('business_no', sa.String(length=32), nullable=True))
|
||||
if 'business_date' not in columns:
|
||||
batch.add_column(sa.Column('business_date', sa.Date(), nullable=True))
|
||||
if 'business_sequence_value' not in columns:
|
||||
batch.add_column(sa.Column('business_sequence_value', sa.Integer(), nullable=True))
|
||||
if 'business_numbering_rule_version' not in columns:
|
||||
batch.add_column(sa.Column('business_numbering_rule_version', sa.String(length=32), nullable=True))
|
||||
indexes = {item['name'] for item in inspector.get_indexes('shipping_notices')}
|
||||
if 'ix_shipping_notices_business_no' not in indexes:
|
||||
batch.create_index('ix_shipping_notices_business_no', ['business_no'], unique=False)
|
||||
uniques = {item['name'] for item in inspector.get_unique_constraints('shipping_notices')}
|
||||
if 'uq_shipping_notices_business_no' not in uniques:
|
||||
batch.create_unique_constraint('uq_shipping_notices_business_no', ['business_no'])
|
||||
|
||||
bind = op.get_bind()
|
||||
bind.execute(sa.text("""
|
||||
UPDATE shipping_notices AS n
|
||||
SET business_no = c.business_no,
|
||||
business_date = c.business_date,
|
||||
business_sequence_value = c.sequence_value,
|
||||
business_numbering_rule_version = c.numbering_rule_version
|
||||
FROM shipping_cases AS c
|
||||
WHERE n.shipping_case_id = c.id
|
||||
AND c.business_no IS NOT NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM shipping_notices AS other
|
||||
WHERE other.business_no = c.business_no AND other.id <> n.id
|
||||
)
|
||||
"""))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""移除 Notice 独立编号字段;旧 Case 编号仍保留。"""
|
||||
with op.batch_alter_table('shipping_notices') as batch:
|
||||
batch.drop_constraint('uq_shipping_notices_business_no', type_='unique')
|
||||
batch.drop_index('ix_shipping_notices_business_no')
|
||||
batch.drop_column('business_numbering_rule_version')
|
||||
batch.drop_column('business_sequence_value')
|
||||
batch.drop_column('business_date')
|
||||
batch.drop_column('business_no')
|
||||
@@ -0,0 +1,60 @@
|
||||
"""将客户身份事实迁移到 ShippingNotice。
|
||||
|
||||
用途:支持 Notice 在不依赖 ShippingCase 的情况下完成客户确认、业务编号和履约。
|
||||
依赖:shipping_notices、shipping_customers、shipping_cases。
|
||||
逻辑:新增 Notice 客户字段并从历史 Case 回填;旧 Case 外键暂留只读兼容。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'ho8p9q0r1s2t'
|
||||
down_revision = 'hn7o8p9q0r1s'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""新增 Notice 客户字段并回填历史 Case 客户事实。"""
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
columns = {item['name'] for item in inspector.get_columns('shipping_notices')}
|
||||
with op.batch_alter_table('shipping_notices') as batch:
|
||||
if 'customer_id' not in columns:
|
||||
batch.add_column(sa.Column('customer_id', sa.Integer(), nullable=True))
|
||||
if 'customer_name_snapshot' not in columns:
|
||||
batch.add_column(sa.Column('customer_name_snapshot', sa.String(length=200), nullable=True))
|
||||
if 'customer_short_code_snapshot' not in columns:
|
||||
batch.add_column(sa.Column('customer_short_code_snapshot', sa.String(length=6), nullable=True))
|
||||
indexes = {item['name'] for item in inspector.get_indexes('shipping_notices')}
|
||||
if 'ix_shipping_notices_customer_id' not in indexes:
|
||||
batch.create_index('ix_shipping_notices_customer_id', ['customer_id'], unique=False)
|
||||
foreign_keys = {item.get('name') for item in inspector.get_foreign_keys('shipping_notices')}
|
||||
if 'fk_shipping_notices_customer_id' not in foreign_keys:
|
||||
batch.create_foreign_key(
|
||||
'fk_shipping_notices_customer_id', 'shipping_customers', ['customer_id'], ['id'],
|
||||
)
|
||||
|
||||
bind = op.get_bind()
|
||||
bind.execute(sa.text("""
|
||||
UPDATE shipping_notices AS n
|
||||
SET customer_id = c.customer_id,
|
||||
customer_name_snapshot = c.customer_name_snapshot,
|
||||
customer_short_code_snapshot = c.customer_short_code_snapshot
|
||||
FROM shipping_cases AS c
|
||||
WHERE n.shipping_case_id = c.id
|
||||
AND n.customer_id IS NULL
|
||||
AND c.customer_id IS NOT NULL
|
||||
"""))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""回滚 Notice 客户字段;历史 Case 字段不受影响。"""
|
||||
with op.batch_alter_table('shipping_notices') as batch:
|
||||
batch.drop_constraint('fk_shipping_notices_customer_id', type_='foreignkey')
|
||||
batch.drop_index('ix_shipping_notices_customer_id')
|
||||
batch.drop_column('customer_short_code_snapshot')
|
||||
batch.drop_column('customer_name_snapshot')
|
||||
batch.drop_column('customer_id')
|
||||
@@ -0,0 +1,105 @@
|
||||
"""物理移除已废弃的 ShippingCase 聚合层。
|
||||
|
||||
用途:在历史 Notice 已自持客户与 business_no 后,删除 shipping_cases 表及 Notice 外键。
|
||||
依赖:shipping_notices、shipping_customers、shipping_business_sequences。
|
||||
逻辑:先检查残留客户/编号事实,再断开 Notice 旧外键并删除历史聚合表;不影响 Review Case 或收料 RPA。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'hp9q0r1s2t3u'
|
||||
down_revision = 'ho8p9q0r1s2t'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""校验 Notice 已完成迁移后删除 Case 表和兼容字段。"""
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
if not inspector.has_table('shipping_cases'):
|
||||
return
|
||||
|
||||
# 历史库可能错误标记为已执行 hn/ho,但物理表仍缺少其应新增的列。
|
||||
# 最终删表迁移必须先自愈这些可由旧 Case 无损恢复的事实,不能引用缺失列。
|
||||
notice_columns = {item['name'] for item in inspector.get_columns('shipping_notices')}
|
||||
missing_columns = [
|
||||
('business_no', sa.String(length=32)),
|
||||
('business_date', sa.Date()),
|
||||
('business_sequence_value', sa.Integer()),
|
||||
('business_numbering_rule_version', sa.String(length=32)),
|
||||
('customer_id', sa.Integer()),
|
||||
('customer_name_snapshot', sa.String(length=200)),
|
||||
('customer_short_code_snapshot', sa.String(length=6)),
|
||||
]
|
||||
missing_notice_fields = {name for name, _ in missing_columns if name not in notice_columns}
|
||||
with op.batch_alter_table('shipping_notices') as batch:
|
||||
for name, column_type in missing_columns:
|
||||
if name in missing_notice_fields:
|
||||
batch.add_column(sa.Column(name, column_type, nullable=True))
|
||||
|
||||
# 重新读取反射信息,确保修复后的 PostgreSQL/SQLite 结构都用于后续约束和校验。
|
||||
inspector = sa.inspect(bind)
|
||||
notice_columns = {item['name'] for item in inspector.get_columns('shipping_notices')}
|
||||
case_columns = {item['name'] for item in inspector.get_columns('shipping_cases')}
|
||||
copied_fields = [
|
||||
('business_no', 'business_no'),
|
||||
('business_date', 'business_date'),
|
||||
('business_sequence_value', 'sequence_value'),
|
||||
('business_numbering_rule_version', 'numbering_rule_version'),
|
||||
('customer_id', 'customer_id'),
|
||||
('customer_name_snapshot', 'customer_name_snapshot'),
|
||||
('customer_short_code_snapshot', 'customer_short_code_snapshot'),
|
||||
]
|
||||
assignments = [
|
||||
f"{notice_field} = COALESCE(n.{notice_field}, c.{case_field})"
|
||||
for notice_field, case_field in copied_fields
|
||||
if notice_field in missing_notice_fields and case_field in case_columns
|
||||
]
|
||||
if assignments:
|
||||
bind.execute(sa.text(f"""
|
||||
UPDATE shipping_notices AS n
|
||||
SET {', '.join(assignments)}
|
||||
FROM shipping_cases AS c
|
||||
WHERE c.id = n.shipping_case_id
|
||||
"""))
|
||||
|
||||
# Notice 是最终事实;旧 Case 若仍有未迁移编号或客户,拒绝静默丢失。
|
||||
residual = bind.execute(sa.text("""
|
||||
SELECT COUNT(*)
|
||||
FROM shipping_notices AS n
|
||||
JOIN shipping_cases AS c ON c.id = n.shipping_case_id
|
||||
WHERE (c.business_no IS NOT NULL AND n.business_no IS NULL)
|
||||
OR (c.customer_id IS NOT NULL AND n.customer_id IS NULL)
|
||||
""")).scalar_one()
|
||||
if int(residual or 0):
|
||||
raise RuntimeError(
|
||||
f'shipping_case migration incomplete: {int(residual)} Notice rows still miss copied facts'
|
||||
)
|
||||
|
||||
notice_indexes = {item['name'] for item in inspector.get_indexes('shipping_notices')}
|
||||
notice_fks = inspector.get_foreign_keys('shipping_notices')
|
||||
with op.batch_alter_table('shipping_notices') as batch:
|
||||
for fk in notice_fks:
|
||||
referred = str(fk.get('referred_table') or '')
|
||||
columns = set(fk.get('constrained_columns') or [])
|
||||
if referred == 'shipping_cases' and 'shipping_case_id' in columns:
|
||||
if fk.get('name'):
|
||||
batch.drop_constraint(fk['name'], type_='foreignkey')
|
||||
if 'ix_shipping_notices_shipping_case_id' in notice_indexes:
|
||||
batch.drop_index('ix_shipping_notices_shipping_case_id')
|
||||
if 'shipping_case_id' in notice_columns:
|
||||
batch.drop_column('shipping_case_id')
|
||||
|
||||
# PostgreSQL 的 UNIQUE 约束拥有同名底层索引,不能先以 index 操作删除;
|
||||
# DROP TABLE 会一并清理 Case 表上的普通索引和约束索引。
|
||||
op.drop_table('shipping_cases')
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""不自动恢复已删除的历史 Case 数据。"""
|
||||
raise RuntimeError('ShippingCase removal migration is irreversible')
|
||||
Reference in new issue
Block a user