fix(fulfillment): 优先刷新联合皮肤全资格账号
This commit is contained in:
@@ -31,6 +31,10 @@ from ...platform_fulfillment.account_selector import (
|
||||
capability_refresh_candidate_accounts,
|
||||
storehome_refresh_candidate_accounts,
|
||||
)
|
||||
from ...platform_fulfillment.policy import (
|
||||
bundle_requirements_for_commodity,
|
||||
normalize_allocation_policy,
|
||||
)
|
||||
|
||||
JOB_KEY = "account_capability_refresh"
|
||||
DEFAULT_BACKFILL_RESERVED_SLOTS = 8
|
||||
@@ -95,6 +99,27 @@ def _backfill_reserved_slots() -> int:
|
||||
return DEFAULT_BACKFILL_RESERVED_SLOTS
|
||||
|
||||
|
||||
def _preferred_bundle_groups(
|
||||
db: Session,
|
||||
commodity_ids: list[str],
|
||||
) -> tuple[tuple[str, ...], ...]:
|
||||
if not commodity_ids:
|
||||
return ()
|
||||
row = db.get(PlatformFulfillmentSetting, 1)
|
||||
policy = normalize_allocation_policy(
|
||||
row.allocation_policy_json if row is not None else None
|
||||
)
|
||||
groups: list[tuple[str, ...]] = []
|
||||
for commodity_id in commodity_ids:
|
||||
required, _ = bundle_requirements_for_commodity(policy, commodity_id)
|
||||
if not required:
|
||||
continue
|
||||
group = tuple(dict.fromkeys((commodity_id, *required)))
|
||||
if group not in groups:
|
||||
groups.append(group)
|
||||
return tuple(groups)
|
||||
|
||||
|
||||
def _build_select_query(
|
||||
db: Session,
|
||||
job: AutomationJob,
|
||||
@@ -159,6 +184,8 @@ def _refresh_candidates(
|
||||
limit=backfill_limit,
|
||||
now=moment,
|
||||
exclude_account_ids=tuple(demand_ids),
|
||||
scope="elite",
|
||||
preferred_commodity_groups=_preferred_bundle_groups(db, commodity_ids),
|
||||
)
|
||||
if backfill_limit > 0
|
||||
else []
|
||||
|
||||
@@ -21,7 +21,7 @@ from ...models import (
|
||||
AccountLease,
|
||||
AccountSyncState,
|
||||
)
|
||||
from ..account_capability_service import OFFER_STATE_ACTIVE
|
||||
from ..account_capability_service import OFFER_STATE_ACTIVE, OFFER_STATE_BEFORE
|
||||
from ..account_events_service import (
|
||||
CATEGORY_OPS,
|
||||
EVENT_LEASE_ACQUIRED,
|
||||
@@ -723,6 +723,8 @@ def storehome_refresh_candidate_accounts(
|
||||
limit: int = 20,
|
||||
now: datetime | None = None,
|
||||
exclude_account_ids: tuple[int, ...] | list[int] = (),
|
||||
scope: str = "elite",
|
||||
preferred_commodity_groups: tuple[tuple[str, ...], ...] = (),
|
||||
) -> list[Account]:
|
||||
"""选择 CK 有效但缺少新鲜 storehome 快照的账号做全量能力补全。
|
||||
|
||||
@@ -745,6 +747,44 @@ def storehome_refresh_candidate_accounts(
|
||||
excluded_ids = tuple(
|
||||
dict.fromkeys(int(account_id) for account_id in exclude_account_ids)
|
||||
)
|
||||
preferred_groups = tuple(
|
||||
dict.fromkeys(
|
||||
tuple(
|
||||
dict.fromkeys(
|
||||
str(commodity_id).strip()
|
||||
for commodity_id in group
|
||||
if str(commodity_id).strip()
|
||||
)
|
||||
)
|
||||
for group in preferred_commodity_groups
|
||||
)
|
||||
)
|
||||
preferred_groups = tuple(group for group in preferred_groups if len(group) >= 2)
|
||||
preferred_exprs = [
|
||||
and_(
|
||||
*[
|
||||
select(AccountCommodityState.id)
|
||||
.where(
|
||||
AccountCommodityState.account_id == Account.id,
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity_id,
|
||||
AccountCommodityState.account_eligibility == "eligible",
|
||||
AccountCommodityState.offer_state.in_(
|
||||
(OFFER_STATE_ACTIVE, OFFER_STATE_BEFORE)
|
||||
),
|
||||
or_(
|
||||
AccountCommodityState.last_exchange_at.is_(None),
|
||||
AccountCommodityState.last_exchange_at
|
||||
< moment - timedelta(days=EXCHANGE_COOLDOWN_DAYS),
|
||||
),
|
||||
)
|
||||
.exists()
|
||||
for commodity_id in group
|
||||
]
|
||||
)
|
||||
for group in preferred_groups
|
||||
]
|
||||
query = db.query(Account).filter(
|
||||
~fresh_storehome,
|
||||
cookie_present_filter(),
|
||||
@@ -755,11 +795,11 @@ def storehome_refresh_candidate_accounts(
|
||||
)
|
||||
if excluded_ids:
|
||||
query = query.filter(~Account.id.in_(excluded_ids))
|
||||
return (
|
||||
query.order_by(*_refresh_candidate_order_by(moment))
|
||||
.limit(max(1, min(int(limit), 100)))
|
||||
.all()
|
||||
)
|
||||
order_by: list[Any] = []
|
||||
if preferred_exprs:
|
||||
order_by.append(case((or_(*preferred_exprs), 0), else_=1))
|
||||
order_by.extend(_refresh_candidate_order_by(moment))
|
||||
return query.order_by(*order_by).limit(max(1, min(int(limit), 100))).all()
|
||||
|
||||
|
||||
def has_recent_exchange(
|
||||
|
||||
Reference in New Issue
Block a user