import { formatPercent, formatRatioPercent } from '../../helpers/safeDisplay'; import React, { useMemo, useState, useEffect, useCallback } from 'react'; import { Card, Spin, Tag, Progress } from '@/components/ui'; import CollapsibleHeader from './CollapsibleHeader'; import { VChart } from '@visactor/react-vchart'; import { API, showError } from '../../helpers'; import { Briefcase, Cpu, Users, TrendingUp, BarChart3, PieChart, } from 'lucide-react'; // 职业类型标签配置 const JOB_TYPE_CONFIG = { 1: { label: '技术岗位', color: 'blue', icon: '💻', desc: '开发/测试/运维等' }, 2: { label: '技术周边', color: 'green', icon: '🎨', desc: '产品/设计/运营等', }, 3: { label: '其他行业', color: 'orange', icon: '🏢', desc: '非IT行业' }, }; // AI工具使用标签配置 const AI_TOOL_CONFIG = { 1: { label: '正在使用', color: 'green', icon: '🚀', desc: '活跃用户' }, 2: { label: '了解/尝试过', color: 'blue', icon: '🔍', desc: '潜在用户' }, 3: { label: '有兴趣', color: 'orange', icon: '✨', desc: '待转化' }, 4: { label: '暂不需要', color: 'grey', icon: '💤', desc: '观望用户' }, }; const UserSurveyStats = ({ title, icon, t }) => { const [loading, setLoading] = useState(false); const [surveyStats, setSurveyStats] = useState(null); const [surveyTrend, setSurveyTrend] = useState([]); const [collapsed, setCollapsed] = useState(true); // 默认折叠 // 获取统计数据 const fetchSurveyStats = useCallback(async () => { try { const res = await API.get('/api/analytics/survey/stats'); if (res.data.success) { setSurveyStats(res.data.data); } else { showError(res.data.message); } } catch (error) { console.error('获取问卷统计失败:', error); } }, []); // 获取趋势数据 const fetchSurveyTrend = useCallback(async () => { try { const res = await API.get('/api/analytics/survey/trend'); if (res.data.success) { setSurveyTrend(res.data.data || []); } } catch (error) { console.error('获取问卷趋势失败:', error); } }, []); // 初始加载 useEffect(() => { const loadData = async () => { setLoading(true); await Promise.all([fetchSurveyStats(), fetchSurveyTrend()]); setLoading(false); }; loadData(); }, [fetchSurveyStats, fetchSurveyTrend]); // 职业分布饼图配置 const jobTypePieSpec = useMemo(() => { if (!surveyStats?.job_type_stats || surveyStats.job_type_stats.length === 0) return null; const chartData = surveyStats.job_type_stats.map((item) => ({ name: t(JOB_TYPE_CONFIG[item.job_type]?.label || '未知'), value: item.count, percentage: item.percentage, })); return { type: 'pie', data: [{ id: 'jobType', values: chartData }], valueField: 'value', categoryField: 'name', outerRadius: 0.8, innerRadius: 0.5, padAngle: 0.02, pie: { style: { cornerRadius: 4, }, }, tooltip: { mark: { content: [ { key: (datum) => datum?.name, value: (datum) => t('{{n}} 人', { n: datum?.value || 0, count: datum?.value || 0, }), }, { key: () => t('占比'), value: (datum) => formatPercent(datum?.percentage || 0, 1), }, ], }, }, legends: { visible: true, position: 'bottom', orient: 'horizontal', }, label: { visible: true, position: 'outside', content: '{name}', }, color: ['#3b82f6', '#10b981', '#f59e0b'], }; }, [surveyStats, t]); // AI工具使用饼图配置 const aiToolPieSpec = useMemo(() => { if (!surveyStats?.ai_tool_stats || surveyStats.ai_tool_stats.length === 0) return null; const chartData = surveyStats.ai_tool_stats.map((item) => ({ name: t(AI_TOOL_CONFIG[item.ai_tool_usage]?.label || '未知'), value: item.count, percentage: item.percentage, })); return { type: 'pie', data: [{ id: 'aiTool', values: chartData }], valueField: 'value', categoryField: 'name', outerRadius: 0.8, innerRadius: 0.5, padAngle: 0.02, pie: { style: { cornerRadius: 4, }, }, tooltip: { mark: { content: [ { key: (datum) => datum?.name, value: (datum) => t('{{n}} 人', { n: datum?.value || 0, count: datum?.value || 0, }), }, { key: () => t('占比'), value: (datum) => formatPercent(datum?.percentage || 0, 1), }, ], }, }, legends: { visible: true, position: 'bottom', orient: 'horizontal', }, label: { visible: true, position: 'outside', content: '{name}', }, color: ['#10b981', '#3b82f6', '#f59e0b', '#9ca3af'], }; }, [surveyStats, t]); // 趋势图配置 const trendSpec = useMemo(() => { if (!surveyTrend || surveyTrend.length === 0) return null; const chartData = [...surveyTrend].reverse().map((item) => ({ date: item.date, count: item.count, })); return { type: 'bar', data: [{ id: 'trend', values: chartData }], xField: 'date', yField: 'count', bar: { style: { cornerRadius: [4, 4, 0, 0], fill: { gradient: 'linear', x0: 0, y0: 0, x1: 0, y1: 1, stops: [ { offset: 0, color: '#8b5cf6' }, { offset: 1, color: '#6366f1' }, ], }, }, }, tooltip: { mark: { content: [ { key: () => t('日期'), value: (datum) => datum?.date, }, { key: () => t('新增'), value: (datum) => t('{{n}} 人', { n: datum?.count || 0, count: datum?.count || 0, }), }, ], }, }, axes: [ { orient: 'bottom', label: { autoRotate: true, autoRotateAngle: [0, 45], }, }, { orient: 'left', label: { formatMethod: (v) => Math.round(v), }, }, ], }; }, [surveyTrend, t]); // 计算统计数据 const totalUsers = surveyStats?.total || 0; const techUsers = surveyStats?.job_type_stats?.find((s) => s.job_type === 1)?.count || 0; const activeAIUsers = surveyStats?.ai_tool_stats?.find((s) => s.ai_tool_usage === 1)?.count || 0; const interestedUsers = surveyStats?.ai_tool_stats?.find((s) => s.ai_tool_usage === 3)?.count || 0; return (
setCollapsed((value) => !value)} extra={ <> {totalUsers > 0 && ( {t('{{n}} 人已填写', { n: totalUsers, count: totalUsers })} )} } />
{!collapsed && ( {totalUsers === 0 ? (
{t('暂无数据')}
) : (
{/* 概览统计卡片 */}
{t('总填写')}
{totalUsers}
{t('技术岗位')}
{techUsers}
{formatRatioPercent(techUsers, totalUsers, 1, '0.0%')}
{t('正在使用AI')}
{activeAIUsers}
{formatRatioPercent( activeAIUsers, totalUsers, 1, '0.0%', )}
{t('有兴趣尝试')}
{interestedUsers}
{formatRatioPercent( interestedUsers, totalUsers, 1, '0.0%', )}
{/* 图表区域 */}
{/* 职业分布 */}
{t('职业分布')}
{jobTypePieSpec ? ( <>
{surveyStats?.job_type_stats?.map((item) => { const config = JOB_TYPE_CONFIG[item.job_type]; return (
{config?.icon} {t(config?.label || '')}
{item.count}
); })}
) : (
{t('暂无数据')}
)}
{/* AI工具使用情况 */}
{t('AI工具使用情况')}
{aiToolPieSpec ? ( <>
{surveyStats?.ai_tool_stats?.map((item) => { const config = AI_TOOL_CONFIG[item.ai_tool_usage]; const colors = { green: '#10b981', blue: '#3b82f6', orange: '#f59e0b', grey: '#9ca3af', }; return (
{config?.icon} {t(config?.label || '')}
{item.count}
); })}
) : (
{t('暂无数据')}
)}
{/* 填写趋势 */}
{t('近30天填写趋势')}
{trendSpec ? ( ) : (
{t('暂无数据')}
)}
{/* 交叉分析表格 */} {surveyStats?.cross_analysis && surveyStats.cross_analysis.length > 0 && (
{t('交叉分析')}
{surveyStats.cross_analysis.map((item, index) => ( ))}
{t('职业类型')} {t('AI工具使用')} {t('人数')}
{JOB_TYPE_CONFIG[item.job_type]?.icon} {t( JOB_TYPE_CONFIG[item.job_type] ?.label || '', )}
{t( AI_TOOL_CONFIG[item.ai_tool_usage] ?.label || '', )} {item.count}
)}
)}
)}
); }; export default UserSurveyStats;