perf(fulfillment): 优化能力刷新候选查询并新增精英能力投影

This commit is contained in:
yml
2026-11-29 13:06:54 +08:00
parent 2ea105a924
commit be7146a9ef
12 changed files with 548 additions and 54 deletions
@@ -7,6 +7,7 @@
from __future__ import annotations
import os
from collections.abc import Iterable
from dataclasses import dataclass
from datetime import UTC, datetime, timedelta
from typing import Any
@@ -70,6 +71,58 @@ def _apply_commodity_state_values(
return changed
def refresh_account_elite_capability_projection(
db: Session,
*,
account_id: int,
account: Account | None = None,
states: Iterable[AccountCommodityState] | None = None,
) -> None:
"""同步维护账号表上的精英域覆盖与可兑换有效期投影。"""
target = account if account is not None else db.get(Account, int(account_id))
if target is None:
return
if states is not None:
covered = any(row.observed_at is not None for row in states)
eligible_until = max(
(
row.valid_until
for row in states
if row.account_eligibility == ACCOUNT_ELIGIBILITY_ELIGIBLE
and row.offer_state in (OFFER_STATE_ACTIVE, OFFER_STATE_BEFORE)
and row.valid_until is not None
),
default=None,
)
else:
covered = bool(
db.query(AccountCommodityState.id)
.filter(
AccountCommodityState.account_id == int(account_id),
AccountCommodityState.platform == "douyu",
AccountCommodityState.scope == "elite",
AccountCommodityState.observed_at.isnot(None),
)
.first()
)
eligible_until = (
db.query(func.max(AccountCommodityState.valid_until))
.filter(
AccountCommodityState.account_id == int(account_id),
AccountCommodityState.platform == "douyu",
AccountCommodityState.scope == "elite",
AccountCommodityState.account_eligibility == ACCOUNT_ELIGIBILITY_ELIGIBLE,
AccountCommodityState.offer_state.in_(
(OFFER_STATE_ACTIVE, OFFER_STATE_BEFORE)
),
AccountCommodityState.valid_until.isnot(None),
)
.scalar()
)
_set_if_changed(target, "elite_covered", covered)
_set_if_changed(target, "elite_eligible_until", eligible_until)
def _env_positive_int(name: str, default: int) -> int:
try:
value = int(os.getenv(name, str(default)))
@@ -606,6 +659,13 @@ def sync_account_goods_states(
state.updated_at = moment
changed += 1
if scope == "elite":
refresh_account_elite_capability_projection(
db,
account_id=int(account.id),
account=account,
states=by_commodity.values(),
)
db.flush()
return changed
@@ -707,6 +767,13 @@ def record_account_commodity_state(
state.next_check_at = state.valid_until
state.updated_at = moment
db.flush()
if scope == "elite" and account.id is not None:
refresh_account_elite_capability_projection(
db,
account_id=int(account.id),
account=account,
)
db.flush()
return state
@@ -758,6 +825,12 @@ def mark_commodity_exchanged(
state.error_code = ""
state.updated_at = moment
db.flush()
if scope == "elite":
refresh_account_elite_capability_projection(
db,
account_id=int(account_id),
)
db.flush()
def invalidate_account_commodity_states(
@@ -789,6 +862,16 @@ def invalidate_account_commodity_states(
synchronize_session=False,
)
)
target = db.get(Account, int(account_id))
if target is not None:
if target.elite_covered is None:
refresh_account_elite_capability_projection(
db,
account_id=int(account_id),
account=target,
)
else:
_set_if_changed(target, "elite_eligible_until", None)
return int(changed or 0)
@@ -911,5 +994,6 @@ __all__ = [
"invalidate_account_commodity_states",
"mark_commodity_exchanged",
"record_account_commodity_state",
"refresh_account_elite_capability_projection",
"sync_account_goods_states",
]