2026-07-08 15:02:03 +08:00
|
|
|
import Link from "next/link";
|
|
|
|
|
import { prisma } from "@/lib/prisma";
|
2026-07-12 00:06:00 +08:00
|
|
|
import { auth } from "@/lib/auth";
|
|
|
|
|
import { isSuperAdmin } from "@/lib/acl";
|
|
|
|
|
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 { Button } from "@/components/ui/button";
|
|
|
|
|
import { CustomerToolbar } from "./customer-toolbar";
|
|
|
|
|
import { DeleteButton } from "./delete-button";
|
2026-07-12 00:06:00 +08:00
|
|
|
import { CustomerView } from "./customer-view";
|
2026-07-08 15:02:03 +08:00
|
|
|
import { buildCustomerWhere } from "./query";
|
|
|
|
|
|
|
|
|
|
const PAGE_SIZE = 15;
|
|
|
|
|
|
|
|
|
|
type SearchParams = {
|
|
|
|
|
keyword?: string;
|
|
|
|
|
start?: string;
|
|
|
|
|
end?: string;
|
|
|
|
|
page?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default async function CustomersPage({
|
|
|
|
|
searchParams,
|
|
|
|
|
}: {
|
|
|
|
|
searchParams: Promise<SearchParams>;
|
|
|
|
|
}) {
|
2026-07-12 00:06:00 +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 keyword = (sp.keyword ?? "").trim();
|
|
|
|
|
const page = Math.max(1, Number(sp.page) || 1);
|
|
|
|
|
const where = buildCustomerWhere(sp);
|
|
|
|
|
|
|
|
|
|
const [rows, total] = await Promise.all([
|
|
|
|
|
prisma.customer.findMany({
|
|
|
|
|
where,
|
|
|
|
|
orderBy: { reportDate: "desc" },
|
|
|
|
|
skip: (page - 1) * PAGE_SIZE,
|
|
|
|
|
take: PAGE_SIZE,
|
|
|
|
|
}),
|
|
|
|
|
prisma.customer.count({ where }),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const totalPages = Math.max(1, Math.ceil(total / PAGE_SIZE));
|
|
|
|
|
const exportQs = new URLSearchParams();
|
|
|
|
|
if (sp.keyword) exportQs.set("keyword", sp.keyword);
|
|
|
|
|
if (sp.start) exportQs.set("start", sp.start);
|
|
|
|
|
if (sp.end) exportQs.set("end", sp.end);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-4">
|
|
|
|
|
<CustomerToolbar
|
|
|
|
|
defaultKeyword={keyword}
|
|
|
|
|
defaultStart={sp.start}
|
|
|
|
|
defaultEnd={sp.end}
|
|
|
|
|
exportHref={`/customers/export?${exportQs.toString()}`}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Card className="overflow-hidden">
|
2026-07-11 23:19:59 +08:00
|
|
|
<Table striped bordered>
|
2026-07-08 15:02:03 +08:00
|
|
|
<THead>
|
|
|
|
|
<tr>
|
2026-07-11 23:19:59 +08:00
|
|
|
<TH>商户名称</TH>
|
2026-07-08 15:02:03 +08:00
|
|
|
<TH>联系人</TH>
|
2026-07-11 23:19:59 +08:00
|
|
|
<TH>结算人</TH>
|
|
|
|
|
<TH>联系电话</TH>
|
|
|
|
|
<TH>产品明细</TH>
|
|
|
|
|
<TH>押金</TH>
|
|
|
|
|
<TH>买断</TH>
|
2026-07-08 15:02:03 +08:00
|
|
|
<TH>生日</TH>
|
2026-07-11 23:19:59 +08:00
|
|
|
<TH>业务员</TH>
|
|
|
|
|
<TH>报单日期</TH>
|
|
|
|
|
<TH>备注</TH>
|
2026-07-08 15:02:03 +08:00
|
|
|
<TH className="text-right">操作</TH>
|
|
|
|
|
</tr>
|
|
|
|
|
</THead>
|
|
|
|
|
<tbody>
|
|
|
|
|
{rows.length === 0 && (
|
|
|
|
|
<tr>
|
2026-07-11 23:19:59 +08:00
|
|
|
<TD colSpan={12} className="py-14 text-center text-[var(--color-muted-foreground)]">
|
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-11 23:19:59 +08:00
|
|
|
<TD>{c.balanceUser}</TD>
|
2026-07-12 00:06:00 +08:00
|
|
|
<TD className="tabular-nums">{superAdmin ? c.phone : maskPhone(c.phone)}</TD>
|
2026-07-11 23:19:59 +08:00
|
|
|
<TD className="max-w-[280px] whitespace-normal break-words">{c.detail}</TD>
|
|
|
|
|
<TD>{c.deposit}</TD>
|
|
|
|
|
<TD>{c.buy}</TD>
|
2026-07-08 15:02:03 +08:00
|
|
|
<TD>
|
|
|
|
|
<span className="tabular-nums">{fmtDate(c.birthday)}</span>
|
|
|
|
|
{c.birthday && (
|
|
|
|
|
<Badge tone={c.birthdayType === 2 ? "warning" : "info"} className="ml-2">
|
|
|
|
|
{c.birthdayType === 2 ? "农历" : "阳历"}
|
|
|
|
|
</Badge>
|
|
|
|
|
)}
|
2026-07-12 16:58:52 +08:00
|
|
|
{c.birthday && (
|
|
|
|
|
<Badge tone="neutral" className="ml-2">
|
|
|
|
|
{c.birthdayOwner === 1 ? "结算人" : "联系人"}
|
|
|
|
|
</Badge>
|
|
|
|
|
)}
|
2026-07-08 15:02:03 +08:00
|
|
|
</TD>
|
2026-07-11 23:19:59 +08:00
|
|
|
<TD>{c.salesman}</TD>
|
|
|
|
|
<TD className="tabular-nums">{fmtDate(c.reportDate)}</TD>
|
|
|
|
|
<TD className="max-w-[280px] whitespace-normal break-words">{c.remark}</TD>
|
2026-07-08 15:02:03 +08:00
|
|
|
<TD>
|
|
|
|
|
<div className="flex justify-end gap-1.5">
|
2026-07-12 00:06:00 +08:00
|
|
|
<CustomerView customer={c} />
|
2026-07-08 15:02:03 +08:00
|
|
|
<Link href={`/customers/${c.id}/edit`}>
|
|
|
|
|
<Button variant="outline" size="sm">编辑</Button>
|
|
|
|
|
</Link>
|
2026-07-12 00:06:00 +08:00
|
|
|
{superAdmin && <DeleteButton id={c.id} name={c.customerName ?? ""} />}
|
2026-07-08 15:02:03 +08:00
|
|
|
</div>
|
|
|
|
|
</TD>
|
|
|
|
|
</TR>
|
|
|
|
|
))}
|
|
|
|
|
</tbody>
|
|
|
|
|
</Table>
|
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
|
|
{/* 分页 */}
|
|
|
|
|
<div className="flex items-center justify-between text-sm text-[var(--color-muted-foreground)]">
|
|
|
|
|
<span>共 <span className="font-medium text-[var(--color-foreground)]">{total.toLocaleString()}</span> 条</span>
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<PageLink page={page - 1} disabled={page <= 1} sp={sp} label="上一页" />
|
|
|
|
|
<span className="px-1 tabular-nums">{page} / {totalPages}</span>
|
|
|
|
|
<PageLink page={page + 1} disabled={page >= totalPages} sp={sp} label="下一页" />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function PageLink({
|
|
|
|
|
page,
|
|
|
|
|
disabled,
|
|
|
|
|
sp,
|
|
|
|
|
label,
|
|
|
|
|
}: {
|
|
|
|
|
page: number;
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
sp: SearchParams;
|
|
|
|
|
label: string;
|
|
|
|
|
}) {
|
|
|
|
|
if (disabled) {
|
|
|
|
|
return (
|
|
|
|
|
<span className="rounded-md border px-3 py-1.5 opacity-40">{label}</span>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
if (sp.keyword) params.set("keyword", sp.keyword);
|
|
|
|
|
if (sp.start) params.set("start", sp.start);
|
|
|
|
|
if (sp.end) params.set("end", sp.end);
|
|
|
|
|
params.set("page", String(page));
|
|
|
|
|
return (
|
|
|
|
|
<Link
|
|
|
|
|
href={`/customers?${params.toString()}`}
|
|
|
|
|
className="rounded-md border bg-[var(--color-surface)] px-3 py-1.5 hover:bg-[var(--color-muted)]"
|
|
|
|
|
>
|
|
|
|
|
{label}
|
|
|
|
|
</Link>
|
|
|
|
|
);
|
|
|
|
|
}
|