优化(账号分配): 重构智能候选池与定量发放
按生命周期、租约、预售台账、积分、宝典和皮肤能力筛选账号池;支持指定客服定量发放、手工分配撤回,并为后续自助领取复用同一发放链路。
This commit is contained in:
@@ -18,14 +18,22 @@ import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
from tests.support.db import get_test_engine, make_test_session
|
||||
from web.backend.models import Account, User
|
||||
from web.backend.models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountLease,
|
||||
DouyuExternalOrder,
|
||||
User,
|
||||
)
|
||||
from web.backend.routers.accounts import (
|
||||
assign_account,
|
||||
assignment_options,
|
||||
assignments_summary,
|
||||
batch_assign_accounts,
|
||||
dispatch_assignments,
|
||||
list_accounts,
|
||||
)
|
||||
from web.backend.schemas import AccountAssign, BatchAssign
|
||||
from web.backend.schemas import AccountAssign, AssignmentDispatch, BatchAssign
|
||||
|
||||
|
||||
class _Base:
|
||||
@@ -197,6 +205,139 @@ class TestAssignablePool(_Base):
|
||||
assert "有效 CK 检测" in exc_info.value.detail
|
||||
|
||||
|
||||
class TestAssignmentPools(_Base):
|
||||
def test_occupancy_filter_excludes_lease_and_collab(self):
|
||||
free = self.make_account("pool-free")
|
||||
leased = self.make_account("pool-leased")
|
||||
collab = self.make_account("pool-collab")
|
||||
self.session.add(
|
||||
AccountLease(
|
||||
account_id=leased.id,
|
||||
holder_user_id=self.admin.id,
|
||||
purpose="platform_fulfillment",
|
||||
status="active",
|
||||
)
|
||||
)
|
||||
self.session.add(
|
||||
DouyuExternalOrder(
|
||||
order_no="ASSIGN-POOL-COLLAB",
|
||||
account_id=collab.id,
|
||||
account_username=collab.username,
|
||||
status="incomplete",
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
assert self.names(
|
||||
assign_status="unassigned",
|
||||
assignable_only=True,
|
||||
occupancy="free",
|
||||
) == [free.username]
|
||||
assert set(
|
||||
self.names(
|
||||
assign_status="unassigned",
|
||||
assignable_only=True,
|
||||
occupancy="no_collab",
|
||||
)
|
||||
) == {free.username, leased.username}
|
||||
|
||||
def test_commodity_filter_uses_fresh_eligible_capability(self):
|
||||
match = self.make_account("pool-skin-match")
|
||||
stale = self.make_account("pool-skin-stale")
|
||||
self.session.add_all(
|
||||
[
|
||||
AccountCommodityState(
|
||||
account_id=match.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id="skin-001",
|
||||
commodity_name="测试皮肤",
|
||||
availability="available",
|
||||
account_eligibility="eligible",
|
||||
offer_state="active",
|
||||
valid_until=self.now + timedelta(hours=1),
|
||||
),
|
||||
AccountCommodityState(
|
||||
account_id=stale.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id="skin-001",
|
||||
commodity_name="测试皮肤",
|
||||
availability="available",
|
||||
account_eligibility="eligible",
|
||||
offer_state="active",
|
||||
valid_until=self.now - timedelta(hours=1),
|
||||
),
|
||||
]
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
assert self.names(
|
||||
assign_status="unassigned",
|
||||
assignable_only=True,
|
||||
commodity_id="skin-001",
|
||||
) == [match.username]
|
||||
|
||||
def test_dispatch_selects_requested_quantity_from_free_pool(self):
|
||||
free = [self.make_account(f"dispatch-free-{index}") for index in range(3)]
|
||||
leased = self.make_account("dispatch-leased")
|
||||
self.session.add(
|
||||
AccountLease(
|
||||
account_id=leased.id,
|
||||
holder_user_id=self.admin.id,
|
||||
purpose="platform_fulfillment",
|
||||
status="active",
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
result = dispatch_assignments(
|
||||
AssignmentDispatch(
|
||||
assigned_to=self.support.id,
|
||||
quantity=2,
|
||||
occupancy="free",
|
||||
),
|
||||
db=self.session,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
assert result["count"] == 2
|
||||
assigned = [
|
||||
account
|
||||
for account in free
|
||||
if self.session.get(Account, account.id).assigned_to == self.support.id
|
||||
]
|
||||
self.session.refresh(leased)
|
||||
assert len(assigned) == 2
|
||||
assert leased.assigned_to is None
|
||||
|
||||
def test_assignment_options_exposes_skin_counts(self):
|
||||
account = self.make_account("options-skin")
|
||||
self.session.add(
|
||||
AccountCommodityState(
|
||||
account_id=account.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id="skin-options",
|
||||
commodity_name="选项皮肤",
|
||||
availability="available",
|
||||
account_eligibility="eligible",
|
||||
offer_state="active",
|
||||
valid_until=self.now + timedelta(hours=1),
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
result = assignment_options(db=self.session, current=self.admin)
|
||||
|
||||
assert result["occupancy"]["free"] == 1
|
||||
skin = next(
|
||||
item for item in result["skins"] if item["commodity_id"] == "skin-options"
|
||||
)
|
||||
assert skin["commodity_name"] == "选项皮肤"
|
||||
assert skin["account_count"] == 1
|
||||
|
||||
|
||||
class TestAllMatchingAssign(_Base):
|
||||
def test_assigns_every_matching_unassigned(self):
|
||||
free = [self.make_account(f"mf-{i}") for i in range(3)]
|
||||
|
||||
+368
-26
@@ -13,8 +13,10 @@ from ..models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountEvent,
|
||||
AccountLease,
|
||||
AccountSupplyProfile,
|
||||
AuditLog,
|
||||
DouyuExternalOrder,
|
||||
DouyuTask,
|
||||
User,
|
||||
)
|
||||
@@ -30,10 +32,16 @@ from ..schemas import (
|
||||
AccountOut,
|
||||
AccountRemark,
|
||||
AccountTag,
|
||||
AssignmentDispatch,
|
||||
BatchAssign,
|
||||
)
|
||||
from ..services.account_binding import bind_payload
|
||||
from ..services.account_capability_service import account_capability_counts
|
||||
from ..services.account_capability_service import (
|
||||
ACCOUNT_ELIGIBILITY_ELIGIBLE,
|
||||
OFFER_STATE_ACTIVE,
|
||||
OFFER_STATE_BEFORE,
|
||||
account_capability_counts,
|
||||
)
|
||||
from ..services.account_deletion import AccountDeleteBlocked, delete_accounts
|
||||
from ..services.account_events_service import (
|
||||
CATEGORY_OPS,
|
||||
@@ -60,6 +68,11 @@ from ..services.account_lifecycle import (
|
||||
STATUS_MANUAL,
|
||||
set_manual_state,
|
||||
)
|
||||
from ..services.account_query_filters import (
|
||||
ACTIVE_COLLAB_STATUSES,
|
||||
active_collab_order_exists,
|
||||
active_lease_exists,
|
||||
)
|
||||
from ..services.account_service import parse_and_build_accounts
|
||||
from ..services.account_value import (
|
||||
high_value_unknown_expr,
|
||||
@@ -101,6 +114,17 @@ def _assignable_account_filter():
|
||||
)
|
||||
|
||||
|
||||
def _assignment_support_target(db: Session, user_id: int) -> User:
|
||||
target = db.query(User).filter(User.id == user_id).first()
|
||||
if target is None:
|
||||
raise HTTPException(status_code=404, detail="目标用户不存在")
|
||||
if target.role != "support":
|
||||
raise HTTPException(status_code=400, detail="只能分配给客服角色")
|
||||
if not target.is_active:
|
||||
raise HTTPException(status_code=400, detail="目标客服已停用")
|
||||
return target
|
||||
|
||||
|
||||
def _query_str(value) -> str:
|
||||
"""把查询参数安全地取成字符串。
|
||||
|
||||
@@ -154,6 +178,9 @@ def _filter_accounts_query(
|
||||
elite_opened: bool | None = None,
|
||||
esports_opened: bool | None = None,
|
||||
role_bind_status: str = "",
|
||||
occupancy: str = "",
|
||||
commodity_id: str = "",
|
||||
commodity_scope: str = "elite",
|
||||
):
|
||||
"""复用列表筛选条件,供分页列表和全量批量操作保持一致。"""
|
||||
if _query_bool(has_cookie):
|
||||
@@ -205,6 +232,45 @@ def _filter_accounts_query(
|
||||
if role_bind:
|
||||
query = query.filter(Account.role_bind_status == role_bind)
|
||||
|
||||
occupancy_value = _query_str(occupancy)
|
||||
if occupancy_value:
|
||||
has_lease = active_lease_exists()
|
||||
in_collab = active_collab_order_exists()
|
||||
if occupancy_value == "free":
|
||||
query = query.filter(~has_lease, ~in_collab)
|
||||
elif occupancy_value == "no_lease":
|
||||
query = query.filter(~has_lease)
|
||||
elif occupancy_value == "no_collab":
|
||||
query = query.filter(~in_collab)
|
||||
elif occupancy_value == "leased":
|
||||
query = query.filter(has_lease)
|
||||
elif occupancy_value == "collab":
|
||||
query = query.filter(in_collab)
|
||||
|
||||
commodity_value = _query_str(commodity_id)
|
||||
if commodity_value:
|
||||
now = datetime.now(UTC)
|
||||
commodity_filters = [
|
||||
AccountCommodityState.account_id == Account.id,
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.commodity_id == commodity_value,
|
||||
AccountCommodityState.account_eligibility
|
||||
== ACCOUNT_ELIGIBILITY_ELIGIBLE,
|
||||
AccountCommodityState.offer_state.in_(
|
||||
(OFFER_STATE_ACTIVE, OFFER_STATE_BEFORE)
|
||||
),
|
||||
AccountCommodityState.valid_until.isnot(None),
|
||||
AccountCommodityState.valid_until > now,
|
||||
]
|
||||
scope_value = _query_str(commodity_scope)
|
||||
if scope_value:
|
||||
commodity_filters.append(AccountCommodityState.scope == scope_value)
|
||||
query = query.filter(
|
||||
db.query(AccountCommodityState.id)
|
||||
.filter(*commodity_filters)
|
||||
.exists()
|
||||
)
|
||||
|
||||
# 高价值套装筛选
|
||||
hv = _query_str(high_value_filter)
|
||||
if hv:
|
||||
@@ -271,37 +337,45 @@ def _filter_accounts_query(
|
||||
return query
|
||||
|
||||
|
||||
def _selection_query(db: Session, current: User, req: AccountBulkSelection):
|
||||
"""把批量操作请求还原成与账号列表完全一致的筛选查询。"""
|
||||
return _filter_accounts_query(
|
||||
db,
|
||||
_visible_accounts_query(db, current),
|
||||
current,
|
||||
assigned_only=req.assigned_only,
|
||||
assign_status=req.assign_status,
|
||||
assigned_user_id=req.assigned_user_id,
|
||||
assignable_only=req.assignable_only,
|
||||
tag=req.tag,
|
||||
has_cookie=req.has_cookie,
|
||||
search=req.search,
|
||||
account_names=req.account_names,
|
||||
status=req.status,
|
||||
ck_status=req.ck_status,
|
||||
failure_kind=req.failure_kind,
|
||||
bind_status=req.bind_status,
|
||||
high_value_filter=req.high_value_filter,
|
||||
min_points=req.min_points,
|
||||
max_points=req.max_points,
|
||||
min_gold=req.min_gold,
|
||||
max_gold=req.max_gold,
|
||||
elite_opened=req.elite_opened,
|
||||
esports_opened=req.esports_opened,
|
||||
role_bind_status=req.role_bind_status,
|
||||
occupancy=req.occupancy,
|
||||
commodity_id=req.commodity_id,
|
||||
commodity_scope=req.commodity_scope,
|
||||
)
|
||||
|
||||
|
||||
def _selected_account_ids(
|
||||
db: Session, current: User, req: AccountBulkSelection
|
||||
) -> list[int]:
|
||||
"""解析批量操作目标:当前筛选全部或显式选择的 ID。"""
|
||||
if req.all_matching:
|
||||
rows = (
|
||||
_filter_accounts_query(
|
||||
db,
|
||||
_visible_accounts_query(db, current),
|
||||
current,
|
||||
assigned_only=req.assigned_only,
|
||||
assign_status=req.assign_status,
|
||||
assigned_user_id=req.assigned_user_id,
|
||||
assignable_only=req.assignable_only,
|
||||
tag=req.tag,
|
||||
has_cookie=req.has_cookie,
|
||||
search=req.search,
|
||||
account_names=req.account_names,
|
||||
status=req.status,
|
||||
ck_status=req.ck_status,
|
||||
failure_kind=req.failure_kind,
|
||||
bind_status=req.bind_status,
|
||||
high_value_filter=req.high_value_filter,
|
||||
min_points=req.min_points,
|
||||
max_points=req.max_points,
|
||||
min_gold=req.min_gold,
|
||||
max_gold=req.max_gold,
|
||||
elite_opened=req.elite_opened,
|
||||
esports_opened=req.esports_opened,
|
||||
role_bind_status=req.role_bind_status,
|
||||
)
|
||||
_selection_query(db, current, req)
|
||||
.with_entities(Account.id)
|
||||
.order_by(None)
|
||||
.all()
|
||||
@@ -472,6 +546,12 @@ def list_accounts(
|
||||
role_bind_status: str = Query(
|
||||
"", description="精英角色绑定状态: unknown/unbound/pending/bound"
|
||||
),
|
||||
occupancy: str = Query(
|
||||
"",
|
||||
description="占用筛选:free/no_lease/no_collab/leased/collab;空=不限",
|
||||
),
|
||||
commodity_id: str = Query("", description="指定斗鱼商品/皮肤能力 ID"),
|
||||
commodity_scope: str = Query("elite", description="商品能力作用域,默认 elite"),
|
||||
sort: str = Query(
|
||||
"",
|
||||
description="排序:username/status/bind/points/high_value/exchange/tag;空=按 id",
|
||||
@@ -508,6 +588,9 @@ def list_accounts(
|
||||
elite_opened=elite_opened,
|
||||
esports_opened=esports_opened,
|
||||
role_bind_status=role_bind_status,
|
||||
occupancy=occupancy,
|
||||
commodity_id=commodity_id,
|
||||
commodity_scope=commodity_scope,
|
||||
)
|
||||
|
||||
total = None
|
||||
@@ -563,6 +646,38 @@ def list_accounts(
|
||||
# 而不是 `douyu_tasks` 的 JSON —— 后者是执行日志,S5 起业务事实已经在事件里了。
|
||||
exchange_stats: dict[int, tuple[int, Any, str]] = {}
|
||||
ids = [acc.id for acc in accounts]
|
||||
lease_map: dict[int, AccountLease] = {}
|
||||
collab_account_ids: set[int] = set()
|
||||
if ids:
|
||||
priority = {"platform_fulfillment": 0, "douyu_collab": 1}
|
||||
for lease in (
|
||||
db.query(AccountLease)
|
||||
.filter(
|
||||
AccountLease.account_id.in_(ids),
|
||||
AccountLease.status == "active",
|
||||
)
|
||||
.all()
|
||||
):
|
||||
account_id = int(lease.account_id)
|
||||
current = lease_map.get(account_id)
|
||||
if current is None or priority.get(
|
||||
str(lease.purpose), 9
|
||||
) < priority.get(str(current.purpose), 9):
|
||||
lease_map[account_id] = lease
|
||||
collab_account_ids = {
|
||||
int(account_id)
|
||||
for (account_id,) in (
|
||||
db.query(DouyuExternalOrder.account_id)
|
||||
.filter(
|
||||
DouyuExternalOrder.account_id.in_(ids),
|
||||
DouyuExternalOrder.archived_at.is_(None),
|
||||
DouyuExternalOrder.status.in_(ACTIVE_COLLAB_STATUSES),
|
||||
)
|
||||
.distinct()
|
||||
.all()
|
||||
)
|
||||
if account_id is not None
|
||||
}
|
||||
if ids:
|
||||
# ① 次数 + 最近一次时间(分组聚合,走 (account_id, event_type, occurred_at) 复合索引)
|
||||
for account_id, count, last_at in (
|
||||
@@ -656,6 +771,8 @@ def list_accounts(
|
||||
ck_checked_at=acc.ck_checked_at,
|
||||
points=acc.points,
|
||||
gold_balance=acc.gold_balance,
|
||||
elite_opened=acc.elite_opened,
|
||||
esports_opened=acc.esports_opened,
|
||||
exchange_count=exchange_stats.get(acc.id, (0, None, ""))[0],
|
||||
last_exchange_at=exchange_stats.get(acc.id, (0, None, ""))[1],
|
||||
last_exchange_name=exchange_stats.get(acc.id, (0, None, ""))[2],
|
||||
@@ -675,6 +792,13 @@ def list_accounts(
|
||||
ck_remaining_hours=(
|
||||
profile_map[acc.id].ck_remaining_hours if acc.id in profile_map else None
|
||||
),
|
||||
active_lease_purpose=(
|
||||
str(lease_map[acc.id].purpose or "") if acc.id in lease_map else ""
|
||||
),
|
||||
active_lease_expires_at=(
|
||||
lease_map[acc.id].expires_at if acc.id in lease_map else None
|
||||
),
|
||||
in_active_collab=acc.id in collab_account_ids,
|
||||
)
|
||||
# 只有管理员可看完整字段(密码、邮箱等)
|
||||
if can_include_sensitive:
|
||||
@@ -715,6 +839,9 @@ def list_account_ids(
|
||||
elite_opened: bool | None = Query(None),
|
||||
esports_opened: bool | None = Query(None),
|
||||
role_bind_status: str = Query(""),
|
||||
occupancy: str = Query(""),
|
||||
commodity_id: str = Query(""),
|
||||
commodity_scope: str = Query("elite"),
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
@@ -746,6 +873,9 @@ def list_account_ids(
|
||||
elite_opened=elite_opened,
|
||||
esports_opened=esports_opened,
|
||||
role_bind_status=role_bind_status,
|
||||
occupancy=occupancy,
|
||||
commodity_id=commodity_id,
|
||||
commodity_scope=commodity_scope,
|
||||
)
|
||||
selected_names = _parse_account_names(_query_str(account_names))
|
||||
if selected_names:
|
||||
@@ -1195,6 +1325,121 @@ def batch_assign_accounts(
|
||||
}
|
||||
|
||||
|
||||
@router.post("/assignments/dispatch")
|
||||
def dispatch_assignments(
|
||||
req: AssignmentDispatch,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("account:assign")),
|
||||
):
|
||||
"""按候选池筛选定量分配账号。
|
||||
|
||||
这是当前“管理员手工发送账号”的执行链路。后续客服自助领取可以复用
|
||||
同一筛选与锁行逻辑,只把目标客服改为当前登录客服,并增加领取配额。
|
||||
"""
|
||||
target = _assignment_support_target(db, req.assigned_to)
|
||||
query = (
|
||||
_selection_query(db, current, req)
|
||||
.filter(
|
||||
Account.assigned_to.is_(None),
|
||||
_assignable_account_filter(),
|
||||
)
|
||||
)
|
||||
if req.strategy == "points":
|
||||
query = query.order_by(
|
||||
Account.points.is_(None).asc(),
|
||||
Account.points.desc(),
|
||||
Account.id.asc(),
|
||||
)
|
||||
elif req.strategy == "fresh_ck":
|
||||
query = query.order_by(
|
||||
Account.ck_checked_at.is_(None).asc(),
|
||||
Account.ck_checked_at.desc(),
|
||||
Account.id.asc(),
|
||||
)
|
||||
elif req.strategy == "oldest":
|
||||
query = query.order_by(Account.id.asc())
|
||||
else:
|
||||
query = query.order_by(
|
||||
Account.value_tier.is_(None).asc(),
|
||||
Account.value_tier.asc(),
|
||||
Account.ck_checked_at.is_(None).asc(),
|
||||
Account.ck_checked_at.desc(),
|
||||
Account.id.asc(),
|
||||
)
|
||||
|
||||
candidate_ids = [
|
||||
int(account_id)
|
||||
for (account_id,) in (
|
||||
query.with_for_update(skip_locked=True)
|
||||
.limit(req.quantity)
|
||||
.with_entities(Account.id)
|
||||
.all()
|
||||
)
|
||||
]
|
||||
if not candidate_ids:
|
||||
raise HTTPException(status_code=400, detail="当前候选池没有可分配账号")
|
||||
|
||||
accounts = (
|
||||
db.query(Account)
|
||||
.filter(Account.id.in_(candidate_ids))
|
||||
.order_by(Account.id.asc())
|
||||
.all()
|
||||
)
|
||||
db.query(Account).filter(Account.id.in_(candidate_ids)).update(
|
||||
{Account.assigned_to: req.assigned_to},
|
||||
synchronize_session=False,
|
||||
)
|
||||
db.query(DouyuTask).filter(DouyuTask.account_id.in_(candidate_ids)).update(
|
||||
{DouyuTask.account_assigned_to: req.assigned_to},
|
||||
synchronize_session=False,
|
||||
)
|
||||
record_audit(
|
||||
db,
|
||||
current,
|
||||
action="account:assign",
|
||||
target=f"定向分配给 {target.username} {len(candidate_ids)} 个账号",
|
||||
detail={
|
||||
"assigned_to": req.assigned_to,
|
||||
"assigned_username": target.username,
|
||||
"quantity": req.quantity,
|
||||
"count": len(candidate_ids),
|
||||
"strategy": req.strategy,
|
||||
"occupancy": req.occupancy,
|
||||
"commodity_id": req.commodity_id,
|
||||
"mode": "manual_dispatch",
|
||||
},
|
||||
)
|
||||
emit_bulk(
|
||||
db,
|
||||
[
|
||||
{
|
||||
"account_id": account.id,
|
||||
"event_type": EVENT_ASSIGNED,
|
||||
"title": f"定向分配:{target.username}",
|
||||
"category": CATEGORY_OPS,
|
||||
"detail": {
|
||||
"assigned_to": req.assigned_to,
|
||||
"assigned_username": target.username,
|
||||
"mode": "manual_dispatch",
|
||||
},
|
||||
"source": "audit",
|
||||
"actor_type": "user",
|
||||
"actor_id": current.id,
|
||||
}
|
||||
for account in accounts
|
||||
],
|
||||
)
|
||||
db.commit()
|
||||
return {
|
||||
"success": True,
|
||||
"count": len(candidate_ids),
|
||||
"assigned_to": req.assigned_to,
|
||||
"assigned_username": target.username,
|
||||
"account_ids": candidate_ids,
|
||||
"message": f"已从候选池分配 {len(candidate_ids)} 个账号给 {target.username}",
|
||||
}
|
||||
|
||||
|
||||
@router.get("/assignments/summary")
|
||||
def assignments_summary(
|
||||
db: Session = Depends(get_db),
|
||||
@@ -1240,6 +1485,103 @@ def assignments_summary(
|
||||
}
|
||||
|
||||
|
||||
@router.get("/assignments/options")
|
||||
def assignment_options(
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("account:assign")),
|
||||
):
|
||||
"""分配中心的候选池统计与可用皮肤能力。"""
|
||||
del current
|
||||
now = datetime.now(UTC)
|
||||
base_filters = [
|
||||
Account.assigned_to.is_(None),
|
||||
_assignable_account_filter(),
|
||||
]
|
||||
has_lease = active_lease_exists()
|
||||
in_collab = active_collab_order_exists()
|
||||
|
||||
def count_with(*filters) -> int:
|
||||
return int(
|
||||
db.query(func.count(Account.id))
|
||||
.filter(*base_filters, *filters)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
occupancy = {
|
||||
"assignable": count_with(),
|
||||
"free": count_with(~has_lease, ~in_collab),
|
||||
"no_lease": count_with(~has_lease),
|
||||
"no_collab": count_with(~in_collab),
|
||||
"leased": count_with(has_lease),
|
||||
"collab": count_with(in_collab),
|
||||
}
|
||||
presets = {
|
||||
"zero_points": count_with(
|
||||
Account.points.isnot(None),
|
||||
Account.points <= 0,
|
||||
~has_lease,
|
||||
~in_collab,
|
||||
),
|
||||
"elite_points": count_with(
|
||||
Account.elite_opened.is_(True),
|
||||
Account.points > 0,
|
||||
~has_lease,
|
||||
~in_collab,
|
||||
),
|
||||
"high_value": count_with(
|
||||
or_(
|
||||
suit_available_expr("bairimeng"),
|
||||
suit_available_expr("mengtu"),
|
||||
),
|
||||
~has_lease,
|
||||
~in_collab,
|
||||
),
|
||||
}
|
||||
|
||||
skin_rows = (
|
||||
db.query(
|
||||
AccountCommodityState.commodity_id,
|
||||
func.max(AccountCommodityState.commodity_name).label("commodity_name"),
|
||||
AccountCommodityState.scope,
|
||||
func.count(func.distinct(AccountCommodityState.account_id)).label(
|
||||
"account_count"
|
||||
),
|
||||
)
|
||||
.join(Account, Account.id == AccountCommodityState.account_id)
|
||||
.filter(
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.account_eligibility
|
||||
== ACCOUNT_ELIGIBILITY_ELIGIBLE,
|
||||
AccountCommodityState.offer_state.in_(
|
||||
(OFFER_STATE_ACTIVE, OFFER_STATE_BEFORE)
|
||||
),
|
||||
AccountCommodityState.valid_until.isnot(None),
|
||||
AccountCommodityState.valid_until > now,
|
||||
*base_filters,
|
||||
~has_lease,
|
||||
~in_collab,
|
||||
)
|
||||
.group_by(AccountCommodityState.commodity_id, AccountCommodityState.scope)
|
||||
.order_by(func.count(func.distinct(AccountCommodityState.account_id)).desc())
|
||||
.limit(200)
|
||||
.all()
|
||||
)
|
||||
return {
|
||||
"occupancy": occupancy,
|
||||
"presets": presets,
|
||||
"skins": [
|
||||
{
|
||||
"commodity_id": str(commodity_id),
|
||||
"commodity_name": str(commodity_name or commodity_id),
|
||||
"scope": str(scope or "elite"),
|
||||
"account_count": int(account_count or 0),
|
||||
}
|
||||
for commodity_id, commodity_name, scope, account_count in skin_rows
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@router.put("/{account_id}/lifecycle")
|
||||
def set_account_lifecycle(
|
||||
account_id: int,
|
||||
|
||||
@@ -227,6 +227,12 @@ class AccountBulkSelection(BaseModel):
|
||||
elite_opened: bool | None = None
|
||||
esports_opened: bool | None = None
|
||||
role_bind_status: str = ""
|
||||
#: 账号占用:free=无租约且不在预售台账 / no_lease / no_collab /
|
||||
#: leased=有生效租约 / collab=在活动预售台账 / 空=不限制
|
||||
occupancy: str = ""
|
||||
#: 指定斗鱼商品/皮肤能力;只匹配新鲜且账号级可兑换的快照
|
||||
commodity_id: str = ""
|
||||
commodity_scope: str = "elite"
|
||||
|
||||
|
||||
class AccountExportRequest(AccountBulkSelection):
|
||||
@@ -246,6 +252,21 @@ class BatchAssign(AccountBulkSelection):
|
||||
assigned_to: int | None = None
|
||||
|
||||
|
||||
class AssignmentDispatch(AccountBulkSelection):
|
||||
"""按当前候选池定量发放给指定客服。
|
||||
|
||||
这是人工“发送账号”的统一入口;后续客服自助领取可以复用同一组筛选和
|
||||
定量选择逻辑,只替换权限与目标客服来源。
|
||||
"""
|
||||
|
||||
assign_status: str = "unassigned"
|
||||
assignable_only: bool = True
|
||||
occupancy: str = "free"
|
||||
assigned_to: int = Field(..., gt=0)
|
||||
quantity: int = Field(10, ge=1, le=500)
|
||||
strategy: str = Field("balanced", pattern="^(balanced|points|fresh_ck|oldest)$")
|
||||
|
||||
|
||||
class AccountBulkTag(AccountBulkSelection):
|
||||
tag_value: str = ""
|
||||
|
||||
@@ -303,6 +324,8 @@ class AccountOut(BaseModel):
|
||||
ck_checked_at: datetime | None = None
|
||||
points: int | None = None
|
||||
gold_balance: int | None = None
|
||||
elite_opened: bool | None = None
|
||||
esports_opened: bool | None = None
|
||||
role_bind_status: str = "unknown"
|
||||
role_bind_label: str = ""
|
||||
esports_bind_status: str = "unknown"
|
||||
@@ -321,6 +344,9 @@ class AccountOut(BaseModel):
|
||||
quality_score: int | None = None
|
||||
redundancy_class: str = ""
|
||||
ck_remaining_hours: int | None = None
|
||||
active_lease_purpose: str = ""
|
||||
active_lease_expires_at: datetime | None = None
|
||||
in_active_collab: bool = False
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
@@ -349,6 +375,8 @@ class AccountOut(BaseModel):
|
||||
"ck_checked_at": _isoformat(self.ck_checked_at),
|
||||
"points": self.points,
|
||||
"gold_balance": self.gold_balance,
|
||||
"elite_opened": self.elite_opened,
|
||||
"esports_opened": self.esports_opened,
|
||||
"role_bind_status": self.role_bind_status,
|
||||
"role_bind_label": self.role_bind_label,
|
||||
"esports_bind_status": self.esports_bind_status,
|
||||
@@ -367,6 +395,9 @@ class AccountOut(BaseModel):
|
||||
"quality_score": self.quality_score,
|
||||
"redundancy_class": self.redundancy_class,
|
||||
"ck_remaining_hours": self.ck_remaining_hours,
|
||||
"active_lease_purpose": self.active_lease_purpose,
|
||||
"active_lease_expires_at": _isoformat(self.active_lease_expires_at),
|
||||
"in_active_collab": self.in_active_collab,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ import type {
|
||||
AccountBulkTagRequest,
|
||||
AccountExportRequest,
|
||||
AccountOverview,
|
||||
AssignmentDispatchRequest,
|
||||
AssignmentOptions,
|
||||
AssignmentsSummary,
|
||||
BasicSummary,
|
||||
BatchAssignRequest,
|
||||
@@ -42,6 +44,9 @@ export const accountApi = {
|
||||
elite_opened?: boolean;
|
||||
esports_opened?: boolean;
|
||||
role_bind_status?: string;
|
||||
occupancy?: string;
|
||||
commodity_id?: string;
|
||||
commodity_scope?: string;
|
||||
sort?: 'username' | 'status' | 'bind' | 'points' | 'high_value' | 'exchange' | 'tag';
|
||||
order?: 'asc' | 'desc';
|
||||
},
|
||||
@@ -69,6 +74,9 @@ export const accountApi = {
|
||||
esports_opened?: boolean;
|
||||
role_bind_status?: string;
|
||||
account_names?: string;
|
||||
occupancy?: string;
|
||||
commodity_id?: string;
|
||||
commodity_scope?: string;
|
||||
},
|
||||
) =>
|
||||
api.get<
|
||||
@@ -87,6 +95,13 @@ export const accountApi = {
|
||||
// 同一接口的选择集版本:可带筛选条件(all_matching),供「按筛选全选」分配
|
||||
batchAssignSelection: (data: BatchAssignRequest) =>
|
||||
api.post<MessageCountResponse, MessageCountResponse>('/accounts/batch-assign', data),
|
||||
dispatchAssignments: (data: AssignmentDispatchRequest) =>
|
||||
api.post<
|
||||
MessageCountResponse & { account_ids: number[]; assigned_username: string },
|
||||
MessageCountResponse & { account_ids: number[]; assigned_username: string }
|
||||
>('/accounts/assignments/dispatch', data),
|
||||
assignmentOptions: () =>
|
||||
api.get<AssignmentOptions, AssignmentOptions>('/accounts/assignments/options'),
|
||||
assignmentsSummary: () =>
|
||||
api.get<AssignmentsSummary, AssignmentsSummary>('/accounts/assignments/summary'),
|
||||
setRemark: (id: number, remark: string) =>
|
||||
|
||||
@@ -51,6 +51,9 @@ export interface AccountBulkSelection {
|
||||
elite_opened?: boolean;
|
||||
esports_opened?: boolean;
|
||||
role_bind_status?: string;
|
||||
occupancy?: string;
|
||||
commodity_id?: string;
|
||||
commodity_scope?: string;
|
||||
}
|
||||
|
||||
export interface AccountExportRequest extends AccountBulkSelection {
|
||||
@@ -66,6 +69,12 @@ export interface BatchAssignRequest extends AccountBulkSelection {
|
||||
assigned_to?: number | null;
|
||||
}
|
||||
|
||||
export interface AssignmentDispatchRequest extends AccountBulkSelection {
|
||||
assigned_to: number;
|
||||
quantity: number;
|
||||
strategy?: 'balanced' | 'points' | 'fresh_ck' | 'oldest';
|
||||
}
|
||||
|
||||
export interface PaginatedResponse<T> {
|
||||
items: T[];
|
||||
total: number;
|
||||
@@ -439,6 +448,8 @@ export interface AccountItem {
|
||||
ck_checked_at: string | null;
|
||||
points: number | null;
|
||||
gold_balance: number | null;
|
||||
elite_opened: boolean | null;
|
||||
esports_opened: boolean | null;
|
||||
role_bind_status: BindStatus;
|
||||
role_bind_label: string;
|
||||
esports_bind_status: BindStatus;
|
||||
@@ -457,6 +468,9 @@ export interface AccountItem {
|
||||
quality_score?: number | null;
|
||||
redundancy_class?: string;
|
||||
ck_remaining_hours?: number | null;
|
||||
active_lease_purpose: string;
|
||||
active_lease_expires_at: string | null;
|
||||
in_active_collab: boolean;
|
||||
}
|
||||
|
||||
export interface AssignmentsSummary {
|
||||
@@ -472,6 +486,30 @@ export interface SupportUserItem {
|
||||
assigned_count: number;
|
||||
}
|
||||
|
||||
export interface AssignmentSkinOption {
|
||||
commodity_id: string;
|
||||
commodity_name: string;
|
||||
scope: string;
|
||||
account_count: number;
|
||||
}
|
||||
|
||||
export interface AssignmentOptions {
|
||||
occupancy: {
|
||||
assignable: number;
|
||||
free: number;
|
||||
no_lease: number;
|
||||
no_collab: number;
|
||||
leased: number;
|
||||
collab: number;
|
||||
};
|
||||
presets: {
|
||||
zero_points: number;
|
||||
elite_points: number;
|
||||
high_value: number;
|
||||
};
|
||||
skins: AssignmentSkinOption[];
|
||||
}
|
||||
|
||||
// ==================== Login Task ====================
|
||||
|
||||
export interface LoginTaskLogItem {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user