refactor(douyu): 统一账号准入与本地查询链路
This commit is contained in:
@@ -10,39 +10,53 @@ from __future__ import annotations
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import exists, or_, select
|
||||
from sqlalchemy import or_, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ....models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountLease,
|
||||
AutomationJob,
|
||||
PlatformFulfillmentSetting,
|
||||
PlatformFulfillmentTask,
|
||||
)
|
||||
from ...account_lifecycle import MANUAL_STATES
|
||||
from ...account_query_filters import (
|
||||
active_collab_order_exists,
|
||||
active_lease_exists,
|
||||
assigned_to_pool_filter,
|
||||
automatable_account_filter,
|
||||
cookie_present_filter,
|
||||
role_change_ready_filter,
|
||||
valid_ck_filter,
|
||||
)
|
||||
from ...account_sync_runner import WORK_STOREHOME_SYNC
|
||||
from ...account_work_queue import enqueue_account_work
|
||||
from ...douyu_service import cookie_operable_filter
|
||||
|
||||
JOB_KEY = "account_capability_refresh"
|
||||
|
||||
|
||||
def _base_query(db: Session, now: datetime):
|
||||
pending_commodities = (
|
||||
pending_commodities_query = (
|
||||
select(PlatformFulfillmentTask.commodity_id)
|
||||
.where(
|
||||
PlatformFulfillmentTask.execution_status == "ready",
|
||||
PlatformFulfillmentTask.account_id.is_(None),
|
||||
PlatformFulfillmentTask.commodity_id != "",
|
||||
PlatformFulfillmentTask.target_platform == "douyu",
|
||||
PlatformFulfillmentTask.scope == "elite",
|
||||
)
|
||||
.distinct()
|
||||
)
|
||||
active_lease = exists(
|
||||
select(AccountLease.id).where(
|
||||
AccountLease.active_account_id == Account.id,
|
||||
)
|
||||
match_started_at = (
|
||||
db.query(PlatformFulfillmentSetting.order_match_started_at)
|
||||
.filter(PlatformFulfillmentSetting.id == 1)
|
||||
.scalar()
|
||||
)
|
||||
if match_started_at is not None:
|
||||
pending_commodities_query = pending_commodities_query.where(
|
||||
PlatformFulfillmentTask.paid_at >= match_started_at
|
||||
)
|
||||
pending_commodities = pending_commodities_query
|
||||
# JOIN 比相关 EXISTS 更容易使用商品能力索引。当前生产账号池约 3 万,
|
||||
# 原写法会按账号逐个探测商品状态,慢查询实测扫描 40 万行。
|
||||
return (
|
||||
@@ -61,11 +75,13 @@ def _base_query(db: Session, now: datetime):
|
||||
AccountCommodityState.next_check_at.is_(None),
|
||||
AccountCommodityState.next_check_at <= now,
|
||||
),
|
||||
cookie_operable_filter(),
|
||||
Account.auto_managed.is_(True),
|
||||
Account.status.notin_(MANUAL_STATES),
|
||||
Account.ck_check_status == "valid",
|
||||
~active_lease,
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
assigned_to_pool_filter(),
|
||||
role_change_ready_filter(now),
|
||||
~active_lease_exists(),
|
||||
~active_collab_order_exists(),
|
||||
)
|
||||
.distinct()
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user