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:
yml2213
2026-10-07 12:29:08 +08:00
parent 281769b440
commit 18bc7be4b1
38 changed files with 5143 additions and 35 deletions
+75 -1
View File
@@ -7,7 +7,7 @@ import type { TableProps } from 'antd';
import {
ArrowLeftOutlined, EditOutlined, ReloadOutlined, ThunderboltOutlined,
} from '@ant-design/icons';
import { accountApi, douyuApi, userApi, type AccountEventItem, type AccountOverview, type UserInfo } from '../api/modules';
import { accountApi, douyuApi, douyuPoolApi, userApi, type AccountEventItem, type AccountOverview, type DouyuPoolLease, type UserInfo } from '../api/modules';
import AccountOverviewPanel from './douyu-tasks/AccountOverviewPanel';
import { getErrorMessage } from '../utils/error';
import { message } from '../utils/antdMessage';
@@ -100,6 +100,7 @@ export default function AccountDetailPage() {
const [eventType, setEventType] = useState('');
const [category, setCategory] = useState('');
const [eventsLoading, setEventsLoading] = useState(false);
const [leases, setLeases] = useState<DouyuPoolLease[]>([]);
const [tagDraft, setTagDraft] = useState('');
// 一键查询:向斗鱼实时查一次该账号的全部关键信息(异步任务 + 轮询结果)
const [ovLoading, setOvLoading] = useState(false);
@@ -209,6 +210,22 @@ export default function AccountDetailPage() {
void loadEvents();
}, [loadEvents]);
// 租赁记录:池化后「这个账号被谁租过、用完还回去没有」。
// 没有 `account:pool_use` 权限的账号会 403 —— 静默留空,不影响页面其它部分。
useEffect(() => {
if (!accountId) return;
let alive = true;
(async () => {
try {
const rows = await douyuPoolApi.leasesOfAccount(accountId);
if (alive) setLeases(rows);
} catch {
if (alive) setLeases([]);
}
})();
return () => { alive = false; };
}, [accountId]);
const columns: TableProps<AccountEventItem>['columns'] = [
{
title: '时间',
@@ -238,6 +255,28 @@ export default function AccountDetailPage() {
render: (val: number | null) =>
val === null || val === undefined ? <Text type="secondary">-</Text> : val,
},
{
// 池化后客服不再长期持有账号,「这件事是谁做的」必须一眼看得到。
// 历史事件(占 71%)没有操作人 —— 如实显示「-」,不要假装是系统做的。
title: '操作人 / 渠道',
width: 170,
render: (_val, record) => {
const channel = (record.detail?.channel as string | undefined) || '';
const channelLabel = channel === 'supplier_api'
? 'API 直充'
: channel === 'wechat_qr'
? '微信扫码'
: channel;
return (
<Space size={4} wrap>
{record.actor_name
? <Tag color="blue">{record.actor_name}</Tag>
: <Text type="secondary">-</Text>}
{channelLabel && <Tag>{channelLabel}</Tag>}
</Space>
);
},
},
];
if (!accountId) {
@@ -678,6 +717,41 @@ export default function AccountDetailPage() {
</Spin>
<Card
title="租赁记录"
extra={<Text type="secondary" style={{ fontSize: 12 }}>客服按需租用,用完自动收回</Text>}
>
<Table
rowKey="id"
size="small"
dataSource={leases}
pagination={false}
locale={{ emptyText: <Empty description="暂无租赁记录" /> }}
columns={[
{
title: '客服', dataIndex: 'holder_name', width: 120,
render: (value: string, row: DouyuPoolLease) => value || `#${row.holder_user_id}`,
},
{ title: '商品', dataIndex: 'commodity_id', width: 200 },
{ title: '批次', dataIndex: 'batch_id', width: 130 },
{
title: '状态', dataIndex: 'status', width: 100,
render: (value: string) => (
<Tag color={value === 'active' ? 'green' : 'default'}>{value}</Tag>
),
},
{
title: '租用时间', dataIndex: 'leased_at', width: 170,
render: (value: string | null) => (value ? formatTime(value) : '-'),
},
{
title: '释放时间', dataIndex: 'expires_at', width: 170,
render: (value: string | null) => (value ? formatTime(value) : '-'),
},
]}
/>
</Card>
<Card
title="账号时间线"
extra={