9145ed5834
统一各中心表格的分页样式和切换行为,保留密钥筛选条件与兑换码搜索分页。
504 lines
13 KiB
React
504 lines
13 KiB
React
import { Download as ActionDownload } from '@/components/common/icons/ActionIcons';
|
|
import React, { useMemo } from 'react';
|
|
import {
|
|
Button,
|
|
Empty,
|
|
Input,
|
|
Select,
|
|
Table,
|
|
Tag,
|
|
Typography,
|
|
} from '@/components/ui';
|
|
import { Search, Table2, UserCog } from 'lucide-react';
|
|
import { ChangeText, OrgNameCell, SectionCard } from './shared';
|
|
import { formatPercent } from '../../../helpers/safeDisplay';
|
|
import {
|
|
ALERT_LEVELS,
|
|
ORG_STATUS,
|
|
changeRate,
|
|
daysSince,
|
|
downloadCsv,
|
|
formatCny,
|
|
formatCst,
|
|
orgDisplayName,
|
|
quotaToCny,
|
|
SEGMENTS,
|
|
TIERS,
|
|
} from './utils';
|
|
|
|
const { Text } = Typography;
|
|
|
|
// ─── 跟进人视图 ───────────────────────────────────────────────────
|
|
export const SalesRepTable = ({ reps, cny, onSelect, t }) => {
|
|
const m = (v) => <span className='tabular-nums'>{formatCny(v, cny)}</span>;
|
|
const count = (color) => (v) =>
|
|
v > 0 ? (
|
|
<Tag size='small' color={color}>
|
|
{v}
|
|
</Tag>
|
|
) : (
|
|
<Text type='tertiary'>0</Text>
|
|
);
|
|
const columns = [
|
|
{
|
|
title: t('跟进人'),
|
|
dataIndex: 'sales_rep',
|
|
width: 120,
|
|
fixed: 'left',
|
|
render: (v) => (
|
|
<button
|
|
type='button'
|
|
className='text-left font-medium hover:text-brand focus-visible:outline focus-visible:outline-2'
|
|
onClick={() => onSelect(v || '__none__')}
|
|
>
|
|
{v || t('未分配')}
|
|
</button>
|
|
),
|
|
},
|
|
{ title: t('企业数'), dataIndex: 'orgs', width: 80, align: 'right' },
|
|
{ title: t('活跃企业'), dataIndex: 'active', width: 90, align: 'right' },
|
|
{
|
|
title: t('本期消耗'),
|
|
dataIndex: 'quota',
|
|
width: 130,
|
|
align: 'right',
|
|
render: m,
|
|
sorter: (a, b) => a.quota - b.quota,
|
|
},
|
|
{
|
|
title: t('变化'),
|
|
dataIndex: 'change',
|
|
width: 90,
|
|
align: 'right',
|
|
render: (_, r) => <ChangeText cur={r.quota} prev={r.prev_quota} t={t} />,
|
|
sorter: (a, b) =>
|
|
(changeRate(a.quota, a.prev_quota) ?? -Infinity) -
|
|
(changeRate(b.quota, b.prev_quota) ?? -Infinity),
|
|
},
|
|
{
|
|
title: t('企业余额'),
|
|
dataIndex: 'balance',
|
|
width: 130,
|
|
align: 'right',
|
|
render: m,
|
|
},
|
|
{
|
|
title: t('续费跟进'),
|
|
dataIndex: 'renewal',
|
|
width: 90,
|
|
align: 'center',
|
|
render: count('red'),
|
|
sorter: (a, b) => a.renewal - b.renewal,
|
|
},
|
|
{
|
|
title: t('流失挽回'),
|
|
dataIndex: 'retention',
|
|
width: 90,
|
|
align: 'center',
|
|
render: count('orange'),
|
|
sorter: (a, b) => a.retention - b.retention,
|
|
},
|
|
{
|
|
title: t('增长机会'),
|
|
dataIndex: 'growth',
|
|
width: 90,
|
|
align: 'center',
|
|
render: count('green'),
|
|
sorter: (a, b) => a.growth - b.growth,
|
|
},
|
|
];
|
|
return (
|
|
<SectionCard
|
|
title={t('跟进人视图')}
|
|
icon={<UserCog className='w-5 h-5' />}
|
|
extra={
|
|
<span className='text-xs text-gray-400'>
|
|
{t('点击跟进人筛选下方企业明细')}
|
|
</span>
|
|
}
|
|
>
|
|
<Table
|
|
columns={columns}
|
|
dataSource={reps}
|
|
rowKey={(r) => r.sales_rep || '__none__'}
|
|
size='small'
|
|
scroll={{ x: 920 }}
|
|
pagination={reps.length > 8 ? { pageSize: 10 } : false}
|
|
empty={<Empty description={t('暂无数据')} />}
|
|
/>
|
|
</SectionCard>
|
|
);
|
|
};
|
|
|
|
// ─── 企业明细 ─────────────────────────────────────────────────────
|
|
export const EMPTY_DETAIL_FILTER = {
|
|
keyword: '',
|
|
segment: '',
|
|
tier: '',
|
|
alert: '',
|
|
salesRep: '',
|
|
};
|
|
|
|
const filterOrgs = (orgs, f) => {
|
|
const kw = f.keyword.trim().toLowerCase();
|
|
return orgs.filter((o) => {
|
|
if (kw && !`${o.name || ''} ${o.code || ''}`.toLowerCase().includes(kw))
|
|
return false;
|
|
if (f.segment && o.segment !== f.segment) return false;
|
|
if (f.tier && o.tier !== f.tier) return false;
|
|
if (
|
|
f.alert === '__any__'
|
|
? !o.alert_level
|
|
: f.alert && o.alert_level !== f.alert
|
|
)
|
|
return false;
|
|
if (f.salesRep && (o.sales_rep || '__none__') !== f.salesRep) return false;
|
|
return true;
|
|
});
|
|
};
|
|
|
|
export const OrgDetailTable = ({ report, filter, setFilter, cny, t }) => {
|
|
const orgs = useMemo(() => report.orgs || [], [report]);
|
|
const rows = useMemo(() => filterOrgs(orgs, filter), [orgs, filter]);
|
|
const reps = useMemo(
|
|
() =>
|
|
Array.from(new Set(orgs.map((o) => o.sales_rep || '__none__'))).sort(),
|
|
[orgs],
|
|
);
|
|
const set = (k) => (v) => setFilter({ ...filter, [k]: v || '' });
|
|
const filtered = Object.values(filter).some(Boolean);
|
|
const m = (v) => <span className='tabular-nums'>{formatCny(v, cny)}</span>;
|
|
|
|
const columns = [
|
|
{
|
|
title: t('企业'),
|
|
dataIndex: 'name',
|
|
width: 190,
|
|
fixed: 'left',
|
|
render: (_, r) => <OrgNameCell org={r} t={t} />,
|
|
},
|
|
{
|
|
title: t('跟进人'),
|
|
dataIndex: 'sales_rep',
|
|
width: 90,
|
|
render: (v) =>
|
|
v ? (
|
|
<span className='truncate'>{v}</span>
|
|
) : (
|
|
<Text type='tertiary'>—</Text>
|
|
),
|
|
},
|
|
{
|
|
title: t('层级'),
|
|
dataIndex: 'tier',
|
|
width: 64,
|
|
render: (v) =>
|
|
v ? (
|
|
<Tag size='small' color={TIERS[v].color}>
|
|
{v}
|
|
</Tag>
|
|
) : (
|
|
<Text type='tertiary'>—</Text>
|
|
),
|
|
},
|
|
{
|
|
title: t('健康'),
|
|
dataIndex: 'segment',
|
|
width: 96,
|
|
render: (v) =>
|
|
SEGMENTS[v] ? (
|
|
<Tag size='small' color={SEGMENTS[v].color}>
|
|
{t(SEGMENTS[v].label)}
|
|
</Tag>
|
|
) : null,
|
|
},
|
|
{
|
|
title: t('本期消耗'),
|
|
dataIndex: 'quota',
|
|
width: 120,
|
|
align: 'right',
|
|
render: m,
|
|
sorter: (a, b) => a.quota - b.quota,
|
|
defaultSortOrder: 'descend',
|
|
},
|
|
{
|
|
title: t('对比期'),
|
|
dataIndex: 'prev_quota',
|
|
width: 110,
|
|
align: 'right',
|
|
render: m,
|
|
sorter: (a, b) => a.prev_quota - b.prev_quota,
|
|
},
|
|
{
|
|
title: t('变化'),
|
|
dataIndex: 'change',
|
|
width: 90,
|
|
align: 'right',
|
|
render: (_, r) => (
|
|
<ChangeText cur={r.quota} prev={r.prev_quota} t={t} showNew={false} />
|
|
),
|
|
sorter: (a, b) => a.quota - a.prev_quota - (b.quota - b.prev_quota),
|
|
},
|
|
{
|
|
title: t('占比'),
|
|
dataIndex: 'share',
|
|
width: 70,
|
|
align: 'right',
|
|
render: (v) =>
|
|
v > 0 ? (
|
|
<span className='tabular-nums'>{formatPercent(v, 1)}</span>
|
|
) : (
|
|
<Text type='tertiary'>—</Text>
|
|
),
|
|
},
|
|
{
|
|
title: t('请求数'),
|
|
dataIndex: 'requests',
|
|
width: 90,
|
|
align: 'right',
|
|
render: (v) => (v || 0).toLocaleString(),
|
|
sorter: (a, b) => a.requests - b.requests,
|
|
},
|
|
{
|
|
title: t('活跃/成员'),
|
|
dataIndex: 'active_members',
|
|
width: 90,
|
|
align: 'right',
|
|
render: (v, r) => (
|
|
<span className='tabular-nums'>
|
|
{v}/{r.member_count}
|
|
</span>
|
|
),
|
|
sorter: (a, b) => a.active_members - b.active_members,
|
|
},
|
|
{
|
|
title: t('余额'),
|
|
dataIndex: 'balance',
|
|
width: 120,
|
|
align: 'right',
|
|
render: m,
|
|
sorter: (a, b) => a.balance - b.balance,
|
|
},
|
|
{
|
|
title: t('预计可用'),
|
|
dataIndex: 'days_left',
|
|
width: 90,
|
|
align: 'right',
|
|
render: (v) =>
|
|
v < 0 ? (
|
|
<Text type='tertiary'>—</Text>
|
|
) : (
|
|
<span className='tabular-nums'>
|
|
{v < 1 ? t('不足 1 天') : t('{{days}} 天', { days: v.toFixed(0) })}
|
|
</span>
|
|
),
|
|
sorter: (a, b) =>
|
|
(a.days_left < 0 ? Infinity : a.days_left) -
|
|
(b.days_left < 0 ? Infinity : b.days_left),
|
|
},
|
|
{
|
|
title: t('余额预警'),
|
|
dataIndex: 'alert_level',
|
|
width: 110,
|
|
render: (v) =>
|
|
ALERT_LEVELS[v] ? (
|
|
<Tag size='small' color={ALERT_LEVELS[v].color}>
|
|
{t(ALERT_LEVELS[v].label)}
|
|
</Tag>
|
|
) : null,
|
|
},
|
|
{
|
|
title: t('最近使用'),
|
|
dataIndex: 'last_active',
|
|
width: 100,
|
|
render: (v) => {
|
|
const d = daysSince(v);
|
|
return d === null ? (
|
|
<Text type='tertiary'>—</Text>
|
|
) : d === 0 ? (
|
|
t('今天')
|
|
) : (
|
|
t('{{days}} 天前', { days: d })
|
|
);
|
|
},
|
|
sorter: (a, b) => a.last_active - b.last_active,
|
|
},
|
|
];
|
|
|
|
const onExport = () => exportOrgDetail(rows, cny, t);
|
|
|
|
return (
|
|
<SectionCard
|
|
id='org-report-detail'
|
|
title={t('企业明细')}
|
|
icon={<Table2 className='w-5 h-5' />}
|
|
extra={
|
|
<span className='text-xs text-gray-400 tabular-nums'>
|
|
{filtered
|
|
? t('筛选 {{count}} / 共 {{total}} 家', {
|
|
count: rows.length,
|
|
total: orgs.length,
|
|
})
|
|
: t('共 {{total}} 家', { total: orgs.length })}
|
|
</span>
|
|
}
|
|
>
|
|
<div className='flex flex-wrap items-center gap-2 mb-3'>
|
|
<Input
|
|
prefix={<Search className='w-4 h-4 ml-2 text-gray-400' />}
|
|
placeholder={t('搜索企业名称或代码')}
|
|
value={filter.keyword}
|
|
onChange={set('keyword')}
|
|
showClear
|
|
style={{ width: 220 }}
|
|
/>
|
|
<Select
|
|
placeholder={t('健康分群')}
|
|
value={filter.segment || undefined}
|
|
onChange={set('segment')}
|
|
showClear
|
|
style={{ width: 130 }}
|
|
optionList={Object.entries(SEGMENTS).map(([k, v]) => ({
|
|
value: k,
|
|
label: t(v.label),
|
|
}))}
|
|
/>
|
|
<Select
|
|
placeholder={t('价值层级')}
|
|
value={filter.tier || undefined}
|
|
onChange={set('tier')}
|
|
showClear
|
|
style={{ width: 110 }}
|
|
optionList={Object.entries(TIERS).map(([k, v]) => ({
|
|
value: k,
|
|
label: t(v.label),
|
|
}))}
|
|
/>
|
|
<Select
|
|
placeholder={t('余额预警')}
|
|
value={filter.alert || undefined}
|
|
onChange={set('alert')}
|
|
showClear
|
|
style={{ width: 130 }}
|
|
optionList={[
|
|
{ value: '__any__', label: t('全部预警') },
|
|
...Object.entries(ALERT_LEVELS).map(([k, v]) => ({
|
|
value: k,
|
|
label: t(v.label),
|
|
})),
|
|
]}
|
|
/>
|
|
<Select
|
|
placeholder={t('跟进人')}
|
|
value={filter.salesRep || undefined}
|
|
onChange={set('salesRep')}
|
|
showClear
|
|
filter
|
|
style={{ width: 130 }}
|
|
optionList={reps.map((r) => ({
|
|
value: r,
|
|
label: r === '__none__' ? t('未分配') : r,
|
|
}))}
|
|
/>
|
|
{filtered ? (
|
|
<Button
|
|
type='tertiary'
|
|
onClick={() => setFilter(EMPTY_DETAIL_FILTER)}
|
|
>
|
|
{t('重置')}
|
|
</Button>
|
|
) : null}
|
|
<Button
|
|
className='ml-auto'
|
|
icon={<ActionDownload className='w-4 h-4' />}
|
|
onClick={onExport}
|
|
disabled={rows.length === 0}
|
|
>
|
|
{t('导出')}
|
|
</Button>
|
|
</div>
|
|
<Table
|
|
columns={columns}
|
|
dataSource={rows}
|
|
rowKey='org_id'
|
|
size='small'
|
|
scroll={{ x: 1480 }}
|
|
pagination={{
|
|
pageSize: 20,
|
|
showSizeChanger: true,
|
|
pageSizeOpts: [20, 50, 100],
|
|
}}
|
|
empty={
|
|
<Empty
|
|
description={filtered ? t('没有符合筛选条件的企业') : t('暂无企业')}
|
|
/>
|
|
}
|
|
/>
|
|
</SectionCard>
|
|
);
|
|
};
|
|
|
|
export const exportOrgDetail = (rows, cny, t) => {
|
|
const yuan = (v) => quotaToCny(v, cny).toFixed(2);
|
|
downloadCsv(
|
|
`${t('企业经营明细')}-${formatCst(Date.now() / 1000, false)}.csv`,
|
|
[
|
|
t('企业ID'),
|
|
t('企业'),
|
|
t('企业代码'),
|
|
t('状态'),
|
|
t('跟进人'),
|
|
t('层级'),
|
|
t('健康分群'),
|
|
t('本期消耗'),
|
|
t('对比期消耗'),
|
|
t('变化率'),
|
|
t('占比'),
|
|
t('请求数'),
|
|
t('活跃成员'),
|
|
t('成员数'),
|
|
t('余额'),
|
|
t('赠送余额'),
|
|
t('近7日日均消耗'),
|
|
t('预计可用天数'),
|
|
t('建议充值'),
|
|
t('余额预警'),
|
|
t('最近使用'),
|
|
t('创建时间'),
|
|
t('到期时间'),
|
|
t('联系人'),
|
|
t('联系电话'),
|
|
],
|
|
rows.map((o) => {
|
|
const rate = changeRate(o.quota, o.prev_quota);
|
|
return [
|
|
o.org_id,
|
|
orgDisplayName(o, t),
|
|
o.code,
|
|
ORG_STATUS[o.status] ? t(ORG_STATUS[o.status].label) : o.status,
|
|
o.sales_rep,
|
|
o.tier,
|
|
SEGMENTS[o.segment] ? t(SEGMENTS[o.segment].label) : '',
|
|
yuan(o.quota),
|
|
yuan(o.prev_quota),
|
|
rate === null ? '' : formatPercent(rate, 1),
|
|
o.share ? formatPercent(o.share, 2) : '',
|
|
o.requests,
|
|
o.active_members,
|
|
o.member_count,
|
|
yuan(o.balance),
|
|
yuan(o.gift_quota),
|
|
yuan(o.daily_avg),
|
|
o.days_left < 0 ? '' : o.days_left.toFixed(1),
|
|
yuan(o.topup_suggest),
|
|
ALERT_LEVELS[o.alert_level] ? t(ALERT_LEVELS[o.alert_level].label) : '',
|
|
o.last_active ? formatCst(o.last_active) : '',
|
|
formatCst(o.created_at, false),
|
|
o.expired_at > 0 ? formatCst(o.expired_at, false) : '',
|
|
o.contact_name,
|
|
o.contact_phone,
|
|
];
|
|
}),
|
|
);
|
|
};
|