feat(fulfillment): 增加账号商品能力索引与生命周期事件
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
"""修复重复订单快照把已分配履约任务打回待分配的问题。
|
||||
|
||||
旧逻辑在每次消费非终态订单快照时都会执行 ``_apply_mapping``,无条件把
|
||||
``execution_status`` 改成 ``ready``,但保留 ``account_id`` 和活跃租约。
|
||||
自动选号只扫描 ``ready + account_id IS NULL``,因此这些任务不会被再次处理。
|
||||
这里只修复带有效租约、且尚未开始绑定的异常任务,并依据兑换状态恢复提示。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261118_0090"
|
||||
down_revision: str | None = "20261118_0089"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.get_bind().execute(
|
||||
sa.text(
|
||||
"UPDATE platform_fulfillment_tasks t "
|
||||
"JOIN account_leases l ON l.id = t.lease_id "
|
||||
"SET t.execution_status = 'allocated', "
|
||||
"t.next_allocate_at = NULL, "
|
||||
"t.issue_code = CASE t.exchange_status "
|
||||
"WHEN 'points_short' THEN 'points_short' "
|
||||
"WHEN 'not_opened' THEN 'elite_not_opened' "
|
||||
"WHEN 'limited' THEN 'commodity_limit_reached' "
|
||||
"WHEN 'check_failed' THEN 'eligibility_check_failed' "
|
||||
"ELSE '' END, "
|
||||
"t.last_error = CASE t.exchange_status "
|
||||
"WHEN 'points_short' THEN '账号积分不足,可充值后继续' "
|
||||
"WHEN 'not_opened' THEN '账号尚未开通精英宝典,可开通后继续' "
|
||||
"WHEN 'limited' THEN '该账号已达到此商品兑换上限' "
|
||||
"WHEN 'check_failed' THEN '兑换资格查询失败,请刷新后重试' "
|
||||
"ELSE '' END, "
|
||||
"t.updated_at = UTC_TIMESTAMP() "
|
||||
"WHERE t.execution_status = 'ready' "
|
||||
"AND t.account_id IS NOT NULL "
|
||||
"AND t.lease_id IS NOT NULL "
|
||||
"AND t.binding_status = 'not_started' "
|
||||
"AND l.purpose = 'platform_fulfillment' "
|
||||
"AND l.status = 'active'"
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# 这是不可逆的数据修复;回滚代码版本也无需重新制造异常状态。
|
||||
pass
|
||||
@@ -0,0 +1,167 @@
|
||||
"""账号商品兑换能力索引。
|
||||
|
||||
本表把“某账号对某商品当前能不能换”从 ``overview_snapshot`` JSON 中拆出来,
|
||||
用于履约秒级候选查询。它只保存最后一次官方查询的派生结论;兑换事实仍写
|
||||
``account_events``。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261118_0091"
|
||||
down_revision: str | None = "20261118_0090"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
TABLE = "account_commodity_states"
|
||||
|
||||
|
||||
def _table_exists(name: str) -> bool:
|
||||
return bool(
|
||||
op.get_bind()
|
||||
.execute(
|
||||
sa.text(
|
||||
"SELECT COUNT(*) FROM information_schema.tables "
|
||||
"WHERE table_schema = DATABASE() AND table_name = :name"
|
||||
),
|
||||
{"name": name},
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
|
||||
def _index_exists(name: 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 = :name"
|
||||
),
|
||||
{"table": TABLE, "name": name},
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
if not _table_exists(TABLE):
|
||||
op.create_table(
|
||||
TABLE,
|
||||
sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
|
||||
sa.Column("account_id", sa.Integer(), nullable=False),
|
||||
sa.Column(
|
||||
"platform",
|
||||
sa.String(length=16),
|
||||
nullable=False,
|
||||
server_default="douyu",
|
||||
),
|
||||
sa.Column(
|
||||
"scope",
|
||||
sa.String(length=16),
|
||||
nullable=False,
|
||||
server_default="elite",
|
||||
),
|
||||
sa.Column("commodity_id", sa.String(length=64), nullable=False),
|
||||
sa.Column(
|
||||
"commodity_name",
|
||||
sa.String(length=256),
|
||||
nullable=False,
|
||||
server_default="",
|
||||
),
|
||||
sa.Column(
|
||||
"availability",
|
||||
sa.String(length=16),
|
||||
nullable=False,
|
||||
server_default="unknown",
|
||||
),
|
||||
sa.Column(
|
||||
"blocking_reason",
|
||||
sa.String(length=255),
|
||||
nullable=False,
|
||||
server_default="",
|
||||
),
|
||||
sa.Column(
|
||||
"raw_status",
|
||||
sa.String(length=16),
|
||||
nullable=False,
|
||||
server_default="",
|
||||
),
|
||||
sa.Column("observed_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("valid_until", sa.DateTime(), nullable=True),
|
||||
sa.Column("next_check_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("last_exchange_at", sa.DateTime(), nullable=True),
|
||||
sa.Column(
|
||||
"exchange_count_365d",
|
||||
sa.Integer(),
|
||||
nullable=False,
|
||||
server_default="0",
|
||||
),
|
||||
sa.Column(
|
||||
"source", sa.String(length=24), nullable=False, server_default=""
|
||||
),
|
||||
sa.Column(
|
||||
"error_code",
|
||||
sa.String(length=64),
|
||||
nullable=False,
|
||||
server_default="",
|
||||
),
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(),
|
||||
nullable=False,
|
||||
server_default=sa.text("CURRENT_TIMESTAMP"),
|
||||
),
|
||||
sa.Column(
|
||||
"updated_at",
|
||||
sa.DateTime(),
|
||||
nullable=False,
|
||||
server_default=sa.text("CURRENT_TIMESTAMP"),
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.ForeignKeyConstraint(
|
||||
["account_id"],
|
||||
["accounts.id"],
|
||||
name="fk_account_commodity_account",
|
||||
ondelete="CASCADE",
|
||||
),
|
||||
sa.UniqueConstraint(
|
||||
"account_id",
|
||||
"platform",
|
||||
"scope",
|
||||
"commodity_id",
|
||||
name="uq_account_commodity_state",
|
||||
),
|
||||
)
|
||||
|
||||
if not _index_exists("ix_account_commodity_match"):
|
||||
op.create_index(
|
||||
"ix_account_commodity_match",
|
||||
TABLE,
|
||||
[
|
||||
"platform",
|
||||
"scope",
|
||||
"commodity_id",
|
||||
"availability",
|
||||
"valid_until",
|
||||
"account_id",
|
||||
],
|
||||
)
|
||||
if not _index_exists("ix_account_commodity_account"):
|
||||
op.create_index(
|
||||
"ix_account_commodity_account",
|
||||
TABLE,
|
||||
["account_id", "platform", "scope"],
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if _index_exists("ix_account_commodity_account"):
|
||||
op.drop_index("ix_account_commodity_account", table_name=TABLE)
|
||||
if _index_exists("ix_account_commodity_match"):
|
||||
op.drop_index("ix_account_commodity_match", table_name=TABLE)
|
||||
if _table_exists(TABLE):
|
||||
op.drop_table(TABLE)
|
||||
@@ -0,0 +1,38 @@
|
||||
"""回填官方兑换事件的商品检索键。
|
||||
|
||||
早期 ``record_official_exchange`` 只把 ``commodity_id`` 放进 JSON detail,
|
||||
没有写 ``subject_key``。履约的 365 天同商品规则按 ``subject_key`` 查询,
|
||||
因此这些官方历史记录实际没有参与匹配。这里从 detail 幂等回填。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261118_0092"
|
||||
down_revision: str | None = "20261118_0091"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.get_bind().execute(
|
||||
sa.text(
|
||||
"UPDATE account_events "
|
||||
"SET subject_key = LEFT("
|
||||
"JSON_UNQUOTE(JSON_EXTRACT(detail, '$.commodity_id')), 96"
|
||||
"), subject_type = 'commodity' "
|
||||
"WHERE event_type = 'exchange' "
|
||||
"AND source = 'official_sync' "
|
||||
"AND subject_key = '' "
|
||||
"AND detail IS NOT NULL "
|
||||
"AND JSON_UNQUOTE(JSON_EXTRACT(detail, '$.commodity_id')) IS NOT NULL "
|
||||
"AND JSON_UNQUOTE(JSON_EXTRACT(detail, '$.commodity_id')) <> ''"
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# 回填的是可重建的检索投影,不回滚,避免影响后续新写入事件。
|
||||
pass
|
||||
@@ -0,0 +1,72 @@
|
||||
"""账号商品能力增加限时兑换窗口。
|
||||
|
||||
斗鱼 ``storehome`` 同时返回账号级 ``status`` 和商品级限时规则:
|
||||
``timeLimited``、``startTime`` / ``endTime``、``specTimes``。这三类信息
|
||||
必须分明,不能把未开始、已结束和账号限兑都显示成同一种“不可兑换”。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261118_0093"
|
||||
down_revision: str | None = "20261118_0092"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
TABLE = "account_commodity_states"
|
||||
|
||||
|
||||
def _column_exists(name: 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 = :name"
|
||||
),
|
||||
{"table": TABLE, "name": name},
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
if not _column_exists("time_limited"):
|
||||
op.add_column(
|
||||
TABLE,
|
||||
sa.Column(
|
||||
"time_limited",
|
||||
sa.Boolean(),
|
||||
nullable=False,
|
||||
server_default="0",
|
||||
),
|
||||
)
|
||||
if not _column_exists("available_from"):
|
||||
op.add_column(
|
||||
TABLE,
|
||||
sa.Column("available_from", sa.DateTime(), nullable=True),
|
||||
)
|
||||
if not _column_exists("available_until"):
|
||||
op.add_column(
|
||||
TABLE,
|
||||
sa.Column("available_until", sa.DateTime(), nullable=True),
|
||||
)
|
||||
if not _column_exists("schedule_json"):
|
||||
op.add_column(
|
||||
TABLE,
|
||||
sa.Column("schedule_json", sa.JSON(), nullable=True),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if _column_exists("schedule_json"):
|
||||
op.drop_column(TABLE, "schedule_json")
|
||||
if _column_exists("available_until"):
|
||||
op.drop_column(TABLE, "available_until")
|
||||
if _column_exists("available_from"):
|
||||
op.drop_column(TABLE, "available_from")
|
||||
if _column_exists("time_limited"):
|
||||
op.drop_column(TABLE, "time_limited")
|
||||
Reference in New Issue
Block a user