"""斗鱼「查询工作台」的数据状态维护。 查询工作台里的一个账号,数据来源必须能区分开: - ``imported``:刚导入,只做过 CK 检测;鱼翅/积分/宝典/换绑还是导入前的旧值; - ``queried``:已在本工作台跑过一键查询,界面展示的是本次查询结果; - ``stale``:重登成功或 CK 重新生效,账号数据已过期,需要按「重登的账号」单独再查一次。 写入口只有三处:导入(`update_workbench_accounts`)、查询成功(任务结束)、 人工重登(`relogin-invalid`)。读取入口是账号列表与 `pending-ids`。 """ from __future__ import annotations import logging from collections.abc import Iterable from datetime import UTC, datetime from sqlalchemy import or_ from sqlalchemy.orm import Session from ..models import Account, DouyuWorkbenchAccount logger = logging.getLogger(__name__) DATA_STATE_IMPORTED = "imported" DATA_STATE_QUERIED = "queried" DATA_STATE_STALE = "stale" DATA_STATES = (DATA_STATE_IMPORTED, DATA_STATE_QUERIED, DATA_STATE_STALE) QUERY_WORKBENCH_SCOPE = "query" def _normalized_ids(account_ids: Iterable[int]) -> list[int]: values: list[int] = [] for raw in account_ids: try: account_id = int(raw) except (TypeError, ValueError): continue if account_id > 0: values.append(account_id) return list(dict.fromkeys(values)) def _query_workbench_rows( db: Session, account_ids: list[int], *, user_id: int | None = None ): query = db.query(DouyuWorkbenchAccount).filter( DouyuWorkbenchAccount.handbook_scope == QUERY_WORKBENCH_SCOPE, DouyuWorkbenchAccount.account_id.in_(account_ids), ) if user_id is not None: query = query.filter(DouyuWorkbenchAccount.user_id == user_id) return query def mark_workbench_queried( db: Session, account_ids: Iterable[int], *, queried_at: datetime | None = None, commit: bool = True, ) -> int: """一键查询成功后把工作台行标记为「已查询」。 只更新已经在查询工作台里的账号;其它工作台(精英/电竞/小店)不参与。 """ ids = _normalized_ids(account_ids) if not ids: return 0 moment = queried_at or datetime.now(UTC) rows = _query_workbench_rows(db, ids).all() for row in rows: row.data_state = DATA_STATE_QUERIED row.queried_at = moment if rows and commit: db.commit() return len(rows) def mark_workbench_stale( db: Session, account_ids: Iterable[int], *, relogin_at: datetime | None = None, commit: bool = True, ) -> int: """人工重登后把工作台行标记为「待重新查询」。 重登可能失败(CK 仍失效),此时界面优先展示 CK 状态,`stale` 只在 CK 恢复 有效后才作为「重登成功、待重新查询」参与批量查询。 """ ids = _normalized_ids(account_ids) if not ids: return 0 moment = relogin_at or datetime.now(UTC) rows = _query_workbench_rows(db, ids).all() for row in rows: row.data_state = DATA_STATE_STALE row.relogin_at = moment if rows and commit: db.commit() return len(rows) def pending_query_ids( db: Session, *, user_id: int, visible_account_ids: Iterable[int] | None = None, ) -> dict[str, list[int] | int]: """返回本工作台里「需要查询」的账号:导入未查 + 重登待重查,且当前有 CK。 无 CK 的账号查了也会失败,交给重登流程,不进批量查询。 """ visible = _normalized_ids(visible_account_ids or []) query = ( db.query( DouyuWorkbenchAccount.account_id, DouyuWorkbenchAccount.data_state, Account.cookie, ) .join(Account, Account.id == DouyuWorkbenchAccount.account_id) .filter( DouyuWorkbenchAccount.user_id == user_id, DouyuWorkbenchAccount.handbook_scope == QUERY_WORKBENCH_SCOPE, DouyuWorkbenchAccount.data_state.in_( (DATA_STATE_IMPORTED, DATA_STATE_STALE) ), Account.cookie.isnot(None), Account.cookie != "", or_(Account.status.is_(None), Account.status != "invalid"), or_( Account.ck_check_status.is_(None), Account.ck_check_status.notin_(("invalid", "error")), ), ) ) if visible: query = query.filter(DouyuWorkbenchAccount.account_id.in_(visible)) imported_ids: list[int] = [] stale_ids: list[int] = [] for account_id, data_state, _cookie in query.order_by( DouyuWorkbenchAccount.sort_order.asc(), DouyuWorkbenchAccount.id.asc() ).all(): if data_state == DATA_STATE_STALE: stale_ids.append(int(account_id)) else: imported_ids.append(int(account_id)) return { "imported_ids": imported_ids, "stale_ids": stale_ids, "account_ids": imported_ids + stale_ids, "total": len(imported_ids) + len(stale_ids), } def workbench_state_map( db: Session, *, user_id: int, account_ids: Iterable[int], handbook_scope: str = QUERY_WORKBENCH_SCOPE, ) -> dict[int, DouyuWorkbenchAccount]: """批量读取工作台行状态,供账号列表回填数据来源标记。""" ids = _normalized_ids(account_ids) if not ids: return {} rows = ( db.query(DouyuWorkbenchAccount) .filter( DouyuWorkbenchAccount.user_id == user_id, DouyuWorkbenchAccount.handbook_scope == handbook_scope, DouyuWorkbenchAccount.account_id.in_(ids), ) .all() ) return {int(row.account_id): row for row in rows}