refactor(douyu): 统一账号准入与本地查询链路

This commit is contained in:
yml
2026-11-22 19:31:32 +08:00
parent e652d30283
commit d78437c6ac
14 changed files with 592 additions and 548 deletions
@@ -701,3 +701,4 @@ Runner 启动前必须有预算 token;没有 token 时留在工作队列。
| 2026-11-21 | 阶段 0-4 完成:全部账号查询域接入本地优先策略、独立同步工作项和兑换记录统一投影;阶段 5 暂不启动 | | 2026-11-21 | 阶段 0-4 完成:全部账号查询域接入本地优先策略、独立同步工作项和兑换记录统一投影;阶段 5 暂不启动 |
| 2026-11-22 | 履约租约改为状态化策略:默认无限,预售/人工独立配置,完成或取消后才设置释放时间 | | 2026-11-22 | 履约租约改为状态化策略:默认无限,预售/人工独立配置,完成或取消后才设置释放时间 |
| 2026-11-22 | 按生产并发、CK 流失和历史领取峰值提高全局供给目标,并安全升级旧默认组合 | | 2026-11-22 | 按生产并发、CK 流失和历史领取峰值提高全局供给目标,并安全升级旧默认组合 |
| 2026-11-22 | 抽取统一账号准入过滤;外部协作推荐改为纯本地能力池,兑换记录统一走本地同步投影 |
+9 -5
View File
@@ -152,12 +152,16 @@
1. **解除账号**:只解除协作单与内部账号的关联,不删除内部账号。账号身份、 1. **解除账号**:只解除协作单与内部账号的关联,不删除内部账号。账号身份、
角色、鱼翅和积分快照会被清空,订单回到「待补资料」;若账号仍被履约任务或 角色、鱼翅和积分快照会被清空,订单回到「待补资料」;若账号仍被履约任务或
其他活动协作单占用,其租约不会被释放。 其他活动协作单占用,其租约不会被释放。
2. **推荐账号**:复用履约候选池的硬条件,实时检查 CK、有效租约、活动协作单、 2. **推荐账号**:复用统一账号准入过滤和本地商品能力投影,检查 CK 结论、
角色换绑冷却、商品限兑和 365 天兑换记录。默认未绑定账号优先,可选择是否 有效租约、活动协作单、号池归属、换绑冷却和 365 天兑换记录。推荐阶段
纳入已经过了换绑冷却的账号。 不请求斗鱼官网;本地没有商品能力时会进入独立能力刷新队列。默认未绑定
账号优先,可选择是否纳入已经过了换绑冷却的账号。
推荐结果不会预占账号。运营确认候选后,仍调用原有账号绑定接口重新校验并在同一 推荐结果不会预占账号。运营确认候选后,仍调用原有账号绑定接口实时校验 CK、
事务内建立协作租约;如果候选在推荐后被其他任务占用,接口会拒绝并要求重新推荐。 角色和商品资格,并在同一事务内建立协作租约;如果候选在推荐后被其他任务占用,
接口会拒绝并要求重新推荐。协作单查询兑换记录也统一走
`account_exchange_sync_service`,先更新 `AccountSyncState` 投影,再基于同一份
本地记录做订单匹配,不再单独实现一套分页和判定查询。
客服没有解除或推荐账号的入口和 API 权限。 客服没有解除或推荐账号的入口和 API 权限。
## 环境 ## 环境
@@ -10,6 +10,7 @@ from web.backend.models import (
AccountLease, AccountLease,
AccountWorkItem, AccountWorkItem,
AutomationJob, AutomationJob,
PlatformFulfillmentSetting,
PlatformFulfillmentTask, PlatformFulfillmentTask,
User, User,
) )
@@ -125,6 +126,68 @@ def test_no_pending_task_means_no_refresh(db):
assert job.build_select_query(db, automation_job, NOW).all() == [] assert job.build_select_query(db, automation_job, NOW).all() == []
def test_refresh_ignores_tasks_before_match_cutoff(db):
db.add(
PlatformFulfillmentSetting(
id=1,
lease_duration_minutes=None,
order_match_started_at=NOW - timedelta(days=1),
)
)
db.add_all(
[
PlatformFulfillmentTask(
source_system="order_site",
source_order_id="old",
source_order_item_id="old",
internal_sku_key="old-skin",
commodity_id="OLD",
execution_status="ready",
binding_status="not_started",
exchange_status="not_started",
paid_at=NOW - timedelta(days=2),
),
PlatformFulfillmentTask(
source_system="order_site",
source_order_id="new",
source_order_item_id="new",
internal_sku_key="new-skin",
commodity_id="NEW",
execution_status="ready",
binding_status="not_started",
exchange_status="not_started",
paid_at=NOW,
),
]
)
old_account = _account(db, "cap-cutoff-old")
new_account = _account(db, "cap-cutoff-new")
_state(
db,
old_account,
commodity_id="OLD",
valid_until=NOW - timedelta(minutes=1),
)
_state(
db,
new_account,
commodity_id="NEW",
valid_until=NOW - timedelta(minutes=1),
)
db.commit()
automation_job = AutomationJob(
key=job.JOB_KEY,
title="履约商品能力补充刷新",
interval_seconds=3600,
batch_size=10,
concurrency=3,
)
selected = job.build_select_query(db, automation_job, NOW).all()
assert [row.id for row in selected] == [new_account.id]
def test_run_enqueues_stale_accounts_without_external_query(db): def test_run_enqueues_stale_accounts_without_external_query(db):
db.add( db.add(
PlatformFulfillmentTask( PlatformFulfillmentTask(
+36
View File
@@ -118,6 +118,42 @@ def test_supply_inventory_separates_available_and_allocatable(db):
assert inventory.needs_replenishment is True assert inventory.needs_replenishment is True
def test_supply_allocatable_uses_the_same_pool_owner_filter_as_selection(db):
customer = User(
username="supply-customer",
password_hash="h",
role="customer",
is_active=True,
)
free = _account("supply-pool-free", points=400)
customer_assigned = _account("supply-customer-assigned", points=400)
db.add(customer)
db.flush()
customer_assigned.assigned_to = customer.id
db.add_all([free, customer_assigned])
db.flush()
db.add_all([_state(free), _state(customer_assigned)])
db.add(
AccountSupplyTarget(
platform="douyu",
scope="elite",
commodity_id="C",
value_class=VALUE_HIGH,
target_covered=2,
target_available=2,
target_allocatable=2,
)
)
db.commit()
target = db.query(AccountSupplyTarget).one()
inventory = evaluate_supply_target(db, target, now=NOW)
assert inventory.covered == 2
assert inventory.available == 2
assert inventory.allocatable == 1
def test_global_supply_inventory_batches_value_classes_in_two_queries(db): def test_global_supply_inventory_batches_value_classes_in_two_queries(db):
user = User( user = User(
username="global-supply-owner", username="global-supply-owner",
+50 -21
View File
@@ -7,8 +7,10 @@ import pytest
from tests.support.db import get_test_engine, make_test_session from tests.support.db import get_test_engine, make_test_session
from web.backend.models import ( from web.backend.models import (
Account, Account,
AccountCommodityState,
AccountEvent, AccountEvent,
AccountLease, AccountLease,
AccountSyncState,
AuditLog, AuditLog,
DouyuTask, DouyuTask,
PlatformCommodityMapping, PlatformCommodityMapping,
@@ -116,19 +118,6 @@ class FakeNoBindClient(FakeClient):
raise RuntimeError("未绑定角色") raise RuntimeError("未绑定角色")
class FakeUnboundClient(FakeClient):
def bind_info(self, **_):
return {
"is_bound_account": False,
"is_bound_role": False,
"is_bound_act": False,
"role_name": "",
"area_name": "",
"plat_name": "",
"nickname": "",
}
class TestDouyuCollabOrders: class TestDouyuCollabOrders:
def setup_method(self): def setup_method(self):
self.engine = get_test_engine() self.engine = get_test_engine()
@@ -378,7 +367,17 @@ class TestDouyuCollabOrders:
assert order.account_id is None assert order.account_id is None
assert order.status == collab.STATUS_INCOMPLETE assert order.status == collab.STATUS_INCOMPLETE
def test_recommend_accounts_prefers_unbound_and_can_exclude_rebindable(self): def test_recommend_accounts_prefers_unbound_and_can_exclude_rebindable(
self,
monkeypatch,
):
monkeypatch.setattr(
collab,
"probe_account",
lambda *_args, **_kwargs: (_ for _ in ()).throw(
AssertionError("本地推荐不应访问斗鱼官网")
),
)
mapping = PlatformCommodityMapping( mapping = PlatformCommodityMapping(
target_platform="douyu", target_platform="douyu",
internal_sku_key="推荐礼包", internal_sku_key="推荐礼包",
@@ -405,9 +404,12 @@ class TestDouyuCollabOrders:
email_password="mail-password", email_password="mail-password",
cookie="acf_uid=200; acf_nickname=bound", cookie="acf_uid=200; acf_nickname=bound",
status="active", status="active",
ck_check_status="valid",
auto_managed=True, auto_managed=True,
elite_opened=True, elite_opened=True,
points=1000, points=1000,
role_bind_status="bound",
game_name="角色B",
) )
empty = Account( empty = Account(
username="douyu-account-empty", username="douyu-account-empty",
@@ -416,18 +418,37 @@ class TestDouyuCollabOrders:
email_password="mail-password", email_password="mail-password",
cookie="acf_uid=300; acf_nickname=empty", cookie="acf_uid=300; acf_nickname=empty",
status="active", status="active",
ck_check_status="valid",
auto_managed=True, auto_managed=True,
elite_opened=True, elite_opened=True,
points=500, points=500,
role_bind_status="unbound", role_bind_status="unbound",
) )
self.session.add_all([bound, empty]) self.session.add_all([bound, empty])
self.session.flush()
self.session.add_all(
[
AccountCommodityState(
account_id=bound.id,
platform="douyu",
scope="elite",
commodity_id="gift-a",
availability="available",
observed_at=datetime.now(UTC),
valid_until=datetime.now(UTC) + timedelta(hours=1),
),
AccountCommodityState(
account_id=empty.id,
platform="douyu",
scope="elite",
commodity_id="gift-a",
availability="available",
observed_at=datetime.now(UTC),
valid_until=datetime.now(UTC) + timedelta(hours=1),
),
]
)
self.session.commit() self.session.commit()
clients = {
int(self.account.id): FakeClient(),
int(bound.id): FakeClient(),
int(empty.id): FakeUnboundClient(),
}
result = collab.recommend_accounts( result = collab.recommend_accounts(
self.session, self.session,
@@ -435,7 +456,6 @@ class TestDouyuCollabOrders:
order, order,
include_rebindable=True, include_rebindable=True,
limit=5, limit=5,
client_factory=lambda account: clients[int(account.id)],
) )
assert order.commodity_id == "gift-a" assert order.commodity_id == "gift-a"
@@ -450,7 +470,6 @@ class TestDouyuCollabOrders:
order, order,
include_rebindable=False, include_rebindable=False,
limit=5, limit=5,
client_factory=lambda account: clients[int(account.id)],
) )
assert [item["account_id"] for item in unbound_only["candidates"]] == [ assert [item["account_id"] for item in unbound_only["candidates"]] == [
empty.id empty.id
@@ -1611,6 +1630,16 @@ class TestDouyuCollabOrders:
assert client.offsets == [0, 100] assert client.offsets == [0, 100]
assert len(loaded) == 101 assert len(loaded) == 101
sync_state = (
self.session.query(AccountSyncState)
.filter_by(
account_id=order.account_id,
platform="douyu",
domain="exchange_records",
)
.one()
)
assert len(sync_state.raw_payload_json["elite"]) == 101
def test_record_query_rejects_old_record_and_wrong_commodity(self): def test_record_query_rejects_old_record_and_wrong_commodity(self):
order = collab.create_order( order = collab.create_order(
@@ -252,6 +252,47 @@ def classify_goods_items(
return assessments return assessments
def exchange_gate_for_availability(
availability: str | None,
*,
reason: str = "",
limited: bool = False,
allow_not_open: bool = False,
) -> tuple[str, str, str]:
"""把统一能力状态映射为履约兑换门槛,避免各入口重复维护状态表。"""
if limited:
return (
"limited",
"commodity_limit_reached",
reason or "该账号已达到此商品兑换上限",
)
if availability == AVAILABILITY_AVAILABLE:
return "", "", ""
if allow_not_open and availability == AVAILABILITY_NOT_OPEN:
return (
"waiting_release",
"commodity_not_open",
reason or "商品限时兑换尚未开始,可提前绑定角色",
)
issue_map = {
None: ("commodity_not_offered", "官方商品列表本次未返回该商品,可能已下架"),
AVAILABILITY_NOT_OPEN: ("commodity_not_open", "商品限时兑换尚未开始"),
AVAILABILITY_EXPIRED: ("commodity_expired", "商品限时兑换已经结束"),
AVAILABILITY_NO_STOCK: ("commodity_no_stock", "商品当前无库存"),
AVAILABILITY_NOT_OFFERED: ("commodity_not_offered", "商品当前未上架"),
AVAILABILITY_UNKNOWN: ("commodity_status_unknown", "商品资格状态未知"),
AVAILABILITY_LIMITED: (
"commodity_limit_reached",
"该账号已达到此商品兑换上限",
),
}
issue_code, fallback = issue_map.get(
availability,
("commodity_not_exchangeable", "商品当前不可兑换"),
)
return "not_available", issue_code, reason or fallback
def _exchange_stats( def _exchange_stats(
db: Session, db: Session,
*, *,
@@ -642,6 +683,7 @@ __all__ = [
"capability_ttl_seconds", "capability_ttl_seconds",
"classify_goods_item", "classify_goods_item",
"classify_goods_items", "classify_goods_items",
"exchange_gate_for_availability",
"goods_time_window", "goods_time_window",
"invalidate_account_commodity_states", "invalidate_account_commodity_states",
"mark_commodity_exchanged", "mark_commodity_exchanged",
@@ -105,10 +105,14 @@ def fetch_exchange_records(
config: dict[str, Any], config: dict[str, Any],
*, *,
limit: int = EXCHANGE_RECORD_FETCH_LIMIT, limit: int = EXCHANGE_RECORD_FETCH_LIMIT,
scopes: tuple[str, ...] | None = None,
max_pages: int = 1,
) -> tuple[list[tuple[str, dict[str, Any]]], dict[str, str]]: ) -> tuple[list[tuple[str, dict[str, Any]]], dict[str, str]]:
"""拉取已配置作用域的官方兑换记录,单项失败不阻断其他作用域。""" """拉取已配置作用域的官方兑换记录,单项失败不阻断其他作用域。"""
pairs: list[tuple[str, dict[str, Any]]] = [] pairs: list[tuple[str, dict[str, Any]]] = []
errors: dict[str, str] = {} errors: dict[str, str] = {}
requested_scopes = set(scopes or ("elite", "esports", "peace"))
page_limit = max(1, min(int(max_pages), 10))
manual_id = str(config.get("manual_id") or "").strip() manual_id = str(config.get("manual_id") or "").strip()
esports_manual_id = str(config.get("esports_manual_id") or "").strip() esports_manual_id = str(config.get("esports_manual_id") or "").strip()
configured = False configured = False
@@ -118,15 +122,23 @@ def fetch_exchange_records(
("elite", "精英宝典", manual_id), ("elite", "精英宝典", manual_id),
("esports", "电竞手册", esports_manual_id), ("esports", "电竞手册", esports_manual_id),
): ):
if scope not in requested_scopes:
continue
if not selected_manual_id: if not selected_manual_id:
continue continue
configured = True configured = True
try: try:
rows = query_exchange_records( rows: list[dict[str, Any]] = []
client, for page in range(page_limit):
manual_id=selected_manual_id, page_rows = query_exchange_records(
limit=limit, client,
) manual_id=selected_manual_id,
limit=limit,
offset=page * limit,
)
rows.extend(page_rows)
if len(page_rows) < limit:
break
except Exception as exc: # noqa: BLE001 - 单个外部查询域独立失败 except Exception as exc: # noqa: BLE001 - 单个外部查询域独立失败
errors[f"exchange_records_{scope}"] = ( errors[f"exchange_records_{scope}"] = (
f"{type(exc).__name__}: {exc}"[:200] f"{type(exc).__name__}: {exc}"[:200]
@@ -135,7 +147,7 @@ def fetch_exchange_records(
successful += 1 successful += 1
pairs.extend((scope, dict(row)) for row in rows if isinstance(row, dict)) pairs.extend((scope, dict(row)) for row in rows if isinstance(row, dict))
if bool(config.get("peace_enabled")): if "peace" in requested_scopes and bool(config.get("peace_enabled")):
xpd_act_alias = str(config.get("xpd_act_alias") or "").strip() xpd_act_alias = str(config.get("xpd_act_alias") or "").strip()
xpd_rid = str(config.get("xpd_rid") or "").strip() xpd_rid = str(config.get("xpd_rid") or "").strip()
xpd_act_id = str(config.get("xpd_act_id") or "").strip() xpd_act_id = str(config.get("xpd_act_id") or "").strip()
@@ -178,10 +190,19 @@ def sync_exchange_records(
*, *,
source: str, source: str,
task_id: int | None = None, task_id: int | None = None,
scopes: tuple[str, ...] | None = None,
limit: int = EXCHANGE_RECORD_FETCH_LIMIT,
max_pages: int = 1,
) -> dict[str, Any]: ) -> dict[str, Any]:
"""同步官方记录、补事件并保存可回放的按作用域原始投影。""" """同步官方记录、补事件并保存可回放的按作用域原始投影。"""
observed_at = datetime.now(UTC) observed_at = datetime.now(UTC)
pairs, errors = fetch_exchange_records(client, config) pairs, errors = fetch_exchange_records(
client,
config,
limit=limit,
scopes=scopes,
max_pages=max_pages,
)
changed = persist_official_exchange_records(db, account, pairs) changed = persist_official_exchange_records(db, account, pairs)
previous = get_sync_state( previous = get_sync_state(
db, db,
@@ -26,6 +26,7 @@ from ..models import (
DouyuExternalOrder, DouyuExternalOrder,
PlatformFulfillmentTask, PlatformFulfillmentTask,
) )
from .account_query_filters import ACTIVE_COLLAB_STATUSES
CK_HEALTH_JOB_KEY = "ck_health" CK_HEALTH_JOB_KEY = "ck_health"
PROTECTION_CONFIG_KEY = "protection_groups" PROTECTION_CONFIG_KEY = "protection_groups"
@@ -35,17 +36,7 @@ MIN_PROBE_INTERVAL_MINUTES = 15
MAX_PROBE_INTERVAL_MINUTES = 24 * 60 MAX_PROBE_INTERVAL_MINUTES = 24 * 60
MAX_TARGET_COUNT = 100_000 MAX_TARGET_COUNT = 100_000
# 与 douyu_collab_service.COLLAB_LEASE_STATUSES 保持一致。这里只放状态值, COLLAB_ACTIVE_STATUSES = ACTIVE_COLLAB_STATUSES
# 避免服务层为了两个常量导入整个协作业务模块。
COLLAB_ACTIVE_STATUSES = (
"incomplete",
"to_recharge",
"recharged",
"scripted",
"points_error",
"role_error",
"failed",
)
GROUP_DEFINITIONS: dict[str, dict[str, Any]] = { GROUP_DEFINITIONS: dict[str, dict[str, Any]] = {
"presale_bound": { "presale_bound": {
@@ -0,0 +1,160 @@
"""账号池、租约和商品能力的共享 SQL 准入条件。"""
from __future__ import annotations
from datetime import datetime, timedelta
from sqlalchemy import and_, or_, select
from ..models import (
Account,
AccountCommodityState,
AccountLease,
DouyuExternalOrder,
User,
)
from .account_capability_service import (
AVAILABILITY_AVAILABLE,
AVAILABILITY_NOT_OPEN,
)
from .account_lifecycle import MANUAL_STATES
ACTIVE_COLLAB_STATUSES: tuple[str, ...] = (
"incomplete",
"to_recharge",
"recharged",
"scripted",
"points_error",
"role_error",
"failed",
)
POOL_OWNER_ROLES: tuple[str, ...] = ("super_admin", "operation", "support")
CK_FRESH_SECONDS = 24 * 60 * 60
def pool_owner_ids():
return select(User.id).where(
User.role.in_(POOL_OWNER_ROLES),
User.is_active.is_(True),
)
def cookie_present_filter():
return and_(Account.cookie.isnot(None), Account.cookie != "")
def valid_ck_filter(*, checked_since: datetime | None = None):
filters = [Account.ck_check_status == "valid"]
if checked_since is not None:
filters.extend(
[
Account.ck_checked_at.isnot(None),
Account.ck_checked_at >= checked_since,
]
)
return and_(*filters)
def automatable_account_filter():
return and_(
Account.auto_managed.is_(True),
Account.status.notin_(MANUAL_STATES),
)
def assigned_to_pool_filter():
return or_(
Account.assigned_to.is_(None),
Account.assigned_to.in_(pool_owner_ids()),
)
def active_lease_exists():
"""命中生成列唯一索引;有活动租约的账号不能被业务再次占用。"""
return (
select(AccountLease.id)
.where(AccountLease.active_account_id == Account.id)
.exists()
)
def active_collab_order_exists():
return (
select(DouyuExternalOrder.id)
.where(
DouyuExternalOrder.account_id == Account.id,
DouyuExternalOrder.archived_at.is_(None),
DouyuExternalOrder.status.in_(ACTIVE_COLLAB_STATUSES),
)
.exists()
)
def role_change_ready_filter(now: datetime):
return or_(
Account.role_change_available_at.is_(None),
Account.role_change_available_at <= now,
)
def fresh_ck_cutoff(now: datetime) -> datetime:
return now - timedelta(seconds=CK_FRESH_SECONDS)
def fresh_available_state_filter(
now: datetime,
*,
allow_not_open: bool = False,
):
allowed = (
(AVAILABILITY_AVAILABLE, AVAILABILITY_NOT_OPEN)
if allow_not_open
else (AVAILABILITY_AVAILABLE,)
)
available_window = and_(
AccountCommodityState.availability == AVAILABILITY_AVAILABLE,
or_(
AccountCommodityState.available_from.is_(None),
AccountCommodityState.available_from <= now,
),
or_(
AccountCommodityState.available_until.is_(None),
AccountCommodityState.available_until >= now,
),
)
if not allow_not_open:
return and_(
AccountCommodityState.availability.in_(allowed),
AccountCommodityState.valid_until.isnot(None),
AccountCommodityState.valid_until > now,
available_window,
)
not_open_window = and_(
AccountCommodityState.availability == AVAILABILITY_NOT_OPEN,
or_(
AccountCommodityState.available_until.is_(None),
AccountCommodityState.available_until >= now,
),
)
return and_(
AccountCommodityState.valid_until.isnot(None),
AccountCommodityState.valid_until > now,
or_(available_window, not_open_window),
)
__all__ = [
"ACTIVE_COLLAB_STATUSES",
"CK_FRESH_SECONDS",
"POOL_OWNER_ROLES",
"active_collab_order_exists",
"active_lease_exists",
"assigned_to_pool_filter",
"automatable_account_filter",
"cookie_present_filter",
"fresh_available_state_filter",
"fresh_ck_cutoff",
"pool_owner_ids",
"role_change_ready_filter",
"valid_ck_filter",
]
+21 -67
View File
@@ -4,22 +4,29 @@ from __future__ import annotations
import os import os
from dataclasses import dataclass from dataclasses import dataclass
from datetime import UTC, datetime, timedelta from datetime import UTC, datetime
from sqlalchemy import and_, case, func, or_, select from sqlalchemy import and_, case, func, select
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from ..models import ( from ..models import (
Account, Account,
AccountCommodityState, AccountCommodityState,
AccountLease,
AccountSupplyTarget, AccountSupplyTarget,
AccountSyncState, AccountSyncState,
DouyuExternalOrder,
) )
from .account_capability_service import AVAILABILITY_AVAILABLE from .account_query_filters import (
from .account_lifecycle import MANUAL_STATES active_collab_order_exists,
active_lease_exists,
assigned_to_pool_filter,
automatable_account_filter,
cookie_present_filter,
fresh_available_state_filter,
fresh_ck_cutoff,
role_change_ready_filter,
valid_ck_filter,
)
from .account_sync_runner import WORK_STOREHOME_SYNC from .account_sync_runner import WORK_STOREHOME_SYNC
from .account_value import is_high_value_expr from .account_value import is_high_value_expr
from .account_work_queue import enqueue_account_work from .account_work_queue import enqueue_account_work
@@ -27,16 +34,6 @@ from .account_work_queue import enqueue_account_work
VALUE_HIGH = "high_value" VALUE_HIGH = "high_value"
VALUE_ORDINARY = "ordinary" VALUE_ORDINARY = "ordinary"
VALID_VALUES = frozenset({VALUE_HIGH, VALUE_ORDINARY}) VALID_VALUES = frozenset({VALUE_HIGH, VALUE_ORDINARY})
CK_FRESH_SECONDS = 24 * 3600
ACTIVE_COLLAB_STATUSES = (
"incomplete",
"to_recharge",
"recharged",
"scripted",
"points_error",
"role_error",
"failed",
)
DEFAULT_SUPPLY_TARGETS = ( DEFAULT_SUPPLY_TARGETS = (
{ {
"scope": "elite", "scope": "elite",
@@ -92,30 +89,6 @@ class SupplyReplenishmentResult:
shortages: int shortages: int
def _active_lease_exists():
return (
select(AccountLease.id)
.where(
# active_account_id 是生成列,只在 status='active' 时有值。
# 直接命中唯一索引,避免再按 account_id 扫历史租约。
AccountLease.active_account_id == Account.id,
)
.exists()
)
def _active_collab_exists():
return (
select(DouyuExternalOrder.id)
.where(
DouyuExternalOrder.account_id == Account.id,
DouyuExternalOrder.archived_at.is_(None),
DouyuExternalOrder.status.in_(ACTIVE_COLLAB_STATUSES),
)
.exists()
)
def _value_filter(value_class: str, *, now: datetime): def _value_filter(value_class: str, *, now: datetime):
high_value = is_high_value_expr(now=now) high_value = is_high_value_expr(now=now)
if value_class == VALUE_HIGH: if value_class == VALUE_HIGH:
@@ -126,15 +99,10 @@ def _value_filter(value_class: str, *, now: datetime):
def _base_account_filter(*, now: datetime): def _base_account_filter(*, now: datetime):
ck_fresh_cutoff = now - timedelta(seconds=CK_FRESH_SECONDS)
return and_( return and_(
Account.cookie.isnot(None), cookie_present_filter(),
Account.cookie != "", valid_ck_filter(checked_since=fresh_ck_cutoff(now)),
Account.ck_check_status == "valid", automatable_account_filter(),
Account.ck_checked_at.isnot(None),
Account.ck_checked_at >= ck_fresh_cutoff,
Account.auto_managed.is_(True),
Account.status.notin_(MANUAL_STATES),
) )
@@ -153,29 +121,15 @@ def _base_account_query(
def _allocatable_filter(now: datetime): def _allocatable_filter(now: datetime):
return and_( return and_(
~_active_lease_exists(), ~active_lease_exists(),
~_active_collab_exists(), ~active_collab_order_exists(),
or_( assigned_to_pool_filter(),
Account.role_change_available_at.is_(None), role_change_ready_filter(now),
Account.role_change_available_at <= now,
),
) )
def _fresh_available_filter(now: datetime): def _fresh_available_filter(now: datetime):
return and_( return fresh_available_state_filter(now)
AccountCommodityState.availability == AVAILABILITY_AVAILABLE,
AccountCommodityState.valid_until.isnot(None),
AccountCommodityState.valid_until > now,
or_(
AccountCommodityState.available_from.is_(None),
AccountCommodityState.available_from <= now,
),
or_(
AccountCommodityState.available_until.is_(None),
AccountCommodityState.available_until >= now,
),
)
def _commodity_query( def _commodity_query(
@@ -10,39 +10,53 @@ from __future__ import annotations
from datetime import UTC, datetime from datetime import UTC, datetime
from typing import Any from typing import Any
from sqlalchemy import exists, or_, select from sqlalchemy import or_, select
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from ....models import ( from ....models import (
Account, Account,
AccountCommodityState, AccountCommodityState,
AccountLease,
AutomationJob, AutomationJob,
PlatformFulfillmentSetting,
PlatformFulfillmentTask, 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_sync_runner import WORK_STOREHOME_SYNC
from ...account_work_queue import enqueue_account_work from ...account_work_queue import enqueue_account_work
from ...douyu_service import cookie_operable_filter
JOB_KEY = "account_capability_refresh" JOB_KEY = "account_capability_refresh"
def _base_query(db: Session, now: datetime): def _base_query(db: Session, now: datetime):
pending_commodities = ( pending_commodities_query = (
select(PlatformFulfillmentTask.commodity_id) select(PlatformFulfillmentTask.commodity_id)
.where( .where(
PlatformFulfillmentTask.execution_status == "ready", PlatformFulfillmentTask.execution_status == "ready",
PlatformFulfillmentTask.account_id.is_(None), PlatformFulfillmentTask.account_id.is_(None),
PlatformFulfillmentTask.commodity_id != "", PlatformFulfillmentTask.commodity_id != "",
PlatformFulfillmentTask.target_platform == "douyu",
PlatformFulfillmentTask.scope == "elite",
) )
.distinct() .distinct()
) )
active_lease = exists( match_started_at = (
select(AccountLease.id).where( db.query(PlatformFulfillmentSetting.order_match_started_at)
AccountLease.active_account_id == Account.id, .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 万, # JOIN 比相关 EXISTS 更容易使用商品能力索引。当前生产账号池约 3 万,
# 原写法会按账号逐个探测商品状态,慢查询实测扫描 40 万行。 # 原写法会按账号逐个探测商品状态,慢查询实测扫描 40 万行。
return ( return (
@@ -61,11 +75,13 @@ def _base_query(db: Session, now: datetime):
AccountCommodityState.next_check_at.is_(None), AccountCommodityState.next_check_at.is_(None),
AccountCommodityState.next_check_at <= now, AccountCommodityState.next_check_at <= now,
), ),
cookie_operable_filter(), cookie_present_filter(),
Account.auto_managed.is_(True), valid_ck_filter(),
Account.status.notin_(MANUAL_STATES), automatable_account_filter(),
Account.ck_check_status == "valid", assigned_to_pool_filter(),
~active_lease, role_change_ready_filter(now),
~active_lease_exists(),
~active_collab_order_exists(),
) )
.distinct() .distinct()
) )
+79 -214
View File
@@ -14,8 +14,6 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable, Mapping, Sequence from collections.abc import Callable, Mapping, Sequence
from concurrent.futures import ThreadPoolExecutor, as_completed
from concurrent.futures import TimeoutError as FutureTimeoutError
from datetime import UTC, datetime, timedelta from datetime import UTC, datetime, timedelta
from typing import Any from typing import Any
from unicodedata import normalize as unicode_normalize from unicodedata import normalize as unicode_normalize
@@ -45,12 +43,6 @@ from ..permissions import user_has_permission
from .account_binding import ( from .account_binding import (
binding_channel, binding_channel,
binding_role_from_result, binding_role_from_result,
can_change_role,
role_change_available_at,
)
from .account_capability_service import (
AVAILABILITY_AVAILABLE,
classify_goods_items,
) )
from .account_events_service import ( from .account_events_service import (
CATEGORY_OPS, CATEGORY_OPS,
@@ -58,6 +50,10 @@ from .account_events_service import (
EVENT_LEASE_ACQUIRED, EVENT_LEASE_ACQUIRED,
emit, emit,
) )
from .account_exchange_sync_service import sync_exchange_records
from .account_query_filters import ACTIVE_COLLAB_STATUSES
from .account_sync_runner import WORK_STOREHOME_SYNC
from .account_work_queue import enqueue_account_work
from .audit_service import record_account_action, record_audit from .audit_service import record_account_action, record_audit
from .business_events import record_account_query from .business_events import record_account_query
from .collab_common import apply_order_visibility, count_archived from .collab_common import apply_order_visibility, count_archived
@@ -75,7 +71,6 @@ from .douyu_account_probe import (
persist_probe_result, persist_probe_result,
probe_account, probe_account,
) )
from .douyu_activity_query import query_exchange_records
from .douyu_goods_service import ( from .douyu_goods_service import (
serialize_live_goods as _serialize_live_goods, serialize_live_goods as _serialize_live_goods,
) )
@@ -106,15 +101,7 @@ STATUS_ROLE_ERROR = "role_error"
STATUS_FAILED = "failed" STATUS_FAILED = "failed"
STATUS_VOID = "void" STATUS_VOID = "void"
LEASE_PURPOSE_DOUYU_COLLAB = "douyu_collab" LEASE_PURPOSE_DOUYU_COLLAB = "douyu_collab"
COLLAB_LEASE_STATUSES: tuple[str, ...] = ( COLLAB_LEASE_STATUSES = ACTIVE_COLLAB_STATUSES
STATUS_INCOMPLETE,
STATUS_TO_RECHARGE,
STATUS_RECHARGED,
STATUS_SCRIPTED,
STATUS_POINTS_ERROR,
STATUS_ROLE_ERROR,
STATUS_FAILED,
)
ORDER_STATUSES: tuple[str, ...] = ( ORDER_STATUSES: tuple[str, ...] = (
STATUS_INCOMPLETE, STATUS_INCOMPLETE,
@@ -150,9 +137,7 @@ STATUS_FLOW: tuple[str, ...] = (
SOURCE_TYPE_PLATFORM_FULFILLMENT = "platform_fulfillment" SOURCE_TYPE_PLATFORM_FULFILLMENT = "platform_fulfillment"
ACCOUNT_REPLACE_ROLES = frozenset({"super_admin", "operation"}) ACCOUNT_REPLACE_ROLES = frozenset({"super_admin", "operation"})
ACCOUNT_RECOMMEND_DEFAULT_SCAN_LIMIT = 8 ACCOUNT_RECOMMEND_DEFAULT_SCAN_LIMIT = 20
ACCOUNT_RECOMMEND_CONCURRENCY = 8
ACCOUNT_RECOMMEND_TIMEOUT_SECONDS = 20
PLATFORM_TAKEOVER_BLOCKING_TASK_TYPES = frozenset( PLATFORM_TAKEOVER_BLOCKING_TASK_TYPES = frozenset(
{ {
"lock_goods", "lock_goods",
@@ -628,24 +613,6 @@ def unlink_account(
return order return order
def _recommendation_account_snapshot(account: Account) -> Account:
"""复制探针所需的标量字段,避免工作线程访问 SQLAlchemy Session。"""
return Account(
id=int(account.id),
username=str(account.username or ""),
password="",
email="",
email_password="",
cookie=str(account.cookie or ""),
uid=str(account.uid or ""),
nickname=str(account.nickname or ""),
elite_opened=account.elite_opened,
points=account.points,
gold_balance=account.gold_balance,
points_expire_at=account.points_expire_at,
)
def recommend_accounts( def recommend_accounts(
db: Session, db: Session,
current: User, current: User,
@@ -659,9 +626,11 @@ def recommend_accounts(
scan_limit: int | None = None, scan_limit: int | None = None,
client_factory: Callable[[Account], Any] | None = None, client_factory: Callable[[Account], Any] | None = None,
) -> dict[str, Any]: ) -> dict[str, Any]:
"""推荐可绑定/可换绑账号;只读探测,不创建租约。""" """从本地账号与能力投影推荐账号,不访问斗鱼官网。"""
_assert_account_replace_permission(current) _assert_account_replace_permission(current)
_assert_account_replaceable(order) _assert_account_replaceable(order)
# 保留参数兼容旧调用;实时校验统一放到“使用此账号”的 set_account()。
del client_factory
name, cid, points = _resolve_gift( name, cid, points = _resolve_gift(
db, db,
@@ -678,9 +647,10 @@ def recommend_accounts(
1, 1,
min( min(
int(scan_limit or ACCOUNT_RECOMMEND_DEFAULT_SCAN_LIMIT), int(scan_limit or ACCOUNT_RECOMMEND_DEFAULT_SCAN_LIMIT),
ACCOUNT_RECOMMEND_CONCURRENCY, 100,
), ),
) )
now = _now()
candidates_by_id: dict[int, Account] = {} candidates_by_id: dict[int, Account] = {}
if cid: if cid:
for account in capability_candidate_accounts( for account in capability_candidate_accounts(
@@ -691,21 +661,44 @@ def recommend_accounts(
scope="elite", scope="elite",
): ):
candidates_by_id[int(account.id)] = account candidates_by_id[int(account.id)] = account
for account in live_candidate_accounts( if not candidates_by_id:
db, refresh_seeds = live_candidate_accounts(
limit=effective_scan_limit, db,
required_points=required, limit=effective_scan_limit,
commodity_id=cid, required_points=required,
prefer_unbound=True, commodity_id=cid,
empty_only=not include_rebindable, prefer_unbound=True,
): empty_only=not include_rebindable,
candidates_by_id.setdefault(int(account.id), account) )
for account in refresh_seeds:
enqueue_account_work(
db,
account_id=int(account.id),
work_type=WORK_STOREHOME_SYNC,
priority=20,
due_at=now,
reason=f"外部协作单推荐缺少商品能力: {cid}",
payload={"commodity_id": cid, "order_id": int(order.id)},
)
if refresh_seeds:
db.commit()
else:
for account in live_candidate_accounts(
db,
limit=effective_scan_limit,
required_points=required,
commodity_id="",
prefer_unbound=True,
empty_only=not include_rebindable,
):
candidates_by_id.setdefault(int(account.id), account)
candidates = [ candidates = [
account account
for account in candidates_by_id.values() for account in candidates_by_id.values()
if (not order.account_id or int(account.id) != int(order.account_id)) if (not order.account_id or int(account.id) != int(order.account_id))
and (include_rebindable or account.role_bind_status == "unbound") and (include_rebindable or account.role_bind_status == "unbound")
and account.ck_check_status == "valid"
] ]
candidates.sort( candidates.sort(
key=lambda account: ( key=lambda account: (
@@ -717,109 +710,23 @@ def recommend_accounts(
) )
candidates = candidates[:effective_scan_limit] candidates = candidates[:effective_scan_limit]
config = resolve_config(db)
config_info = config_mapping(
{
"manual_id": str(config.manual_id or ""),
"rid": str(config.rid or ""),
"bind_act_alias": str(config.bind_act_alias or ""),
"confirm_act_alias": str(config.confirm_act_alias or ""),
"legacy_act_alias": str(config.legacy_act_alias or ""),
}
)
output: list[dict[str, Any]] = [] output: list[dict[str, Any]] = []
scanned = 0
result_limit = max(1, min(int(limit), 10)) result_limit = max(1, min(int(limit), 10))
for account in candidates:
def inspect_candidate(account: Account) -> tuple[Account, dict[str, Any]] | None: role_status = str(account.role_bind_status or ROLE_UNKNOWN)
try:
active_client = (
client_factory(account)
if client_factory
else activity_client(
str(account.cookie or ""),
logger_callback=lambda message: logger.debug(
"[douyu-collab] recommend account={} {}",
account.id,
message,
),
)
)
probe = probe_account(
account,
config_info,
client=active_client,
session_checker=(
None if client_factory is not None else check_douyu_cookie
),
with_elite=True,
with_eligibility=False,
with_limited_goods=bool(cid),
with_gold=False,
with_goods=False,
with_elite_role=True,
use_cache=client_factory is None,
)
return account, probe
except Exception as exc: # noqa: BLE001 - 单账号失败继续扫描
logger.info(
"[douyu-collab] 推荐账号跳过 account_id={} error={}",
account.id,
exc,
)
return None
def accept_candidate(account: Account, probe: dict[str, Any]) -> None:
session = probe.get("session") or {}
if str(session.get("status") or "") != SESSION_VALID:
return
role = (probe.get("roles") or {}).get("elite") or {}
role_status = str(role.get("status") or ROLE_UNKNOWN)
bind_info = (
role.get("bind_info") if isinstance(role.get("bind_info"), dict) else None
)
if role_status == ROLE_UNBOUND: if role_status == ROLE_UNBOUND:
match_type = "empty" match_type = "empty"
match_label = "未绑定" match_label = "未绑定"
elif role_status == ROLE_BOUND and can_change_role(bind_info): elif role_status == ROLE_BOUND:
if not include_rebindable: if not include_rebindable:
return continue
match_type = "rebind" match_type = "rebind"
match_label = "可换绑" match_label = "可换绑"
else: else:
return continue
elite_opened = account.elite_opened is True
eligibility = probe.get("eligibility") or {} live_points = _to_int(account.points)
limited_ids = { ready = bool(elite_opened and live_points is not None and live_points >= required)
str(item)
for item in eligibility.get("limited_ids") or []
if str(item)
}
if cid and cid in limited_ids:
return
if cid:
assessments = classify_goods_items(
[
item
for item in eligibility.get("storehome_goods") or []
if isinstance(item, dict)
]
)
assessment = assessments.get(str(cid))
if assessments and (
assessment is None
or assessment.availability != AVAILABILITY_AVAILABLE
):
return
elite = probe.get("elite") or {}
live_points = _to_int(eligibility.get("points"))
if live_points is None:
live_points = _to_int(elite.get("manual_score"))
elite_opened = bool(elite.get("opened"))
ready = bool(
elite_opened and live_points is not None and live_points >= required
)
reasons = [ reasons = [
"未绑定角色,可直接绑定" "未绑定角色,可直接绑定"
if match_type == "empty" if match_type == "empty"
@@ -829,24 +736,19 @@ def recommend_accounts(
reasons.append("精英宝典未开通") reasons.append("精英宝典未开通")
if live_points is None or live_points < required: if live_points is None or live_points < required:
reasons.append(f"积分不足({live_points or 0}/{required})") reasons.append(f"积分不足({live_points or 0}/{required})")
identity = probe.get("identity") or {}
output.append( output.append(
{ {
"account_id": int(account.id), "account_id": int(account.id),
# 后续“使用此账号”会按内部 username 反查,不能用接口返回的展示昵称。 # 后续“使用此账号”按内部 username 反查,不能用展示昵称。
"account_username": str(account.username or ""), "account_username": str(account.username or ""),
"account_uid": str(identity.get("uid") or account.uid or ""), "account_uid": str(account.uid or ""),
"account_nickname": str( "account_nickname": str(
identity.get("nickname") account.nickname or account.username or ""
or account.nickname
or account.username
or ""
), ),
"ck_status": CK_VALID, "ck_status": CK_VALID,
"role_status": role_status, "role_status": role_status,
"bind_game_role": str(role.get("role_name") or ""), "bind_game_role": str(account.game_name or ""),
"role_change_available_at": role_change_available_at(bind_info), "role_change_available_at": account.role_change_available_at,
"points": live_points, "points": live_points,
"elite_opened": elite_opened, "elite_opened": elite_opened,
"match_type": match_type, "match_type": match_type,
@@ -855,38 +757,8 @@ def recommend_accounts(
"reasons": reasons, "reasons": reasons,
} }
) )
if len(output) >= result_limit:
snapshots = [_recommendation_account_snapshot(account) for account in candidates] break
if snapshots:
executor = ThreadPoolExecutor(
max_workers=min(ACCOUNT_RECOMMEND_CONCURRENCY, len(snapshots))
)
futures = {
executor.submit(inspect_candidate, account): account
for account in snapshots
}
scanned = len(futures)
try:
for future in as_completed(
futures,
timeout=ACCOUNT_RECOMMEND_TIMEOUT_SECONDS,
):
result = future.result()
if result is not None:
accept_candidate(*result)
if len(output) >= result_limit:
break
except FutureTimeoutError:
logger.warning(
"[douyu-collab] 推荐账号实时校验超时,已返回当前结果 scanned={} "
"timeout={}s",
scanned,
ACCOUNT_RECOMMEND_TIMEOUT_SECONDS,
)
finally:
for future in futures:
future.cancel()
executor.shutdown(wait=False, cancel_futures=True)
output.sort( output.sort(
key=lambda item: ( key=lambda item: (
@@ -898,9 +770,13 @@ def recommend_accounts(
) )
selected = output[: max(1, min(int(limit), 10))] selected = output[: max(1, min(int(limit), 10))]
message = ( message = (
f"找到 {len(selected)} 个可用账号,未绑定账号优先" f"从本地能力池找到 {len(selected)} 个可用账号,未绑定账号优先"
if selected if selected
else "当前没有找到未绑定或已过换绑冷却的可用账号" else (
f"本地暂无 {cid} 的新鲜商品能力,已加入后台刷新队列"
if cid
else "本地暂未找到未绑定或已过换绑冷却的可用账号"
)
) )
if not include_rebindable: if not include_rebindable:
message += ";当前仅搜索未绑定账号" message += ";当前仅搜索未绑定账号"
@@ -910,7 +786,7 @@ def recommend_accounts(
"commodity_id": cid, "commodity_id": cid,
"required_points": points, "required_points": points,
"include_rebindable": bool(include_rebindable), "include_rebindable": bool(include_rebindable),
"scanned": scanned, "scanned": len(candidates),
"candidates": selected, "candidates": selected,
"message": message, "message": message,
} }
@@ -2771,32 +2647,21 @@ def _load_exchange_records_for_orders(
raise CollabError("订单关联的斗鱼账号不存在", code="account_not_found") raise CollabError("订单关联的斗鱼账号不存在", code="account_not_found")
config = resolve_config(db) config = resolve_config(db)
active_client = client or _client_for_account(account) active_client = client or _client_for_account(account)
cutoffs = [ result = sync_exchange_records(
item.scripted_at - timedelta(seconds=ACTIVITY_RECORD_TIME_SKEW_SECONDS) db,
for item in orders account,
if item.scripted_at is not None active_client,
config_mapping(config),
source="douyu_collab.activity",
scopes=("elite",),
limit=ACTIVITY_RECORD_FETCH_LIMIT,
max_pages=ACTIVITY_RECORD_MAX_PAGES,
)
return [
dict(record)
for record in (result.get("records") or {}).get("elite", [])
if isinstance(record, dict)
] ]
not_before = min(cutoffs) if cutoffs else None
records: list[dict[str, Any]] = []
for page in range(ACTIVITY_RECORD_MAX_PAGES):
page_records = query_exchange_records(
active_client,
manual_id=str(config.manual_id or ""),
limit=ACTIVITY_RECORD_FETCH_LIMIT,
offset=page * ACTIVITY_RECORD_FETCH_LIMIT,
)
records.extend(page_records)
if len(page_records) < ACTIVITY_RECORD_FETCH_LIMIT:
break
if not_before is not None:
page_times = [
record_time
for record in page_records
if (record_time := _exchange_record_time(record)) is not None
]
if page_times and max(page_times) < not_before:
break
return records
def query_activity( def query_activity(
@@ -9,7 +9,7 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from datetime import UTC, datetime, timedelta from datetime import UTC, datetime, timedelta
from sqlalchemy import and_, case, or_, select from sqlalchemy import case, or_, select
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
@@ -18,12 +18,9 @@ from ...models import (
AccountCommodityState, AccountCommodityState,
AccountEvent, AccountEvent,
AccountLease, AccountLease,
DouyuExternalOrder,
User,
) )
from ..account_capability_service import ( from ..account_capability_service import (
AVAILABILITY_AVAILABLE, AVAILABILITY_AVAILABLE,
AVAILABILITY_NOT_OPEN,
) )
from ..account_events_service import ( from ..account_events_service import (
CATEGORY_OPS, CATEGORY_OPS,
@@ -31,21 +28,20 @@ from ..account_events_service import (
EVENT_LEASE_RELEASED, EVENT_LEASE_RELEASED,
emit, emit,
) )
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,
fresh_available_state_filter,
role_change_ready_filter,
valid_ck_filter,
)
VALIDATION_LEASE_MINUTES = 5 VALIDATION_LEASE_MINUTES = 5
BINDING_LEASE_MINUTES = 120 BINDING_LEASE_MINUTES = 120
EXCHANGE_COOLDOWN_DAYS = 365 EXCHANGE_COOLDOWN_DAYS = 365
POOL_OWNER_ROLES = ("super_admin", "operation", "support")
ACTIVE_COLLAB_ORDER_STATUSES = (
"incomplete",
"to_recharge",
"recharged",
"scripted",
"points_error",
"role_error",
"failed",
)
@dataclass(frozen=True) @dataclass(frozen=True)
@@ -75,26 +71,6 @@ def _now(now: datetime | None = None) -> datetime:
return now or datetime.now(UTC) return now or datetime.now(UTC)
def _pool_owner_ids():
return select(User.id).where(
User.role.in_(POOL_OWNER_ROLES),
User.is_active.is_(True),
)
def _active_collab_order():
"""账号即使缺少协作租约,只要还有活动协作单也不能进入自动选号。"""
return (
select(DouyuExternalOrder.id)
.where(
DouyuExternalOrder.account_id == Account.id,
DouyuExternalOrder.archived_at.is_(None),
DouyuExternalOrder.status.in_(ACTIVE_COLLAB_ORDER_STATUSES),
)
.exists()
)
def eligible_accounts( def eligible_accounts(
db: Session, db: Session,
*, *,
@@ -106,13 +82,6 @@ def eligible_accounts(
"""筛选能人工兑换指定精英宝典商品的账号。""" """筛选能人工兑换指定精英宝典商品的账号。"""
moment = _now(now) moment = _now(now)
since = moment - timedelta(days=EXCHANGE_COOLDOWN_DAYS) since = moment - timedelta(days=EXCHANGE_COOLDOWN_DAYS)
owner_ids = _pool_owner_ids()
active_lease = (
select(AccountLease.id)
.where(AccountLease.active_account_id == Account.id)
.exists()
)
active_collab = _active_collab_order()
exchanged = ( exchanged = (
select(AccountEvent.id) select(AccountEvent.id)
.where( .where(
@@ -127,21 +96,17 @@ def eligible_accounts(
db.query(Account) db.query(Account)
.filter( .filter(
Account.status == "active", Account.status == "active",
Account.ck_check_status == "valid", cookie_present_filter(),
Account.cookie.isnot(None), valid_ck_filter(),
Account.cookie != "", automatable_account_filter(),
Account.auto_managed.is_(True),
Account.elite_opened.is_(True), Account.elite_opened.is_(True),
Account.points.isnot(None), Account.points.isnot(None),
Account.points >= max(0, int(required_points)), Account.points >= max(0, int(required_points)),
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)), assigned_to_pool_filter(),
~active_lease, ~active_lease_exists(),
~active_collab, ~active_collab_order_exists(),
~exchanged, ~exchanged,
or_( role_change_ready_filter(moment),
Account.role_change_available_at.is_(None),
Account.role_change_available_at <= moment,
),
) )
.order_by( .order_by(
Account.points_expire_at.is_(None), Account.points_expire_at.is_(None),
@@ -176,25 +141,13 @@ def live_candidate_accounts(
empty_only: bool = False, empty_only: bool = False,
) -> list[Account]: ) -> list[Account]:
"""按缓存质量优先排序,但保留实时检测作为最终准入判断。""" """按缓存质量优先排序,但保留实时检测作为最终准入判断。"""
owner_ids = _pool_owner_ids()
active_lease = (
select(AccountLease.id)
.where(AccountLease.active_account_id == Account.id)
.exists()
)
active_collab = _active_collab_order()
query = db.query(Account).filter( query = db.query(Account).filter(
Account.cookie.isnot(None), cookie_present_filter(),
Account.cookie != "", automatable_account_filter(),
Account.auto_managed.is_(True), role_change_ready_filter(_now()),
Account.status.notin_(MANUAL_STATES), assigned_to_pool_filter(),
or_( ~active_lease_exists(),
Account.role_change_available_at.is_(None), ~active_collab_order_exists(),
Account.role_change_available_at <= _now(),
),
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
~active_lease,
~active_collab,
) )
commodity = commodity_id.strip() commodity = commodity_id.strip()
if commodity: if commodity:
@@ -292,50 +245,10 @@ def _capability_candidate_query(
return None return None
moment = _now(now) moment = _now(now)
since = moment - timedelta(days=EXCHANGE_COOLDOWN_DAYS) since = moment - timedelta(days=EXCHANGE_COOLDOWN_DAYS)
owner_ids = _pool_owner_ids() availability_filter = fresh_available_state_filter(
active_lease = ( moment,
select(AccountLease.id) allow_not_open=allow_not_open,
.where(AccountLease.active_account_id == Account.id)
.exists()
) )
active_collab = _active_collab_order()
allowed_availability = (
(AVAILABILITY_AVAILABLE, AVAILABILITY_NOT_OPEN)
if allow_not_open
else (AVAILABILITY_AVAILABLE,)
)
window_filter = or_(
and_(
AccountCommodityState.availability == AVAILABILITY_AVAILABLE,
or_(
AccountCommodityState.available_from.is_(None),
AccountCommodityState.available_from <= moment,
),
or_(
AccountCommodityState.available_until.is_(None),
AccountCommodityState.available_until >= moment,
),
),
and_(
AccountCommodityState.availability == AVAILABILITY_NOT_OPEN,
or_(
AccountCommodityState.available_until.is_(None),
AccountCommodityState.available_until >= moment,
),
),
)
if not allow_not_open:
window_filter = and_(
AccountCommodityState.availability == AVAILABILITY_AVAILABLE,
or_(
AccountCommodityState.available_from.is_(None),
AccountCommodityState.available_from <= moment,
),
or_(
AccountCommodityState.available_until.is_(None),
AccountCommodityState.available_until >= moment,
),
)
return ( return (
db.query(Account, AccountCommodityState) db.query(Account, AccountCommodityState)
.join( .join(
@@ -346,26 +259,18 @@ def _capability_candidate_query(
AccountCommodityState.platform == "douyu", AccountCommodityState.platform == "douyu",
AccountCommodityState.scope == scope, AccountCommodityState.scope == scope,
AccountCommodityState.commodity_id == commodity, AccountCommodityState.commodity_id == commodity,
AccountCommodityState.availability.in_(allowed_availability), availability_filter,
AccountCommodityState.valid_until.isnot(None),
AccountCommodityState.valid_until > moment,
window_filter,
or_( or_(
AccountCommodityState.last_exchange_at.is_(None), AccountCommodityState.last_exchange_at.is_(None),
AccountCommodityState.last_exchange_at < since, AccountCommodityState.last_exchange_at < since,
), ),
Account.cookie.isnot(None), cookie_present_filter(),
Account.cookie != "", valid_ck_filter(),
Account.ck_check_status == "valid", automatable_account_filter(),
Account.auto_managed.is_(True), role_change_ready_filter(moment),
Account.status.notin_(MANUAL_STATES), assigned_to_pool_filter(),
or_( ~active_lease_exists(),
Account.role_change_available_at.is_(None), ~active_collab_order_exists(),
Account.role_change_available_at <= moment,
),
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
~active_lease,
~active_collab,
) )
.order_by( .order_by(
case( case(
@@ -38,16 +38,21 @@ from ..account_binding import (
) )
from ..account_capability_service import ( from ..account_capability_service import (
AVAILABILITY_AVAILABLE, AVAILABILITY_AVAILABLE,
AVAILABILITY_EXPIRED,
AVAILABILITY_LIMITED, AVAILABILITY_LIMITED,
AVAILABILITY_NO_STOCK, AVAILABILITY_NO_STOCK,
AVAILABILITY_NOT_OFFERED, AVAILABILITY_NOT_OFFERED,
AVAILABILITY_NOT_OPEN, AVAILABILITY_NOT_OPEN,
AVAILABILITY_UNKNOWN,
classify_goods_items, classify_goods_items,
exchange_gate_for_availability,
record_account_commodity_state, record_account_commodity_state,
) )
from ..account_lifecycle import MANUAL_STATES from ..account_query_filters import (
active_lease_exists,
assigned_to_pool_filter,
automatable_account_filter,
cookie_present_filter,
valid_ck_filter,
)
from ..account_sync_runner import WORK_STOREHOME_SYNC from ..account_sync_runner import WORK_STOREHOME_SYNC
from ..account_work_queue import enqueue_account_work from ..account_work_queue import enqueue_account_work
from ..business_events import record_account_query, record_douyu_task_started from ..business_events import record_account_query, record_douyu_task_started
@@ -117,47 +122,24 @@ def _exchange_gate(
allow_not_open: bool = False, allow_not_open: bool = False,
) -> tuple[str, str, str]: ) -> tuple[str, str, str]:
"""把实时商品资格映射为履约可展示的硬门槛。""" """把实时商品资格映射为履约可展示的硬门槛。"""
if commodity_id in state.limited_ids: limited = commodity_id in state.limited_ids
return (
"limited",
"commodity_limit_reached",
"该账号已达到此商品兑换上限",
)
if not state.commodity_availability: if not state.commodity_availability:
return "", "", "" return (
exchange_gate_for_availability(
AVAILABILITY_AVAILABLE,
limited=limited,
)
if limited
else ("", "", "")
)
availability = state.commodity_availability.get(commodity_id) availability = state.commodity_availability.get(commodity_id)
reason = state.commodity_blocking_reasons.get(commodity_id, "") reason = state.commodity_blocking_reasons.get(commodity_id, "")
if availability is None: return exchange_gate_for_availability(
return (
"not_available",
"commodity_not_offered",
reason or "官方商品列表本次未返回该商品,可能已下架",
)
if availability == AVAILABILITY_AVAILABLE:
return "", "", ""
if allow_not_open and availability == AVAILABILITY_NOT_OPEN:
# 2026-11-22:预售单礼包未开放不阻止提前绑定,兑换阶段继续等待开放。
return (
"waiting_release",
"commodity_not_open",
reason or "商品限时兑换尚未开始,可提前绑定角色",
)
issue_map = {
AVAILABILITY_NOT_OPEN: ("commodity_not_open", "商品限时兑换尚未开始"),
AVAILABILITY_EXPIRED: ("commodity_expired", "商品限时兑换已经结束"),
AVAILABILITY_NO_STOCK: ("commodity_no_stock", "商品当前无库存"),
AVAILABILITY_NOT_OFFERED: ("commodity_not_offered", "商品当前未上架"),
AVAILABILITY_UNKNOWN: ("commodity_status_unknown", "商品资格状态未知"),
AVAILABILITY_LIMITED: (
"commodity_limit_reached",
"该账号已达到此商品兑换上限",
),
}
issue_code, fallback = issue_map.get(
availability, availability,
("commodity_not_exchangeable", "商品当前不可兑换"), reason=reason,
limited=limited,
allow_not_open=allow_not_open,
) )
return "not_available", issue_code, reason or fallback
_SYNC_LOCK = Lock() _SYNC_LOCK = Lock()
@@ -2414,14 +2396,11 @@ def _seed_capability_refresh(
AccountCommodityState.platform == TARGET_PLATFORM, AccountCommodityState.platform == TARGET_PLATFORM,
AccountCommodityState.scope == (task.scope or SCOPE_ELITE), AccountCommodityState.scope == (task.scope or SCOPE_ELITE),
AccountCommodityState.commodity_id == task.commodity_id, AccountCommodityState.commodity_id == task.commodity_id,
Account.cookie.isnot(None), cookie_present_filter(),
Account.cookie != "", valid_ck_filter(),
Account.ck_check_status == "valid", automatable_account_filter(),
Account.auto_managed.is_(True), assigned_to_pool_filter(),
Account.status.notin_(MANUAL_STATES), ~active_lease_exists(),
~db.query(AccountLease.id)
.filter(AccountLease.active_account_id == Account.id)
.exists(),
) )
.order_by( .order_by(
AccountCommodityState.observed_at.is_(None).desc(), AccountCommodityState.observed_at.is_(None).desc(),
@@ -3563,48 +3542,26 @@ def refresh_exchange_eligibility(
is_presale = classification.normalize_sale_mode(task.sale_mode) == "presale" is_presale = classification.normalize_sale_mode(task.sale_mode) == "presale"
if task.commodity_id in limited_ids: if task.commodity_id in limited_ids:
availability = AVAILABILITY_LIMITED availability = AVAILABILITY_LIMITED
commodity_status = "limited"
issue_code = "commodity_limit_reached"
commodity_message = "该账号已达到此商品兑换上限" commodity_message = "该账号已达到此商品兑换上限"
elif assessments and assessment is None: elif assessments and assessment is None:
availability = AVAILABILITY_NOT_OFFERED availability = AVAILABILITY_NOT_OFFERED
commodity_status = "not_available"
issue_code = "commodity_not_offered"
commodity_message = "官方商品列表本次未返回该商品,可能已下架" commodity_message = "官方商品列表本次未返回该商品,可能已下架"
elif (
is_presale
and assessment is not None
and assessment.availability == AVAILABILITY_NOT_OPEN
):
availability = assessment.availability
commodity_status = "waiting_release"
issue_code = "commodity_not_open"
commodity_message = (
assessment.blocking_reason or "商品限时兑换尚未开始,可提前绑定角色"
)
elif assessment is not None and assessment.availability != AVAILABILITY_AVAILABLE: elif assessment is not None and assessment.availability != AVAILABILITY_AVAILABLE:
availability = assessment.availability availability = assessment.availability
commodity_status = "not_available" commodity_message = assessment.blocking_reason
issue_code = {
AVAILABILITY_NOT_OPEN: "commodity_not_open",
AVAILABILITY_EXPIRED: "commodity_expired",
AVAILABILITY_NO_STOCK: "commodity_no_stock",
AVAILABILITY_NOT_OFFERED: "commodity_not_offered",
AVAILABILITY_UNKNOWN: "commodity_status_unknown",
}.get(assessment.availability, "commodity_not_exchangeable")
commodity_message = (
assessment.blocking_reason or "商品当前不可兑换"
)
else: else:
availability = AVAILABILITY_AVAILABLE availability = AVAILABILITY_AVAILABLE
commodity_status = ""
issue_code = ""
commodity_message = "" commodity_message = ""
if commodity_status: gate_status, gate_issue, gate_message = exchange_gate_for_availability(
task.exchange_status = commodity_status availability,
task.issue_code = issue_code reason=commodity_message,
task.last_error = commodity_message allow_not_open=is_presale,
)
if gate_status:
task.exchange_status = gate_status
task.issue_code = gate_issue
task.last_error = gate_message
elif not account.elite_opened: elif not account.elite_opened:
task.exchange_status = "not_opened" task.exchange_status = "not_opened"
task.issue_code = "elite_not_opened" task.issue_code = "elite_not_opened"