feat: 重做协作台账状态机并支持充值核账

This commit is contained in:
yml2213
2026-10-19 16:06:02 +08:00
parent 781b27e63a
commit 3a6649a054
15 changed files with 2330 additions and 363 deletions
@@ -0,0 +1,137 @@
"""虎牙协作台账:待充值 / 积分异常 + 礼包积分快照。
2026-10-19 用户拍板:
- 删除 pending(待处理)/ bound(已确认绑定);
- 资料齐全且 CK 有效自动进 to_recharge(待充值);
- 充值完成按同一 bind_game_account 整组核账,结余为负或超过阈值进
points_error(积分异常)。
同时新增 required_points 与最近一次核账快照字段。可重复执行:逐列检查。
"""
from __future__ import annotations
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
revision: str = "20261019_0061"
down_revision: str | None = "20261019_0060"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
TABLE = "huya_external_orders"
def _columns(bind) -> set[str]:
return {column["name"] for column in sa.inspect(bind).get_columns(TABLE)}
def upgrade() -> None:
bind = op.get_bind()
if TABLE not in set(sa.inspect(bind).get_table_names()):
return
columns = _columns(bind)
additions = {
"required_points": sa.Column("required_points", sa.Integer(), nullable=True),
"points_checked_at": sa.Column(
"points_checked_at", sa.DateTime(), nullable=True
),
"points_balance": sa.Column("points_balance", sa.Integer(), nullable=True),
"points_required_total": sa.Column(
"points_required_total", sa.Integer(), nullable=True
),
"points_surplus": sa.Column("points_surplus", sa.Integer(), nullable=True),
"points_threshold": sa.Column("points_threshold", sa.Integer(), nullable=True),
"points_check_reason": sa.Column(
"points_check_reason",
sa.String(length=16),
nullable=False,
server_default="",
),
"points_check_message": sa.Column(
"points_check_message",
sa.String(length=512),
nullable=False,
server_default="",
),
"points_check_detail": sa.Column(
"points_check_detail", sa.JSON(), nullable=True
),
}
for name, column in additions.items():
if name not in columns:
op.add_column(TABLE, column)
op.alter_column(
TABLE,
"status",
existing_type=sa.String(length=16),
existing_nullable=False,
server_default="incomplete",
)
# 旧 pending/bound:资料齐全的转待充值,其余退回待补资料。
bind.execute(
sa.text(
f"""
UPDATE {TABLE}
SET status = 'to_recharge'
WHERE status IN ('pending', 'bound')
AND cookie IS NOT NULL AND cookie <> ''
AND ck_status = 'valid'
AND bind_game_account IS NOT NULL AND bind_game_account <> ''
AND bind_game_role IS NOT NULL AND bind_game_role <> ''
AND gift_name IS NOT NULL AND gift_name <> ''
AND home_images IS NOT NULL
AND JSON_LENGTH(home_images) > 0
"""
)
)
bind.execute(
sa.text(
f"""
UPDATE {TABLE}
SET status = 'incomplete'
WHERE status IN ('pending', 'bound')
"""
)
)
def downgrade() -> None:
bind = op.get_bind()
if TABLE not in set(sa.inspect(bind).get_table_names()):
return
# 新状态尽量映射回旧口径;历史 pending 不恢复。
bind.execute(
sa.text(f"UPDATE {TABLE} SET status = 'bound' WHERE status = 'to_recharge'")
)
bind.execute(
sa.text(
f"UPDATE {TABLE} SET status = 'recharged' WHERE status = 'points_error'"
)
)
op.alter_column(
TABLE,
"status",
existing_type=sa.String(length=16),
existing_nullable=False,
server_default="pending",
)
columns = _columns(bind)
for column in (
"points_check_detail",
"points_check_message",
"points_check_reason",
"points_threshold",
"points_surplus",
"points_required_total",
"points_balance",
"points_checked_at",
"required_points",
):
if column in columns:
op.drop_column(TABLE, column)