From d19809b4cab7de09226d9405064d7d8628e19d89 Mon Sep 17 00:00:00 2001 From: yml Date: Thu, 19 Nov 2026 22:57:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(douyu):=20=E4=BC=98=E5=8C=96=E4=BE=9B?= =?UTF-8?q?=E5=BA=94=E5=95=86=E7=9B=B4=E5=85=85=E4=BC=9A=E8=AF=9D=E4=B8=8E?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=97=B6=E9=97=B4=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_migrations.py | 2 +- ...61119_0099_douyu_tasks_created_at_index.py | 34 ++++++ web/backend/models.py | 4 +- .../src/pages/douyu-tasks/DouyuTasksPage.tsx | 107 +++++++++++++++++- web/frontend/src/pages/douyu-tasks/config.tsx | 11 ++ 5 files changed, 151 insertions(+), 7 deletions(-) create mode 100644 web/backend/migrations/versions/20261119_0099_douyu_tasks_created_at_index.py diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 91f9ee4..b70c81c 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -40,7 +40,7 @@ TESTS_DIR = Path(__file__).resolve().parent PROJECT_ROOT = TESTS_DIR.parent VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions" -HEAD_REVISION = "20261119_0098" +HEAD_REVISION = "20261119_0099" # 迁移新增、但不声明在模型里的复合查询索引。 EXTRA_INDEXES = ( diff --git a/web/backend/migrations/versions/20261119_0099_douyu_tasks_created_at_index.py b/web/backend/migrations/versions/20261119_0099_douyu_tasks_created_at_index.py new file mode 100644 index 0000000..fcd2a8f --- /dev/null +++ b/web/backend/migrations/versions/20261119_0099_douyu_tasks_created_at_index.py @@ -0,0 +1,34 @@ +"""add douyu_tasks.created_at index + +历史任务清理和按时间范围统计都以 created_at 为条件。表已接近 50 万行, +没有索引时清理作业需要扫描约 20 万行;单列索引即可覆盖这些低频但重扫描的查询。 +""" + +from __future__ import annotations + +import sqlalchemy as sa +from alembic import op + +revision: str = "20261119_0099" +down_revision: str | None = "20261119_0098" +branch_labels: str | None = None +depends_on: str | None = None + + +def upgrade() -> None: + inspector = sa.inspect(op.get_bind()) + indexes = {item["name"] for item in inspector.get_indexes("douyu_tasks")} + if "ix_douyu_tasks_created_at" not in indexes: + op.create_index( + "ix_douyu_tasks_created_at", + "douyu_tasks", + ["created_at"], + unique=False, + ) + + +def downgrade() -> None: + inspector = sa.inspect(op.get_bind()) + indexes = {item["name"] for item in inspector.get_indexes("douyu_tasks")} + if "ix_douyu_tasks_created_at" in indexes: + op.drop_index("ix_douyu_tasks_created_at", table_name="douyu_tasks") diff --git a/web/backend/models.py b/web/backend/models.py index 5220b28..36f3038 100644 --- a/web/backend/models.py +++ b/web/backend/models.py @@ -356,7 +356,9 @@ class DouyuTask(Base): message: Mapped[str] = mapped_column(String(512), default="") result: Mapped[dict | None] = mapped_column(JSON, nullable=True) created_by: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False) - created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=_utcnow) + created_at: Mapped[datetime] = mapped_column( + UTCDateTime, default=_utcnow, index=True + ) finished_at: Mapped[datetime | None] = mapped_column(UTCDateTime, nullable=True) platform_task_id: Mapped[int | None] = mapped_column( BigInteger, diff --git a/web/frontend/src/pages/douyu-tasks/DouyuTasksPage.tsx b/web/frontend/src/pages/douyu-tasks/DouyuTasksPage.tsx index 14c5f15..69cfdfa 100644 --- a/web/frontend/src/pages/douyu-tasks/DouyuTasksPage.tsx +++ b/web/frontend/src/pages/douyu-tasks/DouyuTasksPage.tsx @@ -54,6 +54,7 @@ import { hasBindQrcode, hasPaymentQrcode, paymentArrived, + isSupplierApiRechargeTask, bindPhaseText, goodsLabel, xpdGoodsLabel, @@ -84,6 +85,7 @@ interface DouyuPaySessionView { doneCount: number; errorCount: number; active: boolean; + supplierApi: boolean; } function parseAccountNames(text: string): string[] { @@ -785,6 +787,7 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind }) message.warning('该充值批次属于其他用户,请在自己的充值会话中操作'); return; } + if (isSupplierApiRechargeTask(ownerTask)) return; const ordered = [...batchTasks].sort((a, b) => a.id - b.id); addPaySession({ batchId, @@ -1000,7 +1003,10 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind }) }); // 只有本页面创建的批次才允许主动弹二维码,防止其他工作台/浏览器的任务打扰当前操作。 locallyStartedBatchIds.current.add(result.batch_id); - if (DOUYU_PAYMENT_TASK_TYPE_SET.has(taskType)) { + if ( + DOUYU_PAYMENT_TASK_TYPE_SET.has(taskType) + && !(taskType === 'create_gold_qr' && goldRechargeChannel === 'supplier_api') + ) { addPaySession({ batchId: result.batch_id, taskType, @@ -1901,6 +1907,10 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind }) for (const task of batchTasks.sort((a, b) => a.id - b.id)) { taskByAccount.set(task.account_id, task); } + const supplierApi = ( + batchTasks.length > 0 + && batchTasks.every((task) => isSupplierApiRechargeTask(task)) + ); const accountIds = [ ...session.accountIds, ...Array.from(taskByAccount.keys()).filter( @@ -2030,6 +2040,7 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind }) doneCount, errorCount, active, + supplierApi, }; }) ), [ @@ -2040,9 +2051,63 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind }) runningBatchIds, tasks, ]); - const activePaySessionView = paySessionViews.find( + const visiblePaySessionViews = useMemo( + () => paySessionViews.filter((view) => ( + !view.supplierApi + && !( + view.doneCount > 0 + && view.doneCount === view.session.accountIds.length + && view.items.length === 0 + ) + )), + [paySessionViews], + ); + useEffect(() => { + const removable = new Set( + paySessionViews + .filter((view) => ( + view.supplierApi + || ( + view.doneCount > 0 + && view.doneCount === view.session.accountIds.length + && view.items.length === 0 + ) + )) + .map((view) => view.session.batchId), + ); + if (removable.size === 0) return; + setPaySessionsSafely((sessions) => { + const next = sessions.filter( + (session) => !removable.has(session.batchId), + ); + return next.length === sessions.length ? sessions : next; + }); + setPaySessionTasks((current) => { + const next = { ...current }; + let changed = false; + removable.forEach((batchId) => { + if (!(batchId in next)) return; + delete next[batchId]; + changed = true; + }); + return changed ? next : current; + }); + setActivePayBatchId((current) => ( + current && removable.has(current) ? null : current + )); + }, [paySessionViews, setPaySessionsSafely]); + useEffect(() => { + if ( + payOpen + && paySessions.length > 0 + && visiblePaySessionViews.length === 0 + ) { + setPayOpen(false); + } + }, [payOpen, paySessions.length, visiblePaySessionViews.length]); + const activePaySessionView = visiblePaySessionViews.find( (view) => view.session.batchId === activePayBatchId, - ) || paySessionViews[0]; + ) || visiblePaySessionViews[0]; const payReadyCount = activePaySessionView?.readyCount || 0; const payQueuedCount = activePaySessionView?.queuedCount || 0; const payDoneCount = activePaySessionView?.doneCount || 0; @@ -2051,6 +2116,32 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind }) activePaySessionView && activePaySessionView.session.ownerUserId === user?.id, ); + const historicalPaySessionIds = useMemo( + () => paySessionViews + .filter((view) => ( + !view.active + && !view.items.some((item) => item.phase === 'ready') + )) + .map((view) => view.session.batchId), + [paySessionViews], + ); + const clearHistoricalPaySessions = () => { + if (historicalPaySessionIds.length === 0) return; + const remove = new Set(historicalPaySessionIds); + const remaining = paySessionsRef.current.filter( + (session) => !remove.has(session.batchId), + ); + setPaySessionsSafely((sessions) => sessions.filter( + (session) => !remove.has(session.batchId), + )); + setPaySessionTasks((current) => { + const next = { ...current }; + remove.forEach((batchId) => delete next[batchId]); + return next; + }); + setActivePayBatchId(remaining[0]?.batchId || null); + if (remaining.length === 0) setPayOpen(false); + }; const zoomPayUrl = taskQrUrl(zoomPayTask); const copyPaymentLink = async (value: string) => { if (!value) { @@ -3024,6 +3115,12 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind }) )} + @@ -3032,14 +3129,14 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind }) centered width={920} > - {paySessionViews.length > 0 ? ( + {visiblePaySessionViews.length > 0 ? ( { setActivePayBatchId(batchId); void loadPaySessionTasks([batchId]); }} - items={paySessionViews.map((view) => ({ + items={visiblePaySessionViews.map((view) => ({ key: view.session.batchId, label: ( diff --git a/web/frontend/src/pages/douyu-tasks/config.tsx b/web/frontend/src/pages/douyu-tasks/config.tsx index 8e07eaa..4be78d2 100644 --- a/web/frontend/src/pages/douyu-tasks/config.tsx +++ b/web/frontend/src/pages/douyu-tasks/config.tsx @@ -208,8 +208,19 @@ export function hasPaymentQrcode(task: DouyuTaskItem | null | undefined): boolea ); } +export function isSupplierApiRechargeTask( + task: DouyuTaskItem | null | undefined, +): boolean { + return Boolean( + task + && task.task_type === 'create_gold_qr' + && resultText(task.result, 'recharge_channel') === 'supplier_api', + ); +} + export function paymentArrived(task: DouyuTaskItem | null | undefined): boolean { if (!task || !DOUYU_PAYMENT_TASK_TYPE_SET.has(task.task_type)) return false; + if (isSupplierApiRechargeTask(task) && task.status === 'success') return true; if (task.status !== 'success') return false; return task.result?.gold_recharged === true || task.result?.elite_opened === true