fix(douyu): 自动校验兑换资格并稳定账号导入顺序

This commit is contained in:
yml
2026-11-19 15:36:47 +08:00
parent ddead2284d
commit d611c0ed14
8 changed files with 275 additions and 18 deletions
@@ -0,0 +1,49 @@
"""保存斗鱼工作台账号的显式展示顺序。"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision: str = "20261119_0097"
down_revision: str | None = "20261118_0096"
branch_labels: str | None = None
depends_on: str | None = None
TABLE = "douyu_workbench_accounts"
COLUMN = "sort_order"
def _column_exists() -> 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 upgrade() -> None:
if not _column_exists():
op.add_column(
TABLE,
sa.Column(
COLUMN,
sa.Integer(),
nullable=False,
server_default="0",
),
)
# 历史数据没有显式顺序,用原自增 ID 恢复当时的插入顺序。
op.execute(f"UPDATE `{TABLE}` SET `{COLUMN}` = `id`")
def downgrade() -> None:
if _column_exists():
op.drop_column(TABLE, COLUMN)
+3
View File
@@ -397,6 +397,9 @@ class DouyuWorkbenchAccount(Base):
account_id: Mapped[int] = mapped_column(
ForeignKey("accounts.id"), nullable=False, index=True
)
sort_order: Mapped[int] = mapped_column(
Integer, nullable=False, default=0, server_default="0"
)
created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=_utcnow)
__table_args__ = (
+50 -14
View File
@@ -329,7 +329,12 @@ def _apply_task_account_filters(
ids: str = "",
):
"""按工作台导入弹窗的搜索、标签、多行账号名筛选任务账号。"""
id_list = [int(item) for item in ids.split(",") if item.strip().isdigit()]
id_list = [
int(item)
for item in dict.fromkeys(
value.strip() for value in ids.split(",") if value.strip().isdigit()
)
]
if id_list:
query = query.filter(Account.id.in_(id_list))
tag_text = (tag or "").strip()
@@ -352,10 +357,14 @@ def _apply_task_account_filters(
Account.xpd_game_name.ilike(pattern),
)
)
return query, selected_names
return query, selected_names, id_list
def _order_task_accounts(query, selected_names: list[str]):
def _order_task_accounts(
query,
selected_names: list[str],
selected_ids: list[int] | None = None,
):
"""自定义账号名按粘贴顺序排序,未指定时保持最新账号优先。"""
if selected_names:
input_order = case(
@@ -364,6 +373,13 @@ def _order_task_accounts(query, selected_names: list[str]):
else_=len(selected_names),
)
return query.order_by(input_order, Account.id.desc())
if selected_ids:
input_order = case(
{account_id: index for index, account_id in enumerate(selected_ids)},
value=Account.id,
else_=len(selected_ids),
)
return query.order_by(input_order, Account.id.desc())
return query.order_by(Account.id.desc())
@@ -463,7 +479,7 @@ def list_task_accounts(
tag 按账号标签精确筛选;ids 为逗号分隔的账号 ID,用于工作台按已导入账号过滤;
account_names 为多行斗鱼账户名(每行一个),按粘贴顺序精确匹配;为空时返回全部。
"""
query, selected_names = _apply_task_account_filters(
query, selected_names, selected_ids = _apply_task_account_filters(
_visible_task_accounts_query(db, current),
search=search,
tag=tag,
@@ -473,7 +489,7 @@ def list_task_accounts(
total = None
if page is not None:
total = query.order_by(None).count()
query = _order_task_accounts(query, selected_names)
query = _order_task_accounts(query, selected_names, selected_ids)
if page is not None:
query = query.offset((page - 1) * page_size).limit(page_size)
else:
@@ -498,7 +514,7 @@ def list_task_account_ids(
current: User = Depends(get_current_user),
):
"""返回当前用户可导入工作台的全部账号 ID,不受列表分页限制。"""
query, selected_names = _apply_task_account_filters(
query, selected_names, selected_ids = _apply_task_account_filters(
_visible_task_accounts_query(db, current),
search=search,
tag=tag,
@@ -509,6 +525,7 @@ def list_task_account_ids(
for (account_id,) in _order_task_accounts(
query.enable_eagerloads(False).with_entities(Account.id),
selected_names,
selected_ids,
).all()
]
return {"account_ids": account_ids, "total": len(account_ids)}
@@ -581,7 +598,10 @@ def list_workbench_accounts(
rows = rows.filter(Account.assigned_to == current.id)
account_ids = [
account_id
for (account_id,) in rows.order_by(DouyuWorkbenchAccount.id.asc()).all()
for (account_id,) in rows.order_by(
DouyuWorkbenchAccount.sort_order.asc(),
DouyuWorkbenchAccount.id.asc(),
).all()
]
configured = (
db.query(DouyuWorkbench.id)
@@ -602,7 +622,7 @@ def update_workbench_accounts(
current: User = Depends(require_permission("douyu:task")),
):
"""用当前完整账号集合覆盖一个工作台,作为跨浏览器的同步状态。"""
account_ids = sorted(set(req.account_ids))
account_ids = list(dict.fromkeys(req.account_ids))
if any(account_id < 1 for account_id in account_ids):
raise HTTPException(status_code=400, detail="无效的账号ID")
if account_ids:
@@ -629,8 +649,9 @@ def update_workbench_accounts(
user_id=current.id,
handbook_scope=req.handbook_scope,
account_id=account_id,
sort_order=index,
)
for account_id in account_ids
for index, account_id in enumerate(account_ids)
]
)
db.commit()
@@ -774,6 +795,26 @@ async def create_task_batch(
req.account_ids,
platform_task_id=req.platform_task_id,
)
platform_task = (
db.query(PlatformFulfillmentTask)
.filter(PlatformFulfillmentTask.id == int(req.platform_task_id))
.first()
if req.platform_task_id
else None
)
if platform_task is not None and req.task_type == "exchange_goods":
eligibility = platform_fulfillment_service.refresh_exchange_eligibility(
db,
task_id=int(platform_task.id),
current=current,
)
refreshed_task = eligibility["task"]
if refreshed_task.get("exchangeStatus") != "eligible":
raise HTTPException(
status_code=409,
detail=refreshed_task.get("lastError") or "当前账号不满足兑换条件",
)
platform_task = db.get(PlatformFulfillmentTask, int(platform_task.id))
cleanup_orphan_douyu_tasks(
db,
@@ -799,11 +840,6 @@ async def create_task_batch(
status_code=400, detail="没有可执行的斗鱼账号,请先登录获取 Cookie"
)
if req.platform_task_id:
platform_task = (
db.query(PlatformFulfillmentTask)
.filter(PlatformFulfillmentTask.id == int(req.platform_task_id))
.first()
)
douyu_task_ids = [
int(task_id)
for (task_id,) in (