feat(shipping-notice): 收敛复核阶段同源投影与 OT 后履约推进
统一 Case/Progress 阶段 label_key、修复 Job 成功未推进 OT 的可重试失败投影与写路径,并落地 OT 后行级匹配/波次履约与版面诊断开关。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
74270adc22
commit
fd6a23f870
100 files changed
+12138
-1017
No files matched your search
+146
@@ -0,0 +1,146 @@
|
||||
"""
|
||||
migrations/versions/cr2b3c4d5e6f_add_shipping_notice_line_inventory_matches.py
|
||||
|
||||
用途:正式 OT 行级库存匹配事实表 + 行履约决定 decision_source 列(Slice 0)。
|
||||
依赖:alembic.op、shipping_notices、shipping_inventory_files、shipping_order_review_cases、
|
||||
shipping_order_review_line_decisions。
|
||||
逻辑:Revises cq1a2b3c4d5e;新建 shipping_notice_line_inventory_matches;
|
||||
为 line_decisions 增加可空 decision_source;自动履约 flag 默认关闭不依赖本表写入。
|
||||
|
||||
Revision ID: cr2b3c4d5e6f
|
||||
Revises: cq1a2b3c4d5e
|
||||
Create Date: 2026-08-07 08:50:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'cr2b3c4d5e6f'
|
||||
down_revision = 'cq1a2b3c4d5e'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""创建 OT 行库存匹配事实表,并扩展行决定来源字段。"""
|
||||
# [STEP 1] OT 行级匹配事实(历史快照不可覆盖)
|
||||
op.create_table(
|
||||
'shipping_notice_line_inventory_matches',
|
||||
sa.Column('id', sa.Integer(), primary_key=True),
|
||||
sa.Column(
|
||||
'task_id',
|
||||
sa.Integer(),
|
||||
sa.ForeignKey('shipping_notices.id', ondelete='CASCADE'),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column(
|
||||
'review_case_id',
|
||||
sa.Integer(),
|
||||
sa.ForeignKey('shipping_order_review_cases.id', ondelete='SET NULL'),
|
||||
nullable=True,
|
||||
),
|
||||
sa.Column('line_key', sa.String(length=64), nullable=False),
|
||||
sa.Column('line_index', sa.Integer(), nullable=True),
|
||||
sa.Column(
|
||||
'inventory_snapshot_id',
|
||||
sa.Integer(),
|
||||
sa.ForeignKey('shipping_inventory_files.id', ondelete='SET NULL'),
|
||||
nullable=True,
|
||||
),
|
||||
sa.Column('match_result', sa.String(length=32), nullable=False),
|
||||
sa.Column('inventory_status', sa.String(length=32), nullable=False),
|
||||
sa.Column('required_qty', sa.String(length=32), nullable=True),
|
||||
sa.Column('available_qty', sa.String(length=32), nullable=True),
|
||||
sa.Column('shortage_qty', sa.String(length=32), nullable=True),
|
||||
sa.Column('reservation_id', sa.String(length=128), nullable=True),
|
||||
sa.Column('part_number', sa.String(length=128), nullable=True),
|
||||
sa.Column('unit', sa.String(length=32), nullable=True),
|
||||
sa.Column('matching_rule_version', sa.String(length=64), nullable=True),
|
||||
sa.Column('inventory_source_code', sa.String(length=64), nullable=True),
|
||||
sa.Column('is_current', sa.Boolean(), nullable=False, server_default=sa.text('true')),
|
||||
sa.Column('matched_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('detail_json', sa.JSON(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.UniqueConstraint(
|
||||
'task_id',
|
||||
'line_key',
|
||||
'inventory_snapshot_id',
|
||||
name='uq_shipping_notice_line_inv_match_task_line_snap',
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"match_result IN ("
|
||||
"'matched', 'not_found', 'result_missing', 'preparing', "
|
||||
"'source_conflict', 'invalid_key', 'mapping_failed', 'unavailable'"
|
||||
")",
|
||||
name='ck_shipping_notice_line_inv_match_result',
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"inventory_status IN ("
|
||||
"'ready', 'preparing', 'mapping_failed', 'source_conflict', "
|
||||
"'unavailable', 'invalid_key'"
|
||||
")",
|
||||
name='ck_shipping_notice_line_inv_match_status',
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
'ix_shipping_notice_line_inv_match_task',
|
||||
'shipping_notice_line_inventory_matches',
|
||||
['task_id'],
|
||||
)
|
||||
op.create_index(
|
||||
'ix_shipping_notice_line_inv_match_case',
|
||||
'shipping_notice_line_inventory_matches',
|
||||
['review_case_id'],
|
||||
)
|
||||
op.create_index(
|
||||
'ix_shipping_notice_line_inv_match_snapshot',
|
||||
'shipping_notice_line_inventory_matches',
|
||||
['inventory_snapshot_id'],
|
||||
)
|
||||
# [STEP 2] PostgreSQL:当前事实唯一;其他方言由应用层保证 is_current
|
||||
bind = op.get_bind()
|
||||
if bind.dialect.name == 'postgresql':
|
||||
op.execute(
|
||||
sa.text(
|
||||
'CREATE UNIQUE INDEX uq_shipping_notice_line_inv_match_current '
|
||||
'ON shipping_notice_line_inventory_matches (task_id, line_key) '
|
||||
'WHERE is_current IS TRUE'
|
||||
)
|
||||
)
|
||||
|
||||
# [STEP 3] 行决定来源:system | user(历史可空)
|
||||
with op.batch_alter_table('shipping_order_review_line_decisions', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('decision_source', sa.String(length=16), nullable=True))
|
||||
batch_op.create_check_constraint(
|
||||
'ck_shipping_review_line_decisions_source',
|
||||
"decision_source IS NULL OR decision_source IN ('system', 'user', 'unknown')",
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""回滚事实表与 decision_source。"""
|
||||
with op.batch_alter_table('shipping_order_review_line_decisions', schema=None) as batch_op:
|
||||
batch_op.drop_constraint('ck_shipping_review_line_decisions_source', type_='check')
|
||||
batch_op.drop_column('decision_source')
|
||||
|
||||
bind = op.get_bind()
|
||||
if bind.dialect.name == 'postgresql':
|
||||
op.execute(sa.text('DROP INDEX IF EXISTS uq_shipping_notice_line_inv_match_current'))
|
||||
|
||||
op.drop_index(
|
||||
'ix_shipping_notice_line_inv_match_snapshot',
|
||||
table_name='shipping_notice_line_inventory_matches',
|
||||
)
|
||||
op.drop_index(
|
||||
'ix_shipping_notice_line_inv_match_case',
|
||||
table_name='shipping_notice_line_inventory_matches',
|
||||
)
|
||||
op.drop_index(
|
||||
'ix_shipping_notice_line_inv_match_task',
|
||||
table_name='shipping_notice_line_inventory_matches',
|
||||
)
|
||||
op.drop_table('shipping_notice_line_inventory_matches')
|
||||
@@ -0,0 +1,96 @@
|
||||
"""
|
||||
migrations/versions/cs3c4d5e6f7a_add_shipping_review_wave_idempotency.py
|
||||
|
||||
用途:post-OT wave 独立幂等表(BE-3)。
|
||||
依赖:alembic.op、shipping_order_review_cases、shipping_notices。
|
||||
逻辑:Revises cr2b3c4d5e6f;idempotency_key 唯一;同 key 参数冲突由服务层 409。
|
||||
|
||||
Revision ID: cs3c4d5e6f7a
|
||||
Revises: cr2b3c4d5e6f
|
||||
Create Date: 2026-08-07 09:40:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'cs3c4d5e6f7a'
|
||||
down_revision = 'cr2b3c4d5e6f'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""创建 shipping_order_review_wave_idempotency。"""
|
||||
op.create_table(
|
||||
'shipping_order_review_wave_idempotency',
|
||||
sa.Column('id', sa.Integer(), primary_key=True),
|
||||
sa.Column('idempotency_key', sa.String(length=191), nullable=False),
|
||||
sa.Column(
|
||||
'review_case_id',
|
||||
sa.Integer(),
|
||||
sa.ForeignKey('shipping_order_review_cases.id', ondelete='CASCADE'),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column(
|
||||
'task_id',
|
||||
sa.Integer(),
|
||||
sa.ForeignKey('shipping_notices.id', ondelete='CASCADE'),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column('task_no', sa.String(length=64), nullable=False),
|
||||
sa.Column('wave_no', sa.Integer(), nullable=True),
|
||||
sa.Column('decision_source', sa.String(length=16), nullable=False),
|
||||
sa.Column('line_keys_json', sa.JSON(), nullable=False),
|
||||
sa.Column('line_keys_hash', sa.String(length=64), nullable=False),
|
||||
sa.Column('inventory_snapshot_id', sa.Integer(), nullable=True),
|
||||
sa.Column(
|
||||
'inventory_snapshot_fingerprint',
|
||||
sa.String(length=256),
|
||||
nullable=False,
|
||||
server_default='',
|
||||
),
|
||||
sa.Column('status', sa.String(length=32), nullable=False, server_default='succeeded'),
|
||||
sa.Column('result_json', sa.JSON(), nullable=False),
|
||||
sa.Column('progress_status', sa.String(length=32), nullable=False, server_default='pending'),
|
||||
sa.Column('progress_job_id', sa.Integer(), nullable=True),
|
||||
sa.Column('progress_error', sa.Text(), nullable=True),
|
||||
sa.Column('progress_idempotency_key', sa.String(length=191), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.UniqueConstraint('idempotency_key', name='uq_shipping_review_wave_idempotency_key'),
|
||||
)
|
||||
op.create_index(
|
||||
'ix_shipping_review_wave_idempotency_case',
|
||||
'shipping_order_review_wave_idempotency',
|
||||
['review_case_id'],
|
||||
)
|
||||
op.create_index(
|
||||
'ix_shipping_review_wave_idempotency_task',
|
||||
'shipping_order_review_wave_idempotency',
|
||||
['task_id'],
|
||||
)
|
||||
op.create_index(
|
||||
'ix_shipping_review_wave_idempotency_wave',
|
||||
'shipping_order_review_wave_idempotency',
|
||||
['review_case_id', 'wave_no'],
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""删除幂等表。"""
|
||||
op.drop_index(
|
||||
'ix_shipping_review_wave_idempotency_wave',
|
||||
table_name='shipping_order_review_wave_idempotency',
|
||||
)
|
||||
op.drop_index(
|
||||
'ix_shipping_review_wave_idempotency_task',
|
||||
table_name='shipping_order_review_wave_idempotency',
|
||||
)
|
||||
op.drop_index(
|
||||
'ix_shipping_review_wave_idempotency_case',
|
||||
table_name='shipping_order_review_wave_idempotency',
|
||||
)
|
||||
op.drop_table('shipping_order_review_wave_idempotency')
|
||||
@@ -0,0 +1,152 @@
|
||||
"""
|
||||
migrations/versions/ct4d5e6f7a8b_post_ot_auto_fulfill_p1.py
|
||||
|
||||
用途:BE-7 匹配事实失败列 + BE-4 补偿排程种子。
|
||||
依赖:alembic.op、shipping_notice_line_inventory_matches、schedule_tasks。
|
||||
逻辑:Revises cs3c4d5e6f7a;为匹配事实增加 auto_fulfill_failed 列;
|
||||
种子 SCH-POST-OT-AUTO-FULFILL-COMP 每 15 分钟。
|
||||
|
||||
Revision ID: ct4d5e6f7a8b
|
||||
Revises: cs3c4d5e6f7a
|
||||
Create Date: 2026-08-07 10:20:00.000000
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'ct4d5e6f7a8b'
|
||||
down_revision = 'cs3c4d5e6f7a'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
_SCHEDULE_NO = 'SCH-POST-OT-AUTO-FULFILL-COMP'
|
||||
_CRON_EXPR = '*/15 * * * *'
|
||||
_TZ_NAME = 'Asia/Shanghai'
|
||||
|
||||
|
||||
def _compute_next_run_at_utc_naive() -> datetime:
|
||||
"""按 */15 与 Asia/Shanghai 计算下次执行(UTC naive)。"""
|
||||
try:
|
||||
from croniter import croniter
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
tz = ZoneInfo(_TZ_NAME)
|
||||
base = datetime.now(tz)
|
||||
next_local = croniter(_CRON_EXPR, base).get_next(datetime)
|
||||
if next_local.tzinfo is None:
|
||||
next_local = next_local.replace(tzinfo=tz)
|
||||
return next_local.astimezone(timezone.utc).replace(tzinfo=None)
|
||||
except Exception:
|
||||
tz = timezone(timedelta(hours=8))
|
||||
now_local = datetime.now(tz)
|
||||
minute = (now_local.minute // 15 + 1) * 15
|
||||
if minute >= 60:
|
||||
next_local = now_local.replace(minute=0, second=0, microsecond=0) + timedelta(hours=1)
|
||||
else:
|
||||
next_local = now_local.replace(minute=minute, second=0, microsecond=0)
|
||||
return next_local.astimezone(timezone.utc).replace(tzinfo=None)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""增加失败列并种子补偿排程。"""
|
||||
op.add_column(
|
||||
'shipping_notice_line_inventory_matches',
|
||||
sa.Column(
|
||||
'auto_fulfill_failed',
|
||||
sa.Boolean(),
|
||||
nullable=False,
|
||||
server_default=sa.text('false'),
|
||||
),
|
||||
)
|
||||
op.add_column(
|
||||
'shipping_notice_line_inventory_matches',
|
||||
sa.Column('auto_fulfill_error_code', sa.String(length=128), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
'shipping_notice_line_inventory_matches',
|
||||
sa.Column('auto_fulfill_error_message', sa.Text(), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
'shipping_notice_line_inventory_matches',
|
||||
sa.Column('auto_fulfill_failed_at', sa.DateTime(), nullable=True),
|
||||
)
|
||||
op.create_index(
|
||||
'ix_shipping_notice_line_inv_match_auto_fail',
|
||||
'shipping_notice_line_inventory_matches',
|
||||
['auto_fulfill_failed'],
|
||||
)
|
||||
|
||||
conn = op.get_bind()
|
||||
exists = conn.execute(
|
||||
sa.text(
|
||||
"SELECT 1 FROM schedule_tasks "
|
||||
"WHERE schedule_no = :sno OR platform_api_path = 'shipping-post-ot-auto-fulfill-compensation' "
|
||||
"LIMIT 1"
|
||||
),
|
||||
{'sno': _SCHEDULE_NO},
|
||||
).first()
|
||||
if exists:
|
||||
return
|
||||
|
||||
owner = conn.execute(
|
||||
sa.text("SELECT id FROM users WHERE role = 'admin' ORDER BY id ASC LIMIT 1")
|
||||
).first()
|
||||
if owner is None:
|
||||
owner = conn.execute(sa.text('SELECT id FROM users ORDER BY id ASC LIMIT 1')).first()
|
||||
if owner is None:
|
||||
return
|
||||
|
||||
now = datetime.utcnow()
|
||||
next_run = _compute_next_run_at_utc_naive()
|
||||
conn.execute(
|
||||
sa.text(
|
||||
"""
|
||||
INSERT INTO schedule_tasks (
|
||||
schedule_no, name, schedule_type, owner_user_id,
|
||||
cron_expr, schedule_timezone, wait_seconds,
|
||||
platform_api_path, job_params,
|
||||
kb_sync_enabled, notify_enabled,
|
||||
enabled, next_run_at, last_run_at,
|
||||
created_by, updated_by, created_at, updated_at
|
||||
) VALUES (
|
||||
:schedule_no, :name, 'platform_job', :owner_user_id,
|
||||
:cron_expr, 'Asia/Shanghai', 0,
|
||||
'shipping-post-ot-auto-fulfill-compensation', :job_params,
|
||||
false, false,
|
||||
true, :next_run_at, NULL,
|
||||
:owner_user_id, :owner_user_id, :now, :now
|
||||
)
|
||||
"""
|
||||
),
|
||||
{
|
||||
'schedule_no': _SCHEDULE_NO,
|
||||
'name': 'OT 后行级自动履约补偿',
|
||||
'owner_user_id': int(owner[0]),
|
||||
'cron_expr': _CRON_EXPR,
|
||||
'job_params': '{"limit": 50}',
|
||||
'next_run_at': next_run,
|
||||
'now': now,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""删除补偿排程与失败列。"""
|
||||
conn = op.get_bind()
|
||||
conn.execute(
|
||||
sa.text('DELETE FROM schedule_tasks WHERE schedule_no = :sno'),
|
||||
{'sno': _SCHEDULE_NO},
|
||||
)
|
||||
op.drop_index(
|
||||
'ix_shipping_notice_line_inv_match_auto_fail',
|
||||
table_name='shipping_notice_line_inventory_matches',
|
||||
)
|
||||
op.drop_column('shipping_notice_line_inventory_matches', 'auto_fulfill_failed_at')
|
||||
op.drop_column('shipping_notice_line_inventory_matches', 'auto_fulfill_error_message')
|
||||
op.drop_column('shipping_notice_line_inventory_matches', 'auto_fulfill_error_code')
|
||||
op.drop_column('shipping_notice_line_inventory_matches', 'auto_fulfill_failed')
|
||||
Reference in new issue
Block a user