diff --git a/web/backend/routers/douyu.py b/web/backend/routers/douyu.py index d573662..d128baa 100644 --- a/web/backend/routers/douyu.py +++ b/web/backend/routers/douyu.py @@ -350,6 +350,85 @@ def _list_visible_tasks_for_support( return [tasks_by_id[task_id] for task_id in page_ids if task_id in tasks_by_id] +def _list_visible_tasks_for_admin( + db: Session, + *, + batch_id: str | None, + handbook_scope: str | None, + limit: int, + offset: int = 0, +) -> list[DouyuTask]: + """管理员任务列表按作用域拆索引分支,避免 OR + JOIN 触发大排序。""" + if handbook_scope: + scope_filters: list[tuple[Any, ...]] = [ + (DouyuTask.handbook_scope == handbook_scope,), + ( + DouyuTask.handbook_scope == "legacy", + DouyuTask.task_type.in_(DOUYU_HANDBOOK_TASK_TYPES[handbook_scope]), + ), + ] + else: + scope_filters = [()] + + take = offset + limit + task_ids: list[int] = [] + for scope_filter in scope_filters: + query = db.query(DouyuTask.id).filter(*scope_filter) + if batch_id: + query = query.filter(DouyuTask.batch_id == batch_id) + rows = query.order_by(DouyuTask.id.desc()).limit(take).all() + task_ids.extend(int(row[0]) for row in rows) + + page_ids = sorted(set(task_ids), reverse=True)[offset : offset + limit] + if not page_ids: + return [] + + account_loader = joinedload(DouyuTask.account).load_only( + Account.id, + Account.username, + Account.uid, + Account.nickname, + Account.assigned_to, + ) + tasks_by_id = { + task.id: task + for task in ( + db.query(DouyuTask) + .options(account_loader) + .filter(DouyuTask.id.in_(page_ids)) + .all() + ) + } + return [tasks_by_id[task_id] for task_id in page_ids if task_id in tasks_by_id] + + +def _count_visible_tasks_for_admin( + db: Session, + *, + batch_id: str | None, + handbook_scope: str | None, +) -> int: + """按作用域分支统计管理员任务数,避免 OR 条件走低效排序。""" + if handbook_scope: + scope_filters: list[tuple[Any, ...]] = [ + (DouyuTask.handbook_scope == handbook_scope,), + ( + DouyuTask.handbook_scope == "legacy", + DouyuTask.task_type.in_(DOUYU_HANDBOOK_TASK_TYPES[handbook_scope]), + ), + ] + else: + scope_filters = [()] + + total = 0 + for scope_filter in scope_filters: + query = db.query(func.count(DouyuTask.id)).filter(*scope_filter) + if batch_id: + query = query.filter(DouyuTask.batch_id == batch_id) + total += int(query.scalar() or 0) + return total + + def _count_visible_tasks_for_support( db: Session, current: User, @@ -1097,34 +1176,19 @@ def list_tasks( offset = (page - 1) * page_size if page is not None else 0 if _can_view_all(current): - # 管理员保留原有全量查询路径,避免改变管理端的可见范围。 - query = _visible_tasks_query(db, current) - if batch_id: - query = query.filter(DouyuTask.batch_id == batch_id) - if handbook_scope: - query = query.filter( - or_( - DouyuTask.handbook_scope == handbook_scope, - (DouyuTask.handbook_scope == "legacy") - & DouyuTask.task_type.in_( - DOUYU_HANDBOOK_TASK_TYPES[handbook_scope] - ), - ) - ) + tasks = _list_visible_tasks_for_admin( + db, + batch_id=batch_id, + handbook_scope=handbook_scope, + limit=page_size, + offset=offset, + ) if page is not None: - total = ( - query.enable_eagerloads(False) - .order_by(None) - .with_entities(func.count(DouyuTask.id)) - .scalar() - or 0 + total = _count_visible_tasks_for_admin( + db, + batch_id=batch_id, + handbook_scope=handbook_scope, ) - query = query.order_by(DouyuTask.id.desc()) - if page is not None: - query = query.offset(offset).limit(page_size) - else: - query = query.limit(page_size) - tasks = query.all() else: if not user_has_permission(current, "account:view_assigned"): raise HTTPException(status_code=403, detail="无权查看斗鱼任务")