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),
|
||||
|
||||
@@ -1001,13 +1001,19 @@ def list_tasks(
|
||||
text = keyword.strip()
|
||||
if text:
|
||||
pattern = f"%{text}%"
|
||||
query = query.filter(
|
||||
query = query.outerjoin(
|
||||
Account,
|
||||
Account.id == PlatformFulfillmentTask.account_id,
|
||||
).filter(
|
||||
or_(
|
||||
PlatformFulfillmentTask.platform_order_id.ilike(pattern),
|
||||
PlatformFulfillmentTask.sku_name.ilike(pattern),
|
||||
PlatformFulfillmentTask.sku_code.ilike(pattern),
|
||||
PlatformFulfillmentTask.internal_sku_key.ilike(pattern),
|
||||
PlatformFulfillmentTask.commodity_id.ilike(pattern),
|
||||
Account.username.ilike(pattern),
|
||||
Account.nickname.ilike(pattern),
|
||||
Account.uid.ilike(pattern),
|
||||
)
|
||||
)
|
||||
total = query.count()
|
||||
@@ -2581,6 +2587,48 @@ def restore_cancelled_task(
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
|
||||
def prepare_platform_task_for_collab_takeover(
|
||||
db: Session,
|
||||
*,
|
||||
task: PlatformFulfillmentTask,
|
||||
current: User,
|
||||
collab_order_no: str,
|
||||
) -> None:
|
||||
"""把平台履约任务转为人工,但不提交事务;由协作单接管流程原子提交。"""
|
||||
if task.execution_status in {"completed", "cancelled"}:
|
||||
raise HTTPException(status_code=409, detail="已结束的履约任务不能被协作单接管")
|
||||
account_id = task.account_id
|
||||
lease_id = task.lease_id
|
||||
reason = f"外部协作单 {collab_order_no} 人工接管账号"
|
||||
task.account_id = None
|
||||
task.lease_id = None
|
||||
task.bind_task_id = None
|
||||
task.exchange_task_id = None
|
||||
task.execution_status = "manual_required"
|
||||
task.binding_status = "not_started"
|
||||
task.exchange_status = "not_started"
|
||||
task.issue_code = "manual_required"
|
||||
task.next_allocate_at = None
|
||||
task.operator_id = current.id
|
||||
task.last_error = reason
|
||||
task.updated_at = _now()
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"collab_takeover",
|
||||
actor_type="user",
|
||||
actor_id=current.id,
|
||||
detail={
|
||||
"reason": reason,
|
||||
"collabOrderNo": collab_order_no,
|
||||
"releasedAccountId": account_id,
|
||||
"releasedLeaseId": lease_id,
|
||||
},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
db.flush()
|
||||
|
||||
|
||||
def release_task_to_manual(
|
||||
db: Session,
|
||||
*,
|
||||
|
||||
Reference in New Issue
Block a user