import type { OrganizationTreeDto } from '@/api/rbac'; /** 组织类型 → 编码前缀(1 公司 / 2 部门 / 3 小组;岗位走员工档案字典,不作为组织类型) */ export const ORGANIZATION_TYPE_CODES: Record = { 1: 'GS', // 公司 2: 'BM', // 部门 3: 'XZ', // 小组 }; export function getOrganizationTypeCode(type?: number | null): string { return ORGANIZATION_TYPE_CODES[type ?? 0] ?? 'ORG'; } export function flattenOrganizationTree( nodes: OrganizationTreeDto[] | null | undefined, ): OrganizationTreeDto[] { const list: OrganizationTreeDto[] = []; const walk = (items: OrganizationTreeDto[]) => { for (const item of items) { list.push(item); if (item.children?.length) { walk(item.children); } } }; if (nodes?.length) { walk(nodes); } return list; } /** * 按「组织类型编码 + 两位序号」生成唯一组织编码,如 BM01、GW02。 */ export function generateOrganizationCode( type?: number | null, organizations: Array<{ code?: string | null }> = [], ): string { const typeCode = getOrganizationTypeCode(type); const pattern = new RegExp(`^${escapeRegExp(typeCode)}(\\d+)$`, 'i'); let maxSeq = 0; for (const org of organizations) { const code = org.code?.trim(); if (!code) continue; const matched = code.match(pattern); if (!matched) continue; const seq = Number.parseInt(matched[1], 10); if (Number.isFinite(seq) && seq > maxSeq) { maxSeq = seq; } } const next = maxSeq + 1; const width = Math.max(2, String(next).length); return `${typeCode}${String(next).padStart(width, '0')}`; } function escapeRegExp(value: string): string { return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }