录入、列表和导入导出带上商户编号。生日提醒展示与录入一致的商户资料,旧阴历生日仍按农历提醒。 Co-authored-by: Cursor <cursoragent@cursor.com>
191 lines
7.5 KiB
TypeScript
191 lines
7.5 KiB
TypeScript
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<string, string> = {
|
|
primary: "var(--color-primary-soft)",
|
|
info: "var(--color-info-soft)",
|
|
warning: "var(--color-warning-soft)",
|
|
success: "var(--color-success-soft)",
|
|
};
|
|
const toneFg: Record<string, string> = {
|
|
primary: "var(--color-primary)",
|
|
info: "var(--color-info)",
|
|
warning: "var(--color-warning)",
|
|
success: "var(--color-success)",
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
{/* 统计卡片 */}
|
|
<div className="grid grid-cols-2 gap-3 lg:grid-cols-4">
|
|
{stats.map((s) => {
|
|
const Icon = s.icon;
|
|
return (
|
|
<Card key={s.label}>
|
|
<CardContent className="flex items-center gap-2.5 md:gap-4">
|
|
<span
|
|
className="flex h-9 w-9 shrink-0 items-center justify-center rounded-xl md:h-12 md:w-12"
|
|
style={{ background: toneBg[s.tone], color: toneFg[s.tone] }}
|
|
>
|
|
<Icon className="h-5 w-5 md:h-6 md:w-6" />
|
|
</span>
|
|
<div className="min-w-0">
|
|
<div className="text-xs text-[var(--color-muted-foreground)] md:text-sm">{s.label}</div>
|
|
<div className="mt-0.5 text-xl font-semibold tabular-nums md:text-2xl">{s.value}</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
);
|
|
})}
|
|
</div>
|
|
|
|
{/* 月度新增报单 */}
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>月度新增报单(近 12 个月)</CardTitle>
|
|
<span className="text-[13px] text-[var(--color-muted-foreground)]">
|
|
合计 <span className="font-semibold text-[var(--color-foreground)]">{last12Total.toLocaleString()}</span> 单
|
|
</span>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<MonthlyReportChart data={chartData} />
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* 最近商户 */}
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>最近报单商户</CardTitle>
|
|
<Link
|
|
href="/customers"
|
|
className="flex min-h-11 items-center gap-1 text-[13px] font-medium text-[var(--color-primary)] hover:underline md:min-h-0"
|
|
>
|
|
查看全部 <ArrowUpRight className="h-3.5 w-3.5" />
|
|
</Link>
|
|
</CardHeader>
|
|
<div className="hidden md:block">
|
|
<Table>
|
|
<THead>
|
|
<tr>
|
|
<TH>商户名称</TH>
|
|
<TH>商户编号</TH>
|
|
<TH>联系人</TH>
|
|
<TH>业务员</TH>
|
|
<TH>报单时间</TH>
|
|
<TH>生日</TH>
|
|
</tr>
|
|
</THead>
|
|
<tbody>
|
|
{recent.map((c) => (
|
|
<TR key={c.id}>
|
|
<TD className="font-medium">{c.customerName}</TD>
|
|
<TD className="break-all">{c.customerNo || "—"}</TD>
|
|
<TD>{c.contacts}</TD>
|
|
<TD>{c.salesman}</TD>
|
|
<TD className="tabular-nums">{fmtDate(c.reportDate)}</TD>
|
|
<TD className="tabular-nums">{fmtDate(c.birthday)}</TD>
|
|
</TR>
|
|
))}
|
|
</tbody>
|
|
</Table>
|
|
</div>
|
|
<div className="divide-y md:hidden">
|
|
{recent.length === 0 && (
|
|
<div className="px-4 py-10 text-center text-sm text-[var(--color-muted-foreground)]">
|
|
暂无报单
|
|
</div>
|
|
)}
|
|
{recent.map((c) => (
|
|
<div key={c.id} className="flex items-start justify-between gap-3 px-4 py-3">
|
|
<div className="min-w-0">
|
|
<div className="truncate font-medium">{c.customerName}</div>
|
|
{c.customerNo ? (
|
|
<div className="mt-0.5 break-all text-xs text-[var(--color-muted-foreground)]">
|
|
编号 {c.customerNo}
|
|
</div>
|
|
) : null}
|
|
<div className="mt-1 text-xs text-[var(--color-muted-foreground)]">
|
|
{c.contacts || "—"}
|
|
{c.salesman ? ` · ${c.salesman}` : ""}
|
|
</div>
|
|
</div>
|
|
<div className="shrink-0 text-right">
|
|
<div className="text-xs tabular-nums text-[var(--color-muted-foreground)]">
|
|
{fmtDate(c.reportDate)}
|
|
</div>
|
|
{c.birthday ? (
|
|
<div className="mt-1 text-xs tabular-nums text-[var(--color-muted-foreground)]">
|
|
{fmtDate(c.birthday)}
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}
|