fix(fulfillment): 按支付时间过滤履约订单

This commit is contained in:
yml
2026-11-18 14:44:19 +08:00
parent 96fa9c7086
commit b7b09c90bd
9 changed files with 193 additions and 26 deletions
@@ -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")
+3
View File
@@ -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)
+1
View File
@@ -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>
),
},