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;
|
2026-07-19 23:45:20 +08:00
|
|
|
contactsPhone: string | null;
|
|
|
|
|
balanceUser: string | null;
|
|
|
|
|
balanceUserPhone: string | null;
|
|
|
|
|
phone: string | null; // 旧公共电话,仅作新字段为空时的回退
|
2026-07-08 15:02:03 +08:00
|
|
|
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,
|
2026-07-19 23:45:20 +08:00
|
|
|
contacts_phone AS contactsPhone,
|
|
|
|
|
balance_user AS balanceUser,
|
|
|
|
|
balance_user_phone AS balanceUserPhone,
|
2026-07-08 15:02:03 +08:00
|
|
|
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">
|
2026-08-29 21:55:10 +08:00
|
|
|
<Card className="flex flex-col gap-3 p-3 sm:flex-row sm:flex-wrap sm:items-center sm:justify-between">
|
2026-07-08 15:02:03 +08:00
|
|
|
<BirthdayControl mode={mode} date={dateStr} />
|
2026-08-29 21:55:10 +08:00
|
|
|
<div className="text-sm text-[var(--color-muted-foreground)] sm:pr-1">
|
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>
|
|
|
|
|
|
2026-08-29 21:55:10 +08:00
|
|
|
<Card className="hidden overflow-hidden md:block">
|
2026-07-08 15:02:03 +08:00
|
|
|
<Table>
|
|
|
|
|
<THead>
|
|
|
|
|
<tr>
|
2026-07-12 00:30:17 +08:00
|
|
|
<TH>商户名称</TH>
|
2026-07-19 23:45:20 +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-19 23:45:20 +08:00
|
|
|
<TD colSpan={6} 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>
|
|
|
|
|
)}
|
2026-07-19 23:45:20 +08:00
|
|
|
{rows.map((c) => {
|
|
|
|
|
// 生日归属人的姓名和手机号;新手机号为空时回退旧公共 phone(历史数据)
|
|
|
|
|
const ownerName = c.birthdayOwner === 1 ? c.balanceUser : c.contacts;
|
|
|
|
|
const ownerPhone =
|
|
|
|
|
(c.birthdayOwner === 1 ? c.balanceUserPhone : c.contactsPhone) || c.phone;
|
|
|
|
|
return (
|
2026-07-08 15:02:03 +08:00
|
|
|
<TR key={c.id}>
|
|
|
|
|
<TD className="font-medium">{c.customerName}</TD>
|
2026-07-19 23:45:20 +08:00
|
|
|
<TD>
|
|
|
|
|
<div>{ownerName}</div>
|
|
|
|
|
<div className="text-xs tabular-nums text-[var(--color-muted-foreground)]">
|
|
|
|
|
{superAdmin ? ownerPhone : maskPhone(ownerPhone)}
|
|
|
|
|
</div>
|
|
|
|
|
</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>
|
2026-07-19 23:45:20 +08:00
|
|
|
);
|
|
|
|
|
})}
|
2026-07-08 15:02:03 +08:00
|
|
|
</tbody>
|
|
|
|
|
</Table>
|
|
|
|
|
</Card>
|
2026-08-29 21:55:10 +08:00
|
|
|
|
|
|
|
|
<div className="space-y-3 md:hidden">
|
|
|
|
|
{rows.length === 0 && (
|
|
|
|
|
<Card className="py-14 text-center text-sm text-[var(--color-muted-foreground)]">
|
|
|
|
|
该时段暂无生日商户
|
|
|
|
|
</Card>
|
|
|
|
|
)}
|
|
|
|
|
{rows.map((c) => {
|
|
|
|
|
const ownerName = c.birthdayOwner === 1 ? c.balanceUser : c.contacts;
|
|
|
|
|
const ownerPhone =
|
|
|
|
|
(c.birthdayOwner === 1 ? c.balanceUserPhone : c.contactsPhone) || c.phone;
|
|
|
|
|
return (
|
|
|
|
|
<Card key={c.id} className="p-4">
|
|
|
|
|
<div className="flex items-start justify-between gap-3">
|
|
|
|
|
<div className="min-w-0">
|
|
|
|
|
<div className="truncate font-medium">{c.customerName}</div>
|
|
|
|
|
<div className="mt-1 text-xs text-[var(--color-muted-foreground)]">
|
|
|
|
|
{c.salesman || "—"}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<Badge tone={c.birthdayType === 2 ? "warning" : "info"} className="shrink-0">
|
|
|
|
|
{c.birthdayType === 2 ? "农历" : "阳历"}
|
|
|
|
|
</Badge>
|
|
|
|
|
</div>
|
|
|
|
|
<dl className="mt-3 grid grid-cols-2 gap-x-3 gap-y-2 text-sm">
|
|
|
|
|
<div>
|
|
|
|
|
<dt className="text-xs text-[var(--color-muted-foreground)]">归属人</dt>
|
|
|
|
|
<dd>{ownerName || "—"}</dd>
|
|
|
|
|
<dd className="text-xs tabular-nums text-[var(--color-muted-foreground)]">
|
|
|
|
|
{superAdmin ? ownerPhone : maskPhone(ownerPhone)}
|
|
|
|
|
</dd>
|
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<dt className="text-xs text-[var(--color-muted-foreground)]">生日</dt>
|
|
|
|
|
<dd className="tabular-nums">{fmtDate(c.birthday)}</dd>
|
|
|
|
|
<dd className="text-xs text-[var(--color-muted-foreground)]">
|
|
|
|
|
{c.birthdayOwner === 1 ? "结算人" : "联系人"}
|
|
|
|
|
</dd>
|
|
|
|
|
</div>
|
|
|
|
|
</dl>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
2026-07-08 15:02:03 +08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|