"use client" import { useState } from "react" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Label } from "@/components/ui/label" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { FileText, Plus, Download, RefreshCw, Eye, Calendar, Users, TrendingUp, PieChart, BarChart3, Clock, CheckCircle2, Loader2, Send, Mail, MessageSquare, Play, Sparkles, } from "lucide-react" // 报告规则接口 interface ReportRule { id: string name: string templateId: string schedule: string enabled: boolean isDefault: boolean config: { dateRange: string includeCharts: boolean sendTo: string[] format: 'pdf' | 'html' | 'markdown' } promptTemplate: string } // 报告模板 interface ReportTemplate { id: string name: string description: string type: 'daily' | 'weekly' | 'monthly' | 'custom' sections: string[] icon: string } // 默认报告规则 const DEFAULT_RULES: ReportRule[] = [ { id: 'rule_daily', name: '每日运营日报规则', templateId: 'tpl_daily', schedule: '每日 23:59', enabled: true, isDefault: true, config: { dateRange: 'today', includeCharts: true, sendTo: ['admin@company.com'], format: 'pdf', }, promptTemplate: `生成每日运营日报,包含以下内容: 1. 核心指标 - 总用户数及日增量 - 活跃用户数及活跃率 - 新增用户数 2. 用户活跃 - 各等级用户活跃情况 - 活跃时段分布 3. 渠道消息 - 各渠道消息量统计 - 响应时间分析 4. AI查询统计 - 查询次数 - 平均响应时间 - 热门查询类型 输出格式:结构化报告,包含数据表格和趋势描述`, }, { id: 'rule_weekly', name: '周度数据质量规则', templateId: 'tpl_weekly', schedule: '每周日 00:00', enabled: true, isDefault: true, config: { dateRange: 'week', includeCharts: true, sendTo: ['data@company.com'], format: 'pdf', }, promptTemplate: `生成周度数据质量报告: 1. 质量概览 - 数据完整度评分 - 数据准确度评分 - 本周质量趋势 2. 清洗统计 - 清洗记录数 - 格式化字段数 - 异常数据处理 3. 去重效果 - 去重前后对比 - 重复率统计 4. 数据完整度 - 各字段填充率 - 缺失字段分析`, }, { id: 'rule_monthly', name: '月度资产报告规则', templateId: 'tpl_monthly', schedule: '每月1日 08:00', enabled: true, isDefault: true, config: { dateRange: 'month', includeCharts: true, sendTo: ['admin@company.com', 'ceo@company.com'], format: 'pdf', }, promptTemplate: `生成月度用户资产报告: 1. 资产总览 - 用户总资产价值 - 月度变化趋势 - 核心指标汇总 2. 用户增长 - 新增用户分析 - 流失用户分析 - 净增长情况 3. RFM分析 - RFM评分分布 - 各等级用户占比 - 价值迁移分析 4. 流量池分布 - 各流量池规模 - 月度变化 - 转化分析 5. 标签覆盖 - 标签覆盖率 - 热门标签TOP10`, }, ] // 预定义报告模板 const REPORT_TEMPLATES: ReportTemplate[] = [ { id: 'tpl_daily', name: '每日运营日报', description: '包含当日用户活跃、渠道消息、AI查询等实时数据汇总', type: 'daily', sections: ['核心指标', '用户活跃', '渠道消息', 'AI查询统计'], icon: '📊', }, { id: 'tpl_weekly', name: '周度数据质量报告', description: '数据清洗、去重、补全等质量指标周度汇总', type: 'weekly', sections: ['质量概览', '清洗统计', '去重效果', '数据完整度'], icon: '📈', }, { id: 'tpl_monthly', name: '月度用户资产报告', description: '用户增长、RFM分布、流量池变化等核心资产指标', type: 'monthly', sections: ['资产总览', '用户增长', 'RFM分析', '流量池分布', '标签覆盖'], icon: '📑', }, { id: 'tpl_rfm', name: 'RFM分析报告', description: '详细的RFM评分分布和用户价值分层分析', type: 'custom', sections: ['RFM概览', '评分分布', '等级分层', '价值趋势'], icon: '💎', }, { id: 'tpl_portrait', name: '用户画像报告', description: '用户画像特征分析,包含地域、年龄、偏好等维度', type: 'custom', sections: ['画像概览', '地域分布', '行为特征', '偏好分析'], icon: '👤', }, { id: 'tpl_traffic', name: '流量池分析报告', description: '各流量池用户规模、转化率、价值贡献分析', type: 'custom', sections: ['池规模', '转化漏斗', '价值贡献', '增长趋势'], icon: '🏊', }, ] // 生成的报告 interface GeneratedReport { id: string templateId: string title: string generatedAt: string status: 'ready' | 'generating' content?: string metrics: { totalUsers: number activeUsers: number newUsers: number rfmAvg: number } } const GENERATED_REPORTS: GeneratedReport[] = [ { id: 'rpt_1', templateId: 'tpl_monthly', title: '2024年1月用户资产月报', generatedAt: '2024-02-01 08:00', status: 'ready', metrics: { totalUsers: 2013000000, activeUsers: 450000000, newUsers: 15000000, rfmAvg: 52.3 }, }, { id: 'rpt_2', templateId: 'tpl_weekly', title: '本周数据质量报告', generatedAt: '2024-01-28 00:00', status: 'ready', metrics: { totalUsers: 2013000000, activeUsers: 320000000, newUsers: 2500000, rfmAvg: 51.8 }, }, { id: 'rpt_3', templateId: 'tpl_daily', title: '今日运营日报', generatedAt: '2024-01-30 23:59', status: 'ready', metrics: { totalUsers: 2013000000, activeUsers: 45000000, newUsers: 350000, rfmAvg: 52.5 }, }, ] export default function ReportPage() { const [reports, setReports] = useState(GENERATED_REPORTS) const [rules, setRules] = useState(DEFAULT_RULES) const [activeTab, setActiveTab] = useState("templates") const [generating, setGenerating] = useState(null) const [showGenerateDialog, setShowGenerateDialog] = useState(false) const [selectedTemplate, setSelectedTemplate] = useState(null) const [showPreviewDialog, setShowPreviewDialog] = useState(false) const [previewReport, setPreviewReport] = useState(null) const [showRuleDialog, setShowRuleDialog] = useState(false) const [selectedRule, setSelectedRule] = useState(null) const [editingPrompt, setEditingPrompt] = useState('') const formatNumber = (num: number): string => { if (num >= 1000000000) return `${(num / 1000000000).toFixed(2)}B` if (num >= 1000000) return `${(num / 1000000).toFixed(1)}M` if (num >= 1000) return `${(num / 1000).toFixed(0)}K` return num.toLocaleString() } // 通过Skill生成报告 const generateReport = async (template: ReportTemplate) => { setGenerating(template.id) setShowGenerateDialog(false) try { // 调用AI Chat API执行报告生成 const response = await fetch('/api/ai-chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: `生成${template.name},包含以下章节:${template.sections.join('、')}` }) }) const data = await response.json() // 添加新报告到列表 const newReport: GeneratedReport = { id: `rpt_${Date.now()}`, templateId: template.id, title: `${template.name} - ${new Date().toLocaleDateString()}`, generatedAt: new Date().toLocaleString(), status: 'ready', content: data.success ? data.response?.content : '报告生成失败', metrics: { totalUsers: 2013000000, activeUsers: 450000000, newUsers: 15000000, rfmAvg: 52.3, }, } setReports([newReport, ...reports]) } catch (error) { console.error('报告生成失败:', error) } finally { setGenerating(null) } } const openPreview = (report: GeneratedReport) => { setPreviewReport(report) setShowPreviewDialog(true) } return (
{/* 顶部标题 */}

