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]
+10 -2
View File
@@ -532,12 +532,20 @@ def binding_snapshot(info: dict | None) -> dict[str, Any]:
data = info if isinstance(info, dict) else {}
role_name = _binding_role_name(data)
bound_status = derive_from_bind_info(data)
confirmed = bound_status == BIND_BOUND
phase = {
BIND_BOUND: "confirmed",
BIND_PENDING: "role_ready",
BIND_UNBOUND: "unbound",
}.get(bound_status or "", "unknown")
bound_act = _explicit_flag(data.get("is_bound_act")) is True
# 腾讯接口在未完成 bindAct 时会返回 changeRoleWaitTime=2592000 的占位值。
# 这不是冷却;未确认绑定必须归一为没有倒计时,原始值仍保留在 bind_info 供排查。
change_role_wait_time = _to_int(data.get("change_role_wait_time"))
can_change_time = _to_int(data.get("can_change_time"))
if not confirmed:
change_role_wait_time = 0
can_change_time = None
return {
"role_name": role_name,
"area_name": str(data.get("area_name") or "").strip(),
@@ -552,8 +560,8 @@ def binding_snapshot(info: dict | None) -> dict[str, Any]:
"is_bound_act": bound_act,
"need_bind_act": _truthy_flag(data.get("need_bind_act")),
"need_bind_role": _truthy_flag(data.get("need_bind_role")),
"change_role_wait_time": _to_int(data.get("change_role_wait_time")),
"can_change_time": _to_int(data.get("can_change_time")),
"change_role_wait_time": change_role_wait_time,
"can_change_time": can_change_time,
"can_change_role": can_change_role(data),
"is_change_cooling": is_role_change_cooling(data),
"bind_status": bound_status,
+3 -13
View File
@@ -2166,20 +2166,10 @@ class BindMixin:
f"查询换绑时间候选 act={item.get('act_alias') or '-'} "
f"{self._format_bind_summary(item)}",
)
# 换绑时间看“当前已绑定”活动态,不要优先 cjm(cjm 常无 changeRoleWaitTime)
action_aliases = self._action_act_aliases(config)
bind_info = next(
(
item
for alias in action_aliases
for item in raw_candidates
if str(item.get("act_alias") or "") == alias
and item.get("change_role_wait_time") is not None
),
None,
)
# 与其他绑定流程共用“当前已绑定优先”的候选选择。
# cjm 常没有 changeRoleWaitTime,不能靠原始字段是否存在来决定业务口径。
bind_info = (
bind_info
self._pick_change_wait_bind_info(raw_candidates, config)
or (
role.get("bind_info")
if isinstance(role.get("bind_info"), dict)