import { Trans } from 'react-i18next';
import { I18N_TECHNICAL_TEXT } from '../../../i18n/technicalText.js';
import { Download as ActionDownload } from '@/components/common/icons/ActionIcons';
import React from 'react';
import { Banner, Button, Card, Tag, Tooltip } from '@/components/ui';
import {
AlertOctagon,
AlertTriangle,
CheckCircle2,
Info,
Lightbulb,
} from 'lucide-react';
import { ChangeText, KpiCard, SectionCard } from './shared';
import { formatCny, formatCst, pct, SEGMENTS } from './utils';
import {
formatPercent,
formatRatioPercent,
} from '../../../helpers/safeDisplay';
// ─── 报表抬头:统计口径 + 数据状态 ─────────────────────────────────
export const ReportHeader = ({ report, onExport, t }) => {
const m = report.meta || {};
const now = Date.now() / 1000;
const stale = m.rollup_ready && now - m.data_as_of > 2 * 3600;
const beyondFloor =
m.rollup_ready && m.history_floor > 0 && m.prev_start < m.history_floor;
return (
{t('企业经营决策报表')}
}}
/>
{t('数据截至 {{time}},按整点统计;对比期与本期按相同时长对齐', {
time: formatCst(m.data_as_of),
})}
}
onClick={onExport}
disabled={!report.orgs?.length}
>
{t('导出企业明细')}
{!m.rollup_ready ? (
) : null}
{m.rollup_ready && !m.history_ready ? (
) : null}
{beyondFloor ? (
) : null}
{stale ? (
) : null}
);
};
// ─── 本期要点 ─────────────────────────────────────────────────────
const FINDING_STYLE = {
danger: { icon: AlertOctagon, color: 'var(--ui-color-danger)' },
warning: { icon: AlertTriangle, color: 'var(--ui-color-warning)' },
success: { icon: CheckCircle2, color: 'var(--ui-color-success)' },
info: { icon: Info, color: 'var(--ui-color-primary)' },
};
export const KeyFindings = ({ findings, onJump, t }) => (
}
extra={
{t('根据本期数据自动生成,点击可定位到明细')}
}
>
{findings.length === 0 ? (
{t('本期暂无需要关注的变化')}
) : (
{findings.map((f, i) => {
const style = FINDING_STYLE[f.level] || FINDING_STYLE.info;
const Icon = style.icon;
return (
-
);
})}
)}
);
// ─── 核心指标 ─────────────────────────────────────────────────────
export const KpiGrid = ({ report, lists, cny, onJump, t }) => {
const k = report.kpi || {};
const servingOrgs = (k.normal_orgs || 0) + (k.arrears_orgs || 0);
const avgPerOrg = k.active_orgs > 0 ? k.org_quota / k.active_orgs : 0;
const prevAvgPerOrg =
k.prev_active_orgs > 0 ? k.prev_org_quota / k.prev_active_orgs : 0;
const platform = (k.org_quota || 0) + (k.personal_quota || 0);
const prevPlatform = (k.prev_org_quota || 0) + (k.prev_personal_quota || 0);
const share = pct(k.org_quota, platform);
const prevShare =
prevPlatform > 0 ? pct(k.prev_org_quota, prevPlatform) : null;
const shareDelta = prevShare === null ? null : share - prevShare;
const coverDays =
k.run_rate_daily > 0 ? k.total_balance / k.run_rate_daily : null;
const cards = [
{
title: t('企业消耗'),
value: formatCny(k.org_quota, cny),
sub: `${t('对比期')} ${formatCny(k.prev_org_quota, cny)}`,
compare: ,
onClick: () => onJump('attribution'),
},
{
title: t('活跃企业'),
value: t('{{n}} 家', {
n: (k.active_orgs || 0).toLocaleString(),
count: k.active_orgs || 0,
}),
sub: `${t('活跃率')} ${formatRatioPercent(k.active_orgs, servingOrgs)} · ${t('新增')} ${k.new_orgs || 0}`,
compare: (
),
tip: t('本期有调用的企业数;活跃率 = 活跃企业 / 正常与欠费企业'),
onClick: () => onJump('segments'),
},
{
title: t('家均消耗'),
value: formatCny(avgPerOrg, cny),
sub: `${t('人均')} ${formatCny(k.active_members > 0 ? k.org_quota / k.active_members : 0, cny)}`,
compare: ,
tip: t('企业消耗 / 活跃企业数;人均 = 企业消耗 / 活跃成员数'),
},
{
title: t('企业活跃成员'),
value: t('{{n}} 人', {
n: (k.active_members || 0).toLocaleString(),
count: k.active_members || 0,
}),
sub: `${t('渗透率')} ${formatPercent(Math.min(100, pct(k.active_members, k.total_members)))} · ${t('成员')} ${(k.total_members || 0).toLocaleString()}`,
tip: t('本期通过企业密钥产生调用的去重成员数(按自然日统计)'),
onClick: () => onJump('growth'),
},
{
title: t('本月预计企业消耗'),
value: formatCny(k.month_forecast, cny),
sub: `${t('本月至今')} ${formatCny(k.month_to_date, cny)}`,
compare: (
),
tip: t('本月至今 + 近 7 个完整日的日均消耗 × 本月剩余天数;对比上月全月'),
accent: 'var(--ui-color-tertiary)',
},
{
title: t('未来 30 天充值缺口'),
value: formatCny(k.topup_gap_30d, cny),
sub: t('{{n}} 家需续费跟进', {
n: lists.renewal.length,
count: lists.renewal.length,
}),
tip: t('按各企业近 7 日日均消耗覆盖 30 天所需金额,减去当前余额后累加'),
accent:
k.topup_gap_30d > 0
? 'var(--ui-color-danger)'
: 'var(--ui-color-success)',
onClick: () => onJump('renewal'),
},
{
title: t('企业总余额'),
value: formatCny(k.total_balance, cny),
sub:
coverDays === null
? t('近 7 日无消耗')
: t('按当前速度可支撑 {{days}} 天', { days: coverDays.toFixed(0) }),
tip: t('全部企业主余额 + 赠送余额'),
accent: 'var(--ui-color-success)',
},
{
title: t('企业消耗占平台'),
value: formatPercent(share),
sub: `${t('个人消耗')} ${formatCny(k.personal_quota, cny)}`,
compare:
shareDelta === null ? null : (
= 0
? 'var(--ui-color-success)'
: 'var(--ui-color-danger)',
}}
>
{shareDelta >= 0 ? '+' : ''}
{shareDelta.toFixed(1)}
{I18N_TECHNICAL_TEXT.PERCENTAGE_POINTS}
),
tip: t('企业消耗 /(企业 + 个人消耗);pp 为百分点变化'),
onClick: () => onJump('trend'),
},
];
return (
{cards.map((c) => (
))}
);
};
// ─── 客户健康分群 ─────────────────────────────────────────────────
export const HealthSegments = ({ report, cny, activeSegment, onSelect, t }) => {
const segs = report.segments || [];
const totalCount = segs.reduce((s, x) => s + x.count, 0);
return (
{segs.map((s) => {
const meta = SEGMENTS[s.key] || { label: s.key, color: 'grey' };
const active = activeSegment === s.key;
const delta = (s.quota || 0) - (s.prev_quota || 0);
return (
);
})}
);
};