feat(account): S3 生命周期状态收口 —— 6 状态状态机、唯一推导入口、停用/归档闭环
`status` 列从「空壳」变成可信状态:S1 建了列、S2 让账号独立,本提交让状态真正
反映账号处境,并把「哪些账号要人处理」变成可筛选的事实。
## 状态机(7 → 6 个状态)
`imported` / `active` / `invalid` / `manual` / `disabled` / `archived`
**砍掉 `relogin`**:它完全等价于 `invalid + relogin_next_at is not None`,
独立成状态就多出一个必须与 `relogin_next_at` 同步的字段 —— 正是要消灭的「双份真相」。
## 唯一推导入口(防「双份真相」)
新增 `services/account_lifecycle.py`:
- `derive_status(account)` —— **纯函数**,按 cookie / ck_check_status / relogin_*
推导状态;人工态(disabled/archived)优先级最高,不被覆盖;
- `sync_status(db, account)` —— 推导 + 变了才写,**幂等**(没变则不写库、不发事件,
否则巡检每轮都往时间线刷一条);
- `set_manual_state(db, account, state)` —— 停用/归档/`auto`(恢复自动推导);
- `on_login_success(db, account)` —— **「登录成功对账号意味着什么」只定义一次**:
作废本轮重登退避痕迹 + 重算状态。于是**「待人工」的账号被人工登录救回来时状态
会自动恢复**,而不是卡在 manual。
- `MANUAL_KINDS` 从 `jobs/ck_relogin.py` 挪到这里(service 不该反向依赖 job)。
**9 处底层判据写入点全部接线**:登录成功、CK 清除(单个/批量)、ck_health 巡检、
人工 CK 检测、工作台执行前检测、重登复检、重登结算(成功/失败退避)。
## 消费端:补上一个真缺口 + 删掉两处临时派生
- ⚠️ **缺口**:`ck_health` / `ck_relogin` / `count_pending_relogin` 此前只按底层判据
选行,**没排除 disabled/archived** —— S2 提交里已标注,状态一上线就会变成
「停用的账号照样被自动重登」。现已全部排除。
- `automation.manual_queue` 原本用 4 个条件自己拼「需要人工」,现在一句
`status == 'manual'`。同一判据从「3 处各写一遍」收敛成「1 处推导」。
## 接口与前端
- `PUT /api/accounts/{id}/lifecycle`(disabled/archived/auto + 原因,写审计)
- `PUT /api/accounts/{id}/automation`(**接上死开关**:`auto_managed` 此前没有任何
接口/前端能改,永远是默认 True)
- 新增权限点 `account:manage`(运营默认拥有)
- 列表支持 `?status=` 筛选;**归档默认不出现**(要看需显式筛选)
- `AccountOut` / 概览接口补 status / status_label / status_reason / auto_managed
- 前端:账号列表加「状态」列与状态筛选;详情页状态卡 + 停用/归档/恢复/自动化开关
## 两个自查出来的问题(都已修)
1. **幂等键反而丢事件**:状态事件一开始用 `status:{id}:{target}:{秒级时间戳}` 作
`source_ref`,结果同一账号在同一秒内两次切到同一状态就撞唯一键,`emit` 静默吞掉
—— **状态改了、时间线却少一条**,正是本模块要消灭的「悄悄不一致」。
改为不传 `source_ref`:`sync_status` 本身已幂等,不存在重复写入路径。已加用例。
2. **迁移与运行时规则分叉**:迁移 0042 的状态回填 CASE 原本会写出已被砍掉的
`relogin`,且判据与 `derive_status` 不一致(次数用尽的会回填成 invalid 而非 manual)。
已改成逐字对应,并加一条用例**真跑那段 SQL 与纯函数逐条比对**(已验证:
临时改回旧 CASE 该用例立刻红)。
## 验证
- `TZ=Asia/Shanghai`:**404 passed / 6 skipped**(S2 后 373 → 新增 31 条)
- 迁移链 3 passed;ruff check/format 全绿;前端 `tsc -b && vite build` 通过
- 真实开发库核对:落库状态分布 active 4 / imported 8,**与 derive_status 逐账号比对无漂移**
- 新增用例覆盖:derive 真值表(每状态入口 + 人工态优先)、sync 幂等、
人工态切换、**全生命周期走查**(导入→登录→失效→重登→转人工→人工救回→清 CK→停用)、
自动化排除 disabled/archived、列表状态筛选、接口
未做(后续阶段):S4 事件发射点补齐、S5 业务事实直发事件、S6 账号页重构。
This commit is contained in:
@@ -20,9 +20,11 @@ from ..models import (
|
||||
from ..permissions import user_has_permission
|
||||
from ..schemas import (
|
||||
AccountAssign,
|
||||
AccountAutomationUpdate,
|
||||
AccountBulkSelection,
|
||||
AccountBulkTag,
|
||||
AccountImport,
|
||||
AccountLifecycleUpdate,
|
||||
AccountOut,
|
||||
AccountTag,
|
||||
BatchAssign,
|
||||
@@ -34,6 +36,11 @@ from ..services.account_events_service import (
|
||||
EVENT_EXCHANGE,
|
||||
EVENT_RECHARGE,
|
||||
)
|
||||
from ..services.account_lifecycle import (
|
||||
STATUS_ARCHIVED,
|
||||
STATUS_LABELS,
|
||||
set_manual_state,
|
||||
)
|
||||
from ..services.account_service import (
|
||||
cookie_account_ids_query,
|
||||
parse_and_build_accounts,
|
||||
@@ -53,6 +60,16 @@ def _visible_accounts_query(db: Session, current: User):
|
||||
raise HTTPException(status_code=403, detail="无权查看账号")
|
||||
|
||||
|
||||
def _query_str(value) -> str:
|
||||
"""把查询参数安全地取成字符串。
|
||||
|
||||
存在的理由:本仓库的测试**直接调用路由函数**(不经 FastAPI),此时
|
||||
`Query(...)` 的默认值不会被解析,参数拿到的仍是 `Query` 对象 ——
|
||||
对它调 `.strip()` 会炸。这里统一兜住,免得每加一个筛选参数就要改一遍用例。
|
||||
"""
|
||||
return value.strip() if isinstance(value, str) else ""
|
||||
|
||||
|
||||
def _filter_accounts_query(
|
||||
db: Session,
|
||||
query,
|
||||
@@ -62,15 +79,24 @@ def _filter_accounts_query(
|
||||
tag: str | None = None,
|
||||
has_cookie: bool = False,
|
||||
search: str = "",
|
||||
status: str = "",
|
||||
):
|
||||
"""复用列表筛选条件,供分页列表和全量批量操作保持一致。"""
|
||||
if has_cookie:
|
||||
query = query.filter(Account.id.in_(cookie_account_ids_query(db)))
|
||||
if assigned_only and user_has_permission(current, "account:view_all"):
|
||||
query = query.filter(Account.assigned_to.isnot(None))
|
||||
if tag:
|
||||
query = query.filter(Account.tag == tag)
|
||||
search_text = (search or "").strip()
|
||||
tag_value = _query_str(tag)
|
||||
if tag_value:
|
||||
query = query.filter(Account.tag == tag_value)
|
||||
status_value = _query_str(status)
|
||||
if status_value:
|
||||
query = query.filter(Account.status == status_value)
|
||||
else:
|
||||
# 归档账号默认不出现 —— 「归档」本身就是「收起来别烦我」的意思。
|
||||
# 要看它们必须显式带 status=archived。
|
||||
query = query.filter(Account.status != STATUS_ARCHIVED)
|
||||
search_text = _query_str(search)
|
||||
if search_text:
|
||||
pattern = f"%{search_text}%"
|
||||
query = query.filter(
|
||||
@@ -138,6 +164,7 @@ def list_accounts(
|
||||
tag: str = Query(None),
|
||||
has_cookie: bool = Query(False),
|
||||
search: str = Query(""),
|
||||
status: str = Query("", description="按生命周期状态筛选;空=全部(不含归档)"),
|
||||
page: int | None = Query(None, ge=1),
|
||||
page_size: int = Query(20, ge=1, le=200),
|
||||
include_sensitive: bool = Query(False),
|
||||
@@ -153,6 +180,7 @@ def list_accounts(
|
||||
tag=tag,
|
||||
has_cookie=has_cookie,
|
||||
search=search,
|
||||
status=status,
|
||||
)
|
||||
|
||||
total = None
|
||||
@@ -185,6 +213,10 @@ def list_accounts(
|
||||
assigned_to=acc.assigned_to,
|
||||
assigned_username=acc.assigned_user.username if acc.assigned_user else None,
|
||||
created_at=acc.created_at,
|
||||
status=acc.status or "",
|
||||
status_label=STATUS_LABELS.get(acc.status or "", acc.status or ""),
|
||||
status_reason=acc.status_reason or "",
|
||||
auto_managed=bool(acc.auto_managed),
|
||||
)
|
||||
# 只有管理员可看完整字段(密码、邮箱等)
|
||||
if can_include_sensitive:
|
||||
@@ -415,6 +447,85 @@ def assignments_summary(
|
||||
}
|
||||
|
||||
|
||||
@router.put("/{account_id}/lifecycle")
|
||||
def set_account_lifecycle(
|
||||
account_id: int,
|
||||
req: AccountLifecycleUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("account:manage")),
|
||||
):
|
||||
"""人工停用 / 归档 / 恢复账号(生命周期状态机,见 `services/account_lifecycle`)。
|
||||
|
||||
- `disabled`:账号不可用(封号、退单、归还)—— 自动化**不再碰它**;
|
||||
- `archived`:软删除,默认列表不再展示;
|
||||
- `auto`:解除人工态,立刻按底层判据重新推导(例如账号又能用了)。
|
||||
"""
|
||||
acc = db.query(Account).filter(Account.id == account_id).first()
|
||||
if not acc:
|
||||
raise HTTPException(status_code=404, detail="账号不存在")
|
||||
if acc.status == req.status:
|
||||
return {"message": "状态未变化", "status": acc.status, "success": True}
|
||||
|
||||
set_manual_state(
|
||||
db,
|
||||
acc,
|
||||
req.status,
|
||||
reason=req.reason or "",
|
||||
actor_id=current.id,
|
||||
actor_type="user",
|
||||
)
|
||||
db.add(
|
||||
AuditLog(
|
||||
user_id=current.id,
|
||||
username=current.username,
|
||||
action=f"account:lifecycle:{req.status}",
|
||||
target=acc.username,
|
||||
account_id=acc.id,
|
||||
detail=req.reason or "",
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
return {
|
||||
"message": "状态已更新",
|
||||
"status": acc.status,
|
||||
"success": True,
|
||||
}
|
||||
|
||||
|
||||
@router.put("/{account_id}/automation")
|
||||
def set_account_automation(
|
||||
account_id: int,
|
||||
req: AccountAutomationUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("account:manage")),
|
||||
):
|
||||
"""自动化开关。
|
||||
|
||||
与 `status=disabled` 的区别:`auto_managed=False` 的账号**仍然可用**
|
||||
(人照样能手动跑任务),只是巡检/自动重登绕过它 —— 用于敏感账号人工维护。
|
||||
"""
|
||||
acc = db.query(Account).filter(Account.id == account_id).first()
|
||||
if not acc:
|
||||
raise HTTPException(status_code=404, detail="账号不存在")
|
||||
acc.auto_managed = bool(req.auto_managed)
|
||||
db.add(
|
||||
AuditLog(
|
||||
user_id=current.id,
|
||||
username=current.username,
|
||||
action="account:automation",
|
||||
target=acc.username,
|
||||
account_id=acc.id,
|
||||
detail="参与自动化" if req.auto_managed else "不参与自动化",
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
return {
|
||||
"message": "自动化开关已更新",
|
||||
"auto_managed": acc.auto_managed,
|
||||
"success": True,
|
||||
}
|
||||
|
||||
|
||||
@router.put("/{account_id}/tag")
|
||||
def set_account_tag(
|
||||
account_id: int,
|
||||
@@ -654,6 +765,12 @@ def account_overview(
|
||||
"remark": acc.remark,
|
||||
"assigned_to": acc.assigned_to,
|
||||
"created_at": acc.created_at, # 导入时间
|
||||
# 生命周期状态(账号现在到底能不能用、要不要人处理)
|
||||
"status": acc.status or "",
|
||||
"status_label": STATUS_LABELS.get(acc.status or "", acc.status or ""),
|
||||
"status_reason": acc.status_reason or "",
|
||||
"status_changed_at": acc.status_changed_at,
|
||||
"auto_managed": bool(acc.auto_managed),
|
||||
# 当前状态(覆盖式字段)
|
||||
"points": acc.points,
|
||||
"gold_balance": acc.gold_balance,
|
||||
@@ -667,7 +784,6 @@ def account_overview(
|
||||
"esports_bind_status": acc.esports_bind_status,
|
||||
"esports_game_name": acc.esports_game_name,
|
||||
"esports_can_change_time": acc.esports_can_change_time,
|
||||
"auto_managed": acc.auto_managed,
|
||||
# 统计
|
||||
"stats": {
|
||||
"recharge_total": int(stats.get("recharge_total") or 0),
|
||||
|
||||
Reference in New Issue
Block a user