fix(fulfillment): 按支付时间过滤履约订单
This commit is contained in:
@@ -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")
|
||||
Reference in New Issue
Block a user