feat(pool): 账号自动分配与回收(租约 + 补给链 + 独立工作台)
在既有「管理员分配 → 客服导入工作台 → 手动选号」之外**新增**一条 「客服选皮肤 + 数量 → 系统分配账号 + 按需补积分 → 用完自动收回」的路径。 旧路径只做并集与可选参数扩展,行为不变(有用例守着)。 - 归属与使用权分离:新增 account_leases(生成列 + 唯一索引,并发抢号由数据库裁决), assigned_to 收窄为管理员/运营的维护责任;客服可见范围扩为「名下 ∪ 租用中」 - account_pool 服务:候选筛选、两梯队排序(先花快过期的 / 先选充值少的)、 供给与成本预估、租约领取/释放/回收 - 补给链:查余额 → 充鱼翅(单次 ≤800 元,超限拆单)→ 赠精英令 → 兑换; 运行期两道闸门:_skip_when(余额已够就别再花钱)、_depends_on(前置未成功就跳过) - 转人工:鱼翅支付等 10 分钟未到账转 manual_required,租约先不还; account_lease_reaper 兜 TTL 到期 / 漏释放 / 孤儿批次 - 运营流水:业务事件补 actor_id = task.created_by;账号详情页展示操作人、渠道 与租赁记录 - 新工作台:/douyu/pool + 菜单「账号分配台」(新建,不改造旧工作台) - 修复 D1:官方补录的兑换事件漏写 subject_key(10,612 条主键为空), 会让 365 天同皮肤规则漏判 87% 的历史;补代码 + 回填脚本 - 迁移 20261007_0052;权限 account:pool_use / account:pool_admin 测试:766 passed, 6 skipped(改动前基线 727);ruff 与 tsc -b 干净。 设计稿与实施记录:docs/设计-账号自动分配与回收.md
This commit is contained in:
@@ -11,6 +11,7 @@ from ..deps import get_current_user, require_permission
|
||||
from ..models import (
|
||||
Account,
|
||||
AccountEvent,
|
||||
AccountLease,
|
||||
AuditLog,
|
||||
DouyuTask,
|
||||
DouyuWorkbenchAccount,
|
||||
@@ -72,12 +73,23 @@ router = APIRouter(prefix="/api/accounts", tags=["账号管理"])
|
||||
|
||||
|
||||
def _visible_accounts_query(db: Session, current: User):
|
||||
"""返回当前用户可见的斗鱼账号查询。"""
|
||||
"""返回当前用户可见的斗鱼账号查询。
|
||||
|
||||
可见 = **我名下** ∪ **我当前持有有效租约**。
|
||||
后半条是账号池加的:池化后客服名下不再挂账号(归属是管理员/运营的维护责任),
|
||||
不把自己租到的号算进来,客服连刚做完的那单都看不到。
|
||||
"""
|
||||
query = db.query(Account)
|
||||
if user_has_permission(current, "account:view_all"):
|
||||
return query
|
||||
if user_has_permission(current, "account:view_assigned"):
|
||||
return query.filter(Account.assigned_to == current.id)
|
||||
my_leases = select(AccountLease.account_id).where(
|
||||
AccountLease.holder_user_id == current.id,
|
||||
AccountLease.status == "active",
|
||||
)
|
||||
return query.filter(
|
||||
or_(Account.assigned_to == current.id, Account.id.in_(my_leases))
|
||||
)
|
||||
raise HTTPException(status_code=403, detail="无权查看账号")
|
||||
|
||||
|
||||
@@ -1428,15 +1440,21 @@ def account_events(
|
||||
category: str = Query(
|
||||
"", description="按粗分类筛选:lifecycle / auth / business / ops"
|
||||
),
|
||||
actor_id: int | None = Query(
|
||||
None, description="按操作人筛选:谁做的这件事(池化后按客服查流水)"
|
||||
),
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""账号时间线(倒序分页)。
|
||||
|
||||
权限沿用账号可见性:客服只能看自己名下账号的事件。
|
||||
权限沿用账号可见性:客服能看**自己名下 ∪ 自己租用中**的账号事件。
|
||||
|
||||
`category` 是给页面用的「大分类」过滤(状态变迁 / 登录与 CK / 业务 / 人工操作),
|
||||
比逐一点 `event_type` 好用:客服最常看的是「人工操作」与「状态变迁」两条线。
|
||||
|
||||
`actor_id` 是池化后新增的:客服不再长期持有账号,所以"这个账号被谁动过、
|
||||
动了什么"必须能按人查 —— 配合 `_task_actor` 落库的 `actor_id` 形成完整流水。
|
||||
"""
|
||||
acc = _visible_accounts_query(db, current).filter(Account.id == account_id).first()
|
||||
if not acc:
|
||||
@@ -1454,6 +1472,9 @@ def account_events(
|
||||
if sql_category:
|
||||
where += " AND category = :category"
|
||||
params["category"] = sql_category
|
||||
if isinstance(actor_id, int) and not isinstance(actor_id, bool):
|
||||
where += " AND actor_id = :actor_id"
|
||||
params["actor_id"] = int(actor_id)
|
||||
|
||||
total = (
|
||||
db.execute(
|
||||
@@ -1480,8 +1501,23 @@ def account_events(
|
||||
.all()
|
||||
)
|
||||
|
||||
items = [dict(row) for row in rows]
|
||||
# 补上操作人的用户名:池化后「谁做的」是这一屏最重要的信息之一,
|
||||
# 前端拿到 id 还要再查一次不值当。一次 IN 查询即可。
|
||||
actor_ids = {item.get("actor_id") for item in items if item.get("actor_id")}
|
||||
actor_names: dict[int, str] = {}
|
||||
if actor_ids:
|
||||
actor_names = {
|
||||
user_id: name
|
||||
for user_id, name in db.query(User.id, User.username)
|
||||
.filter(User.id.in_(actor_ids))
|
||||
.all()
|
||||
}
|
||||
for item in items:
|
||||
item["actor_name"] = actor_names.get(item.get("actor_id"), "")
|
||||
|
||||
return {
|
||||
"items": [dict(row) for row in rows],
|
||||
"items": items,
|
||||
"total": int(total),
|
||||
"page": page,
|
||||
"page_size": page_size,
|
||||
|
||||
Reference in New Issue
Block a user