feat(fulfillment): 增加账号商品能力索引与生命周期事件
This commit is contained in:
@@ -13,7 +13,14 @@ from sqlalchemy import case, or_, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ...models import Account, AccountEvent, AccountLease, User
|
||||
from ...models import Account, AccountCommodityState, AccountEvent, AccountLease, User
|
||||
from ..account_capability_service import AVAILABILITY_AVAILABLE
|
||||
from ..account_events_service import (
|
||||
CATEGORY_OPS,
|
||||
EVENT_LEASE_ACQUIRED,
|
||||
EVENT_LEASE_RELEASED,
|
||||
emit,
|
||||
)
|
||||
from ..account_lifecycle import MANUAL_STATES
|
||||
|
||||
VALIDATION_LEASE_MINUTES = 5
|
||||
@@ -136,6 +143,10 @@ def live_candidate_accounts(
|
||||
Account.cookie != "",
|
||||
Account.auto_managed.is_(True),
|
||||
Account.status.notin_(MANUAL_STATES),
|
||||
or_(
|
||||
Account.role_change_available_at.is_(None),
|
||||
Account.role_change_available_at <= _now(),
|
||||
),
|
||||
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
|
||||
~active_lease,
|
||||
)
|
||||
@@ -172,6 +183,80 @@ def live_candidate_accounts(
|
||||
)
|
||||
|
||||
|
||||
def capability_candidate_accounts(
|
||||
db: Session,
|
||||
*,
|
||||
commodity_id: str,
|
||||
required_points: int = 0,
|
||||
scope: str = "elite",
|
||||
limit: int = 20,
|
||||
now: datetime | None = None,
|
||||
) -> list[Account]:
|
||||
"""从新鲜的商品能力索引读取候选账号,不产生外部请求。"""
|
||||
commodity = commodity_id.strip()
|
||||
if not commodity:
|
||||
return []
|
||||
moment = _now(now)
|
||||
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()
|
||||
)
|
||||
return (
|
||||
db.query(Account)
|
||||
.join(
|
||||
AccountCommodityState,
|
||||
AccountCommodityState.account_id == Account.id,
|
||||
)
|
||||
.filter(
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity,
|
||||
AccountCommodityState.availability == AVAILABILITY_AVAILABLE,
|
||||
AccountCommodityState.valid_until.isnot(None),
|
||||
AccountCommodityState.valid_until > moment,
|
||||
or_(
|
||||
AccountCommodityState.available_from.is_(None),
|
||||
AccountCommodityState.available_from <= moment,
|
||||
),
|
||||
or_(
|
||||
AccountCommodityState.available_until.is_(None),
|
||||
AccountCommodityState.available_until >= moment,
|
||||
),
|
||||
or_(
|
||||
AccountCommodityState.last_exchange_at.is_(None),
|
||||
AccountCommodityState.last_exchange_at < since,
|
||||
),
|
||||
Account.cookie.isnot(None),
|
||||
Account.cookie != "",
|
||||
Account.auto_managed.is_(True),
|
||||
Account.status.notin_(MANUAL_STATES),
|
||||
or_(
|
||||
Account.role_change_available_at.is_(None),
|
||||
Account.role_change_available_at <= moment,
|
||||
),
|
||||
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
|
||||
~active_lease,
|
||||
)
|
||||
.order_by(
|
||||
case((Account.ck_check_status == "valid", 0), else_=1),
|
||||
case((Account.status == "active", 0), else_=1),
|
||||
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(),
|
||||
)
|
||||
.limit(max(1, min(int(limit), 100)))
|
||||
.all()
|
||||
)
|
||||
|
||||
|
||||
def has_recent_exchange(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -231,6 +316,26 @@ def lease_account(
|
||||
if lease in db:
|
||||
db.expunge(lease)
|
||||
return None
|
||||
emit(
|
||||
db,
|
||||
account_id=int(candidate.account_id),
|
||||
event_type=EVENT_LEASE_ACQUIRED,
|
||||
title=f"履约占用:任务 #{task_id}",
|
||||
category=CATEGORY_OPS,
|
||||
detail={
|
||||
"lease_id": lease.id,
|
||||
"task_id": task_id,
|
||||
"holder_user_id": holder_user_id,
|
||||
"commodity_id": commodity_id,
|
||||
"purpose": "platform_fulfillment",
|
||||
},
|
||||
source="lease",
|
||||
subject_type="task",
|
||||
subject_key=str(task_id),
|
||||
actor_type="user",
|
||||
actor_id=holder_user_id,
|
||||
source_ref=f"account_lease_acquired:{lease.id}",
|
||||
)
|
||||
db.commit()
|
||||
return lease
|
||||
|
||||
@@ -265,6 +370,23 @@ def release_lease(
|
||||
lease.status = "released"
|
||||
lease.released_at = _now(now)
|
||||
lease.release_reason = str(reason or "")[:64]
|
||||
emit(
|
||||
db,
|
||||
account_id=int(lease.account_id),
|
||||
event_type=EVENT_LEASE_RELEASED,
|
||||
title=f"释放履约占用:{reason}",
|
||||
category=CATEGORY_OPS,
|
||||
detail={
|
||||
"lease_id": lease.id,
|
||||
"task_id": lease.batch_id,
|
||||
"reason": str(reason or "")[:64],
|
||||
},
|
||||
source="lease",
|
||||
subject_type="lease",
|
||||
subject_key=str(lease.id),
|
||||
actor_type="system",
|
||||
source_ref=f"account_lease_released:{lease.id}",
|
||||
)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
@@ -292,6 +414,24 @@ def expire_platform_fulfillment_leases(
|
||||
lease.status = "expired"
|
||||
lease.released_at = moment
|
||||
lease.release_reason = "ttl_expired"
|
||||
emit(
|
||||
db,
|
||||
account_id=int(lease.account_id),
|
||||
event_type=EVENT_LEASE_RELEASED,
|
||||
title="履约租约已过期",
|
||||
category=CATEGORY_OPS,
|
||||
occurred_at=moment,
|
||||
detail={
|
||||
"lease_id": lease.id,
|
||||
"task_id": lease.batch_id,
|
||||
"reason": "ttl_expired",
|
||||
},
|
||||
source="lease",
|
||||
subject_type="lease",
|
||||
subject_key=str(lease.id),
|
||||
actor_type="system",
|
||||
source_ref=f"account_lease_released:{lease.id}",
|
||||
)
|
||||
if leases:
|
||||
db.commit()
|
||||
return len(leases)
|
||||
@@ -302,6 +442,7 @@ __all__ = [
|
||||
"EXCHANGE_COOLDOWN_DAYS",
|
||||
"VALIDATION_LEASE_MINUTES",
|
||||
"AccountCandidate",
|
||||
"capability_candidate_accounts",
|
||||
"eligible_accounts",
|
||||
"expire_platform_fulfillment_leases",
|
||||
"has_recent_exchange",
|
||||
|
||||
Reference in New Issue
Block a user