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
@@ -16,6 +16,7 @@ from sqlalchemy.orm import Session
from ...database import WorkerSessionLocal
from ...models import PlatformFulfillmentTask
from . import service
from .common import FLOW_VERSION_CLEAN
DEFAULT_INTERVAL_SECONDS = 20
DEFAULT_MIN_INTERVAL_SECONDS = 5
@@ -138,6 +139,8 @@ def run_auto_allocation_once(
due_cutoff = moment + timedelta(seconds=1)
query = db.query(PlatformFulfillmentTask).filter(
PlatformFulfillmentTask.execution_status == "ready",
# 存量任务只允许人工处理,自动选号只服务 Clean Cutover 之后的新单。
PlatformFulfillmentTask.flow_version == FLOW_VERSION_CLEAN,
PlatformFulfillmentTask.account_id.is_(None),
PlatformFulfillmentTask.commodity_id != "",
or_(
@@ -21,6 +21,8 @@ from ..douyu_account_state import DouyuLiveAccountState
TARGET_PLATFORM = "douyu"
SCOPE_ELITE = "elite"
FLOW_VERSION_LEGACY = 1
FLOW_VERSION_CLEAN = 2
BINDING_COOLDOWN_ISSUES = frozenset({"account_role_cooldown", "customer_role_cooldown"})
ACTIVE_STATUSES = {
"ready",
@@ -148,6 +150,33 @@ def _order_match_started_at(db: Session) -> datetime | None:
return row.order_match_started_at if row is not None else None
def _clean_flow_started_at(db: Session) -> datetime | None:
row = (
db.query(PlatformFulfillmentSetting)
.filter(PlatformFulfillmentSetting.id == 1)
.first()
)
return row.clean_flow_started_at if row is not None else None
def _flow_version_for_order(
db: Session,
*,
paid_at: datetime | None,
created_at: datetime | None,
) -> int:
"""按订单支付/创建时间划分 legacy/clean,避免旧订单补建任务混入新流程。"""
cutoff = _clean_flow_started_at(db)
basis = paid_at or created_at
if cutoff is None or basis is None:
return FLOW_VERSION_CLEAN
if cutoff.tzinfo is None:
cutoff = cutoff.replace(tzinfo=UTC)
if basis.tzinfo is None:
basis = basis.replace(tzinfo=UTC)
return FLOW_VERSION_LEGACY if basis < cutoff else FLOW_VERSION_CLEAN
def _apply_order_match_cutoff(
query,
match_started_at: datetime | None,
@@ -23,6 +23,7 @@ from .common import (
AUTO_SYNC_INTERVAL_SECONDS,
SCOPE_ELITE,
TARGET_PLATFORM,
_flow_version_for_order,
_jsonable,
_now,
_order_match_started_at,
@@ -181,6 +182,11 @@ def _upsert_order_snapshot(
),
target_platform=TARGET_PLATFORM,
scope=SCOPE_ELITE,
flow_version=_flow_version_for_order(
db,
paid_at=paid_at,
created_at=order_created_at,
),
created_at=_now(),
updated_at=_now(),
)
@@ -534,6 +540,11 @@ def _sync_pending_tasks(
source_order_id=str(row.get("source_order_id") or ""),
source_order_item_id=item_id,
source_fulfillment_id=str(row.get("source_fulfillment_id") or ""),
flow_version=_flow_version_for_order(
db,
paid_at=_parse_snapshot_datetime(row.get("paid_at")),
created_at=_now(),
),
created_at=_now(),
updated_at=_now(),
)
@@ -79,6 +79,7 @@ def _count_cache_key(
status: str,
queue_bucket: str,
sale_mode: str,
flow_version: int | None,
keyword: str,
) -> tuple[Any, ...]:
return (
@@ -86,6 +87,7 @@ def _count_cache_key(
status,
queue_bucket,
sale_mode,
flow_version,
keyword.strip(),
)
@@ -209,13 +211,18 @@ def list_tasks(
status: str = "",
queue_bucket: str = "",
sale_mode: str = "",
flow_version: int | None = None,
keyword: str = "",
page: int = 1,
page_size: int = 20,
) -> dict[str, Any]:
cutoff = _order_match_started_at(db)
query = db.query(PlatformFulfillmentTask)
query = _apply_order_match_cutoff(query, cutoff)
if flow_version is not None:
query = query.filter(PlatformFulfillmentTask.flow_version == flow_version)
# 历史任务不受订单起算时间限制,否则很多旧单在工作台里永远看不到。
if flow_version != 1:
query = _apply_order_match_cutoff(query, cutoff)
# 待映射队列本身就是要展示“还没有映射”的任务,不能再套映射存在条件。
if queue_bucket != "pending_mapping":
query = query.filter(
@@ -310,6 +317,7 @@ def list_tasks(
status=status,
queue_bucket=queue_bucket,
sale_mode=sale_mode,
flow_version=flow_version,
keyword=keyword,
),
)
@@ -426,6 +434,7 @@ def _task_out(
"goodsName": goods_name,
"requiredPoints": task.required_points,
"saleMode": sale_mode,
"flowVersion": int(task.flow_version or 1),
"campaignKey": task.campaign_key,
"campaignName": task.campaign_name,
"offerAvailableFrom": task.offer_available_from,