import { Download as ActionDownload } from '@/components/common/icons/ActionIcons'; import React from 'react'; import { Button, Empty, Table, TabPane, Tabs, Tag, Typography, } from '@/components/ui'; import { ClipboardList } from 'lucide-react'; import { ChangeText, OrgNameCell, ReasonTags, SectionCard } from './shared'; import { formatRatioPercent } from '../../../helpers/safeDisplay'; import { daysSince, downloadCsv, formatCny, formatCst, orgDisplayName, quotaToCny, REASONS, } from './utils'; const { Text } = Typography; export const ACTION_TABS = ['renewal', 'retention', 'growth', 'activation']; const TAB_META = { renewal: { label: '续费跟进', desc: '余额耗尽 / 即将不足 / 低于预警线,以及合同即将到期的企业;按紧急程度排序', }, retention: { label: '流失挽回', desc: '本期停用、消耗下滑、近 7 日腰斩,以及 60 天内还在使用的沉睡企业;按消耗减少金额排序', }, growth: { label: '增长机会', desc: '高速增长、新客起量、回流,以及成员活跃率低于 30% 的企业;按本期消耗排序', }, activation: { label: '激活跟进', desc: '待激活或开通后从未使用的企业;按创建时间倒序', }, }; const money = (cny) => (v) => ( {formatCny(v, cny)} ); const salesRepCol = (t) => ({ title: t('跟进人'), dataIndex: 'sales_rep', width: 100, render: (v) => v ? ( {v} ) : ( {t('未分配')} ), }); const contactCol = (t) => ({ title: t('联系人'), dataIndex: 'contact_name', width: 150, render: (v, r) => v || r.contact_phone ? (
{v || '—'}
{r.contact_phone ? (
{r.contact_phone}
) : null}
) : ( — ), }); const baseCols = (t) => [ { title: t('企业'), dataIndex: 'name', width: 190, fixed: 'left', render: (_, r) => , }, salesRepCol(t), { title: t('原因'), dataIndex: 'reasons', width: 170, render: (v) => , }, ]; const actionCol = (t) => ({ title: t('建议动作'), dataIndex: 'action', width: 190, render: (v) => {t(v)}, }); const columnsFor = (tab, cny, t, atRisk) => { const m = money(cny); switch (tab) { case 'renewal': return [ ...baseCols(t), { title: t('当前余额'), dataIndex: 'balance', width: 120, align: 'right', render: m, }, { title: t('日均消耗'), dataIndex: 'daily_avg', width: 110, align: 'right', render: (v) => (v > 0 ? m(v) : —), }, { title: t('预计可用'), dataIndex: 'days_left', width: 100, align: 'right', render: (v, r) => { if (v < 0) return {t('近期无消耗')}; return ( {v < 1 ? t('不足 1 天') : t('{{days}} 天', { days: v.toFixed(1) })} ); }, }, { title: t('建议充值'), dataIndex: 'topup_suggest', width: 120, align: 'right', render: (v) => v > 0 ? ( {m(v)} ) : ( — ), }, { title: t('到期时间'), dataIndex: 'expired_at', width: 110, render: (v) => v > 0 ? formatCst(v, false) : —, }, contactCol(t), actionCol(t), ]; case 'retention': return [ ...baseCols(t), { title: t('对比期消耗'), dataIndex: 'prev_quota', width: 120, align: 'right', render: m, }, { title: t('本期消耗'), dataIndex: 'quota', width: 120, align: 'right', render: m, }, { title: t('变化'), dataIndex: 'change', width: 90, align: 'right', render: (_, r) => ( ), }, { title: t('消耗减少'), dataIndex: 'at_risk', width: 110, align: 'right', render: (_, r) => ( {formatCny(atRisk(r), cny)} ), }, { title: t('最近使用'), dataIndex: 'last_active', width: 110, render: (v) => { const d = daysSince(v); return d === null ? ( — ) : d === 0 ? ( t('今天') ) : ( t('{{days}} 天前', { days: d }) ); }, }, { title: t('余额'), dataIndex: 'balance', width: 110, align: 'right', render: m, }, contactCol(t), actionCol(t), ]; case 'growth': return [ ...baseCols(t), { title: t('本期消耗'), dataIndex: 'quota', width: 120, align: 'right', render: m, }, { title: t('变化'), dataIndex: 'change', width: 100, align: 'right', render: (_, r) => ( ), }, { title: t('成员活跃'), dataIndex: 'active_members', width: 120, align: 'right', render: (v, r) => ( {v}/{r.member_count} {r.member_count > 0 ? ( {formatRatioPercent( Math.min(100, Number(v) || 0), r.member_count, 0, )} ) : null} ), }, { title: t('层级'), dataIndex: 'tier', width: 70, render: (v) => v ? {v} : —, }, { title: t('余额'), dataIndex: 'balance', width: 110, align: 'right', render: m, }, actionCol(t), ]; default: return [ ...baseCols(t), { title: t('创建时间'), dataIndex: 'created_at', width: 110, render: (v) => formatCst(v, false), }, { title: t('已开通'), dataIndex: 'age', width: 90, align: 'right', render: (_, r) => { const d = daysSince(r.created_at); return d === null ? '—' : t('{{days}} 天', { days: d }); }, }, { title: t('成员数'), dataIndex: 'member_count', width: 80, align: 'right', }, { title: t('余额'), dataIndex: 'balance', width: 110, align: 'right', render: m, }, contactCol(t), actionCol(t), ]; } }; const exportList = (tab, rows, cny, t, atRisk) => { const yuan = (v) => quotaToCny(v, cny).toFixed(2); const common = (o) => [ orgDisplayName(o, t), o.code, o.sales_rep || t('未分配'), o.reasons.map((r) => t(REASONS[r]?.label || r)).join(' / '), ]; const head = [t('企业'), t('企业代码'), t('跟进人'), t('原因')]; let headers; let body; switch (tab) { case 'renewal': headers = [ ...head, t('当前余额'), t('日均消耗'), t('预计可用天数'), t('建议充值'), t('到期时间'), t('联系人'), t('联系电话'), t('建议动作'), ]; body = rows.map((o) => [ ...common(o), yuan(o.balance), yuan(o.daily_avg), o.days_left < 0 ? '' : o.days_left.toFixed(1), yuan(o.topup_suggest), o.expired_at > 0 ? formatCst(o.expired_at, false) : '', o.contact_name, o.contact_phone, t(o.action), ]); break; case 'retention': headers = [ ...head, t('对比期消耗'), t('本期消耗'), t('消耗减少'), t('最近使用'), t('余额'), t('联系人'), t('联系电话'), t('建议动作'), ]; body = rows.map((o) => [ ...common(o), yuan(o.prev_quota), yuan(o.quota), yuan(atRisk(o)), formatCst(o.last_active), yuan(o.balance), o.contact_name, o.contact_phone, t(o.action), ]); break; case 'growth': headers = [ ...head, t('本期消耗'), t('对比期消耗'), t('活跃成员'), t('成员数'), t('层级'), t('余额'), t('建议动作'), ]; body = rows.map((o) => [ ...common(o), yuan(o.quota), yuan(o.prev_quota), o.active_members, o.member_count, o.tier, yuan(o.balance), t(o.action), ]); break; default: headers = [ ...head, t('创建时间'), t('成员数'), t('余额'), t('联系人'), t('联系电话'), t('建议动作'), ]; body = rows.map((o) => [ ...common(o), formatCst(o.created_at, false), o.member_count, yuan(o.balance), o.contact_name, o.contact_phone, t(o.action), ]); } downloadCsv( `${t(TAB_META[tab].label)}-${formatCst(Date.now() / 1000, false)}.csv`, headers, body, ); }; export const ActionCenter = ({ lists, activeTab, onTabChange, cny, t }) => ( } extra={ {t('按跟进场景拆分的待办企业,可直接导出分派')} } bodyStyle={{ paddingTop: 4 }} > } disabled={lists[activeTab].length === 0} onClick={() => exportList(activeTab, lists[activeTab], cny, t, lists.atRisk) } > {t('导出')} } > {ACTION_TABS.map((tab) => ( {t(TAB_META[tab].label)} 0 ? tab === 'growth' ? 'green' : 'red' : 'grey' } > {lists[tab].length} } >
{t(TAB_META[tab].desc)}
10 ? { pageSize: 10 } : false} empty={} /> ))} );