fix(fulfillment): 按支付时间过滤履约订单
This commit is contained in:
@@ -35,7 +35,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261118_0087"
|
||||
HEAD_REVISION = "20261118_0088"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
|
||||
@@ -148,6 +148,7 @@ class _Base:
|
||||
execution_status="ready" if mapping else "pending_mapping",
|
||||
exchange_status="not_started",
|
||||
source_snapshot_json={"order_no": "ORDER-101"},
|
||||
paid_at=NOW,
|
||||
created_at=NOW,
|
||||
updated_at=NOW,
|
||||
)
|
||||
@@ -227,6 +228,7 @@ class TestSyncAndMapping(_Base):
|
||||
task = self.session.query(PlatformFulfillmentTask).one()
|
||||
assert task.execution_status == "ready"
|
||||
assert task.commodity_id == COMMODITY
|
||||
assert task.paid_at == NOW
|
||||
|
||||
def test_list_only_returns_tasks_that_have_a_commodity_mapping(self):
|
||||
legacy = self.task(mapped=False)
|
||||
@@ -254,6 +256,7 @@ class TestSyncAndMapping(_Base):
|
||||
execution_status="ready",
|
||||
exchange_status="not_started",
|
||||
source_snapshot_json={"order_no": "ORDER-103"},
|
||||
paid_at=NOW,
|
||||
created_at=NOW,
|
||||
updated_at=NOW,
|
||||
)
|
||||
@@ -266,7 +269,7 @@ class TestSyncAndMapping(_Base):
|
||||
assert listed["pagination"]["total"] == 1
|
||||
assert legacy.id != mapped.id
|
||||
|
||||
def test_list_hides_tasks_created_before_order_match_cutoff(self):
|
||||
def test_list_hides_tasks_paid_before_order_match_cutoff(self):
|
||||
mapping = self.mapping()
|
||||
task = PlatformFulfillmentTask(
|
||||
source_system="order_site",
|
||||
@@ -286,8 +289,9 @@ class TestSyncAndMapping(_Base):
|
||||
binding_status="not_started",
|
||||
execution_status="ready",
|
||||
exchange_status="not_started",
|
||||
created_at=NOW - timedelta(hours=2),
|
||||
updated_at=NOW - timedelta(hours=2),
|
||||
paid_at=NOW - timedelta(hours=2),
|
||||
created_at=NOW,
|
||||
updated_at=NOW,
|
||||
)
|
||||
self.session.add(task)
|
||||
self.session.commit()
|
||||
@@ -302,15 +306,53 @@ class TestSyncAndMapping(_Base):
|
||||
|
||||
assert listed["pagination"]["total"] == 0
|
||||
|
||||
def test_list_includes_recent_paid_task_created_before_order_match_cutoff(self):
|
||||
mapping = self.mapping()
|
||||
task = PlatformFulfillmentTask(
|
||||
source_system="order_site",
|
||||
source_order_id="106",
|
||||
source_order_item_id="107",
|
||||
source_fulfillment_id="207",
|
||||
platform_order_id="ORDER-107",
|
||||
sku_code=INTERNAL_SKU,
|
||||
sku_name=INTERNAL_SKU,
|
||||
internal_sku_key=INTERNAL_SKU,
|
||||
quantity=1,
|
||||
target_platform="douyu",
|
||||
scope="elite",
|
||||
commodity_mapping_id=mapping.id,
|
||||
commodity_id=COMMODITY,
|
||||
required_points=1280,
|
||||
binding_status="not_started",
|
||||
execution_status="ready",
|
||||
exchange_status="not_started",
|
||||
paid_at=NOW,
|
||||
created_at=NOW - timedelta(hours=2),
|
||||
updated_at=NOW,
|
||||
)
|
||||
self.session.add(task)
|
||||
self.session.commit()
|
||||
service.update_settings(
|
||||
self.session,
|
||||
lease_duration_minutes=None,
|
||||
order_match_started_at=NOW - timedelta(hours=1),
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
listed = service.list_tasks(self.session)
|
||||
|
||||
assert [item["id"] for item in listed["items"]] == [task.id]
|
||||
|
||||
def test_customer_session_ignores_task_before_order_match_cutoff(
|
||||
self,
|
||||
monkeypatch,
|
||||
):
|
||||
task = self.task()
|
||||
assert task.paid_at is not None
|
||||
service.update_settings(
|
||||
self.session,
|
||||
lease_duration_minutes=None,
|
||||
order_match_started_at=task.created_at + timedelta(minutes=1),
|
||||
order_match_started_at=task.paid_at + timedelta(minutes=1),
|
||||
current=self.admin,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
|
||||
@@ -32,6 +32,7 @@ def test_auto_allocator_only_scans_ready_unassigned_tasks(db):
|
||||
execution_status="ready",
|
||||
binding_status="not_started",
|
||||
exchange_status="not_started",
|
||||
paid_at=datetime.now(UTC),
|
||||
updated_at=datetime.now(UTC) - timedelta(minutes=2),
|
||||
)
|
||||
mapped_pending = PlatformFulfillmentTask(
|
||||
@@ -44,6 +45,7 @@ def test_auto_allocator_only_scans_ready_unassigned_tasks(db):
|
||||
execution_status="pending_mapping",
|
||||
binding_status="not_started",
|
||||
exchange_status="not_started",
|
||||
paid_at=datetime.now(UTC),
|
||||
updated_at=datetime.now(UTC) - timedelta(minutes=2),
|
||||
)
|
||||
old_ready = PlatformFulfillmentTask(
|
||||
@@ -56,7 +58,8 @@ def test_auto_allocator_only_scans_ready_unassigned_tasks(db):
|
||||
execution_status="ready",
|
||||
binding_status="not_started",
|
||||
exchange_status="not_started",
|
||||
created_at=datetime.now(UTC) - timedelta(days=2),
|
||||
paid_at=datetime.now(UTC) - timedelta(days=2),
|
||||
created_at=datetime.now(UTC),
|
||||
updated_at=datetime.now(UTC) - timedelta(days=2),
|
||||
)
|
||||
db.add_all([actor, ready, mapped_pending, old_ready])
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
"""直播账号履约任务保存订单支付时间
|
||||
|
||||
订单匹配起算必须按 order_site 的支付时间判断。旧实现误用了本地入库
|
||||
``created_at``,导致起算前已支付、但后来才同步的订单进入工作台。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261118_0088"
|
||||
down_revision: str | None = "20261118_0087"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
TABLE = "platform_fulfillment_tasks"
|
||||
INDEX = "ix_platform_fulfillment_tasks_paid_at"
|
||||
|
||||
|
||||
def _column_exists(column: str) -> bool:
|
||||
return bool(
|
||||
op.get_bind()
|
||||
.execute(
|
||||
sa.text(
|
||||
"SELECT COUNT(*) FROM information_schema.columns "
|
||||
"WHERE table_schema = DATABASE() "
|
||||
"AND table_name = :table AND column_name = :column"
|
||||
),
|
||||
{"table": TABLE, "column": column},
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
|
||||
def _index_exists(index: str) -> bool:
|
||||
return bool(
|
||||
op.get_bind()
|
||||
.execute(
|
||||
sa.text(
|
||||
"SELECT COUNT(*) FROM information_schema.statistics "
|
||||
"WHERE table_schema = DATABASE() "
|
||||
"AND table_name = :table AND index_name = :index"
|
||||
),
|
||||
{"table": TABLE, "index": index},
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
|
||||
def _json_object(value: Any) -> dict[str, Any]:
|
||||
if isinstance(value, dict):
|
||||
return value
|
||||
if isinstance(value, (str, bytes, bytearray)):
|
||||
try:
|
||||
parsed = json.loads(value)
|
||||
except (TypeError, ValueError):
|
||||
return {}
|
||||
return parsed if isinstance(parsed, dict) else {}
|
||||
return {}
|
||||
|
||||
|
||||
def _utc_naive(value: Any) -> datetime | None:
|
||||
if not isinstance(value, str) or not value.strip():
|
||||
return None
|
||||
try:
|
||||
parsed = datetime.fromisoformat(value.strip())
|
||||
except ValueError:
|
||||
return None
|
||||
if parsed.tzinfo is None:
|
||||
return parsed
|
||||
return parsed.astimezone(UTC).replace(tzinfo=None)
|
||||
|
||||
|
||||
def _backfill_paid_at() -> None:
|
||||
conn = op.get_bind()
|
||||
rows = conn.execute(
|
||||
sa.text(
|
||||
"SELECT id, source_snapshot_json FROM platform_fulfillment_tasks "
|
||||
"WHERE paid_at IS NULL AND source_snapshot_json IS NOT NULL"
|
||||
)
|
||||
).fetchall()
|
||||
updates: list[dict[str, Any]] = []
|
||||
for task_id, raw_snapshot in rows:
|
||||
snapshot = _json_object(raw_snapshot)
|
||||
paid_at = _utc_naive(snapshot.get("paid_at"))
|
||||
if paid_at is not None:
|
||||
updates.append({"task_id": task_id, "paid_at": paid_at})
|
||||
if updates:
|
||||
conn.execute(
|
||||
sa.text(
|
||||
"UPDATE platform_fulfillment_tasks "
|
||||
"SET paid_at = :paid_at WHERE id = :task_id"
|
||||
),
|
||||
updates,
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
if not _column_exists("paid_at"):
|
||||
op.add_column(
|
||||
TABLE,
|
||||
sa.Column("paid_at", sa.DateTime(), nullable=True),
|
||||
)
|
||||
_backfill_paid_at()
|
||||
if not _index_exists(INDEX):
|
||||
op.create_index(INDEX, TABLE, ["paid_at"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if _index_exists(INDEX):
|
||||
op.drop_index(INDEX, table_name=TABLE)
|
||||
if _column_exists("paid_at"):
|
||||
op.drop_column(TABLE, "paid_at")
|
||||
@@ -1865,6 +1865,9 @@ class PlatformFulfillmentTask(Base):
|
||||
ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
completed_at: Mapped[datetime | None] = mapped_column(UTCDateTime, nullable=True)
|
||||
paid_at: Mapped[datetime | None] = mapped_column(
|
||||
UTCDateTime, nullable=True, index=True
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=_utcnow)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
UTCDateTime, default=_utcnow, onupdate=_utcnow
|
||||
|
||||
@@ -57,11 +57,10 @@ def run_auto_allocation_once(
|
||||
PlatformFulfillmentTask.commodity_id != "",
|
||||
PlatformFulfillmentTask.updated_at <= moment - timedelta(seconds=retry_seconds),
|
||||
)
|
||||
match_started_at = service._order_match_started_at(db)
|
||||
if match_started_at is not None:
|
||||
query = query.filter(
|
||||
PlatformFulfillmentTask.created_at >= match_started_at,
|
||||
)
|
||||
query = service._apply_order_match_cutoff(
|
||||
query,
|
||||
service._order_match_started_at(db),
|
||||
)
|
||||
candidates = (
|
||||
query.order_by(PlatformFulfillmentTask.updated_at.asc()).limit(batch_size).all()
|
||||
)
|
||||
|
||||
@@ -179,6 +179,15 @@ def _order_match_started_at(db: Session) -> datetime | None:
|
||||
return row.order_match_started_at if row is not None else None
|
||||
|
||||
|
||||
def _apply_order_match_cutoff(
|
||||
query,
|
||||
match_started_at: datetime | None,
|
||||
):
|
||||
if match_started_at is None:
|
||||
return query
|
||||
return query.filter(PlatformFulfillmentTask.paid_at >= match_started_at)
|
||||
|
||||
|
||||
def _integration_actor(db: Session) -> User:
|
||||
raw_user_id = os.getenv("PLATFORM_FULFILLMENT_INTEGRATION_ACTOR_ID", "").strip()
|
||||
query = db.query(User).filter(User.is_active.is_(True))
|
||||
@@ -210,11 +219,7 @@ def _find_customer_task(
|
||||
PlatformFulfillmentTask.source_system == "order_site",
|
||||
PlatformFulfillmentTask.source_order_item_id == item_id,
|
||||
)
|
||||
match_started_at = _order_match_started_at(db)
|
||||
if match_started_at is not None:
|
||||
query = query.filter(
|
||||
PlatformFulfillmentTask.created_at >= match_started_at,
|
||||
)
|
||||
query = _apply_order_match_cutoff(query, _order_match_started_at(db))
|
||||
order_no = str(platform_order_id or "").strip()
|
||||
if order_no:
|
||||
query = query.filter(PlatformFulfillmentTask.platform_order_id == order_no)
|
||||
@@ -398,6 +403,8 @@ def _sync_pending_tasks(
|
||||
task.sku_name = str(row.get("sku_name") or "")
|
||||
task.internal_sku_key = internal_sku_key
|
||||
task.quantity = 1
|
||||
if isinstance(row.get("paid_at"), datetime):
|
||||
task.paid_at = row["paid_at"]
|
||||
task.order_amount_fen = int(
|
||||
row.get("revenue_amount_fen") or row.get("total_amount") or 0
|
||||
)
|
||||
@@ -466,11 +473,7 @@ def list_tasks(
|
||||
page_size: int = 20,
|
||||
) -> dict[str, Any]:
|
||||
query = db.query(PlatformFulfillmentTask)
|
||||
match_started_at = _order_match_started_at(db)
|
||||
if match_started_at is not None:
|
||||
query = query.filter(
|
||||
PlatformFulfillmentTask.created_at >= match_started_at,
|
||||
)
|
||||
query = _apply_order_match_cutoff(query, _order_match_started_at(db))
|
||||
query = query.filter(
|
||||
or_(
|
||||
PlatformFulfillmentTask.commodity_mapping_id.isnot(None),
|
||||
@@ -569,6 +572,7 @@ def _task_out(
|
||||
"lastError": task.last_error,
|
||||
"operatorId": task.operator_id,
|
||||
"completedAt": task.completed_at,
|
||||
"paidAt": task.paid_at,
|
||||
"createdAt": task.created_at,
|
||||
"updatedAt": task.updated_at,
|
||||
"account": (
|
||||
@@ -879,17 +883,13 @@ def upsert_mapping(
|
||||
row.updated_at = _now()
|
||||
db.flush()
|
||||
|
||||
match_started_at = _order_match_started_at(db)
|
||||
pending = db.query(PlatformFulfillmentTask).filter(
|
||||
PlatformFulfillmentTask.internal_sku_key == sku_key,
|
||||
PlatformFulfillmentTask.execution_status.in_(
|
||||
["pending_mapping", "ready", "manual_required"]
|
||||
),
|
||||
)
|
||||
if match_started_at is not None:
|
||||
pending = pending.filter(
|
||||
PlatformFulfillmentTask.created_at >= match_started_at,
|
||||
)
|
||||
pending = _apply_order_match_cutoff(pending, _order_match_started_at(db))
|
||||
pending = pending.all()
|
||||
for task in pending:
|
||||
_apply_mapping(task, row if row.enabled else None)
|
||||
|
||||
@@ -2165,6 +2165,7 @@ export interface PlatformFulfillmentTask {
|
||||
lastError: string;
|
||||
operatorId: number | null;
|
||||
completedAt: string | null;
|
||||
paidAt: string | null;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
account: PlatformFulfillmentAccount | null;
|
||||
|
||||
@@ -597,6 +597,7 @@ export default function PlatformFulfillmentPage() {
|
||||
<Text type="secondary">
|
||||
{providerLabel(task.provider)} / {task.shopName || task.shopId || '-'}
|
||||
</Text>
|
||||
<Text type="secondary">支付时间:{task.paidAt ? formatTime(task.paidAt) : '-'}</Text>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user