Reports & Analytics | 更新时间: 2026-02-08
reports/page.tsx, reports/layout.tsx, reports/ReportsLayoutClient.tsx,
reports/business/page.tsx, reports/finance/page.tsx, reports/employee/page.tsx,
reports/operational/page.tsx, reports/loyalty/page.tsx, reports/sales/page.tsx,
reports/profit/page.tsx, reports/entitlements/page.tsx, reports/working-hours/page.tsx,
reports/platform/page.tsx, reports/schedules/page.tsx
Reports/KPICard.tsx, Reports/KPICardGrid,
Reports/ExportMenu.tsx, Reports/ExportButton, Reports/ExportProgress,
Reports/ReportChart.tsx, Reports/ReportTable.tsx,
Reports/DateRangePicker.tsx, Reports/ComparisonBadge.tsx,
Reports/StoreSelector.tsx, Reports/TenantSelector.tsx,
Reports/ScheduleForm.tsx,
Reports/RetentionChart.tsx, Reports/ChurnWarningTable.tsx,
Reports/MembershipTierChart.tsx, Reports/PointsStatsCard.tsx,
Reports/GiftCardStatsCard.tsx, Reports/EmployeeRankingChart.tsx,
Reports/UtilizationChart.tsx, Reports/AppointmentSourceChart.tsx,
Reports/TimeSlotHeatmap.tsx, Reports/PaymentMethodChart.tsx,
Reports/PlatformOverview.tsx
hooks/useReportData.ts, hooks/useReportExport.ts, hooks/useReportSchedule.ts
api/reports/index.js, api/reports/business.js, api/reports/finance.js,
api/reports/employee.js, api/reports/operational.js, api/reports/loyalty.js,
api/reports/sales.js, api/reports/profit.js, api/reports/entitlements.js,
api/reports/working-hours.js, api/reports/platform.js,
api/reports/schedules.js, api/reports/export.js
services/reports/report-engine.js, services/reports/export-service.js,
services/reports/export-worker.js, services/reports/scheduler.js,
services/reports/report-cache.js, services/reports/audit-logger.js,
services/reports/queries/*.js (23 query modules)
flowchart TB
subgraph Frontend["前端 — React Query + Components"]
RP["reports/page.tsx 首页"]
PAGES["10 个报表页面"]
HOOKS["useReportData / useReportExport / useReportSchedule"]
COMP["23 个可复用组件 (KPICard, Chart, Table...)"]
end
subgraph Backend["后端 — Report Engine"]
API["API Layer (13 route files)"]
ENGINE["ReportEngine (report-engine.js)"]
CACHE["ReportCache (Redis)"]
QUERIES["Query Modules (23 modules, 90+ functions)"]
EXPORT["ExportService + ExportWorker"]
SCHED["Scheduler (cron-based)"]
AUDIT["AuditLogger"]
end
subgraph Data["数据层"]
DB["PostgreSQL (tenant schema)"]
S3["S3 (export files)"]
end
RP --> PAGES
PAGES --> HOOKS
HOOKS --> API
API --> ENGINE
ENGINE --> CACHE
ENGINE --> QUERIES
QUERIES --> DB
API --> EXPORT
EXPORT --> S3
API --> SCHED
ENGINE --> AUDIT
| 类别 | API 前缀 | 权限 | 子报表数 | 说明 |
|---|---|---|---|---|
| Business | /reports/business | reports:view | 1 (KPI Dashboard) | 营收、客流、客单价、同环比 |
| Finance | /reports/finance | reports:finance | 3 (Revenue/Tips/PaymentMethods) | 收入分布、小费、支付方式 |
| Employee | /reports/employee | reports:view_employee | 4 (Performance/Utilization/Ranking/Detail) | 绩效排名、利用率 |
| Operational | /reports/operational | reports:operational | 3 (Appointments/Cancellations/Heatmap) | 预约来源、爽约、热力图 |
| Loyalty | /reports/loyalty | reports:view | 5 (Retention/Churn/Membership/Points/GiftCards) | 留存队列、流失、会员、积分 |
| Sales | /reports/sales | reports:sales | 3 (Consumption/Detail/Summary) | 消费明细、销售汇总 (US10) |
| Profit | /reports/profit | reports:profit | 5 (SellCard/ConsumeCard/Comparison/Trend/IncomeExpense) | 双算法利润、收支 (US11) |
| Entitlements | /reports/entitlements | reports:view | 4 (Summary/Customers/ByTier/Expiring) | 权益汇总、到期提醒 (US12) |
| Working Hours | /reports/working-hours | reports:view | 4 (Summary/ByEmployee/Trend/ByDay) | 工时统计 (US4-EXT) |
| Platform | /reports/platform | reports:view_platform | 2 (Overview/Comparison) | 跨租户平台级报表 |
REPORT_QUERY_MAP 将 30+ 种 reportType 映射到对应的 query function。
Engine 负责:(1) 执行查询 (2) Redis 缓存 (3) 计算同环比 comparison (YoY/MoM/WoW) (4) 返回 {data, meta: {fromCache}}。
前端通过 useReportData<T>(reportType, params) 消费。
| CUJ | 优先级 | 描述 | 触发点 | 业务价值 | E2E 状态 |
|---|---|---|---|---|---|
| H1 | P0 | Business KPI Dashboard | 管理员查看 /reports/business | 核心数据洞察 — 营收/客流/客单价 | 已覆盖 |
| H2 | P0 | Finance 财务报表 | 查看 /reports/finance | 财务分析 — 收入/小费/支付方式 | 已覆盖 |
| H3 | P1 | Employee 员工绩效 | 查看 /reports/employee | 绩效管理 — 排名/利用率/明细 | 已覆盖 |
| H4 | P1 | Operational 运营报表 | 查看 /reports/operational | 运营优化 — 预约来源/爽约/热力图 | 已覆盖 |
| H5 | P1 | Loyalty 忠诚度报表 | 查看 /reports/loyalty | 留存分析 — 留存队列/流失/会员 | 部分覆盖 |
| H6 | P1 | Sales 销售报表 | 查看 /reports/sales | 销售分析 — 消费明细/销售汇总 (US10) | 部分覆盖 |
| H7 | P1 | Profit 利润报表 | 查看 /reports/profit | 利润洞察 — 双算法对比/收支 (US11) | 部分覆盖 |
| H8 | P2 | Entitlements 权益报表 | 查看 /reports/entitlements | 权益管理 — 汇总/到期提醒 (US12) | 未覆盖 |
| H9 | P2 | Working Hours 工时报表 | 查看 /reports/working-hours | 工时管理 — 出勤/趋势 (US4-EXT) | 未覆盖 |
| H10 | P2 | Platform 平台级报表 | platform_superadmin 查看 | 平台运营 — 跨租户对比 | 未覆盖 |
| H11 | P0 | Report Export 导出 | 任意报表页点击导出 | 数据可移植 — Excel/PDF | 部分覆盖 |
| H12 | P2 | Report Schedules 定时报表 | 管理员配置定时发送 | 自动化 — 定期邮件报表 | 未覆盖 |
P0 核心洞察 — 管理员快速了解营收、客流、客单价等关键指标
reports/business/page.tsx →
Reports/KPICard.tsx, Reports/ReportChart.tsx, Reports/DateRangePicker.tsx, Reports/StoreSelector.tsx, Reports/ExportMenu.tsx, Reports/ComparisonBadge.tsx →
hooks/useReportData.ts (useKPIData) →
api/reports/business.js (GET /kpi) →
queries/revenue.js, queries/traffic.js
flowchart TD
A["侧边栏 Reports"] --> B["报表首页 /reports"]
B --> C["7 类报表卡片"]
C -->|"Business"| D["/reports/business"]
D --> E["KPI Cards Grid"]
E --> F["Revenue / Traffic / Avg Ticket"]
E --> G["Appointments / Walk-Ins / Tips"]
E --> H["Return Rate / Cancel Rate"]
D --> I["Hourly Traffic Chart"]
D --> J["Customer Breakdown Pie"]
D --> K["Store Selector (multi)"]
D --> L["DateRangePicker"]
D --> M{"导出?"}
M -->|"Excel/PDF"| N["ExportMenu"]
style D fill:#2196F3,stroke:#1565C0,color:#fff
style E fill:#4CAF50,stroke:#2E7D32,color:#fff
| 指标 | 格式 | 颜色 | 说明 |
|---|---|---|---|
| Total Revenue | currency | blue | 总营收(含小费) |
| Total Traffic | number | green | 客流量(预约+Walk-in) |
| Average Ticket | currency | purple | 客单价 = 营收/客流 |
| Appointments | number | indigo | 预约数量 |
| Walk-Ins | number | yellow | Walk-in 数量 |
| Total Tips | currency | green | 小费总额 |
| Return Rate | percent | blue | 回头客比例 |
| Cancel Rate | percent | red | 爽约率 |
reports:view 权限,且具备对应范围(scope)/reports/business测试: reports/business-report.spec.ts
测试: reports/business-report.spec.ts
P0 财务分析 — 收入分布、小费明细、支付方式占比
reports/finance/page.tsx (3 tabs: Revenue / Tips / PaymentMethods) →
Reports/PaymentMethodChart.tsx, Reports/KPICard.tsx →
api/reports/finance.js (GET /revenue, GET /tips, GET /payment-methods) →
queries/revenue.js, queries/tips.js, queries/payment-methods.js
| Tab | API | KPI 指标 | 图表 |
|---|---|---|---|
| Revenue | GET /finance/revenue | 总收入、按期间分布、按门店分布 | Period 柱状图 |
| Tips | GET /finance/tips | 总小费、按员工、按天、百分比分布 | Employee 排名图 |
| Payment Methods | GET /finance/payment-methods | 按支付方式分布 | PaymentMethodChart 饼图 |
reports:finance 权限,且具备对应范围(scope)/reports/finance,默认 Revenue tab测试: reports/finance-report.spec.ts
测试: reports/finance-report.spec.ts
P1 绩效管理 — 员工排名、利用率分析、个人明细
reports/employee/page.tsx (3 tabs: Performance / Utilization / Detail) →
Reports/EmployeeRankingChart.tsx, Reports/UtilizationChart.tsx →
api/reports/employee.js (4 endpoints) →
queries/employee.js, queries/utilization.js, queries/ranking.js
reports:view_employee 权限,且具备对应范围(scope)测试: reports/employee-report.spec.ts
P1 运营优化 — 预约来源分析、爽约分析、时段热力图
reports/operational/page.tsx (3 tabs: Appointments / Cancellations / Heatmap) →
Reports/AppointmentSourceChart.tsx, Reports/TimeSlotHeatmap.tsx →
api/reports/operational.js (3 endpoints) →
queries/appointments.js, queries/cancellations.js
/reports/operational测试: reports/operational-report.spec.ts
P1 留存分析 — 留存队列、流失预警、会员分布、积分/礼品卡负债
reports/loyalty/page.tsx (5 tabs: Retention / Churn / Membership / Points / GiftCards) →
Reports/RetentionChart.tsx, Reports/ChurnWarningTable.tsx,
Reports/MembershipTierChart.tsx, Reports/PointsStatsCard.tsx,
Reports/GiftCardStatsCard.tsx →
api/reports/loyalty.js (5 endpoints) →
queries/retention.js, queries/churn.js, queries/membership.js, queries/points.js, queries/giftcards.js
P1 销售分析 — 消费明细、销售详情、分类汇总 (Feature US10)
reports/sales/page.tsx (3 tabs: Consumption / Detail / Summary) →
api/reports/sales.js (3 endpoints) →
queries/consumption.js, queries/sales-detail.js, queries/sales-summary.js
reports:sales 权限,且具备对应范围(scope)P1 利润洞察 — 双算法利润计算、收支分析 (Feature US11)
reports/profit/page.tsx (2 sections: Profit / IncomeExpense) →
api/reports/profit.js (5 endpoints) →
queries/profit.js, queries/income-expense.js
/profit/comparison 端点并排对比。
reports:profit 权限,且具备对应范围(scope)/reports/profitP2 权益管理 — 客户礼品卡/积分/会员权益汇总与到期提醒 (Feature US12)
reports/entitlements/page.tsx →
api/reports/entitlements.js (5 endpoints) →
queries/entitlements.js
reports:view 权限,且具备对应范围(scope)/reports/entitlementsP2 工时管理 — 员工出勤统计、趋势分析 (Feature US4-EXT)
reports/working-hours/page.tsx →
api/reports/working-hours.js (5 endpoints) →
queries/working-hours.js
/reports/working-hoursP2 平台运营 — 跨租户对比与增长趋势(仅 platform_superadmin)
reports/platform/page.tsx →
Reports/PlatformOverview.tsx, Reports/TenantSelector.tsx, Reports/StoreRankingTable.tsx →
api/reports/platform.js (4 endpoints) →
queries/platform.js
reports:view_platform 权限,且具备平台范围(scope)/reports/platformP0 数据可移植 — 导出 Excel/PDF 用于离线分析或分享
Reports/ExportMenu.tsx, Reports/ExportButton.tsx, Reports/ExportProgress.tsx →
hooks/useReportExport.ts →
api/reports/export.js (POST /export, POST /export/async, GET /export/:taskId) →
services/reports/export-service.js, services/reports/export-worker.js
flowchart LR
A["ExportMenu 选格式"] --> B{"文件大小?"}
B -->|"小"| C["POST /export (同步)"]
C --> D["直接下载文件"]
B -->|"大"| E["POST /export/async"]
E --> F["返回 taskId"]
F --> G["ExportProgress 轮询"]
G --> H["GET /export/:taskId"]
H --> I{"status?"}
I -->|"processing"| G
I -->|"completed"| J["下载 fileUrl"]
I -->|"failed"| K["显示错误"]
style A fill:#2196F3,stroke:#1565C0,color:#fff
style J fill:#4CAF50,stroke:#2E7D32,color:#fff
{reportType}_{startDate}_{endDate}.xlsxP2 自动化 — 配置定时发送报表到指定邮箱
reports/schedules/page.tsx →
Reports/ScheduleForm.tsx →
hooks/useReportSchedule.ts →
api/reports/schedules.js (7 endpoints: CRUD + run + stats + types) →
services/reports/scheduler.js
| 类型 | cron 示例 | 说明 |
|---|---|---|
| daily | 0 8 * * * | 每天早上 8 点 |
| weekly | 0 8 * * 1 | 每周一早上 8 点 |
| monthly | 0 8 1 * * | 每月 1 号早上 8 点 |
| custom | 用户自定义 cron | 自定义频率 |
reports:schedule 权限,且具备对应范围(scope)/reports/schedules 点击"新建"| 依赖方向 | 说明 |
|---|---|
| H → CUJ-B (Appointments) | Business/Operational 报表数据来源:预约表 |
| H → CUJ-F (Payments) | Finance/Sales/Profit 报表数据来源:支付记录 |
| H → CUJ-D (Employees) | Employee 报表数据来源:员工和排班表 |
| H → CUJ-E (Guests) | Loyalty 报表数据来源:客户、会员、积分表 |
| H → CUJ-M (Loyalty/GiftCards) | Entitlements/Loyalty 报表的礼品卡和积分数据 |
| H → CUJ-L (Stores/Services) | StoreSelector 依赖门店列表;Sales 按服务分类 |
| H → CUJ-N (Platform) | Platform 报表需要跨租户数据访问 |
| H → CUJ-C (Day-End Closeout) | Finance 报表的现金差异数据来自日结 |
| CUJ-J (Settings) → H | reports:* 权限配置影响报表访问 |
| # | 规则 | 实现位置 |
|---|---|---|
| 1 | 权限分级: reports:view (基础) / reports:finance (财务) / reports:view_employee (员工) / reports:view_platform (平台) / reports:schedule (定时) | 各 API 路由 requirePermission |
| 2 | 数据租户隔离: 所有查询通过 tenantDb.query(req, ...) 确保只能看到自己租户的数据;platform 报表例外 | queries/*.js + data-access layer |
| 3 | Redis 缓存: ReportEngine 按 reportType + params 生成缓存 key,TTL 按报表类型配置;meta.fromCache 标记是否命中缓存 | report-cache.js + report-engine.js |
| 4 | 同环比计算: comparison.js 自动计算 YoY/MoM/WoW 变化率,ComparisonBadge 前端展示 | queries/comparison.js + ComparisonBadge.tsx |
| 5 | 导出文件格式: Excel (.xlsx) 含多 sheet + 格式化;PDF 含图表渲染。异步导出上传 S3,60 秒轮询超时 | export-service.js + export-worker.js |
| 6 | 审计日志: 所有报表访问和导出操作记录到 audit-logger | audit-logger.js |
| 7 | Profit 双算法: Sell Card (卖卡时确认营收) vs Consume Card (消费时确认营收),/comparison 端点并排对比 | queries/profit.js + profit/page.tsx |
| 8 | Tab Prefetch: Loyalty 页面使用 usePrefetchReport 在切换 tab 前预取下一个 tab 的数据 | loyalty/page.tsx + useReportData.ts |
| 9 | 定时报表 cron: scheduler.js 根据 scheduleType 生成 cron 表达式;支持 daily/weekly/monthly/custom | scheduler.js + schedules.js |
| 10 | 门店筛选: StoreSelector 支持 multi-select,报表数据自动聚合所选门店 | StoreSelector.tsx + 各查询 storeId 参数 |