fix(douyu): 升级工作台任务流并修正换绑冷却

This commit is contained in:
yml
2026-11-22 23:39:02 +08:00
parent 4b57f9b2a7
commit 8e01c3c69e
9 changed files with 343 additions and 113 deletions
+42 -4
View File
@@ -21,6 +21,7 @@ from fastapi import (
from loguru import logger
from sqlalchemy import case, func, or_, select
from sqlalchemy.orm import Session, joinedload
from sqlalchemy.sql import Select
from starlette.concurrency import run_in_threadpool
from core.douyu import (
@@ -64,7 +65,7 @@ from ..schemas import (
DouyuWorkbenchAccountsUpdate,
DouyuXpdGoodsOut,
)
from ..services.account_binding import bind_payload
from ..services.account_binding import BIND_BOUND, bind_payload
from ..services.account_observability import (
account_platform_status,
account_platform_trends,
@@ -355,6 +356,7 @@ def _list_visible_tasks_for_support(
*,
batch_id: str | None,
handbook_scope: str | None,
account_scope_query: Select | None,
limit: int,
offset: int = 0,
) -> list[DouyuTask]:
@@ -369,6 +371,10 @@ def _list_visible_tasks_for_support(
base_query = db.query(DouyuTask.id).filter(*owner_filter)
if batch_id:
base_query = base_query.filter(DouyuTask.batch_id == batch_id)
if account_scope_query is not None:
base_query = base_query.filter(
DouyuTask.account_id.in_(account_scope_query)
)
for query in _split_task_scope_query(base_query, handbook_scope):
rows = query.order_by(DouyuTask.id.desc()).limit(take).all()
task_ids.extend(int(row[0]) for row in rows)
@@ -401,6 +407,7 @@ def _list_visible_tasks_for_admin(
*,
batch_id: str | None,
handbook_scope: str | None,
account_scope_query: Select | None,
limit: int,
offset: int = 0,
) -> list[DouyuTask]:
@@ -410,6 +417,10 @@ def _list_visible_tasks_for_admin(
base_query = db.query(DouyuTask.id)
if batch_id:
base_query = base_query.filter(DouyuTask.batch_id == batch_id)
if account_scope_query is not None:
base_query = base_query.filter(
DouyuTask.account_id.in_(account_scope_query)
)
for query in _split_task_scope_query(base_query, handbook_scope):
rows = query.order_by(DouyuTask.id.desc()).limit(take).all()
task_ids.extend(int(row[0]) for row in rows)
@@ -442,6 +453,7 @@ def _count_visible_tasks_for_admin(
*,
batch_id: str | None,
handbook_scope: str | None,
account_scope_query: Select | None,
) -> int:
"""按作用域分支统计管理员任务数,避免 OR 条件走低效排序。"""
if handbook_scope:
@@ -460,6 +472,8 @@ def _count_visible_tasks_for_admin(
query = db.query(func.count(DouyuTask.id)).filter(*scope_filter)
if batch_id:
query = query.filter(DouyuTask.batch_id == batch_id)
if account_scope_query is not None:
query = query.filter(DouyuTask.account_id.in_(account_scope_query))
total += int(query.scalar() or 0)
return total
@@ -470,6 +484,7 @@ def _count_visible_tasks_for_support(
*,
batch_id: str | None,
handbook_scope: str | None,
account_scope_query: Select | None,
) -> int:
"""用索引分支精确计算归属/创建人并集的任务总数。"""
if handbook_scope:
@@ -489,6 +504,8 @@ def _count_visible_tasks_for_support(
query = db.query(func.count(DouyuTask.id)).filter(*owner_filter, *scope_filter)
if batch_id:
query = query.filter(DouyuTask.batch_id == batch_id)
if account_scope_query is not None:
query = query.filter(DouyuTask.account_id.in_(account_scope_query))
return int(query.scalar() or 0)
assigned_filter = (DouyuTask.account_assigned_to == current.id,)
@@ -631,6 +648,8 @@ def _order_task_accounts(
def _account_out(account: Account) -> DouyuTaskAccountOut:
elite_cooldown = account.role_bind_status == BIND_BOUND
esports_cooldown = account.esports_bind_status == BIND_BOUND
return DouyuTaskAccountOut(
id=account.id,
username=account.username,
@@ -645,14 +664,17 @@ def _account_out(account: Account) -> DouyuTaskAccountOut:
gold_balance=account.gold_balance,
exchange_balance=account.exchange_balance,
**bind_payload(account),
change_role_wait_time=account.change_role_wait_time,
# 未确认绑定时接口倒计时是占位值;API 出口也做一次归一,兼容历史行。
change_role_wait_time=account.change_role_wait_time if elite_cooldown else 0,
elite_opened=account.elite_opened,
elite_expire_time=account.elite_expire_time,
esports_points=account.esports_points,
esports_game_name=account.esports_game_name or "",
esports_game_channel=account.esports_game_channel or "",
esports_change_role_wait_time=account.esports_change_role_wait_time,
esports_can_change_time=account.esports_can_change_time,
esports_change_role_wait_time=(
account.esports_change_role_wait_time if esports_cooldown else 0
),
esports_can_change_time=account.esports_can_change_time if esports_cooldown else None,
xpd_game_name=account.xpd_game_name or "",
xpd_openid=account.xpd_openid or "",
xpd_role_id=account.xpd_role_id or "",
@@ -1441,6 +1463,7 @@ async def create_task_batch(
def list_tasks(
batch_id: str | None = None,
handbook_scope: str | None = Query(None, pattern="^(elite|esports|peace)$"),
workbench_only: bool = False,
include_detail: bool = Query(
False, description="是否返回完整任务结果(默认否,轮询请保持 false)"
),
@@ -1452,12 +1475,24 @@ def list_tasks(
"""查看斗鱼任务记录,默认只返回最近 100 条。"""
total = None
offset = (page - 1) * page_size if page is not None else 0
account_scope_query: Select | None = None
if workbench_only:
if not handbook_scope:
raise HTTPException(status_code=400, detail="工作台任务查询必须指定手册类型")
account_scope_query = (
select(DouyuWorkbenchAccount.account_id)
.where(
DouyuWorkbenchAccount.user_id == current.id,
DouyuWorkbenchAccount.handbook_scope == handbook_scope,
)
)
if _can_view_all(current):
tasks = _list_visible_tasks_for_admin(
db,
batch_id=batch_id,
handbook_scope=handbook_scope,
account_scope_query=account_scope_query,
limit=page_size,
offset=offset,
)
@@ -1466,6 +1501,7 @@ def list_tasks(
db,
batch_id=batch_id,
handbook_scope=handbook_scope,
account_scope_query=account_scope_query,
)
else:
if not user_has_permission(current, "account:view_assigned"):
@@ -1475,6 +1511,7 @@ def list_tasks(
current,
batch_id=batch_id,
handbook_scope=handbook_scope,
account_scope_query=account_scope_query,
limit=page_size,
offset=offset,
)
@@ -1484,6 +1521,7 @@ def list_tasks(
current,
batch_id=batch_id,
handbook_scope=handbook_scope,
account_scope_query=account_scope_query,
)
result = [_task_out(task, include_detail=include_detail) for task in tasks]