fix(douyu): coordinate collab and platform account leases
Avoid automatic allocation of accounts with active external collaboration, require explicit manual takeover of platform leases, and add Douyu account identity search.
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, AccountCommodityState, AccountEvent, AccountLease, User
|
||||
from ...models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountEvent,
|
||||
AccountLease,
|
||||
DouyuExternalOrder,
|
||||
User,
|
||||
)
|
||||
from ..account_capability_service import AVAILABILITY_AVAILABLE
|
||||
from ..account_events_service import (
|
||||
CATEGORY_OPS,
|
||||
@@ -27,6 +34,15 @@ VALIDATION_LEASE_MINUTES = 5
|
||||
BINDING_LEASE_MINUTES = 120
|
||||
EXCHANGE_COOLDOWN_DAYS = 365
|
||||
POOL_OWNER_ROLES = ("super_admin", "operation", "support")
|
||||
ACTIVE_COLLAB_ORDER_STATUSES = (
|
||||
"incomplete",
|
||||
"to_recharge",
|
||||
"recharged",
|
||||
"scripted",
|
||||
"points_error",
|
||||
"role_error",
|
||||
"failed",
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -55,6 +71,19 @@ def _pool_owner_ids():
|
||||
)
|
||||
|
||||
|
||||
def _active_collab_order():
|
||||
"""账号即使缺少协作租约,只要还有活动协作单也不能进入自动选号。"""
|
||||
return (
|
||||
select(DouyuExternalOrder.id)
|
||||
.where(
|
||||
DouyuExternalOrder.account_id == Account.id,
|
||||
DouyuExternalOrder.archived_at.is_(None),
|
||||
DouyuExternalOrder.status.in_(ACTIVE_COLLAB_ORDER_STATUSES),
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
|
||||
|
||||
def eligible_accounts(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -72,6 +101,7 @@ def eligible_accounts(
|
||||
.where(AccountLease.active_account_id == Account.id)
|
||||
.exists()
|
||||
)
|
||||
active_collab = _active_collab_order()
|
||||
exchanged = (
|
||||
select(AccountEvent.id)
|
||||
.where(
|
||||
@@ -95,6 +125,7 @@ def eligible_accounts(
|
||||
Account.points >= max(0, int(required_points)),
|
||||
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
|
||||
~active_lease,
|
||||
~active_collab,
|
||||
~exchanged,
|
||||
or_(
|
||||
Account.role_change_available_at.is_(None),
|
||||
@@ -138,6 +169,7 @@ def live_candidate_accounts(
|
||||
.where(AccountLease.active_account_id == Account.id)
|
||||
.exists()
|
||||
)
|
||||
active_collab = _active_collab_order()
|
||||
query = db.query(Account).filter(
|
||||
Account.cookie.isnot(None),
|
||||
Account.cookie != "",
|
||||
@@ -149,6 +181,7 @@ def live_candidate_accounts(
|
||||
),
|
||||
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
|
||||
~active_lease,
|
||||
~active_collab,
|
||||
)
|
||||
commodity = commodity_id.strip()
|
||||
if commodity:
|
||||
@@ -204,6 +237,7 @@ def capability_candidate_accounts(
|
||||
.where(AccountLease.active_account_id == Account.id)
|
||||
.exists()
|
||||
)
|
||||
active_collab = _active_collab_order()
|
||||
return (
|
||||
db.query(Account)
|
||||
.join(
|
||||
@@ -239,6 +273,7 @@ def capability_candidate_accounts(
|
||||
),
|
||||
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
|
||||
~active_lease,
|
||||
~active_collab,
|
||||
)
|
||||
.order_by(
|
||||
case((Account.ck_check_status == "valid", 0), else_=1),
|
||||
|
||||
Reference in New Issue
Block a user