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
+282
View File
@@ -0,0 +1,282 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import {
Alert, Button, Card, Col, Descriptions, InputNumber, Row, Select, Space,
Statistic, Table, Tag, Typography, theme,
} from 'antd';
import { ReloadOutlined, ThunderboltOutlined } from '@ant-design/icons';
import { message } from '../utils/antdMessage';
import { getErrorMessage } from '../utils/error';
import { douyuPoolApi } from '../api/modules';
import type {
DouyuPoolAccount, DouyuPoolExchangeResult, DouyuPoolLease, DouyuPoolSupply,
} from '../api/modules';
import type { ColumnsType } from 'antd/es/table';
const { Text, Paragraph } = Typography;
/**
* 账号分配台(**新建的工作台**,不是改造精英宝典页)。
*
* 客服在这里只做三件事:选皮肤 → 填数量 → 提交,剩下的(领号、补积分、兑换、
* 用完自动收回)由系统做。旧工作台的手动选号路径完整保留,两者互不影响。
*/
export default function DouyuPoolPage() {
const { token } = theme.useToken();
const [goods, setGoods] = useState<{ commodity_id: string; name: string; score: number | null }[]>([]);
const [commodityId, setCommodityId] = useState<string>('');
const [quantity, setQuantity] = useState(1);
const [allowTopup, setAllowTopup] = useState(true);
const [supply, setSupply] = useState<DouyuPoolSupply | null>(null);
const [result, setResult] = useState<DouyuPoolExchangeResult | null>(null);
const [leases, setLeases] = useState<DouyuPoolLease[]>([]);
const [loading, setLoading] = useState(false);
const [previewing, setPreviewing] = useState(false);
const loadGoods = useCallback(async () => {
try {
const rows = await douyuPoolApi.goods();
const usable = rows.filter((item) => (item.score ?? 0) > 0);
setGoods(usable);
if (usable.length > 0 && !commodityId) setCommodityId(usable[0].commodity_id);
} catch (e: unknown) {
message.error(getErrorMessage(e));
}
}, [commodityId]);
const loadLeases = useCallback(async () => {
try {
setLeases(await douyuPoolApi.leases(true));
} catch (e: unknown) {
message.error(getErrorMessage(e));
}
}, []);
useEffect(() => { void loadGoods(); }, [loadGoods]);
useEffect(() => { void loadLeases(); }, [loadLeases]);
const loadSupply = useCallback(async () => {
if (!commodityId) return;
setPreviewing(true);
try {
setSupply(await douyuPoolApi.supply('elite', commodityId));
} catch (e: unknown) {
setSupply(null);
message.error(getErrorMessage(e));
} finally {
setPreviewing(false);
}
}, [commodityId]);
useEffect(() => { void loadSupply(); }, [loadSupply]);
const submit = async () => {
if (!commodityId) { message.warning('请先选择皮肤'); return; }
setLoading(true);
try {
const data = await douyuPoolApi.exchange({
scope: 'elite', commodity_id: commodityId, quantity,
allow_topup: allowTopup, allow_fallback: true,
});
setResult(data);
if (data.shortfall > 0) {
message.warning(`实配 ${data.allocated} / 需求 ${data.requested},缺 ${data.shortfall}`);
} else {
message.success(`已分配 ${data.allocated} 个账号,批次 ${data.batch_id}`);
}
await Promise.all([loadSupply(), loadLeases()]);
} catch (e: unknown) {
message.error(getErrorMessage(e));
} finally {
setLoading(false);
}
};
const releaseLease = async (leaseId: number) => {
try {
await douyuPoolApi.releaseLease(leaseId);
message.success('已释放');
await Promise.all([loadSupply(), loadLeases()]);
} catch (e: unknown) {
message.error(getErrorMessage(e));
}
};
const goodsOptions = useMemo(
() => goods.map((item) => ({
value: item.commodity_id,
label: `${item.name}(${item.score ?? 0} 分)`,
})),
[goods],
);
const accountColumns: ColumnsType<DouyuPoolAccount> = [
{ title: '账号ID', dataIndex: 'account_id', width: 90 },
{ title: '账号', dataIndex: 'username', ellipsis: true },
{
title: '梯队', dataIndex: 'tier', width: 90,
render: (tier: string) => (
<Tag color={tier === 'A' ? 'green' : 'orange'}>
{tier === 'A' ? '免充值' : '需充值'}
</Tag>
),
},
{ title: '积分', dataIndex: 'points', width: 90 },
{
title: '需充值', width: 160,
render: (_, row) => (row.topup_yuan > 0
? <Text type="warning">{row.topup_yuan} 元({row.recharge_orders.join(' + ')})</Text>
: <Text type="secondary">无需</Text>),
},
{
title: '可换绑时间', dataIndex: 'role_change_available_at', width: 180,
render: (value: string | null) => (value ? value.replace('T', ' ').slice(0, 19) : '—'),
},
];
const leaseColumns: ColumnsType<DouyuPoolLease> = [
{ title: '账号', dataIndex: 'username', ellipsis: true },
{ title: '商品', dataIndex: 'commodity_id', width: 200 },
{ title: '批次', dataIndex: 'batch_id', width: 130 },
{
title: '到期', dataIndex: 'expires_at', width: 170,
render: (value: string | null) => (value ? value.replace('T', ' ').slice(0, 19) : '—'),
},
{
title: '操作', width: 100,
render: (_, row) => (
<Button size="small" danger onClick={() => void releaseLease(row.id)}>释放</Button>
),
},
];
return (
<Space direction="vertical" size={16} style={{ width: '100%' }}>
<Card
title="发起兑换(系统自动分配账号)"
extra={(
<Button icon={<ReloadOutlined />} onClick={() => { void loadGoods(); void loadSupply(); void loadLeases(); }}>
刷新
</Button>
)}
>
<Row gutter={[16, 16]} align="bottom">
<Col xs={24} md={10}>
<Text type="secondary">皮肤</Text>
<Select
showSearch
optionFilterProp="label"
style={{ width: '100%' }}
placeholder="选择要兑换的皮肤"
value={commodityId || undefined}
onChange={setCommodityId}
options={goodsOptions}
/>
</Col>
<Col xs={12} md={4}>
<Text type="secondary">需要几个账号</Text>
<InputNumber
min={1}
max={50}
style={{ width: '100%' }}
value={quantity}
onChange={(value) => setQuantity(Number(value) || 1)}
/>
</Col>
<Col xs={12} md={5}>
<Text type="secondary">积分不足时</Text>
<Select
style={{ width: '100%' }}
value={allowTopup ? 'topup' : 'skip'}
onChange={(value) => setAllowTopup(value === 'topup')}
options={[
{ value: 'topup', label: '自动充值积分' },
{ value: 'skip', label: '只用已有积分' },
]}
/>
</Col>
<Col xs={24} md={5}>
<Button
type="primary"
block
icon={<ThunderboltOutlined />}
loading={loading}
onClick={() => void submit()}
>
提交并自动分配
</Button>
</Col>
</Row>
{supply && (
<Row gutter={16} style={{ marginTop: 16 }}>
<Col xs={12} md={6}>
<Statistic title="可直接兑(免充值)" value={supply.zero_topup} suffix="个" />
</Col>
<Col xs={12} md={6}>
<Statistic title="充值后可兑" value={supply.after_topup} suffix="个" />
</Col>
<Col xs={12} md={6}>
<Statistic
title="全部兑完预计花费"
value={supply.cost.est_yuan}
suffix="元"
loading={previewing}
/>
</Col>
<Col xs={12} md={6}>
<Statistic title="当前租用中" value={supply.leased} suffix="个" />
</Col>
</Row>
)}
{supply?.manual_payment_required && (
<Alert
style={{ marginTop: 12 }}
type="warning"
showIcon
message={`当前鱼翅渠道为「${supply.recharge_channel}」,需要人工扫码支付`}
description="系统会生成支付码并等待 10 分钟;超时后转入人工处理,需要你到「我的租约」里继续或放弃。"
/>
)}
</Card>
{result && (
<Card title={`本次分配结果(批次 ${result.batch_id || '—'})`}>
<Descriptions size="small" column={4} style={{ marginBottom: 12 }}>
<Descriptions.Item label="需求">{result.requested}</Descriptions.Item>
<Descriptions.Item label="实配">{result.allocated}</Descriptions.Item>
<Descriptions.Item label="缺口">
<Text type={result.shortfall > 0 ? 'danger' : undefined}>{result.shortfall}</Text>
</Descriptions.Item>
<Descriptions.Item label="充值渠道">{result.recharge_channel || '—'}</Descriptions.Item>
</Descriptions>
{result.reasons.length > 0 && (
<Paragraph type="warning" style={{ marginBottom: 12 }}>
{result.reasons.map((reason) => <div key={reason}>· {reason}</div>)}
</Paragraph>
)}
<Table
rowKey="account_id"
size="small"
pagination={false}
columns={accountColumns}
dataSource={result.accounts}
locale={{ emptyText: '本次没有分配账号' }}
/>
</Card>
)}
<Card title="我的租约(用完自动收回,也可手动释放)">
<Table
rowKey="id"
size="small"
columns={leaseColumns}
dataSource={leases}
pagination={{ pageSize: 10, showSizeChanger: false }}
locale={{ emptyText: '当前没有租用中的账号' }}
style={{ background: token.colorBgContainer }}
/>
</Card>
</Space>
);
}