import { Users, BookOpen, CheckCircle, Clock, TrendingUp, AlertCircle, BarChart3, Target, Activity, GraduationCapIcon, } from "lucide-react"; import Link from "next/link"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import prisma from "@/lib/prisma"; import { auth } from "@/lib/auth"; import { redirect } from "next/navigation"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Progress } from "@/components/ui/progress"; interface Stats { totalUsers?: number; totalProblems?: number; totalSubmissions?: number; totalStudents?: number; completedProblems?: number; } interface Activity { type: string; title: string; description: string; time: Date; status?: string; } export default async function DashboardPage() { const session = await auth(); const user = session?.user; if (!user) { redirect("/sign-in"); } // 获取用户的完整信息 const fullUser = await prisma.user.findUnique({ where: { id: user.id }, select: { id: true, name: true, email: true, image: true, role: true }, }); if (!fullUser) { redirect("/sign-in"); } // 根据用户角色获取不同的统计数据 let stats: Stats = {}; let recentActivity: Activity[] = []; if (fullUser.role === "ADMIN") { // 管理员统计 const [totalUsers, totalProblems, totalSubmissions, recentUsers] = await Promise.all([ prisma.user.count(), prisma.problem.count(), prisma.submission.count(), prisma.user.findMany({ take: 5, orderBy: { createdAt: "desc" }, select: { id: true, name: true, email: true, role: true, createdAt: true, }, }), ]); stats = { totalUsers, totalProblems, totalSubmissions }; recentActivity = recentUsers.map((user) => ({ type: "新用户注册", title: user.name || user.email, description: `角色: ${user.role}`, time: user.createdAt, })); } else if (fullUser.role === "TEACHER") { // 教师统计 const [totalStudents, totalProblems, totalSubmissions, recentSubmissions] = await Promise.all([ prisma.user.count({ where: { role: "GUEST" } }), prisma.problem.count({ where: { isPublished: true } }), prisma.submission.count(), prisma.submission.findMany({ take: 5, orderBy: { createdAt: "desc" }, include: { user: { select: { name: true, email: true } }, problem: { select: { displayId: true, localizations: { where: { type: "TITLE", locale: "zh" }, select: { content: true }, }, }, }, }, }), ]); stats = { totalStudents, totalProblems, totalSubmissions }; recentActivity = recentSubmissions.map((sub) => ({ type: "学生提交", title: `${sub.user.name || sub.user.email} 提交了题目 ${ sub.problem.displayId }`, description: sub.problem.localizations[0]?.content || `题目${sub.problem.displayId}`, time: sub.createdAt, status: sub.status, })); } else { // 学生统计 const [ totalProblems, completedProblems, totalSubmissions, recentSubmissions, ] = await Promise.all([ prisma.problem.count({ where: { isPublished: true } }), prisma.submission.count({ where: { userId: user.id, status: "AC", }, }), prisma.submission.count({ where: { userId: user.id } }), prisma.submission.findMany({ where: { userId: user.id }, take: 5, orderBy: { createdAt: "desc" }, include: { problem: { select: { displayId: true, localizations: { where: { type: "TITLE", locale: "zh" }, select: { content: true }, }, }, }, }, }), ]); stats = { totalProblems, completedProblems, totalSubmissions }; recentActivity = recentSubmissions.map((sub) => ({ type: "我的提交", title: `题目 ${sub.problem.displayId}`, description: sub.problem.localizations[0]?.content || `题目${sub.problem.displayId}`, time: sub.createdAt, status: sub.status, })); } const getRoleConfig = () => { switch (fullUser.role) { case "ADMIN": return { title: "系统管理后台", description: "管理整个系统的用户、题目和统计数据", stats: [ { label: "总用户数", value: stats.totalUsers, icon: Users, color: "text-blue-600", }, { label: "总题目数", value: stats.totalProblems, icon: BookOpen, color: "text-green-600", }, { label: "总提交数", value: stats.totalSubmissions, icon: Activity, color: "text-purple-600", }, ], actions: [ { label: "管理员管理", href: "/dashboard/management", icon: Target, }, { label: "用户管理", href: "/dashboard/usermanagement/guest", icon: Users, }, { label: "教师管理", href: "/dashboard/usermanagement/teacher", icon: GraduationCapIcon, }, { label: "题目管理", href: "/dashboard/usermanagement/problem", icon: BookOpen, }, ], }; case "TEACHER": return { title: "教师教学平台", description: "查看学生学习情况,管理教学资源", stats: [ { label: "学生数量", value: stats.totalStudents, icon: Users, color: "text-blue-600", }, { label: "题目数量", value: stats.totalProblems, icon: BookOpen, color: "text-green-600", }, { label: "提交数量", value: stats.totalSubmissions, icon: Activity, color: "text-purple-600", }, ], actions: [ { label: "用户管理", href: "/dashboard/usermanagement/guest", icon: Users, }, { label: "题目管理", href: "/dashboard/usermanagement/problem", icon: BookOpen, }, { label: "完成情况", href: "/dashboard/teacher/dashboard", icon: BarChart3, }, ], }; default: return { title: "我的学习中心", description: "继续您的编程学习之旅", stats: [ { label: "总题目数", value: stats.totalProblems, icon: BookOpen, color: "text-blue-600", }, { label: "已完成", value: stats.completedProblems, icon: CheckCircle, color: "text-green-600", }, { label: "提交次数", value: stats.totalSubmissions, icon: Activity, color: "text-purple-600", }, ], actions: [ { label: "我的进度", href: "/dashboard/student/dashboard", icon: TrendingUp, }, { label: "开始做题", href: "/problemset", icon: BookOpen }, { label: "个人设置", href: "/dashboard/management", icon: Target }, ], }; } }; const config = getRoleConfig(); const completionRate = fullUser.role === "GUEST" ? (stats.totalProblems || 0) > 0 ? ((stats.completedProblems || 0) / (stats.totalProblems || 1)) * 100 : 0 : 0; return (
{/* 欢迎区域 */}

{config.title}

{config.description}

{fullUser.role} 欢迎回来,{fullUser.name || fullUser.email}
{/* 统计卡片 */}
{config.stats.map((stat, index) => ( {stat.label}
{stat.value}
))}
{/* 学生进度条 */} {fullUser.role === "GUEST" && ( 学习进度 已完成 {stats.completedProblems || 0} / {stats.totalProblems || 0}{" "} 道题目

完成率: {completionRate.toFixed(1)}%

)} {/* 快速操作 */} 快速操作 常用功能快速访问
{config.actions.map((action, index) => ( ))}
{/* 最近活动 */} 最近活动 查看最新的系统活动
{recentActivity.length > 0 ? ( recentActivity.map((activity, index) => (
{activity.status === "AC" ? ( ) : activity.status ? ( ) : ( )}

{activity.title}

{activity.description}

{new Date(activity.time).toLocaleDateString()}
)) ) : (

暂无活动

)}
); }