fix(fulfillment): 主动补全有效 CK 商品能力缓存

This commit is contained in:
yml
2026-11-28 20:53:25 +08:00
parent c8efa6d808
commit 7ca011c14e
14 changed files with 296 additions and 69 deletions
@@ -623,6 +623,18 @@ def capability_candidate_accounts(
]
def _refresh_candidate_order_by(now: datetime) -> tuple[Any, ...]:
return (
case((active_lease_exists(), 1), else_=0),
case((active_collab_order_exists(), 1), else_=0),
case((role_change_ready_filter(now), 0), else_=1),
case((Account.role_bind_status == "unbound", 0), else_=1),
Account.overview_queried_at.is_(None).desc(),
Account.overview_queried_at.asc(),
Account.id.asc(),
)
def capability_refresh_candidate_accounts(
db: Session,
*,
@@ -699,15 +711,52 @@ def capability_refresh_candidate_accounts(
assigned_to_pool_filter(),
~active_account_work_exists(work_type=WORK_STOREHOME_SYNC),
)
.order_by(
case((active_lease_exists(), 1), else_=0),
case((active_collab_order_exists(), 1), else_=0),
case((role_change_ready_filter(moment), 0), else_=1),
case((Account.role_bind_status == "unbound", 0), else_=1),
Account.overview_queried_at.is_(None).desc(),
Account.overview_queried_at.asc(),
Account.id.asc(),
.order_by(*_refresh_candidate_order_by(moment))
.limit(max(1, min(int(limit), 100)))
.all()
)
def storehome_refresh_candidate_accounts(
db: Session,
*,
limit: int = 20,
now: datetime | None = None,
exclude_account_ids: tuple[int, ...] | list[int] = (),
) -> list[Account]:
"""选择 CK 有效但缺少新鲜 storehome 快照的账号做全量能力补全。
能力缓存过期不等于 CK 失效。只要 CK 结论仍为有效且账号可自动操作,
就可以重新查询 storehome 并把整个商品能力目录写回本地。
"""
moment = _now(now)
fresh_storehome = (
select(AccountSyncState.id)
.where(
AccountSyncState.account_id == Account.id,
AccountSyncState.platform == "douyu",
AccountSyncState.domain == "storehome",
AccountSyncState.status == "success",
AccountSyncState.valid_until.isnot(None),
AccountSyncState.valid_until > moment,
)
.exists()
)
excluded_ids = tuple(
dict.fromkeys(int(account_id) for account_id in exclude_account_ids)
)
query = db.query(Account).filter(
~fresh_storehome,
cookie_present_filter(),
valid_ck_filter(),
automatable_account_filter(),
assigned_to_pool_filter(),
~active_account_work_exists(work_type=WORK_STOREHOME_SYNC),
)
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()
)
@@ -969,4 +1018,5 @@ __all__ = [
"live_candidate_accounts",
"release_lease",
"renew_lease",
"storehome_refresh_candidate_accounts",
]