/** * Pediatric Age-Adaptive Milestone Engine * Calculates age-specific clinical baselines and wake window sleep predictors. */ const PEDIATRIC_BASELINES = [ { minDays: 0, maxDays: 28, stageName: '新生儿期 (0-1月)', feedingIntervalHours: 2.5, wakeWindowMinutes: 60, dailyMilkMlMin: 550, dailyMilkMlMax: 750, targetSleepHoursMin: 16, targetSleepHoursMax: 18, diaperCountMin: 8, diaperCountMax: 10, tips: '胃容量小,以按需喂养为主,清醒时间不宜超过1小时防过度疲劳。' }, { minDays: 29, maxDays: 90, stageName: '婴儿早期 (2-3月)', feedingIntervalHours: 3.2, wakeWindowMinutes: 80, dailyMilkMlMin: 700, dailyMilkMlMax: 850, targetSleepHoursMin: 14, targetSleepHoursMax: 16, diaperCountMin: 6, diaperCountMax: 8, tips: '夜间开始出现4~5小时较长睡眠,傍晚容易肠绞痛与黄昏闹,多做飞机抱。' }, { minDays: 91, maxDays: 180, stageName: '规律过渡期 (4-6月)', feedingIntervalHours: 3.8, wakeWindowMinutes: 120, dailyMilkMlMin: 800, dailyMilkMlMax: 950, targetSleepHoursMin: 13, targetSleepHoursMax: 15, diaperCountMin: 5, diaperCountMax: 6, tips: '面临4月龄睡眠倒退期(睡眠周期成人化),白天小睡逐渐规律为晨/午/傍晚3觉。' }, { minDays: 181, maxDays: 270, stageName: '辅食添加期 (7-9月)', feedingIntervalHours: 4.0, wakeWindowMinutes: 180, dailyMilkMlMin: 600, dailyMilkMlMax: 800, targetSleepHoursMin: 12, targetSleepHoursMax: 14, diaperCountMin: 4, diaperCountMax: 6, tips: '辅食从高铁米粉过渡到肉泥菜泥,白天小睡逐渐合并为上午觉与下午觉。' }, { minDays: 271, maxDays: 365, stageName: '学步转化期 (10-12月)', feedingIntervalHours: 4.2, wakeWindowMinutes: 210, dailyMilkMlMin: 500, dailyMilkMlMax: 600, targetSleepHoursMin: 11, targetSleepHoursMax: 13, diaperCountMin: 4, diaperCountMax: 5, tips: '三餐辅食逐渐成为热量主要来源,夜间可达10~11小时整觉。' } ]; function getBabyAgeContext(birthDateStr) { const birth = new Date(birthDateStr).getTime(); const now = Date.now(); const diffDays = Math.max(0, Math.floor((now - birth) / (1000 * 60 * 60 * 24))); const months = Math.floor(diffDays / 30.4375); const remainingDays = Math.floor(diffDays % 30.4375); let baseline = PEDIATRIC_BASELINES[PEDIATRIC_BASELINES.length - 1]; for (const b of PEDIATRIC_BASELINES) { if (diffDays <= b.maxDays) { baseline = b; break; } } let ageText = ''; if (months === 0) { ageText = `出生 ${diffDays} 天`; } else { ageText = `${months} 个月 ${remainingDays} 天 (出生 ${diffDays} 天)`; } return { diffDays, months, remainingDays, ageText, baseline }; } /** * Calculates wake window and sleep prediction * @param {string|null} lastSleepEndTime - ISO string or YYYY-MM-DD HH:mm:ss of when last sleep ended * @param {boolean} isCurrentlySleeping * @param {object} baseline */ function calculateWakeWindow(lastSleepEndTime, isCurrentlySleeping, baseline) { if (isCurrentlySleeping) { return { isSleeping: true, awakeMinutes: 0, maxWakeMinutes: baseline.wakeWindowMinutes, progressPercent: 0, phase: 'sleeping', phaseText: '睡眠中', hintText: '宝宝正在香甜睡眠中,生长激素正在高效分泌。' }; } if (!lastSleepEndTime) { return { isSleeping: false, awakeMinutes: 45, maxWakeMinutes: baseline.wakeWindowMinutes, progressPercent: 50, phase: 'energetic', phaseText: '精力充沛', hintText: '适宜开展亲子互动、Tummy Time 俯趴抬头练习。' }; } const lastEnd = new Date(lastSleepEndTime).getTime(); const now = Date.now(); const awakeMinutes = Math.max(0, Math.floor((now - lastEnd) / (1000 * 60))); const maxWakeMinutes = baseline.wakeWindowMinutes; const progressPercent = Math.min(100, Math.round((awakeMinutes / maxWakeMinutes) * 100)); let phase = 'energetic'; let phaseText = '精力充沛'; let hintText = '状态良好,适度进行黑白卡、抚触与发音互动。'; if (progressPercent >= 70 && progressPercent <= 100) { phase = 'golden_window'; phaseText = '黄金哄睡期'; const remainMin = Math.max(0, maxWakeMinutes - awakeMinutes); hintText = `预计还有约 ${remainMin} 分钟达到疲劳极限。请留心揉眼、抓耳朵、打哈欠等睡眠信号,适时安排安抚入睡!`; } else if (progressPercent > 100) { phase = 'overtired'; phaseText = '放电过度预警'; const overMin = awakeMinutes - maxWakeMinutes; hintText = `已超时清醒 ${overMin} 分钟!警惕过度疲劳引发歇斯底里闹觉,建议立即调暗光线、开启白噪音轻拍安抚!`; } return { isSleeping: false, awakeMinutes, maxWakeMinutes, progressPercent, phase, phaseText, hintText }; } module.exports = { getBabyAgeContext, calculateWakeWindow, PEDIATRIC_BASELINES };