智能报告

AI自动生成数据分析报告,支持多种报告模板

{/* 标签页 */} 报告模板 规则配置 历史报告 {/* 报告模板 */}
{REPORT_TEMPLATES.map(template => (
{template.icon} { template.type === 'daily' ? '日报' : template.type === 'weekly' ? '周报' : template.type === 'monthly' ? '月报' : '自定义' }

{template.name}

{template.description}

{template.sections.map((section, i) => ( {section} ))}
))}
{/* 规则配置 */} {rules.map(rule => { const template = REPORT_TEMPLATES.find(t => t.id === rule.templateId) return (
{template?.icon || '📄'}

{rule.name}

{rule.isDefault && 默认规则} {rule.enabled ? ( 已启用 ) : ( 已禁用 )}

{template?.name}

{rule.schedule} 格式: {rule.config.format.toUpperCase()} 发送: {rule.config.sendTo.join(', ')}
) })}
{/* 历史报告 */}
{reports.map(report => { const template = REPORT_TEMPLATES.find(t => t.id === report.templateId) return (
{template?.icon || '📄'}

{report.title}

{report.generatedAt} {formatNumber(report.metrics.totalUsers)} 用户 RFM均值: {report.metrics.rfmAvg}
) })}
{/* 生成报告弹窗 */} 生成智能报告 选择报告模板,AI将自动生成分析报告
{selectedTemplate && (

{selectedTemplate.description}

{selectedTemplate.sections.map((section, i) => ( {section} ))}
)}
{/* 报告预览弹窗 */} {previewReport?.title} 生成时间: {previewReport?.generatedAt} {previewReport && (
{/* 核心指标 */}
{formatNumber(previewReport.metrics.totalUsers)}
总用户
{formatNumber(previewReport.metrics.activeUsers)}
活跃用户
{formatNumber(previewReport.metrics.newUsers)}
新增用户
{previewReport.metrics.rfmAvg}
RFM均值
{/* 报告内容 */}

报告摘要

{previewReport.content || (

本报告基于神射手数据中台生成,涵盖用户资产数字化核心指标。

用户规模: 总用户{formatNumber(previewReport.metrics.totalUsers)},其中活跃用户占比{((previewReport.metrics.activeUsers / previewReport.metrics.totalUsers) * 100).toFixed(1)}%

用户价值: 平均RFM评分{previewReport.metrics.rfmAvg},较上期提升0.5分

增长趋势: 新增用户{formatNumber(previewReport.metrics.newUsers)},环比增长8.2%

)}
)}
{/* 规则编辑弹窗 */} 编辑报告规则 {selectedRule?.name} {selectedRule && (