feat(fulfillment): 支持选号优先级、三皮肤联合资格捆绑与同角色共享租约
This commit is contained in:
@@ -8,10 +8,11 @@ from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import and_, case, or_, select
|
||||
from sqlalchemy import and_, case, literal, or_, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.orm import Session, aliased
|
||||
|
||||
from ...models import (
|
||||
Account,
|
||||
@@ -39,6 +40,14 @@ from ..account_query_filters import (
|
||||
)
|
||||
from ..account_sync_runner import WORK_STOREHOME_SYNC
|
||||
from ..account_work_queue import active_account_work_exists
|
||||
from .policy import (
|
||||
PRIORITY_KEY_BUNDLE_MATCH,
|
||||
PRIORITY_KEY_CK_FRESHNESS,
|
||||
PRIORITY_KEY_POINTS_ASC,
|
||||
PRIORITY_KEY_PRESERVE_BUNDLE_POOL,
|
||||
PRIORITY_KEY_RESOURCE_READY,
|
||||
normalize_priority_order,
|
||||
)
|
||||
|
||||
VALIDATION_LEASE_MINUTES = 5
|
||||
BINDING_LEASE_MINUTES = 120
|
||||
@@ -84,6 +93,8 @@ class CapabilityAccount:
|
||||
role_bind_status: str
|
||||
ck_check_status: str
|
||||
game_name: str
|
||||
ck_checked_at: datetime | None = None
|
||||
status: str = ""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -96,6 +107,8 @@ class CapabilityState:
|
||||
available_from: datetime | None
|
||||
valid_until: datetime | None
|
||||
blocking_reason: str
|
||||
bundle_missing_count: int = 0
|
||||
is_protected_bundle: bool = False
|
||||
|
||||
|
||||
def _now(now: datetime | None = None) -> datetime:
|
||||
@@ -252,15 +265,57 @@ def live_candidate_accounts(
|
||||
case((Account.role_bind_status == "unbound", 0), else_=1),
|
||||
)
|
||||
rows.extend(
|
||||
tier_query.order_by(*order_by)
|
||||
.limit(effective_limit - len(rows))
|
||||
.all()
|
||||
tier_query.order_by(*order_by).limit(effective_limit - len(rows)).all()
|
||||
)
|
||||
if len(rows) >= effective_limit:
|
||||
break
|
||||
return rows
|
||||
|
||||
|
||||
def _co_commodity_eligible_exists(
|
||||
commodity_id: str,
|
||||
*,
|
||||
scope: str,
|
||||
moment: datetime,
|
||||
since: datetime,
|
||||
):
|
||||
"""协同商品资格子查询:只要账号本身具备可兑资格(含未开放预售)且近 365 天未兑过。"""
|
||||
co_state = aliased(AccountCommodityState)
|
||||
return (
|
||||
select(co_state.id)
|
||||
.where(
|
||||
co_state.account_id == Account.id,
|
||||
co_state.platform == "douyu",
|
||||
co_state.scope == scope,
|
||||
co_state.commodity_id == commodity_id,
|
||||
fresh_eligible_state_filter(
|
||||
moment,
|
||||
allow_before_open=True,
|
||||
state_model=co_state,
|
||||
),
|
||||
or_(
|
||||
co_state.last_exchange_at.is_(None),
|
||||
co_state.last_exchange_at < since,
|
||||
),
|
||||
)
|
||||
.correlate(Account)
|
||||
.exists()
|
||||
)
|
||||
|
||||
|
||||
def _clean_commodity_group(
|
||||
values: tuple[str, ...] | list[str] | None,
|
||||
*,
|
||||
exclude: str = "",
|
||||
) -> tuple[str, ...]:
|
||||
seen: list[str] = []
|
||||
for raw in values or ():
|
||||
text = str(raw or "").strip()
|
||||
if text and text != exclude and text not in seen:
|
||||
seen.append(text)
|
||||
return tuple(seen)
|
||||
|
||||
|
||||
def _capability_candidate_query(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -270,6 +325,10 @@ def _capability_candidate_query(
|
||||
limit: int = 20,
|
||||
now: datetime | None = None,
|
||||
allow_before_open: bool = False,
|
||||
required_co_commodities: tuple[str, ...] | list[str] = (),
|
||||
preferred_co_commodities: tuple[str, ...] | list[str] = (),
|
||||
protected_bundle_commodities: tuple[str, ...] | list[str] = (),
|
||||
priority_order: tuple[str, ...] | list[str] = (),
|
||||
):
|
||||
commodity = commodity_id.strip()
|
||||
if not commodity:
|
||||
@@ -280,6 +339,123 @@ def _capability_candidate_query(
|
||||
moment,
|
||||
allow_before_open=allow_before_open,
|
||||
)
|
||||
required_co = _clean_commodity_group(required_co_commodities, exclude=commodity)
|
||||
preferred_co = _clean_commodity_group(
|
||||
preferred_co_commodities,
|
||||
exclude=commodity,
|
||||
)
|
||||
preferred_co = tuple(cid for cid in preferred_co if cid not in required_co)
|
||||
protected_co = _clean_commodity_group(protected_bundle_commodities)
|
||||
|
||||
filters = [
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity,
|
||||
eligibility_filter,
|
||||
or_(
|
||||
AccountCommodityState.last_exchange_at.is_(None),
|
||||
AccountCommodityState.last_exchange_at < since,
|
||||
),
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
role_change_ready_filter(moment),
|
||||
assigned_to_pool_filter(),
|
||||
~active_lease_exists(),
|
||||
~active_collab_order_exists(),
|
||||
]
|
||||
for co_cid in required_co:
|
||||
filters.append(
|
||||
_co_commodity_eligible_exists(
|
||||
co_cid,
|
||||
scope=scope,
|
||||
moment=moment,
|
||||
since=since,
|
||||
)
|
||||
)
|
||||
|
||||
if preferred_co:
|
||||
missing_terms = [
|
||||
case(
|
||||
(
|
||||
_co_commodity_eligible_exists(
|
||||
co_cid,
|
||||
scope=scope,
|
||||
moment=moment,
|
||||
since=since,
|
||||
),
|
||||
0,
|
||||
),
|
||||
else_=1,
|
||||
)
|
||||
for co_cid in preferred_co
|
||||
]
|
||||
missing_count_expr = missing_terms[0]
|
||||
for term in missing_terms[1:]:
|
||||
missing_count_expr = missing_count_expr + term
|
||||
else:
|
||||
missing_count_expr = literal(0)
|
||||
|
||||
if protected_co:
|
||||
all_protected_expr = and_(
|
||||
*[
|
||||
_co_commodity_eligible_exists(
|
||||
co_cid,
|
||||
scope=scope,
|
||||
moment=moment,
|
||||
since=since,
|
||||
)
|
||||
for co_cid in protected_co
|
||||
]
|
||||
)
|
||||
protected_flag_expr = case((all_protected_expr, 1), else_=0)
|
||||
else:
|
||||
protected_flag_expr = literal(0)
|
||||
|
||||
order_clauses: list[Any] = [
|
||||
case(
|
||||
(AccountCommodityState.offer_state == OFFER_STATE_ACTIVE, 0),
|
||||
else_=1,
|
||||
)
|
||||
]
|
||||
for key in normalize_priority_order(priority_order):
|
||||
if key == PRIORITY_KEY_BUNDLE_MATCH and preferred_co:
|
||||
order_clauses.append(missing_count_expr.asc())
|
||||
elif key == PRIORITY_KEY_PRESERVE_BUNDLE_POOL and protected_co:
|
||||
order_clauses.append(protected_flag_expr.asc())
|
||||
elif key == PRIORITY_KEY_RESOURCE_READY:
|
||||
order_clauses.extend(
|
||||
[
|
||||
case((Account.elite_opened.is_(True), 0), else_=1),
|
||||
case(
|
||||
(Account.points >= max(0, int(required_points)), 0),
|
||||
else_=1,
|
||||
),
|
||||
]
|
||||
)
|
||||
elif key == PRIORITY_KEY_POINTS_ASC:
|
||||
order_clauses.extend(
|
||||
[
|
||||
Account.points.is_(None).asc(),
|
||||
Account.points.asc(),
|
||||
]
|
||||
)
|
||||
elif key == PRIORITY_KEY_CK_FRESHNESS:
|
||||
order_clauses.extend(
|
||||
[
|
||||
case((Account.ck_check_status == "valid", 0), else_=1),
|
||||
case((Account.status == "active", 0), else_=1),
|
||||
Account.ck_checked_at.is_(None).asc(),
|
||||
Account.ck_checked_at.desc(),
|
||||
]
|
||||
)
|
||||
order_clauses.extend(
|
||||
[
|
||||
AccountCommodityState.observed_at.desc(),
|
||||
Account.id.asc(),
|
||||
]
|
||||
)
|
||||
|
||||
return (
|
||||
db.query(
|
||||
Account.id.label("account_id"),
|
||||
@@ -289,55 +465,24 @@ def _capability_candidate_query(
|
||||
Account.points.label("account_points"),
|
||||
Account.gold_balance.label("account_gold_balance"),
|
||||
Account.points_expire_at.label("account_points_expire_at"),
|
||||
Account.role_change_available_at.label(
|
||||
"account_role_change_available_at"
|
||||
),
|
||||
Account.role_change_available_at.label("account_role_change_available_at"),
|
||||
Account.elite_opened.label("account_elite_opened"),
|
||||
Account.role_bind_status.label("account_role_bind_status"),
|
||||
Account.ck_check_status.label("account_ck_check_status"),
|
||||
Account.ck_checked_at.label("account_ck_checked_at"),
|
||||
Account.status.label("account_status"),
|
||||
Account.game_name.label("account_game_name"),
|
||||
AccountCommodityState.offer_state.label("state_offer_state"),
|
||||
AccountCommodityState.observed_at.label("state_observed_at"),
|
||||
missing_count_expr.label("bundle_missing_count"),
|
||||
protected_flag_expr.label("is_protected_bundle"),
|
||||
)
|
||||
.join(
|
||||
AccountCommodityState,
|
||||
AccountCommodityState.account_id == Account.id,
|
||||
)
|
||||
.filter(
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity,
|
||||
eligibility_filter,
|
||||
or_(
|
||||
AccountCommodityState.last_exchange_at.is_(None),
|
||||
AccountCommodityState.last_exchange_at < since,
|
||||
),
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
role_change_ready_filter(moment),
|
||||
assigned_to_pool_filter(),
|
||||
~active_lease_exists(),
|
||||
~active_collab_order_exists(),
|
||||
)
|
||||
.order_by(
|
||||
case(
|
||||
(AccountCommodityState.offer_state == OFFER_STATE_ACTIVE, 0),
|
||||
else_=1,
|
||||
),
|
||||
case((Account.ck_check_status == "valid", 0), else_=1),
|
||||
case((Account.status == "active", 0), else_=1),
|
||||
Account.ck_checked_at.is_(None).asc(),
|
||||
Account.ck_checked_at.desc(),
|
||||
case((Account.elite_opened.is_(True), 0), else_=1),
|
||||
case(
|
||||
(Account.points >= max(0, int(required_points)), 0),
|
||||
else_=1,
|
||||
),
|
||||
Account.points.asc(),
|
||||
AccountCommodityState.observed_at.desc(),
|
||||
Account.id.asc(),
|
||||
)
|
||||
.filter(*filters)
|
||||
.order_by(*order_clauses)
|
||||
.limit(max(1, min(int(limit), 100)))
|
||||
)
|
||||
|
||||
@@ -351,6 +496,10 @@ def capability_candidate_rows(
|
||||
limit: int = 20,
|
||||
now: datetime | None = None,
|
||||
allow_before_open: bool = False,
|
||||
required_co_commodities: tuple[str, ...] | list[str] = (),
|
||||
preferred_co_commodities: tuple[str, ...] | list[str] = (),
|
||||
protected_bundle_commodities: tuple[str, ...] | list[str] = (),
|
||||
priority_order: tuple[str, ...] | list[str] = (),
|
||||
) -> list[CapabilityCandidate]:
|
||||
"""读取账号与能力快照;只访问本地数据库,不触发官网请求。"""
|
||||
query = _capability_candidate_query(
|
||||
@@ -361,6 +510,10 @@ def capability_candidate_rows(
|
||||
limit=limit,
|
||||
now=now,
|
||||
allow_before_open=allow_before_open,
|
||||
required_co_commodities=required_co_commodities,
|
||||
preferred_co_commodities=preferred_co_commodities,
|
||||
protected_bundle_commodities=protected_bundle_commodities,
|
||||
priority_order=priority_order,
|
||||
)
|
||||
if query is None:
|
||||
return []
|
||||
@@ -400,9 +553,19 @@ def capability_candidate_rows(
|
||||
candidates: list[CapabilityCandidate] = []
|
||||
for row in rows:
|
||||
account_id = int(row.account_id)
|
||||
state = states.get(account_id)
|
||||
if state is None:
|
||||
base_state = states.get(account_id)
|
||||
if base_state is None:
|
||||
continue
|
||||
state = CapabilityState(
|
||||
account_eligibility=base_state.account_eligibility,
|
||||
offer_state=base_state.offer_state,
|
||||
observed_at=base_state.observed_at,
|
||||
available_from=base_state.available_from,
|
||||
valid_until=base_state.valid_until,
|
||||
blocking_reason=base_state.blocking_reason,
|
||||
bundle_missing_count=int(getattr(row, "bundle_missing_count", 0) or 0),
|
||||
is_protected_bundle=bool(getattr(row, "is_protected_bundle", 0)),
|
||||
)
|
||||
candidates.append(
|
||||
CapabilityCandidate(
|
||||
account=CapabilityAccount(
|
||||
@@ -413,13 +576,13 @@ def capability_candidate_rows(
|
||||
points=row.account_points,
|
||||
gold_balance=row.account_gold_balance,
|
||||
points_expire_at=row.account_points_expire_at,
|
||||
role_change_available_at=(
|
||||
row.account_role_change_available_at
|
||||
),
|
||||
role_change_available_at=(row.account_role_change_available_at),
|
||||
elite_opened=row.account_elite_opened,
|
||||
role_bind_status=str(row.account_role_bind_status or ""),
|
||||
ck_check_status=str(row.account_ck_check_status or ""),
|
||||
game_name=str(row.account_game_name or ""),
|
||||
ck_checked_at=getattr(row, "account_ck_checked_at", None),
|
||||
status=str(getattr(row, "account_status", "") or ""),
|
||||
),
|
||||
state=state,
|
||||
)
|
||||
@@ -436,6 +599,10 @@ def capability_candidate_accounts(
|
||||
limit: int = 20,
|
||||
now: datetime | None = None,
|
||||
allow_before_open: bool = False,
|
||||
required_co_commodities: tuple[str, ...] | list[str] = (),
|
||||
preferred_co_commodities: tuple[str, ...] | list[str] = (),
|
||||
protected_bundle_commodities: tuple[str, ...] | list[str] = (),
|
||||
priority_order: tuple[str, ...] | list[str] = (),
|
||||
) -> list[CapabilityAccount]:
|
||||
"""从新鲜的商品能力索引读取候选账号,不产生外部请求。"""
|
||||
return [
|
||||
@@ -448,6 +615,10 @@ def capability_candidate_accounts(
|
||||
limit=limit,
|
||||
now=now,
|
||||
allow_before_open=allow_before_open,
|
||||
required_co_commodities=required_co_commodities,
|
||||
preferred_co_commodities=preferred_co_commodities,
|
||||
protected_bundle_commodities=protected_bundle_commodities,
|
||||
priority_order=priority_order,
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user