customer-subaccounts.ts 2.0 KB

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