import { AssessmentProfile, AssessmentRecord, ScoreThreshold } from '@/lib/assessment-types'; function date(value: string) { return new Date(value).toLocaleString('zh-CN', { hour12: false }); } function thresholdsToText(thresholds: ScoreThreshold[]) { if (!thresholds.length) return '无固定阈值'; return thresholds.map((item) => `${item.min} 分起:${item.label}`).join(';'); } function scoreRange(record: AssessmentRecord) { const { min, max } = record.scoreSummary; return max === undefined ? `最低 ${min}` : `${min} - ${max}`; } export function recordToMarkdown(record: AssessmentRecord, profileName: string) { const lines = [ `# ${record.questionnaireTitle}测评记录`, '', '## 基本信息', `- 档案:${profileName || '未命名档案'}`, `- 测评时间:${date(record.completedAt)}`, `- 量表编号:${record.questionnaireId}`, `- 量表版本:${record.questionnaireVersion}`, `- 计分版本:${record.scoreVersion}`, `- 题目数量:${record.answers.length}`, '', '## 计分规则', `- 分数范围:${scoreRange(record)}`, `- 高分含义:${record.scoreSummary.highScoreMeaning}`, `- 反向题:${record.scoreSummary.reverseItems.length ? record.scoreSummary.reverseItems.join('、') : '无'}`, `- 临界值:${thresholdsToText(record.scoreSummary.thresholds)}`, `- 计分状态:${record.scoreSummary.scoringStatus === 'structured' ? '结构化计分' : '原始分保存'}`, '', ]; if (record.scoreSummary.metrics.length) { lines.push('## 分数摘要'); record.scoreSummary.metrics.forEach((item) => { const range = item.max !== undefined ? ` / ${item.max}` : ''; const level = item.level ? `(${item.level})` : ''; lines.push(`- ${item.label}:${item.value}${range}${level}`); }); if (record.scoreSummary.note) lines.push('', record.scoreSummary.note); lines.push(''); } if (record.analysisText) lines.push('## 结果说明', record.analysisText, ''); lines.push('## 完整问答'); record.answers.forEach((item, index) => { lines.push(`${index + 1}. ${item.question}`, ` 回答:${item.answer}`, ''); }); lines.push( '## 使用说明', '本记录仅供自我了解、教育和研究参考,不构成医学或心理诊断。', ); return lines.join('\n'); } export function profileToMarkdown(profile: AssessmentProfile, records: AssessmentRecord[]) { const lines = [ `# ${profile.name}的完整测评档案`, '', `- 导出时间:${date(new Date().toISOString())}`, `- 测评次数:${records.length}`, '', '---', '', ]; records .slice() .sort((a, b) => a.completedAt.localeCompare(b.completedAt)) .forEach((record) => lines.push(recordToMarkdown(record, profile.name), '', '---', '')); return lines.join('\n'); } export function downloadText(filename: string, content: string, type: string) { const blob = new Blob([content], { type }); const url = URL.createObjectURL(blob); const anchor = document.createElement('a'); anchor.href = url; anchor.download = filename; anchor.click(); URL.revokeObjectURL(url); }