import Link from "next/link"; import { Prisma } from "@prisma/client"; import { Users, Cake, UserCheck, CalendarClock, ArrowUpRight } from "lucide-react"; import { prisma } from "@/lib/prisma"; import { birthdayMatchLists } from "@/lib/lunar"; import { fmtDate } from "@/lib/utils"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Table, THead, TR, TH, TD } from "@/components/ui/table"; import { MonthlyReportChart, type MonthPoint } from "./report-chart"; export default async function DashboardPage() { const now = new Date(); const monthStart = new Date(now.getFullYear(), now.getMonth(), 1); const { solar, lunar } = birthdayMatchLists(now, 1, true); // 近 12 个月窗口 const monthWindow: { key: string; label: string }[] = []; const cur = new Date(now.getFullYear(), now.getMonth(), 1); for (let i = 11; i >= 0; i--) { const d = new Date(cur.getFullYear(), cur.getMonth() - i, 1); const key = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}`; monthWindow.push({ key, label: `${String(d.getFullYear()).slice(2)}/${String(d.getMonth() + 1).padStart(2, "0")}`, }); } const windowStart = new Date(cur.getFullYear(), cur.getMonth() - 11, 1); const [total, admins, monthNew, birthdayToday, recent, monthly] = await Promise.all([ prisma.customer.count({ where: { deletedAt: null } }), prisma.admin.count({ where: { deletedAt: null } }), prisma.customer.count({ where: { deletedAt: null, reportDate: { gte: monthStart } } }), prisma.$queryRaw<{ n: bigint }[]>(Prisma.sql` SELECT COUNT(*) AS n FROM customers WHERE deleted_at IS NULL AND ( (birthday_type = 1 AND DATE_FORMAT(birthday,'%m-%d') IN (${Prisma.join(solar)})) OR (birthday_type = 2 AND DATE_FORMAT(birthday,'%m-%d') IN (${Prisma.join(lunar)})))`), prisma.customer.findMany({ where: { deletedAt: null }, orderBy: { reportDate: "desc" }, take: 6, }), prisma.$queryRaw<{ m: string; c: bigint }[]>(Prisma.sql` SELECT DATE_FORMAT(report_date,'%Y-%m') m, COUNT(*) c FROM customers WHERE deleted_at IS NULL AND report_date >= ${windowStart} GROUP BY m`), ]); const birthdayCount = Number(birthdayToday[0]?.n ?? 0); const cmap = new Map(monthly.map((r) => [r.m, Number(r.c)])); const chartData: MonthPoint[] = monthWindow.map((m) => ({ month: m.key, label: m.label, count: cmap.get(m.key) ?? 0, })); const last12Total = chartData.reduce((s, d) => s + d.count, 0); const stats = [ { label: "商户总数", value: total.toLocaleString(), icon: Users, tone: "primary" as const }, { label: "本月新增", value: monthNew.toLocaleString(), icon: CalendarClock, tone: "info" as const }, { label: "今日生日", value: birthdayCount.toLocaleString(), icon: Cake, tone: "warning" as const }, { label: "后台账号", value: admins.toLocaleString(), icon: UserCheck, tone: "success" as const }, ]; const toneBg: Record = { primary: "var(--color-primary-soft)", info: "var(--color-info-soft)", warning: "var(--color-warning-soft)", success: "var(--color-success-soft)", }; const toneFg: Record = { primary: "var(--color-primary)", info: "var(--color-info)", warning: "var(--color-warning)", success: "var(--color-success)", }; return (
{/* 统计卡片 */}
{stats.map((s) => { const Icon = s.icon; return (
{s.label}
{s.value}
); })}
{/* 月度新增报单 */} 月度新增报单(近 12 个月) 合计 {last12Total.toLocaleString()} 单 {/* 最近商户 */} 最近报单商户 查看全部
{recent.map((c) => ( ))}
商户名称 商户编号 联系人 业务员 报单时间 生日
{c.customerName} {c.customerNo || "—"} {c.contacts} {c.salesman} {fmtDate(c.reportDate)} {fmtDate(c.birthday)}
{recent.length === 0 && (
暂无报单
)} {recent.map((c) => (
{c.customerName}
{c.customerNo ? (
编号 {c.customerNo}
) : null}
{c.contacts || "—"} {c.salesman ? ` · ${c.salesman}` : ""}
{fmtDate(c.reportDate)}
{c.birthday ? (
{fmtDate(c.birthday)}
) : null}
))}
); }