diff --git a/src/app/(dashboard)/customers/[id]/edit/page.tsx b/src/app/(dashboard)/customers/[id]/edit/page.tsx index 73da1e8..e4543c7 100644 --- a/src/app/(dashboard)/customers/[id]/edit/page.tsx +++ b/src/app/(dashboard)/customers/[id]/edit/page.tsx @@ -25,11 +25,11 @@ export default async function EditCustomerPage({ href="/customers" className="inline-flex items-center gap-1.5 text-sm text-[var(--color-muted-foreground)] hover:text-[var(--color-foreground)]" > - 返回客户列表 + 返回商户列表 - 编辑客户 · {customer.customerName} + 编辑商户 · {customer.customerName} diff --git a/src/app/(dashboard)/customers/actions.ts b/src/app/(dashboard)/customers/actions.ts index 31281a3..d94bf3f 100644 --- a/src/app/(dashboard)/customers/actions.ts +++ b/src/app/(dashboard)/customers/actions.ts @@ -8,20 +8,20 @@ import { Prisma } from "@prisma/client"; import { prisma } from "@/lib/prisma"; import { auth } from "@/lib/auth"; import { isSuperAdmin } from "@/lib/acl"; -import { parseDate } from "@/lib/utils"; +import { fmtDate, parseDate } from "@/lib/utils"; // 校验规则对齐老系统必填项(customer_name/contacts/balance_user/detail/salesman/report_date) const schema = z.object({ - customerName: z.string().trim().min(1, "客户名称必填").max(500), + customerName: z.string().trim().min(1, "商户名称必填").max(500), contacts: z.string().trim().min(1, "联系人必填").max(500), balanceUser: z.string().trim().min(1, "结算人必填").max(500), - detail: z.string().trim().min(1, "详情必填"), + detail: z.string().trim().min(1, "产品明细必填"), salesman: z.string().trim().min(1, "业务员必填"), reportDate: z .string() .trim() - .min(1, "报单时间必填") - .refine((s) => parseDate(s) !== null, "报单时间格式应为 YYYY-MM-DD"), + .min(1, "报单日期必填") + .refine((s) => parseDate(s) !== null, "报单日期格式应为 YYYY-MM-DD"), phone: z.string().trim().optional().default(""), deposit: z.string().trim().optional().default(""), buy: z.string().trim().optional().default(""), @@ -30,14 +30,14 @@ const schema = z.object({ remark: z.string().trim().optional().default(""), }); -// 新增客户更严格:名称无空格、手机号必填 11 位、押金/买断/生日必填(仅用于创建,编辑仍用 schema) +// 新增商户更严格:名称无空格、手机号必填 11 位、押金/买断/生日必填(仅用于创建,编辑仍用 schema) const createSchema = schema.extend({ customerName: z .string() .trim() - .min(1, "客户名称必填") + .min(1, "商户名称必填") .max(500) - .refine((s) => !/\s/.test(s), "客户名称不能包含空格"), + .refine((s) => !/\s/.test(s), "商户名称不能包含空格"), phone: z.string().trim().regex(/^1\d{10}$/, "手机号必须为 11 位数字"), deposit: z.string().trim().min(1, "押金必填"), buy: z.string().trim().min(1, "买断必填"), @@ -119,21 +119,23 @@ export async function deleteCustomer(id: number): Promise { // ============ Excel 导入 ============ const HEADER_MAP: Record = { - 客户名称: "customerName", + 商户名称: "customerName", 联系人: "contacts", 结算人: "balanceUser", - 电话: "phone", - 详情: "detail", + 联系电话: "phone", + 产品明细: "detail", 押金: "deposit", 买断: "buy", 生日类型: "birthdayType", 生日: "birthday", 业务员: "salesman", - 报单时间: "reportDate", + 报单日期: "reportDate", 备注: "remark", }; -export type ImportResult = { ok: boolean; inserted?: number; skipped?: number; error?: string }; +export type ImportResult = + | { ok: true; inserted: number } + | { ok: false; error?: string; errorFileBase64?: string; fileName?: string; totalRows?: number }; export async function importCustomers(formData: FormData): Promise { if (!(await auth())) return { ok: false, error: "未登录" }; @@ -146,41 +148,70 @@ export async function importCustomers(formData: FormData): Promise const sheet = wb.Sheets[wb.SheetNames[0]]; const raw = XLSX.utils.sheet_to_json>(sheet, { raw: false, defval: "" }); - const data: Prisma.CustomerCreateManyInput[] = []; - let skipped = 0; + const headers = Object.keys(raw[0] ?? {}); + const validData: Prisma.CustomerCreateManyInput[] = []; + const errorRows: (Record & { _error: string })[] = []; + for (const row of raw) { const rec: Record = {}; for (const [header, val] of Object.entries(row)) { const key = HEADER_MAP[String(header).trim()]; if (key) rec[key] = String(val ?? "").trim(); } - // 报单时间为必填且需可解析,否则跳过 + + const errors: string[] = []; + if (!rec.customerName) errors.push("商户名称必填"); const reportDate = parseDate(rec.reportDate); - if (!rec.customerName || !reportDate) { - skipped++; - continue; + if (!rec.reportDate || !reportDate) errors.push("报单日期必填且格式为 YYYY-MM-DD"); + if (rec.birthday && !parseDate(rec.birthday)) errors.push("生日格式应为 YYYY-MM-DD"); + + if (errors.length > 0) { + errorRows.push({ ...row, _error: errors.join(";") }); + } else { + validData.push({ + customerName: rec.customerName, + contacts: rec.contacts || null, + balanceUser: rec.balanceUser || null, + phone: rec.phone || null, + detail: rec.detail || null, + deposit: rec.deposit || null, + buy: rec.buy || null, + salesman: rec.salesman || null, + remark: rec.remark || null, + birthdayType: rec.birthdayType === "阴历" || rec.birthdayType === "2" ? 2 : 1, + birthday: parseDate(rec.birthday), + reportDate, + }); } - data.push({ - customerName: rec.customerName, - contacts: rec.contacts || null, - balanceUser: rec.balanceUser || null, - phone: rec.phone || null, - detail: rec.detail || null, - deposit: rec.deposit || null, - buy: rec.buy || null, - salesman: rec.salesman || null, - remark: rec.remark || null, - birthdayType: rec.birthdayType === "阴历" || rec.birthdayType === "2" ? 2 : 1, - birthday: parseDate(rec.birthday), - reportDate, - }); } - if (data.length === 0) return { ok: false, error: `无有效数据(跳过 ${skipped} 行)` }; + // 有任何无效行:返回带错误列的 Excel + if (errorRows.length > 0) { + const errorHeaders = [...headers, "导入失败原因"]; + const aoa: (string | number)[][] = [errorHeaders]; + for (const row of errorRows) { + const values = headers.map((h) => String(row[h] ?? "")); + values.push(row._error); + aoa.push(values); + } + const ws = XLSX.utils.aoa_to_sheet(aoa); + ws["!cols"] = errorHeaders.map(() => ({ wch: 18 })); + const errorWb = XLSX.utils.book_new(); + XLSX.utils.book_append_sheet(errorWb, ws, "导入失败明细"); + const errorBuf = XLSX.write(errorWb, { type: "buffer", bookType: "xlsx" }) as Buffer; + return { + ok: false, + errorFileBase64: errorBuf.toString("base64"), + fileName: `导入失败明细_${fmtDate(new Date())}.xlsx`, + totalRows: raw.length, + }; + } - await prisma.customer.createMany({ data }); + if (validData.length === 0) return { ok: false, error: "无有效数据" }; + + await prisma.customer.createMany({ data: validData }); revalidatePath("/customers"); - return { ok: true, inserted: data.length, skipped }; + return { ok: true, inserted: validData.length }; } catch (e) { return { ok: false, error: (e as Error).message }; } diff --git a/src/app/(dashboard)/customers/birthday/page.tsx b/src/app/(dashboard)/customers/birthday/page.tsx index 064cf76..4c7c748 100644 --- a/src/app/(dashboard)/customers/birthday/page.tsx +++ b/src/app/(dashboard)/customers/birthday/page.tsx @@ -67,7 +67,7 @@ export default async function BirthdayPage({
- 命中 {rows.length} 位客户 + 命中 {rows.length} 位商户
@@ -75,9 +75,9 @@ export default async function BirthdayPage({ - + - + @@ -87,7 +87,7 @@ export default async function BirthdayPage({ {rows.length === 0 && ( )} diff --git a/src/app/(dashboard)/customers/customer-form.tsx b/src/app/(dashboard)/customers/customer-form.tsx index 373c7fd..c465f9f 100644 --- a/src/app/(dashboard)/customers/customer-form.tsx +++ b/src/app/(dashboard)/customers/customer-form.tsx @@ -80,18 +80,18 @@ export function CustomerForm({
客户名称商户名称 联系人电话联系电话 业务员 生日 历法
- 该时段暂无生日客户 + 该时段暂无生日商户