/** 公司目录响应解析:Java CompanyVo(snake_case) → 前端 CompanyItem(camelCase)。 */ import { unwrap } from './envelope.ts' import { COMPANY_STATUS_ACTIVE, COMPANY_STATUS_DISABLED, text, trimmedOrNull, type CompanyItem, } from './companies-dto.ts' function numberOrNull(value: unknown): number | null { return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null } /** 单条映射;无 id 视为无效行(列表中直接丢弃,避免编辑时打到 /companies/undefined)。 */ export function toCompanyItem(raw: unknown): CompanyItem | null { if (!raw || typeof raw !== 'object') return null const r = raw as Record const id = numberOrNull(r.id) if (id === null) return null return { id, companyName: text(r.company_name), // status 列 NOT NULL,取不到就是链路异常;未知一律按停用,不把异常状态当启用。 status: r.status === COMPANY_STATUS_ACTIVE ? COMPANY_STATUS_ACTIVE : COMPANY_STATUS_DISABLED, remark: trimmedOrNull(r.remark), createdAt: trimmedOrNull(r.created_at), updatedAt: trimmedOrNull(r.updated_at), } } /** 解析 GET /api/admin/companies:接受 data 为数组或 {items:[...]},success=false 抛后端文案。 */ export function parseCompanyList(payload: unknown): CompanyItem[] { const core = unwrap(payload) if (!core || typeof core !== 'object') return [] const candidate = Array.isArray(core) ? core : (core as { items?: unknown }).items if (!Array.isArray(candidate)) return [] return candidate .map((raw) => toCompanyItem(raw)) .filter((item): item is CompanyItem => item !== null) } /** 解析 POST/PUT 回包(data 为 CompanyVo)。 */ export function parseCompany(payload: unknown): CompanyItem | null { const core = unwrap(payload) if (!core || typeof core !== 'object' || Array.isArray(core)) return null return toCompanyItem(core) }