feat(ad-revenue): 收益报表分页、eCPM 元列、场景筛选与逐行展开

- 列:eCPM 新增「元」列;广告位ID 移到「一致」列后。
- 分页:「每页条数」接后端 offset 真分页,可翻页看当前筛选下全部数据(原仅展示截断的前 N 条)。
- 场景:「场景」下推后端作为全局筛选(随查询生效,同时影响明细/合计/趋势),移除前端仅过滤当前数据的旧逻辑与过时提示。
- 趋势:按小时趋势改用后端全量 hourly,不受分页影响。
- 展开:明细表每行左侧都有展开号——发奖行看金币复算因子,纯展示行看展示明细(eCPM/收益/ADN/底层rit/代码位/来源)。
- 文案:修正金币规则弹窗「信息流每满10秒折1条」→「一条广告=1条」。
- types 新增 AdRevenueHourly + hourly 字段。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
zzhyyyyy
2026-06-27 16:25:25 +08:00
parent 6ece274af6
commit ea6aca18fc
2 changed files with 120 additions and 105 deletions
+107 -102
View File
@@ -9,6 +9,7 @@ import {
Card,
Col,
DatePicker,
Descriptions,
Divider,
InputNumber,
Modal,
@@ -32,6 +33,7 @@ import { api, errMsg } from '@/lib/api';
import { formatUtcTime } from '@/lib/format';
import type {
AdRevenueDaily,
AdRevenueHourly,
AdRevenueRecord,
AdRevenueReport,
AdRevenueRow,
@@ -146,8 +148,8 @@ interface TrendPoint {
revenue: number;
}
// 按小时聚合(0–23),数据源是 items(被 limit 截断时会偏少,调用处给警告)
function aggregateHourly(rows: AdRevenueRow[]): TrendPoint[] {
// 按小时聚合(0–23),数据源是 hourly(后端全量聚合,不受分页影响)
function aggregateHourly(rows: AdRevenueHourly[]): TrendPoint[] {
const byHour = Array.from({ length: 24 }, (_, h) => ({ hour: h, impressions: 0, revenue: 0 }));
for (const r of rows) {
if (r.hour == null || r.hour < 0 || r.hour > 23) continue;
@@ -263,10 +265,11 @@ export default function AdRevenueReportPage() {
const [range, setRange] = useState<[Dayjs, Dayjs]>([dayjs(), dayjs()]);
const [userId, setUserId] = useState<number | null>(null);
const [adType, setAdType] = useState<string | undefined>();
// 「场景」为纯前端过滤(后端 query 暂不支持 feed_scene),只作用于下方明细表,不重新请求、不影响趋势/日汇总
// 「场景」作为后端全局筛选(feed_scene):同时影响明细 / 合计 / 趋势,与「用户 / 类型」一致,点「查询」生效
const [scene, setScene] = useState<string | undefined>();
const [granularity, setGranularity] = useState<'day' | 'hour'>('day');
const [limit, setLimit] = useState<number>(500);
const [limit, setLimit] = useState<number>(500); // 每页条数(分页大小)
const [page, setPage] = useState<number>(1); // 当前页码(后端分页;1 起)
const [queriedGranularity, setQueriedGranularity] = useState<'day' | 'hour'>('day'); // 本次结果对应的粒度,决定是否显示「小时」列
const [queriedMultiDay, setQueriedMultiDay] = useState(false); // 本次结果是否跨多天,决定显示「日期」列 + 按天/按小时图
const [data, setData] = useState<AdRevenueReport | null>(null);
@@ -277,33 +280,40 @@ export default function AdRevenueReportPage() {
// 当前选择是否跨多天:跨多天时「按小时」无意义,粒度强制按天
const rangeMultiDay = range[0].format('YYYY-MM-DD') !== range[1].format('YYYY-MM-DD');
const load = useCallback(async () => {
const from = range[0].format('YYYY-MM-DD');
const to = range[1].format('YYYY-MM-DD');
const multiDay = from !== to;
const gran = multiDay ? 'day' : granularity; // 跨多天强制按天
setLoading(true);
try {
const res = await api.get<AdRevenueReport>('/admin/api/ad-revenue-report', {
params: {
date_from: from,
date_to: to,
user_id: userId ?? undefined,
ad_type: adType ?? undefined,
granularity: gran,
limit,
},
});
setData(res.data);
setQueriedLimit(limit);
setQueriedGranularity(gran);
setQueriedMultiDay(multiDay);
} catch (e) {
message.error(errMsg(e));
} finally {
setLoading(false);
}
}, [range, userId, adType, granularity, limit]);
// load(目标页, 每页条数):分页与筛选都经它。offset 由 页码×每页 算;场景(feed_scene)随筛选下推后端。
const load = useCallback(
async (targetPage = 1, targetLimit = limit) => {
const from = range[0].format('YYYY-MM-DD');
const to = range[1].format('YYYY-MM-DD');
const multiDay = from !== to;
const gran = multiDay ? 'day' : granularity; // 跨多天强制按天
setLoading(true);
try {
const res = await api.get<AdRevenueReport>('/admin/api/ad-revenue-report', {
params: {
date_from: from,
date_to: to,
user_id: userId ?? undefined,
ad_type: adType ?? undefined,
feed_scene: scene ?? undefined,
granularity: gran,
limit: targetLimit,
offset: (targetPage - 1) * targetLimit,
},
});
setData(res.data);
setPage(targetPage);
setQueriedLimit(targetLimit);
setQueriedGranularity(gran);
setQueriedMultiDay(multiDay);
} catch (e) {
message.error(errMsg(e));
} finally {
setLoading(false);
}
},
[range, userId, adType, scene, granularity, limit],
);
useEffect(() => {
load();
@@ -361,13 +371,6 @@ export default function AdRevenueReportPage() {
return <Tag color={t.color}>{t.label}</Tag>;
},
},
{
title: '广告位ID',
dataIndex: 'our_code_id',
width: 110,
render: (v: string | null) =>
v ? <Typography.Text code>{v}</Typography.Text> : <Typography.Text type="secondary">-</Typography.Text>,
},
{
title: 'eCPM(分)',
dataIndex: 'ecpm',
@@ -375,6 +378,17 @@ export default function AdRevenueReportPage() {
align: 'right',
render: (v: string | null) => v ?? '-',
},
{
title: 'eCPM(元)',
key: 'ecpm_yuan',
width: 100,
align: 'right',
render: (_: unknown, r: AdRevenueRow) => {
// eCPM 原始值单位「分/千次」,÷100 转「元/千次」(块);非数字 / 空显示 -。
const cents = r.ecpm == null ? null : Number(r.ecpm);
return cents == null || Number.isNaN(cents) ? '-' : (cents / 100).toFixed(2);
},
},
{
title: '预估收益(元)',
dataIndex: 'revenue_yuan',
@@ -420,6 +434,13 @@ export default function AdRevenueReportPage() {
<Typography.Text type="secondary">-</Typography.Text>
),
},
{
title: '广告位ID',
dataIndex: 'our_code_id',
width: 110,
render: (v: string | null) =>
v ? <Typography.Text code>{v}</Typography.Text> : <Typography.Text type="secondary">-</Typography.Text>,
},
{
title: '底层 ADN',
dataIndex: 'adn',
@@ -447,15 +468,8 @@ export default function AdRevenueReportPage() {
}
: null;
// 「场景」纯前端过滤:只对已加载的明细 items 生效(后端 query 暂不支持 feed_scene);趋势图/日汇总仍用全量 daily/total_*
const allItems = data?.items ?? [];
const filteredItems = scene ? allItems.filter((r) => r.feed_scene === scene) : allItems;
// 选中比价/领券且类型=draw 时,给出该场景在已加载明细内的实发金币小计(仅明细口径,可能受 limit 截断)。
const sceneCoinSubtotal =
adType === 'draw' && (scene === 'comparison' || scene === 'coupon')
? filteredItems.reduce((s, r) => s + (r.has_reward ? r.actual_coin : 0), 0)
: null;
// 明细直接用后端返回的当前页 items(「场景」已由后端 feed_scene 全局过滤,前端不再二次筛)
const items = data?.items ?? [];
return (
<div>
@@ -534,7 +548,6 @@ export default function AdRevenueReportPage() {
onChange={setScene}
allowClear
style={{ width: 130 }}
title="仅前端过滤下方明细,不影响趋势图/汇总"
options={[
{ value: 'comparison', label: '比价' },
{ value: 'coupon', label: '领券' },
@@ -557,15 +570,18 @@ export default function AdRevenueReportPage() {
/>
</Space>
<Space size={6}>
<Typography.Text type="secondary"></Typography.Text>
<Typography.Text type="secondary"></Typography.Text>
<Select
value={limit}
onChange={setLimit}
style={{ width: 110 }}
options={[100, 200, 500, 1000].map((n) => ({ value: n, label: `${n}` }))}
onChange={(v) => {
setLimit(v);
load(1, v); // 改每页大小:回到第 1 页并重查
}}
style={{ width: 120 }}
options={[20, 50, 100, 200, 500, 1000].map((n) => ({ value: n, label: `${n} 条/页` }))}
/>
</Space>
<Button type="primary" onClick={load} loading={loading}>
<Button type="primary" onClick={() => load(1)} loading={loading}>
</Button>
</Space>
@@ -637,36 +653,13 @@ export default function AdRevenueReportPage() {
) : (
<Alert type="success" showIcon message={`${data.total} 条广告事件,应发与实发全部一致。`} />
)}
{data.truncated && (
<Alert
type="warning"
showIcon
message={`明细已截断到 ${queriedLimit} 条,还有更多未显示——请按用户/类型/日期缩小范围,或调大「条数」。合计数字不受影响。`}
/>
)}
{sceneCoinSubtotal != null && (
<Alert
type="info"
showIcon
message={
<span>
,
<Tag color={SCENE_TAG[scene!].color} style={{ marginInline: 4 }}>
{SCENE_TAG[scene!].text}
</Tag>
Draw <b>{sceneCoinSubtotal}</b>( {filteredItems.length} ;
,;/)
</span>
}
/>
)}
</Space>
)}
{data &&
(queriedMultiDay
? (data.daily?.length ?? 0) > 0
: queriedGranularity === 'hour' && (data.items?.length ?? 0) > 0) && (
: queriedGranularity === 'hour' && (data.hourly?.length ?? 0) > 0) && (
<Card
size="small"
title={queriedMultiDay ? '按天趋势' : '按小时趋势'}
@@ -701,27 +694,11 @@ export default function AdRevenueReportPage() {
</Space>
}
>
{/* 趋势图与日汇总恒为全量:按天图用 daily(全量),按小时图用全量 items;「场景」前端过滤只作用于下方明细表,不影响此图。 */}
{!queriedMultiDay && data.truncated && (
<Alert
type="warning"
showIcon
style={{ marginBottom: 12 }}
message={`图表按小时聚合的是已截断到 ${queriedLimit} 组的明细,可能少算——调大「条数」或缩小筛选可还原真实趋势。`}
/>
)}
{scene && (
<Alert
type="info"
showIcon
style={{ marginBottom: 12 }}
message="趋势图为全量数据,不随「场景」筛选变化;场景过滤仅作用于下方明细表与其金币小计。"
/>
)}
{/* 趋势图恒为全量(不受分页影响):按天图用 daily,按小时图用 hourly;「场景」已下推后端,趋势随场景筛选一并变化。 */}
{queriedMultiDay ? (
<TrendChart points={aggregateDaily(data.date_from, data.date_to, data.daily)} />
) : (
<TrendChart points={aggregateHourly(data.items)} />
<TrendChart points={aggregateHourly(data.hourly)} />
)}
</Card>
)}
@@ -729,15 +706,22 @@ export default function AdRevenueReportPage() {
<Table
rowKey="event_key"
columns={columns}
dataSource={filteredItems}
dataSource={items}
loading={loading}
pagination={false}
pagination={{
current: page,
pageSize: queriedLimit,
total: data?.total ?? 0,
showSizeChanger: false,
showTotal: (t) => `${t}`,
onChange: (p) => load(p, queriedLimit),
}}
size="small"
scroll={{ x: 1280 }}
scroll={{ x: 1380 }}
rowClassName={(r) => (r.has_reward && !r.matched ? 'row-mismatch' : '')}
expandable={{
// 只有发了奖的事件可展开,看「这条广告金币怎么算的」;纯展示行无复算、不可展开
rowExpandable: (r) => r.reward_detail != null,
// 每行都可展开(左侧恒有 + 号):有发奖看「金币复算因子」;纯展示看「展示明细」
rowExpandable: () => true,
expandedRowRender: (r) =>
r.reward_detail ? (
<div>
@@ -756,7 +740,28 @@ export default function AdRevenueReportPage() {
rowClassName={(d) => (d.matched ? '' : 'row-mismatch')}
/>
</div>
) : null,
) : (
<div>
<Typography.Text strong></Typography.Text>{' '}
<Typography.Text type="secondary">(,,)</Typography.Text>
<Descriptions size="small" column={3} bordered style={{ marginTop: 8 }}>
<Descriptions.Item label="时间">{formatUtcTime(r.created_at)}</Descriptions.Item>
<Descriptions.Item label="eCPM(分)">{r.ecpm ?? '-'}</Descriptions.Item>
<Descriptions.Item label="eCPM(元)">
{r.ecpm != null && !Number.isNaN(Number(r.ecpm))
? (Number(r.ecpm) / 100).toFixed(2)
: '-'}
</Descriptions.Item>
<Descriptions.Item label="预估收益(元)">
{r.has_impression ? r.revenue_yuan.toFixed(4) : '-'}
</Descriptions.Item>
<Descriptions.Item label="底层 ADN">{r.adn ?? '-'}</Descriptions.Item>
<Descriptions.Item label="底层代码位(rit)">{r.slot_id ?? '-'}</Descriptions.Item>
<Descriptions.Item label="我方代码位">{r.our_code_id ?? '-'}</Descriptions.Item>
<Descriptions.Item label="来源应用">{r.app_env ?? '-'}</Descriptions.Item>
</Descriptions>
</div>
),
}}
/>
<Modal
@@ -772,7 +777,7 @@ export default function AdRevenueReportPage() {
<Typography.Paragraph type="secondary" style={{ fontSize: 13 }}>
· eCPM 穿 SDK <Typography.Text code>getEcpm()</Typography.Text> ,/, ÷100 ; ÷1000
<br />· :<b>1 {COIN_PER_YUAN.toLocaleString()} </b>,
<br />· 2LT (); 1 , 10 1
<br />· 2LT ();<b>广 = 1 </b>( 1 ; 10 ,)
<br />· ,
</Typography.Paragraph>
<Row gutter={16}>
+13 -3
View File
@@ -365,7 +365,7 @@ export interface AdRevenueRecord {
matched: boolean;
}
// 广告收益报表:按日期汇总的一天(按天趋势图用;全量,不受 limit 影响)
// 广告收益报表:按日期汇总的一天(按天趋势图用;全量,不受分页影响)
export interface AdRevenueDaily {
date: string; // 北京时间 YYYY-MM-DD
impressions: number;
@@ -374,6 +374,15 @@ export interface AdRevenueDaily {
actual_coin: number;
}
// 广告收益报表:按北京小时(0–23)汇总的一小时(按小时趋势图用;全量,不受分页影响;按天查询时为空)
export interface AdRevenueHourly {
hour: number; // 北京时间小时 023
impressions: number;
revenue_yuan: number;
expected_coin: number;
actual_coin: number;
}
// 广告收益报表:一次广告事件(逐条一行)。激励视频展示+发奖按 ad_session_id 合并;信息流展示/发奖各自成行。
export interface AdRevenueRow {
event_key: string; // 事件稳定唯一键(前端 rowKey)
@@ -406,8 +415,9 @@ export interface AdRevenueReport {
date_from: string; // 起始日 北京时间 YYYY-MM-DD
date_to: string; // 结束日 北京时间 YYYY-MM-DD(闭区间;单日时与 date_from 相同)
daily: AdRevenueDaily[]; // 按日期汇总序列(全量,供按天趋势图)
total: number; // 广告事件总数(全量,不受 limit 影响)
truncated: boolean;
hourly: AdRevenueHourly[]; // 按小时汇总序列(全量,供按小时趋势图;按天查询时为空)
total: number; // 当前筛选下的分页总条数(全量,不受分页影响)
truncated: boolean; // 当前页之后是否还有更多事件(分页后前端不再据此报警)
total_impressions: number;
total_revenue_yuan: number;
total_expected_coin: number;