diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 446bcad..54fac4b 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -16,7 +16,8 @@ datasource db { /// 客户(系统核心,~1.4 万行) model Customer { id Int @id @default(autoincrement()) - customerName String? @default("") @map("customer_name") + customerName String? @default("") @map("customer_name") // 商户注名称 + businessName String? @map("business_name") // 商户经营名称 customerNo String? @map("customer_no") @db.Text // 商户编号,数字与字母,区分大小写,不限位数 contacts String? // 联系人 contactsPhone String? @default("") @map("contacts_phone") // 联系人手机号 diff --git a/sql/add-business-name.sql b/sql/add-business-name.sql new file mode 100644 index 0000000..c9b3a1d --- /dev/null +++ b/sql/add-business-name.sql @@ -0,0 +1,6 @@ +-- 商户表增加商户经营名称 +-- 执行:本地与生产库均需执行;执行后运行 npm run prisma:generate +-- 历史行保持 NULL,新录入/导入必填 + +ALTER TABLE customers + ADD COLUMN business_name VARCHAR(500) NULL COMMENT '商户经营名称' AFTER customer_name; diff --git a/src/app/(dashboard)/customers/actions.ts b/src/app/(dashboard)/customers/actions.ts index d8fd025..c157bbb 100644 --- a/src/app/(dashboard)/customers/actions.ts +++ b/src/app/(dashboard)/customers/actions.ts @@ -11,14 +11,16 @@ import { isSuperAdmin } from "@/lib/acl"; import { fmtDate, parseDate } from "@/lib/utils"; // 校验规则对齐老系统必填项(customer_name/contacts/balance_user/detail/salesman/report_date) +// 商户编号非必填;有值时仅允许数字和字母 const customerNoSchema = z .string() .trim() - .min(1, "商户编号必填") - .regex(/^[A-Za-z0-9]+$/, "商户编号只能包含数字和字母,区分大小写"); + .default("") + .refine((s) => s === "" || /^[A-Za-z0-9]+$/.test(s), "商户编号只能包含数字和字母,区分大小写"); const schema = z.object({ - customerName: z.string().trim().min(1, "商户名称必填").max(500), + customerName: z.string().trim().min(1, "商户注名称必填").max(500), + businessName: z.string().trim().min(1, "商户经营名称必填").max(500), customerNo: customerNoSchema, contacts: z.string().trim().min(1, "联系人必填").max(500), balanceUser: z.string().trim().min(1, "结算人必填").max(500), @@ -39,14 +41,14 @@ const schema = z.object({ remark: z.string().trim().optional().default(""), }); -// 新增商户更严格:名称无空格、联系人/结算人手机号必填 11 位、押金/买断/生日必填(仅用于创建,编辑仍用 schema) +// 新增商户更严格:注名称无空格、联系人/结算人手机号必填 11 位、押金/买断/生日必填(仅用于 create,编辑仍用 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), "商户注名称不能包含空格"), contactsPhone: z.string().trim().regex(/^1\d{10}$/, "联系人手机号必须为 11 位数字"), balanceUserPhone: z.string().trim().regex(/^1\d{10}$/, "结算人手机号必须为 11 位数字"), deposit: z.string().trim().min(1, "押金必填"), @@ -64,7 +66,8 @@ export type ActionState = { error?: string }; function pickForm(formData: FormData) { return { customerName: formData.get("customerName"), - customerNo: formData.get("customerNo"), + businessName: formData.get("businessName"), + customerNo: formData.get("customerNo") ?? "", contacts: formData.get("contacts"), balanceUser: formData.get("balanceUser"), detail: formData.get("detail"), @@ -83,9 +86,10 @@ function pickForm(formData: FormData) { /** 把校验后的表单值转成 Prisma 写入数据(日期字段转 Date) */ function toData(v: z.infer) { - const { reportDate, birthday, ...rest } = v; + const { reportDate, birthday, customerNo, ...rest } = v; return { ...rest, + customerNo: customerNo || null, reportDate: parseDate(reportDate), birthday: parseDate(birthday), }; @@ -143,7 +147,10 @@ export async function deleteCustomer(id: number): Promise { // ============ Excel 导入 ============ const HEADER_MAP: Record = { + 商户注名称: "customerName", + // 兼容旧版导入模板表头 商户名称: "customerName", + 商户经营名称: "businessName", 商户编号: "customerNo", 联系人: "contacts", 联系人电话: "contactsPhone", @@ -187,9 +194,11 @@ export async function importCustomers(formData: FormData): Promise } const errors: string[] = []; - if (!rec.customerName) errors.push("商户名称必填"); - if (!rec.customerNo) errors.push("商户编号必填"); - else if (!/^[A-Za-z0-9]+$/.test(rec.customerNo)) errors.push("商户编号只能包含数字和字母"); + if (!rec.customerName) errors.push("商户注名称必填"); + if (!rec.businessName) errors.push("商户经营名称必填"); + if (rec.customerNo && !/^[A-Za-z0-9]+$/.test(rec.customerNo)) { + errors.push("商户编号只能包含数字和字母"); + } const reportDate = parseDate(rec.reportDate); if (!rec.reportDate || !reportDate) errors.push("报单日期必填且格式为 YYYY-MM-DD"); if (rec.birthday && !parseDate(rec.birthday)) errors.push("生日格式应为 YYYY-MM-DD"); @@ -199,7 +208,8 @@ export async function importCustomers(formData: FormData): Promise } else { validData.push({ customerName: rec.customerName, - customerNo: rec.customerNo, + businessName: rec.businessName, + customerNo: rec.customerNo || null, contacts: rec.contacts || null, contactsPhone: rec.contactsPhone || null, balanceUser: rec.balanceUser || null, diff --git a/src/app/(dashboard)/customers/birthday/birthday-columns.ts b/src/app/(dashboard)/customers/birthday/birthday-columns.ts index 26f8e34..5d03576 100644 --- a/src/app/(dashboard)/customers/birthday/birthday-columns.ts +++ b/src/app/(dashboard)/customers/birthday/birthday-columns.ts @@ -1,7 +1,8 @@ -export const BIRTHDAY_COLUMN_STORAGE_KEY = "crm-birthday-columns-v1"; +export const BIRTHDAY_COLUMN_STORAGE_KEY = "crm-birthday-columns-v2"; export type BirthdayColumnKey = | "customerName" + | "businessName" | "customerNo" | "contacts" | "balanceUser" @@ -23,13 +24,14 @@ export type BirthdayColumnDef = { }; 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: "customerName", label: "商户注名称", defaultVisible: true, className: "w-[11%]" }, + { key: "businessName", label: "商户经营名称", defaultVisible: true, className: "w-[11%]" }, + { key: "customerNo", label: "商户编号", defaultVisible: true, className: "w-[9%]" }, + { key: "balanceUser", label: "结算人", defaultVisible: true, className: "w-[11%]" }, + { key: "contacts", label: "联系人", defaultVisible: true, className: "w-[11%]" }, + { key: "birthday", label: "生日", defaultVisible: true, className: "w-[13%]" }, + { key: "salesman", label: "业务员", defaultVisible: true, className: "w-[7%]" }, + { key: "remark", label: "备注", defaultVisible: true, className: "w-[16%]" }, { key: "reportDate", label: "报单日期", defaultVisible: false, className: "w-[10%]" }, { key: "deposit", label: "押金", defaultVisible: false, className: "w-[8%]" }, { key: "buy", label: "买断", defaultVisible: false, className: "w-[8%]" }, diff --git a/src/app/(dashboard)/customers/birthday/birthday-list.tsx b/src/app/(dashboard)/customers/birthday/birthday-list.tsx index 1abf59f..c46696c 100644 --- a/src/app/(dashboard)/customers/birthday/birthday-list.tsx +++ b/src/app/(dashboard)/customers/birthday/birthday-list.tsx @@ -18,6 +18,7 @@ import { export type BirthdayRow = { id: number; customerName: string | null; + businessName: string | null; customerNo: string | null; contacts: string | null; contactsPhone: string | null; @@ -96,6 +97,8 @@ function renderCell(key: BirthdayColumnKey, row: BirthdayRow): ReactNode { switch (key) { case "customerName": return {text(row.customerName)}; + case "businessName": + return {text(row.businessName)}; case "customerNo": return {text(row.customerNo)}; case "contacts": @@ -286,6 +289,11 @@ export function BirthdayList({ rows }: { rows: BirthdayRow[] }) {
{text(row.customerName)}
+ {visible.includes("businessName") && row.businessName ? ( +
+ 经营名称 {text(row.businessName)} +
+ ) : null} {visible.includes("customerNo") && (
编号 {text(row.customerNo)} @@ -298,7 +306,13 @@ export function BirthdayList({ rows }: { rows: BirthdayRow[] }) {
{ordered - .filter((c) => c.key !== "customerName" && c.key !== "customerNo" && c.key !== "birthday") + .filter( + (c) => + c.key !== "customerName" && + c.key !== "businessName" && + c.key !== "customerNo" && + c.key !== "birthday" + ) .map((col) => (
{col.label}
diff --git a/src/app/(dashboard)/customers/birthday/page.tsx b/src/app/(dashboard)/customers/birthday/page.tsx index 4eadcab..f74bc53 100644 --- a/src/app/(dashboard)/customers/birthday/page.tsx +++ b/src/app/(dashboard)/customers/birthday/page.tsx @@ -32,6 +32,7 @@ export default async function BirthdayPage({ SELECT id, customer_name AS customerName, + business_name AS businessName, customer_no AS customerNo, contacts, contacts_phone AS contactsPhone, diff --git a/src/app/(dashboard)/customers/customer-form.tsx b/src/app/(dashboard)/customers/customer-form.tsx index 422641f..df02d31 100644 --- a/src/app/(dashboard)/customers/customer-form.tsx +++ b/src/app/(dashboard)/customers/customer-form.tsx @@ -10,6 +10,7 @@ import type { ActionState } from "./actions"; type CustomerValues = { customerName?: string | null; + businessName?: string | null; customerNo?: string | null; contacts?: string | null; contactsPhone?: string | null; @@ -101,20 +102,25 @@ export function CustomerForm({
+ {hasBasic && (
- + +
diff --git a/src/app/(dashboard)/customers/export/route.ts b/src/app/(dashboard)/customers/export/route.ts index b9cba41..470e809 100644 --- a/src/app/(dashboard)/customers/export/route.ts +++ b/src/app/(dashboard)/customers/export/route.ts @@ -32,6 +32,7 @@ export async function GET(req: NextRequest) { for (const c of rows) { aoa.push([ c.customerName ?? "", + c.businessName ?? "", c.customerNo ?? "", c.contacts ?? "", c.contactsPhone ?? "", diff --git a/src/app/(dashboard)/customers/page.tsx b/src/app/(dashboard)/customers/page.tsx index e3fd15d..1c8a897 100644 --- a/src/app/(dashboard)/customers/page.tsx +++ b/src/app/(dashboard)/customers/page.tsx @@ -64,7 +64,8 @@ export default async function CustomersPage({ - + + @@ -81,7 +82,7 @@ export default async function CustomersPage({ {rows.length === 0 && ( - @@ -92,6 +93,7 @@ export default async function CustomersPage({ return ( +
商户名称商户注名称商户经营名称 商户编号 联系人 结算人
+ 暂无数据
{c.customerName}{c.businessName || "—"} {c.customerNo || "—"}
{c.contacts}
@@ -148,6 +150,11 @@ export default async function CustomersPage({
{c.customerName}
+ {c.businessName ? ( +
+ 经营名称 {c.businessName} +
+ ) : null}
编号 {c.customerNo || "—"}
diff --git a/src/app/(dashboard)/customers/query.ts b/src/app/(dashboard)/customers/query.ts index ff2ac4b..3ca98c4 100644 --- a/src/app/(dashboard)/customers/query.ts +++ b/src/app/(dashboard)/customers/query.ts @@ -14,6 +14,7 @@ export function buildCustomerWhere(sp: CustomerSearch): Prisma.CustomerWhereInpu if (keyword) { where.OR = [ { customerName: { contains: keyword } }, + { businessName: { contains: keyword } }, { customerNo: { contains: keyword } }, { contacts: { contains: keyword } }, { contactsPhone: { contains: keyword } }, @@ -32,7 +33,8 @@ export function buildCustomerWhere(sp: CustomerSearch): Prisma.CustomerWhereInpu /** 导出/导入的列定义(表头 ↔ 字段) */ export const EXCEL_COLUMNS: { header: string; key: string }[] = [ - { header: "商户名称", key: "customerName" }, + { header: "商户注名称", key: "customerName" }, + { header: "商户经营名称", key: "businessName" }, { header: "商户编号", key: "customerNo" }, { header: "联系人", key: "contacts" }, { header: "联系人电话", key: "contactsPhone" }, diff --git a/src/app/(dashboard)/customers/template/route.ts b/src/app/(dashboard)/customers/template/route.ts index 11d0feb..4d21063 100644 --- a/src/app/(dashboard)/customers/template/route.ts +++ b/src/app/(dashboard)/customers/template/route.ts @@ -9,7 +9,8 @@ export async function GET() { } const headers = [ - "商户名称", + "商户注名称", + "商户经营名称", "商户编号", "联系人", "联系人电话", @@ -26,7 +27,8 @@ export async function GET() { // 示例行(用户导入前需删除此行) const example = [ - "模板名称_此行删除", + "模板注名称_此行删除", + "模板经营名称", "Ab12Cd", "侯磊", "13813855555", diff --git a/src/app/(dashboard)/page.tsx b/src/app/(dashboard)/page.tsx index f4db66c..f7a0404 100644 --- a/src/app/(dashboard)/page.tsx +++ b/src/app/(dashboard)/page.tsx @@ -129,7 +129,8 @@ export default async function DashboardPage() { - + + @@ -141,6 +142,7 @@ export default async function DashboardPage() { {recent.map((c) => ( + @@ -161,6 +163,11 @@ export default async function DashboardPage() {
{c.customerName}
+ {c.businessName ? ( +
+ 经营名称 {c.businessName} +
+ ) : null} {c.customerNo ? (
编号 {c.customerNo}
商户名称商户注名称商户经营名称 商户编号 联系人 业务员
{c.customerName}{c.businessName || "—"} {c.customerNo || "—"} {c.contacts} {c.salesman}