diff --git a/src/app/(dashboard)/customers/birthday/birthday-columns.ts b/src/app/(dashboard)/customers/birthday/birthday-columns.ts new file mode 100644 index 0000000..26f8e34 --- /dev/null +++ b/src/app/(dashboard)/customers/birthday/birthday-columns.ts @@ -0,0 +1,48 @@ +export const BIRTHDAY_COLUMN_STORAGE_KEY = "crm-birthday-columns-v1"; + +export type BirthdayColumnKey = + | "customerName" + | "customerNo" + | "contacts" + | "balanceUser" + | "birthday" + | "salesman" + | "reportDate" + | "deposit" + | "buy" + | "detail" + | "remark"; + +export type BirthdayColumnDef = { + key: BirthdayColumnKey; + label: string; + /** 默认展示:打电话常用信息 */ + defaultVisible: boolean; + /** 表头/单元格宽度,避免名称列被表格拉得很宽 */ + className?: string; +}; + +export const BIRTHDAY_COLUMNS: BirthdayColumnDef[] = [ + { key: "customerName", label: "商户名称", defaultVisible: true, className: "w-[12%]" }, + { key: "customerNo", label: "商户编号", defaultVisible: true, className: "w-[10%]" }, + { key: "balanceUser", label: "结算人", defaultVisible: true, className: "w-[12%]" }, + { key: "contacts", label: "联系人", defaultVisible: true, className: "w-[12%]" }, + { key: "birthday", label: "生日", defaultVisible: true, className: "w-[14%]" }, + { key: "salesman", label: "业务员", defaultVisible: true, className: "w-[8%]" }, + { key: "remark", label: "备注", defaultVisible: true, className: "w-[20%]" }, + { key: "reportDate", label: "报单日期", defaultVisible: false, className: "w-[10%]" }, + { key: "deposit", label: "押金", defaultVisible: false, className: "w-[8%]" }, + { key: "buy", label: "买断", defaultVisible: false, className: "w-[8%]" }, + { key: "detail", label: "产品明细", defaultVisible: false, className: "w-[18%]" }, +]; + +export function defaultBirthdayColumns(): BirthdayColumnKey[] { + return BIRTHDAY_COLUMNS.filter((c) => c.defaultVisible).map((c) => c.key); +} + +export function normalizeBirthdayColumns(raw: unknown): BirthdayColumnKey[] { + const allowed = new Set(BIRTHDAY_COLUMNS.map((c) => c.key)); + if (!Array.isArray(raw)) return defaultBirthdayColumns(); + const next = raw.filter((k): k is BirthdayColumnKey => typeof k === "string" && allowed.has(k as BirthdayColumnKey)); + return next.length > 0 ? next : defaultBirthdayColumns(); +} diff --git a/src/app/(dashboard)/customers/birthday/birthday-list.tsx b/src/app/(dashboard)/customers/birthday/birthday-list.tsx new file mode 100644 index 0000000..8ca4ec8 --- /dev/null +++ b/src/app/(dashboard)/customers/birthday/birthday-list.tsx @@ -0,0 +1,341 @@ +"use client"; + +import { useEffect, useRef, useState, type ReactNode } from "react"; +import { Columns3, RotateCcw } from "lucide-react"; +import { fmtDate, maskPhone } from "@/lib/utils"; +import { lunarBirthdaySolarThisYear } from "@/lib/lunar"; +import { Card } from "@/components/ui/card"; +import { Table, THead, TR, TH, TD } from "@/components/ui/table"; +import { Button } from "@/components/ui/button"; +import { + BIRTHDAY_COLUMNS, + BIRTHDAY_COLUMN_STORAGE_KEY, + defaultBirthdayColumns, + normalizeBirthdayColumns, + type BirthdayColumnKey, +} from "./birthday-columns"; + +export type BirthdayRow = { + id: number; + customerName: string | null; + customerNo: string | null; + contacts: string | null; + contactsPhone: string | null; + balanceUser: string | null; + balanceUserPhone: string | null; + detail: string | null; + deposit: string | null; + buy: string | null; + salesman: string | null; + reportDate: Date | string | null; + birthday: Date | string | null; + birthdayType: number | null; + remark: string | null; +}; + +function asDate(v: Date | string | null | undefined): Date | null { + if (!v) return null; + if (v instanceof Date) return v; + const d = new Date(v); + return isNaN(d.getTime()) ? null : d; +} + +function text(v?: string | null) { + return v?.trim() ? v : "—"; +} + +function PersonCell({ + name, + phone, + callable, +}: { + name?: string | null; + phone?: string | null; + callable?: boolean; +}) { + const p = phone?.trim() || ""; + return ( +
+
{text(name)}
+ {p ? ( + callable ? ( + + {p} + + ) : ( +
{p}
+ ) + ) : ( +
—
+ )} +
+ ); +} + +function BirthdayCell({ row }: { row: BirthdayRow }) { + const birthday = asDate(row.birthday); + const lunar = Number(row.birthdayType) === 2; + const iso = fmtDate(birthday); + const solarThisYear = lunar ? lunarBirthdaySolarThisYear(iso) : null; + if (!birthday) return —; + if (!lunar) return {iso}; + return ( +
+
农历 {iso.slice(5) || "—"}
+
+ 按农历过生日{solarThisYear ? `,今年是 ${solarThisYear}` : ""} +
+
+ ); +} + +function renderCell(key: BirthdayColumnKey, row: BirthdayRow): ReactNode { + switch (key) { + case "customerName": + return {text(row.customerName)}; + case "customerNo": + return {text(row.customerNo)}; + case "contacts": + return ; + case "balanceUser": + return ; + case "birthday": + return ; + case "salesman": + return text(row.salesman); + case "reportDate": + return {fmtDate(asDate(row.reportDate)) || "—"}; + case "deposit": + return text(row.deposit); + case "buy": + return text(row.buy); + case "detail": + return {text(row.detail)}; + case "remark": + return {text(row.remark)}; + default: + return "—"; + } +} + +function ColumnPicker({ + visible, + onChange, +}: { + visible: BirthdayColumnKey[]; + onChange: (next: BirthdayColumnKey[]) => void; +}) { + const [open, setOpen] = useState(false); + const ref = useRef(null); + + useEffect(() => { + function onClick(e: Event) { + if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false); + } + document.addEventListener("pointerdown", onClick); + return () => document.removeEventListener("pointerdown", onClick); + }, []); + + function toggle(key: BirthdayColumnKey) { + if (visible.includes(key)) { + if (visible.length <= 1) return; + onChange(visible.filter((k) => k !== key)); + } else { + onChange([...visible, key]); + } + } + + return ( +
+ + {open && ( +
+
勾选要显示的列
+
+ {BIRTHDAY_COLUMNS.map((col) => { + const checked = visible.includes(col.key); + return ( + + ); + })} +
+
+ +
+
+ )} +
+ ); +} + +export function BirthdayList({ + rows, + isAdmin, +}: { + rows: BirthdayRow[]; + /** 与商户列表一致:仅管理员看明文手机号 */ + isAdmin: boolean; +}) { + const [visible, setVisible] = useState(defaultBirthdayColumns); + const [ready, setReady] = useState(false); + + useEffect(() => { + try { + const raw = localStorage.getItem(BIRTHDAY_COLUMN_STORAGE_KEY); + if (raw) setVisible(normalizeBirthdayColumns(JSON.parse(raw))); + } catch { + // ignore bad local data + } + setReady(true); + }, []); + + function save(next: BirthdayColumnKey[]) { + const normalized = normalizeBirthdayColumns(next); + setVisible(normalized); + localStorage.setItem(BIRTHDAY_COLUMN_STORAGE_KEY, JSON.stringify(normalized)); + } + + const ordered = BIRTHDAY_COLUMNS.filter((c) => visible.includes(c.key)); + const colSpan = Math.max(1, ordered.length); + + function phoneFor(v?: string | null) { + const value = v?.trim() || ""; + if (!value) return ""; + return isAdmin ? value : maskPhone(value); + } + + return ( +
+
+ +
+ + {!ready ? ( + 加载中… + ) : ( + <> + + + + + {ordered.map((col) => ( + + ))} + + + + {rows.length === 0 && ( + + + + )} + {rows.map((row) => ( + + {ordered.map((col) => ( + + ))} + + ))} + +
+ {col.label} +
+ 没有过生日的商户 +
+ {col.key === "contacts" ? ( + + ) : col.key === "balanceUser" ? ( + + ) : ( + renderCell(col.key, row) + )} +
+
+ +
+ {rows.length === 0 && ( + + 没有过生日的商户 + + )} + {rows.map((row) => ( + +
+
+
{text(row.customerName)}
+ {visible.includes("customerNo") && ( +
+ 编号 {text(row.customerNo)} +
+ )} +
+ {visible.includes("birthday") && ( +
{renderCell("birthday", row)}
+ )} +
+
+ {ordered + .filter((c) => c.key !== "customerName" && c.key !== "customerNo" && c.key !== "birthday") + .map((col) => ( +
+
{col.label}
+
+ {col.key === "contacts" ? ( + + ) : col.key === "balanceUser" ? ( + + ) : ( + renderCell(col.key, row) + )} +
+
+ ))} +
+
+ ))} +
+ + )} +
+ ); +} diff --git a/src/app/(dashboard)/customers/birthday/page.tsx b/src/app/(dashboard)/customers/birthday/page.tsx index 2c757bb..4a103ee 100644 --- a/src/app/(dashboard)/customers/birthday/page.tsx +++ b/src/app/(dashboard)/customers/birthday/page.tsx @@ -1,31 +1,11 @@ -import type { ReactNode } from "react"; import { Prisma } from "@prisma/client"; import { prisma } from "@/lib/prisma"; import { auth } from "@/lib/auth"; -import { isStaff, isSuperAdmin } from "@/lib/acl"; -import { birthdayMatchLists, lunarBirthdaySolarThisYear } from "@/lib/lunar"; -import { fmtDate, maskPhone } from "@/lib/utils"; +import { isSuperAdmin } from "@/lib/acl"; +import { birthdayMatchLists } from "@/lib/lunar"; import { Card } from "@/components/ui/card"; import { BirthdayControl } from "./birthday-control"; - -type Row = { - id: number; - customerName: string | null; - customerNo: string | null; - contacts: string | null; - contactsPhone: string | null; - balanceUser: string | null; - balanceUserPhone: string | null; - phone: string | null; // 旧公共电话,仅作结算人手机号为空时的回退 - detail: string | null; - deposit: string | null; - buy: string | null; - salesman: string | null; - reportDate: Date | null; - birthday: Date | null; - birthdayType: number | null; - remark: string | null; -}; +import { BirthdayList, type BirthdayRow } from "./birthday-list"; function baseDate(s?: string): Date { if (s) { @@ -35,19 +15,13 @@ function baseDate(s?: string): Date { return new Date(); } -function showPhone(raw: string | null | undefined, full: boolean) { - const value = raw?.trim() || ""; - if (!value) return "—"; - return full ? value : maskPhone(value); -} - export default async function BirthdayPage({ searchParams, }: { searchParams: Promise<{ mode?: string; date?: string }>; }) { const session = await auth(); - const fullPhone = isSuperAdmin(session?.user?.roles) || isStaff(session?.user?.roles); + const isAdmin = isSuperAdmin(session?.user?.roles); const sp = await searchParams; const mode = sp.mode === "week" ? "week" : "today"; const dateStr = sp.date ?? ""; @@ -58,7 +32,7 @@ export default async function BirthdayPage({ ? birthdayMatchLists(base, 7, false) : birthdayMatchLists(base, 1, true); - const rows = await prisma.$queryRaw(Prisma.sql` + const rows = await prisma.$queryRaw(Prisma.sql` SELECT id, customer_name AS customerName, @@ -66,8 +40,7 @@ export default async function BirthdayPage({ contacts, contacts_phone AS contactsPhone, balance_user AS balanceUser, - balance_user_phone AS balanceUserPhone, - phone, + COALESCE(NULLIF(balance_user_phone, ''), phone) AS balanceUserPhone, detail, deposit, buy, @@ -94,103 +67,7 @@ export default async function BirthdayPage({ - {rows.length === 0 && ( - - 没有过生日的商户 - - )} - -
- {rows.map((c) => { - const balancePhone = c.balanceUserPhone || c.phone; - const lunar = Number(c.birthdayType) === 2; - const solarThisYear = lunar ? lunarBirthdaySolarThisYear(fmtDate(c.birthday)) : null; - return ( - -
-
-
{c.customerName || "—"}
-
- 编号 {c.customerNo || "—"} -
-
-
-
- {lunar ? "今年是" : "生日"} -
-
- {lunar ? solarThisYear || fmtDate(c.birthday) : fmtDate(c.birthday)} -
-
-
- -
- - - - - - - - - - - - - - {lunar ? ( - - 农历 {fmtDate(c.birthday).slice(5) || "—"} - - 按农历过生日{solarThisYear ? `,今年是 ${solarThisYear}` : ""} - - - ) : ( - fmtDate(c.birthday) || "—" - )} - - - -
- -
- - -
-
- ); - })} -
+ ); } - -function Field({ - label, - value, - pre, - children, -}: { - label: string; - value?: string | null; - pre?: boolean; - children?: ReactNode; -}) { - return ( -
-
{label}
-
- {children ?? (value?.trim() ? value : "—")} -
-
- ); -} - -function Phone({ value, callable }: { value: string; callable: boolean }) { - if (!callable || value === "—") return {value}; - return ( - - {value} - - ); -}