feat(fulfillment): 增加 Clean Cutover,历史任务隔离到人工入口

- 新增 flow_version,存量任务回填 1,Cutover 后新任务写 2
- 迁移设置 clean_flow_started_at,按订单支付/创建时间判定
- 自动选号只处理 clean 任务
- 工作台新增历史任务 Tab,历史列表不受订单起算时间限制
- 历史任务不参与自动重试和预售台账同步
- 补充流程边界测试和计划文档
This commit is contained in:
yml
2026-11-24 23:11:13 +08:00
parent 7dec3c4c48
commit aafac6e536
15 changed files with 294 additions and 7 deletions
+2 -1
View File
@@ -45,7 +45,7 @@ TESTS_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = TESTS_DIR.parent
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
HEAD_REVISION = "20261124_0131"
HEAD_REVISION = "20261124_0132"
# 迁移新增、但不声明在模型里的复合查询索引。
EXTRA_INDEXES = (
@@ -90,6 +90,7 @@ _LATER_ORM_COLUMNS = {
"customer_system": "VARCHAR(16) NOT NULL DEFAULT ''",
"customer_home_images": "JSON NULL",
"customer_profile_updated_at": "DATETIME NULL",
"flow_version": "INTEGER NOT NULL DEFAULT 1",
},
"douyu_external_orders": {
"succeeded_at": "DATETIME NULL",
+38
View File
@@ -1082,6 +1082,44 @@ class TestSnapshotSync(_Base):
outbox = self.session.query(IntegrationOutbox).one()
assert outbox.payload_json["items"][0]["stateVersion"] == 1
def test_snapshot_before_clean_cutover_creates_legacy_task(self):
self.mapping()
setting = self.session.get(PlatformFulfillmentSetting, 1)
setting.clean_flow_started_at = NOW
self.session.commit()
service.sync_order_snapshot(
self.session,
{"data": self.snapshot(paidAt=(NOW - timedelta(hours=1)).isoformat())},
)
task = self.session.query(PlatformFulfillmentTask).one()
assert task.flow_version == 1
assert service.list_tasks(self.session, flow_version=2)["pagination"]["total"] == 0
assert service.list_tasks(self.session, flow_version=1)["pagination"]["total"] == 1
def test_snapshot_after_clean_cutover_creates_clean_task(self):
self.mapping()
setting = self.session.get(PlatformFulfillmentSetting, 1)
setting.clean_flow_started_at = NOW - timedelta(hours=1)
self.session.commit()
service.sync_order_snapshot(self.session, {"data": self.snapshot()})
task = self.session.query(PlatformFulfillmentTask).one()
assert task.flow_version == 2
def test_legacy_list_ignores_order_match_cutoff(self):
task = self.task()
task.flow_version = 1
task.paid_at = NOW - timedelta(days=10)
self.session.commit()
listed = service.list_tasks(self.session, flow_version=1)
assert listed["pagination"]["total"] == 1
assert listed["items"][0]["flowVersion"] == 1
def test_duplicate_snapshot_keeps_allocated_task_state(self):
self.mapping()
account = self.account("snapshot-allocated")
@@ -357,3 +357,44 @@ def test_schedule_retry_preserves_longer_capability_retry(db):
db.refresh(task)
assert task.next_allocate_at == retry_at
def test_auto_allocator_ignores_legacy_tasks(db):
actor = User(
username="auto-legacy-actor", password_hash="h", role="super_admin"
)
db.add_all(
[
actor,
PlatformFulfillmentSetting(
id=1,
lease_duration_minutes=None,
order_match_started_at=datetime.now(UTC) - timedelta(days=1),
),
PlatformFulfillmentTask(
source_system="order_site",
source_order_id="legacy-1",
source_order_item_id="legacy-1",
platform_order_id="AUTO-LEGACY",
internal_sku_key="skin",
commodity_id="commodity-1",
flow_version=1,
execution_status="ready",
binding_status="not_started",
exchange_status="not_started",
paid_at=datetime.now(UTC),
next_allocate_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
),
]
)
db.commit()
with patch.object(
auto_allocator.service,
"allocate_task",
side_effect=AssertionError("legacy tasks must not be auto allocated"),
):
result = auto_allocator.run_auto_allocation_once(db, limit=10)
assert result == {"checked": 0, "allocated": 0, "failed": 0}