/** * 血糖 / 血压 阈值与异常判定工具 * * 阈值规则(与 reception/index.vue 抽离前一致): * - 年龄 < 50:空腹血糖 ≥ 7、餐后/其他血糖 ≥ 9 视为偏高 * - 年龄 ≥ 50:空腹血糖 ≥ 8、餐后/其他血糖 ≥ 10 视为偏高 * - 收缩压 > 140 / 舒张压 > 90 视为偏高 * * 任何字段缺失或非数值 → 视为「不偏高」(避免误报红色 ↑)。 */ /** * 把任意值转成有限数字;空 / 非法 → null */ export function toNumeric(value: unknown): number | null { if (value === null || value === undefined || value === '') return null const num = Number(value as any) return Number.isFinite(num) ? num : null } /** * 根据年龄返回血糖阈值;年龄缺失/非法 → null(=不判定偏高) */ export function getBloodSugarThresholds( ageValue: unknown ): { fasting: number; postprandial: number } | null { const age = toNumeric(ageValue) if (age === null) return null if (age < 50) return { fasting: 7, postprandial: 9 } return { fasting: 8, postprandial: 10 } } export function isHighFastingBloodSugar(value: unknown, ageValue: unknown): boolean { const num = toNumeric(value) const thresholds = getBloodSugarThresholds(ageValue) return num !== null && thresholds !== null && num >= thresholds.fasting } export function isHighPostprandialBloodSugar(value: unknown, ageValue: unknown): boolean { const num = toNumeric(value) const thresholds = getBloodSugarThresholds(ageValue) return num !== null && thresholds !== null && num >= thresholds.postprandial } /** * 「其他血糖」按餐后阈值判定(与 reception 原实现一致) */ export function isHighOtherBloodSugar(value: unknown, ageValue: unknown): boolean { const num = toNumeric(value) const thresholds = getBloodSugarThresholds(ageValue) return num !== null && thresholds !== null && num >= thresholds.postprandial } export function isHighSystolicPressure(value: unknown): boolean { const num = toNumeric(value) return num !== null && num > 140 } export function isHighDiastolicPressure(value: unknown): boolean { const num = toNumeric(value) return num !== null && num > 90 } /** * 一行血糖血压记录是否有任一压偏高 */ export function isHighBloodPressure(row: { systolic_pressure?: unknown diastolic_pressure?: unknown } | null | undefined): boolean { if (!row) return false return isHighSystolicPressure(row.systolic_pressure) || isHighDiastolicPressure(row.diastolic_pressure) } /** * 把一行记录的血压两值格式化为 `xxx / yyy`,缺值 → `—` */ export function formatBp(row: { systolic_pressure?: unknown diastolic_pressure?: unknown } | null | undefined): string { const sys = row?.systolic_pressure const dia = row?.diastolic_pressure if (sys == null && dia == null) return '—' return `${sys ?? '—'} / ${dia ?? '—'}` }