Files
liandong-crm/src/app/(dashboard)/customers/birthday/page.tsx
T

118 lines
3.5 KiB
TypeScript
Raw Normal View History

import { Prisma } from "@prisma/client";
import { prisma } from "@/lib/prisma";
import { birthdayMatchLists } from "@/lib/lunar";
import { fmtDate } from "@/lib/utils";
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;
birthdayOwner: number | null;
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 }>;
}) {
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,
birthday_owner AS birthdayOwner,
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)]">
命中 <span className="font-semibold text-[var(--color-foreground)]">{rows.length}</span> 位商户
</div>
</Card>
<Card className="overflow-hidden">
<Table>
<THead>
<tr>
<TH>商户名称</TH>
<TH>联系人</TH>
<TH>联系电话</TH>
<TH>业务员</TH>
<TH>生日</TH>
<TH>历法</TH>
<TH>归属</TH>
</tr>
</THead>
<tbody>
{rows.length === 0 && (
<tr>
<TD colSpan={7} className="py-14 text-center text-[var(--color-muted-foreground)]">
该时段暂无生日商户
</TD>
</tr>
)}
{rows.map((c) => (
<TR key={c.id}>
<TD className="font-medium">{c.customerName}</TD>
<TD>{c.contacts}</TD>
<TD className="tabular-nums">{c.phone}</TD>
<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>
<TD>{c.birthdayOwner === 1 ? "结算人" : "联系人"}</TD>
</TR>
))}
</tbody>
</Table>
</Card>
</div>
);
}