import React from 'react'; import { Card, Table, Button } from '@/components/ui'; import { IconDownload } from '@/components/ui/icons'; import { renderQuota } from '../../helpers'; import { formatNumber, toFiniteNumber } from '../../helpers/safeDisplay'; const DailyStatsTable = ({ data, title, icon, t, embedded = false }) => { const columns = [ { title: t('日期'), dataIndex: 'date', key: 'date', width: 120, fixed: 'left', render: (text) => {text}, }, { title: t('新增用户'), dataIndex: 'new_users', key: 'new_users', width: 100, align: 'right', render: (val) => ( {(val || 0).toLocaleString()} ), }, { title: t('活跃用户'), dataIndex: 'active_users', key: 'active_users', width: 100, align: 'right', render: (val) => ( {(val || 0).toLocaleString()} ), }, { title: t('充值人数'), dataIndex: 'topup_users', key: 'topup_users', width: 100, align: 'right', render: (val) => {(val || 0).toLocaleString()}, }, { title: t('充值金额'), dataIndex: 'topup_amount', key: 'topup_amount', width: 120, align: 'right', render: (val) => ( ¥{formatNumber(val, 2, '0.00')} ), }, { title: t('消耗额度'), dataIndex: 'consume_quota', key: 'consume_quota', width: 120, align: 'right', render: (val) => ( {renderQuota(val || 0)} ), }, { title: t('请求次数'), dataIndex: 'requests', key: 'requests', width: 120, align: 'right', render: (val) => {(val || 0).toLocaleString()}, }, { title: t('Token消耗'), dataIndex: 'tokens', key: 'tokens', width: 120, align: 'right', render: (val) => { const tokens = toFiniteNumber(val) ?? 0; if (tokens >= 1000000) { return {(tokens / 1000000).toFixed(2)}M; } else if (tokens >= 1000) { return {(tokens / 1000).toFixed(1)}K; } return {tokens.toLocaleString()}; }, }, { title: t('人均消耗'), dataIndex: 'avg_consume', key: 'avg_consume', width: 120, align: 'right', render: (val) => ( {renderQuota(val || 0)} ), }, ]; // 导出CSV const handleExport = () => { if (!data || data.length === 0) return; const headers = [ t('日期'), t('新增用户'), t('活跃用户'), t('充值人数'), t('充值金额'), t('消耗额度'), t('请求次数'), t('Token消耗'), t('人均消耗'), ]; const rows = data.map((row) => [ row.date, row.new_users || 0, row.active_users || 0, row.topup_users || 0, row.topup_amount || 0, row.consume_quota || 0, row.requests || 0, row.tokens || 0, row.avg_consume || 0, ]); const csvContent = '\uFEFF' + [headers.join(','), ...rows.map((row) => row.join(','))].join('\n'); const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); const link = document.createElement('a'); link.href = URL.createObjectURL(blob); link.download = `analytics_${new Date().toISOString().split('T')[0]}.csv`; link.click(); }; return (
{icon} {title}
) } styles={{ body: { padding: '0' } }} > {embedded && (
)} t('共 {{total}} 条', { total, count: total }), }} scroll={{ x: 1100 }} size='small' rowKey='date' empty={
{t('暂无数据')}
} /> ); }; export default DailyStatsTable;