2026-07-08 15:02:03 +08:00
|
|
|
import { Prisma } from "@prisma/client";
|
|
|
|
|
import { prisma } from "@/lib/prisma";
|
2026-07-13 16:46:01 +08:00
|
|
|
import { auth } from "@/lib/auth";
|
|
|
|
|
import { isSuperAdmin } from "@/lib/acl";
|
2026-07-08 15:02:03 +08:00
|
|
|
import { birthdayMatchLists } from "@/lib/lunar";
|
2026-07-13 16:46:01 +08:00
|
|
|
import { fmtDate, maskPhone } from "@/lib/utils";
|
2026-07-08 15:02:03 +08:00
|
|
|
import { Card } from "@/components/ui/card";
|
|
|
|
|
import { Table, THead, TR, TH, TD } from "@/components/ui/table";
|
|
|
|
|
import { Badge } from "@/components/ui/badge";
|
|
|
|
|
import { BirthdayControl } from "./birthday-control";
|
|
|
|
|
|
|
|
|
|
type Row = {
|
|
|
|
|
id: number;
|
|
|
|
|
customerName: string | null;
|
|
|
|
|
contacts: string | null;
|
|
|
|
|
phone: string | null;
|
|
|
|
|
salesman: string | null;
|
|
|
|
|
reportDate: Date | null;
|
|
|
|
|
birthday: Date | null;
|
|
|
|
|
birthdayType: number | null;
|
2026-07-12 16:58:52 +08:00
|
|
|
birthdayOwner: number | null;
|
2026-07-08 15:02:03 +08:00
|
|
|
remark: string | null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function baseDate(s?: string): Date {
|
|
|
|
|
if (s) {
|
|
|
|
|
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(s);
|
|
|
|
|
if (m) return new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3]));
|
|
|
|
|
}
|
|
|
|
|
return new Date();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default async function BirthdayPage({
|
|
|
|
|
searchParams,
|
|
|
|
|
}: {
|
|
|
|
|
searchParams: Promise<{ mode?: string; date?: string }>;
|
|
|
|
|
}) {
|
2026-07-13 16:46:01 +08:00
|
|
|
const session = await auth();
|
|
|
|
|
const superAdmin = isSuperAdmin(session?.user?.roles);
|
2026-07-08 15:02:03 +08:00
|
|
|
const sp = await searchParams;
|
|
|
|
|
const mode = sp.mode === "week" ? "week" : "today";
|
|
|
|
|
const dateStr = sp.date ?? "";
|
|
|
|
|
const base = baseDate(sp.date);
|
|
|
|
|
|
|
|
|
|
const { solar, lunar } =
|
|
|
|
|
mode === "week"
|
|
|
|
|
? birthdayMatchLists(base, 7, false)
|
|
|
|
|
: birthdayMatchLists(base, 1, true);
|
|
|
|
|
|
|
|
|
|
const rows = await prisma.$queryRaw<Row[]>(Prisma.sql`
|
|
|
|
|
SELECT
|
|
|
|
|
id,
|
|
|
|
|
customer_name AS customerName,
|
|
|
|
|
contacts,
|
|
|
|
|
phone,
|
|
|
|
|
salesman,
|
|
|
|
|
report_date AS reportDate,
|
|
|
|
|
birthday,
|
|
|
|
|
birthday_type AS birthdayType,
|
2026-07-12 16:58:52 +08:00
|
|
|
birthday_owner AS birthdayOwner,
|
2026-07-08 15:02:03 +08:00
|
|
|
remark
|
|
|
|
|
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)}))
|
|
|
|
|
)
|
|
|
|
|
ORDER BY DATE_FORMAT(birthday, '%m-%d')
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-4">
|
|
|
|
|
<Card className="flex flex-wrap items-center justify-between gap-3 p-3">
|
|
|
|
|
<BirthdayControl mode={mode} date={dateStr} />
|
|
|
|
|
<div className="pr-1 text-sm text-[var(--color-muted-foreground)]">
|
2026-07-12 00:30:17 +08:00
|
|
|
命中 <span className="font-semibold text-[var(--color-foreground)]">{rows.length}</span> 位商户
|
2026-07-08 15:02:03 +08:00
|
|
|
</div>
|
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
|
|
<Card className="overflow-hidden">
|
|
|
|
|
<Table>
|
|
|
|
|
<THead>
|
|
|
|
|
<tr>
|
2026-07-12 00:30:17 +08:00
|
|
|
<TH>商户名称</TH>
|
2026-07-08 15:02:03 +08:00
|
|
|
<TH>联系人</TH>
|
2026-07-12 00:30:17 +08:00
|
|
|
<TH>联系电话</TH>
|
2026-07-08 15:02:03 +08:00
|
|
|
<TH>业务员</TH>
|
|
|
|
|
<TH>生日</TH>
|
|
|
|
|
<TH>历法</TH>
|
2026-07-12 16:58:52 +08:00
|
|
|
<TH>归属</TH>
|
2026-07-08 15:02:03 +08:00
|
|
|
</tr>
|
|
|
|
|
</THead>
|
|
|
|
|
<tbody>
|
|
|
|
|
{rows.length === 0 && (
|
|
|
|
|
<tr>
|
2026-07-12 16:58:52 +08:00
|
|
|
<TD colSpan={7} className="py-14 text-center text-[var(--color-muted-foreground)]">
|
2026-07-12 00:30:17 +08:00
|
|
|
该时段暂无生日商户
|
2026-07-08 15:02:03 +08:00
|
|
|
</TD>
|
|
|
|
|
</tr>
|
|
|
|
|
)}
|
|
|
|
|
{rows.map((c) => (
|
|
|
|
|
<TR key={c.id}>
|
|
|
|
|
<TD className="font-medium">{c.customerName}</TD>
|
|
|
|
|
<TD>{c.contacts}</TD>
|
2026-07-13 16:46:01 +08:00
|
|
|
<TD className="tabular-nums">{superAdmin ? c.phone : maskPhone(c.phone)}</TD>
|
2026-07-08 15:02:03 +08:00
|
|
|
<TD>{c.salesman}</TD>
|
|
|
|
|
<TD className="tabular-nums">{fmtDate(c.birthday)}</TD>
|
|
|
|
|
<TD>
|
|
|
|
|
<Badge tone={c.birthdayType === 2 ? "warning" : "info"}>
|
|
|
|
|
{c.birthdayType === 2 ? "农历" : "阳历"}
|
|
|
|
|
</Badge>
|
|
|
|
|
</TD>
|
2026-07-12 16:58:52 +08:00
|
|
|
<TD>{c.birthdayOwner === 1 ? "结算人" : "联系人"}</TD>
|
2026-07-08 15:02:03 +08:00
|
|
|
</TR>
|
|
|
|
|
))}
|
|
|
|
|
</tbody>
|
|
|
|
|
</Table>
|
|
|
|
|
</Card>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|