import type { CustomerSubAccount } from "./customer-records"; import type { OrderDemo } from "./order-demo"; import type { SubAccountRow } from "./api/types"; /** * 系统按套餐额度自动补建的席位(需求§4.2):`display_name` 留空,列表按登录名尾号显示「席位 n」。 * * ⛔ 把占位当真实姓名回写编辑表单(`= 席位 3` 存进库里就成了客户姓名)。 */ export function seatDisplayName(row: SubAccountRow): string | null { const name = row.display_name?.trim(); if (name) return name; const seq = row.username?.match(/-(\d+)$/)?.[1]; return seq ? `席位 ${seq}` : null; } /** 该行的姓名是否为占位(未被客户填写过)。 */ export function isSeatPlaceholder(row: SubAccountRow): boolean { return !row.display_name?.trim() && !!row.username?.match(/-(\d+)$/); } /** 待生效和生效中订阅均占额度;到期、作废订阅不再占额度。 */ export function customerOrderQuota(customerNo: string, orders: OrderDemo[], today: string) { return orders.filter((row) => row.enterpriseAccount === customerNo && row.status !== "已到期" && !row.voidOn && row.contractEndDate >= today) .reduce((total, row) => total + (Number.isFinite(row.subAccountQuota) ? Math.max(0, Math.floor(row.subAccountQuota)) : 0), 0); } /** 全部账号共用序号池;额度下降保留原账号,无主账号时暂不补建。 */ export function alignCustomerSubAccounts(accounts: CustomerSubAccount[], masterLogin: string | undefined, quota: number): CustomerSubAccount[] { if (!masterLogin || !Number.isFinite(quota) || accounts.length >= quota) return accounts; const next = [...accounts]; let sequence = Math.max(0, ...accounts.map((account) => Number(account.login.match(/-(\d+)$/)?.[1] ?? 0))); while (next.length < Math.floor(quota)) { sequence += 1; next.push({ login: `${masterLogin}-${sequence}`, name: `席位 ${sequence}`, status: "未开通", openedOn: null, netLogin: "" }); } return next; }