修复(履约与分配): 封堵工作台换绑、预售共号与分配中心越权占用

This commit is contained in:
yml
2026-11-27 13:42:28 +08:00
parent 9100991cb1
commit 5006367217
13 changed files with 1031 additions and 34 deletions
+82 -11
View File
@@ -20,6 +20,7 @@ from ..models import (
AuditLog,
DouyuExternalOrder,
DouyuTask,
PlatformFulfillmentTask,
User,
)
from ..permissions import ROLE_LABELS, user_has_permission
@@ -75,6 +76,8 @@ from ..services.account_query_filters import (
ACTIVE_COLLAB_STATUSES,
active_collab_order_exists,
active_lease_exists,
recent_elite_bind_lock_exists,
role_change_ready_filter,
)
from ..services.account_service import parse_and_build_accounts
from ..services.account_value import (
@@ -184,6 +187,28 @@ def _visible_accounts_query(db: Session, current: User):
raise HTTPException(status_code=403, detail="无权查看账号")
def _active_platform_fulfillment_exists():
return (
select(PlatformFulfillmentTask.id)
.where(
PlatformFulfillmentTask.account_id == Account.id,
PlatformFulfillmentTask.execution_status.notin_(
("completed", "cancelled")
),
)
.exists()
)
def _unoccupied_for_assign_filter():
"""可分配给客服的安全兜底:无活动租约、无活跃预售单、无未结束履约任务。"""
return and_(
~active_lease_exists(),
~active_collab_order_exists(),
~_active_platform_fulfillment_exists(),
)
def _assignable_account_filter():
"""分配池口径:当前 Cookie 必须有一次不早于它的有效 CK 检测。"""
return and_(
@@ -430,15 +455,15 @@ def _filter_accounts_query(
if bind_change_value == "available":
query = query.filter(
Account.role_bind_status == "bound",
or_(
Account.role_change_available_at.is_(None),
Account.role_change_available_at <= now,
),
role_change_ready_filter(now),
)
elif bind_change_value == "cooldown":
query = query.filter(
Account.role_bind_status == "bound",
Account.role_change_available_at > now,
or_(
Account.role_change_available_at > now,
recent_elite_bind_lock_exists(now),
),
)
elif bind_change_value == "unbound":
query = query.filter(Account.role_bind_status == "unbound")
@@ -465,14 +490,15 @@ def _filter_accounts_query(
if occupancy_value:
has_lease = active_lease_exists()
in_collab = active_collab_order_exists()
in_fulfillment = _active_platform_fulfillment_exists()
if occupancy_value == "free":
query = query.filter(~has_lease, ~in_collab)
query = query.filter(~has_lease, ~in_collab, ~in_fulfillment)
elif occupancy_value == "no_lease":
query = query.filter(~has_lease)
query = query.filter(~has_lease, ~in_fulfillment)
elif occupancy_value == "no_collab":
query = query.filter(~in_collab)
elif occupancy_value == "leased":
query = query.filter(has_lease)
query = query.filter(or_(has_lease, in_fulfillment))
elif occupancy_value == "collab":
query = query.filter(in_collab)
@@ -1459,6 +1485,19 @@ def assign_account(
status_code=400,
detail="该账号尚未通过有效 CK 检测,无法分配",
)
is_free = (
db.query(Account.id)
.filter(
Account.id == account_id,
_unoccupied_for_assign_filter(),
)
.first()
)
if not is_free:
raise HTTPException(
status_code=409,
detail="该账号当前被履约订单、预售台账或活动租约占用,禁止分配给客服",
)
if acc.assigned_to != req.assigned_to:
acc.assigned_to = req.assigned_to
@@ -1527,6 +1566,33 @@ def batch_assign_accounts(
status_code=400,
detail=f"以下账号尚未通过有效 CK 检测,无法分配:{name_list}{suffix}",
)
free_ids = {
int(account_id)
for (account_id,) in (
db.query(Account.id)
.filter(
Account.id.in_(ids),
_unoccupied_for_assign_filter(),
)
.all()
)
}
occupied_ids = [account_id for account_id in ids if account_id not in free_ids]
if occupied_ids:
names = (
db.query(Account.username)
.filter(Account.id.in_(occupied_ids))
.all()
)
name_list = ", ".join([n[0] for n in names[:5]])
suffix = "..." if len(occupied_ids) > 5 else ""
raise HTTPException(
status_code=409,
detail=(
f"以下账号当前被履约订单、预售台账或活动租约占用,"
f"禁止分配给客服:{name_list}{suffix}"
),
)
# 先取出账号对象(批量时间线要逐个写事件,光有 count 不够)。
# 「撤回」只处理当前确实有归属的账号;显式勾选里混入未分配账号时,
@@ -1612,6 +1678,7 @@ def dispatch_assignments(
.filter(
Account.assigned_to.is_(None),
_assignable_account_filter(),
_unoccupied_for_assign_filter(),
)
)
if req.strategy == "points":
@@ -1772,6 +1839,7 @@ def assignment_options(
]
has_lease = active_lease_exists()
in_collab = active_collab_order_exists()
in_fulfillment = _active_platform_fulfillment_exists()
def count_with(*filters) -> int:
return int(
@@ -1783,10 +1851,10 @@ def assignment_options(
occupancy = {
"assignable": count_with(),
"free": count_with(~has_lease, ~in_collab),
"no_lease": count_with(~has_lease),
"free": count_with(~has_lease, ~in_collab, ~in_fulfillment),
"no_lease": count_with(~has_lease, ~in_fulfillment),
"no_collab": count_with(~in_collab),
"leased": count_with(has_lease),
"leased": count_with(or_(has_lease, in_fulfillment)),
"collab": count_with(in_collab),
}
presets = {
@@ -1795,12 +1863,14 @@ def assignment_options(
Account.points <= 0,
~has_lease,
~in_collab,
~in_fulfillment,
),
"elite_points": count_with(
Account.elite_opened.is_(True),
Account.points > 0,
~has_lease,
~in_collab,
~in_fulfillment,
),
"high_value": count_with(
or_(
@@ -1809,6 +1879,7 @@ def assignment_options(
),
~has_lease,
~in_collab,
~in_fulfillment,
),
}
+2 -1
View File
@@ -105,6 +105,7 @@ from ..services.douyu_service import (
DOUYU_HANDBOOK_TASK_TYPES,
SUPPORTED_DOUYU_TASK_TYPES,
DouyuActivePaymentError,
DouyuLeaseConflictError,
DouyuTaskBackpressureError,
apply_douyu_config_defaults,
cleanup_orphan_douyu_tasks,
@@ -1541,7 +1542,7 @@ def _prepare_task_batch(
recharge_channel=recharge_channel,
recharge_amount=recharge_amount,
)
except DouyuActivePaymentError as exc:
except (DouyuActivePaymentError, DouyuLeaseConflictError) as exc:
raise HTTPException(status_code=409, detail=str(exc)) from exc
except DouyuTaskBackpressureError as exc:
raise HTTPException(