/** * 以 public/documents/*.xlsx 为准,同步医院/专家数据。 * - hospitals-excel-raw.json * - data/hospitals-content.ts * - data/locale/zh/doctors.zh.ts * - data/locale/zh/hospitals-names.zh.ts * - data/locale/zh/hospitals-intros.zh.ts */ import fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' import XLSX from 'xlsx' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const WEB = path.resolve(__dirname, '..') const DOCS_DIR = path.join(WEB, 'public', 'documents') const DATA_TS = path.join(WEB, 'data', 'hospitals-content.ts') const RAW_JSON = path.join(__dirname, 'hospitals-excel-raw.json') const DOCTORS_ZH = path.join(WEB, 'data', 'locale', 'zh', 'doctors.zh.ts') const HOSPITAL_NAMES_ZH = path.join(WEB, 'data', 'locale', 'zh', 'hospitals-names.zh.ts') const HOSPITAL_INTROS_ZH = path.join(WEB, 'data', 'locale', 'zh', 'hospitals-intros.zh.ts') const EXPERT_INFO_EN = path.join(__dirname, 'doctor-expert-info-en.json') const FOLDER_TO_HOSPITAL = { 中山一院图: '中山大学附属第一医院(越秀院区、南沙院区)', 广东省人民医院: '广东省人民医院', 广东省妇幼保健院: '广东省妇幼保健院', 孙逸仙纪念医院: '中山大学孙逸仙纪念医院', 中山三院: '中山大学附属第三医院', 中山大学中山眼科中心: '中山大学中山眼科中心', 中山肿瘤: '中山大学肿瘤防治中心', 中大口腔: '中山大学附属口腔医院', 中山六院: '中山大学附属第六医院', 南方医院: '南方医科大学南方医院', 珠江医院: '南方医科大学珠江医院', 暨大附一院: '暨南大学附属第一医院', 广东省中医院: '广东省中医院', 汕大附一院: '汕头大学医学院第一附属医院', 广医附一院: '广州医科大学附属第一医院', 广州市一院: '广州市第一人民医院', 广妇儿中心: '广州医科大学附属妇女儿童医疗中心', 深圳人医: '深圳市人民医院', 北大深圳: '北京大学深圳医院', 港大深圳: '香港大学深圳医院', 南方深圳: '南方医科大学深圳医院', 中山5院: '中山大学附属第五医院', 珠海人民医院: '珠海市人民医院', 祈福医院: '广东祈福医院', 佛山复星: '佛山复星禅诚医院', 湘雅二院: '中南大学湘雅二医院' } const HOSPITAL_ZH_TO_ID = Object.fromEntries( Object.entries(FOLDER_TO_HOSPITAL).map(([id, name]) => [name, id]) ) const HOSPITAL_EN = { 中南大学湘雅二医院: 'The Second Xiangya Hospital of Central South University' } const INTRO_EN = { 中南大学湘雅二医院: 'Tracing its roots to 1906, the Second Xiangya Hospital of Central South University is a Grade 3A comprehensive hospital in Changsha, Hunan, directly administered by the National Health Commission and affiliated with Central South University.' } const CERT_EN = { 湘雅国际医疗部于2016年通过JCI认证: 'The Xiangya International Medical Department received JCI accreditation in 2016.' } const DEPT_EN = { 内分泌科: 'Endocrinology', 牙体牙髓病专科: 'Endodontics', 口腔颌面外科: 'Oral & Maxillofacial Surgery', 牙槽外科: 'Oral Surgery', 口腔修复科: 'Prosthodontics', 口腔正畸专科: 'Orthodontics' } const NAME_EN_OVERRIDE = { 区俊文: 'Ou Jun Wen', 张志光: 'Zhang Zhi Guang', 周智广: 'Zhou Zhi Guang', 李霞: 'Li Xia' } function extractDoctorName(expert) { const text = String(expert).trim() if (!text) return text const match = text.match(/([\u4e00-\u9fff]{2,4})$/) if (match) return match[1] const parts = text.split(/\s+/) return parts[parts.length - 1] || text } function deptEn(zh) { const key = String(zh || '').trim() return DEPT_EN[key] || key } function nameToEn(zhName) { if (NAME_EN_OVERRIDE[zhName]) return NAME_EN_OVERRIDE[zhName] return zhName } function expertLabelEn(deptZh, nameEn) { const dept = deptEn(deptZh) return dept ? `${dept} — ${nameEn}` : nameEn } function hospitalNameEn(zhName, excelEn, existingName) { return HOSPITAL_EN[zhName] || excelEn || existingName || zhName } function formatIntroZh(intro, cert) { return [intro, cert].filter(Boolean).join('\n\n') } function hospitalIntroEn(zhName, cert, existingIntro) { const intro = INTRO_EN[zhName] if (!intro) return existingIntro const certLine = cert ? CERT_EN[cert] || cert : '' return certLine ? `${intro}\n\n${certLine}` : intro } function findDoctorImage(hospitalId, zhName, fallback) { const dir = path.join(WEB, 'public', 'images', 'doctors') if (!fs.existsSync(dir)) return fallback || '/images/doctor.jpg' const prefix = `${hospitalId}-${zhName}` const match = fs.readdirSync(dir).find((file) => file.startsWith(prefix)) return match ? `/images/doctors/${match}` : fallback || '/images/doctor.jpg' } function findHospitalImage(hospitalId, fallback) { const dir = path.join(WEB, 'public', 'images', 'hospitals') if (!fs.existsSync(dir)) return fallback || '/images/doctor.jpg' const match = fs.readdirSync(dir).find((file) => file.startsWith(hospitalId)) return match ? `/images/hospitals/${match}` : fallback || '/images/doctor.jpg' } function normalizeExpertInfo(value) { if (!value) return undefined const text = String(value) .replace(/\uFEFF/g, '') .replace(/\r\n/g, '\n') .trim() return text || undefined } function findExcelFile() { const files = fs.readdirSync(DOCS_DIR).filter((name) => name.endsWith('.xlsx')) if (!files.length) throw new Error(`未找到 Excel:${DOCS_DIR}`) return path.join(DOCS_DIR, files.sort().at(-1)) } function parseExcel(filePath) { const wb = XLSX.readFile(filePath) const ws = wb.Sheets[wb.SheetNames[0]] const rows = XLSX.utils.sheet_to_json(ws, { header: 1, defval: null }) const hospitals = [] let current = null for (const row of rows.slice(1)) { const [hospital, dept, expert, hospitalEn, , expertInfo, intro, url, cert] = ( row ?? [] ) .concat(Array(9).fill(null)) .slice(0, 9) if (hospital) { if (current) hospitals.push(current) current = { name: String(hospital).trim(), hospitalEn: hospitalEn ? String(hospitalEn).trim() : '', intro: intro ? String(intro).trim() : '', url: url ? String(url).trim() : '', cert: cert ? String(cert).trim() : '', doctors: [] } } if (current && expert) { const expertText = String(expert).trim() const deptText = dept ? String(dept).trim() : '' current.doctors.push({ department: deptText, expert: expertText, name: extractDoctorName(expertText), expertInfo: normalizeExpertInfo(expertInfo) }) } } if (current) hospitals.push(current) return { rows, hospitals } } function loadTsArrays() { const content = fs.readFileSync(DATA_TS, 'utf8') const hospitalsMatch = content.match( /export const hospitals: HospitalRecord\[\] = (\[[\s\S]*?\])\r?\n\r?\nexport const doctors/ ) const doctorsMatch = content.match( /export const doctors: DoctorRecord\[\] = (\[[\s\S]*?\])\r?\n\r?\nexport function/ ) if (!hospitalsMatch || !doctorsMatch) { throw new Error('无法解析 hospitals-content.ts') } return { hospitals: JSON.parse(hospitalsMatch[1]), doctors: JSON.parse(doctorsMatch[1]) } } function tsString(value) { return JSON.stringify(value) } function writeRawJson(rows) { fs.writeFileSync(RAW_JSON, `${JSON.stringify(rows, null, 2)}\n`, 'utf8') } function writeHospitalsContent(hospitals, doctors) { const lines = [ '/** Auto-generated by web/scripts/build-hospitals-data.py — do not edit manually */', '/** synced from Excel via web/scripts/sync-hospitals-from-excel.mjs */', '', 'export type HospitalRecord = {', ' id: string', ' name: string', ' intro: string', ' url: string', ' image: string', '}', '', 'export type DoctorRecord = {', ' id: string', ' name: string', ' department: string', ' hospitalId: string', ' hospitalName: string', ' image: string', ' /** Full specialist title for future hover bio */', ' expertLabel: string', ' /** Specialist bio — shown when available */', ' expertInfo?: string', '}', '', `export const hospitals: HospitalRecord[] = ${JSON.stringify(hospitals, null, 2)}`, '', `export const doctors: DoctorRecord[] = ${JSON.stringify(doctors, null, 2)}`, '', 'export function getHospitalById(id: string): HospitalRecord | undefined {', ' return hospitals.find((h) => h.id === id)', '}', '', 'export function getDoctorsByHospitalId(hospitalId: string): DoctorRecord[] {', ' return doctors.filter((d) => d.hospitalId === hospitalId)', '}', '' ] fs.writeFileSync(DATA_TS, lines.join('\n'), 'utf8') } function writeDoctorsZh(doctorZh) { const lines = [ "import type { DoctorRecord } from '~/data/hospitals-content'", '', "export type DoctorZhFields = Pick", '', '/** 专家中文 — 来源:医院-科室-专家 Excel */', `export const doctorZh: Record = ${JSON.stringify(doctorZh, null, 2)}`, '' ] fs.writeFileSync(DOCTORS_ZH, lines.join('\n'), 'utf8') } function writeHospitalNamesZh(hospitalNames) { const lines = [ '/** 医院中文名称 — 来源:医院-科室-专家 Excel */', '', 'export const hospitalNameZh: Record = {' ] for (const hid of Object.keys(hospitalNames).sort()) { lines.push(` ${tsString(hid)}: ${tsString(hospitalNames[hid])},`) } lines.push('}', '') fs.writeFileSync(HOSPITAL_NAMES_ZH, lines.join('\n'), 'utf8') } function writeHospitalIntrosZh(hospitalIntros) { const lines = [ '/** 医院简介中文 — 来源:医院-科室-专家 Excel(含认证情况) */', '', 'export const hospitalIntroZh: Record = {' ] for (const hid of Object.keys(hospitalIntros).sort()) { lines.push(` ${tsString(hid)}: ${tsString(hospitalIntros[hid])},`) } lines.push('}', '') fs.writeFileSync(HOSPITAL_INTROS_ZH, lines.join('\n'), 'utf8') } function main() { const excelPath = findExcelFile() const expertInfoEn = JSON.parse(fs.readFileSync(EXPERT_INFO_EN, 'utf8')) const { rows, hospitals: excelHospitals } = parseExcel(excelPath) const { hospitals: oldHospitals, doctors: oldDoctors } = loadTsArrays() const excelByName = Object.fromEntries(excelHospitals.map((h) => [h.name, h])) const oldHospitalById = Object.fromEntries(oldHospitals.map((h) => [h.id, h])) const oldDoctorById = Object.fromEntries(oldDoctors.map((d) => [d.id, d])) const hospitalNamesZh = {} const hospitalIntrosZh = {} const hospitalsOut = [] const excelDoctorIds = [] const doctorZh = {} for (const [hospitalId, hospitalName] of Object.entries(FOLDER_TO_HOSPITAL)) { const excel = excelByName[hospitalName] if (!excel) { console.warn(`WARN: Excel 中缺少医院 ${hospitalName}`) continue } hospitalNamesZh[hospitalId] = hospitalName hospitalIntrosZh[hospitalId] = formatIntroZh(excel.intro, excel.cert) const existing = oldHospitalById[hospitalId] hospitalsOut.push({ id: hospitalId, name: hospitalNameEn(hospitalName, excel.hospitalEn, existing?.name), intro: hospitalIntroEn(hospitalName, excel.cert, existing?.intro), url: excel.url || existing?.url || '', image: findHospitalImage(hospitalId, existing?.image) }) for (const doc of excel.doctors) { const doctorId = `${hospitalId}-${doc.name}` excelDoctorIds.push(doctorId) const fields = { name: doc.name, department: doc.department, hospitalName, expertLabel: doc.expert } if (doc.expertInfo) fields.expertInfo = doc.expertInfo doctorZh[doctorId] = fields } } const orderedDoctorIds = [ ...oldDoctors.map((d) => d.id).filter((id) => excelDoctorIds.includes(id)), ...excelDoctorIds.filter((id) => !oldDoctors.some((d) => d.id === id)) ] const doctorsOut = orderedDoctorIds.map((doctorId) => { const doc = doctorZh[doctorId] const hospitalId = doctorId.slice(0, doctorId.lastIndexOf('-')) const hospital = hospitalsOut.find((h) => h.id === hospitalId) const existing = oldDoctorById[doctorId] const nameEn = existing?.name ?? nameToEn(doc.name) const infoZh = doc.expertInfo const record = { id: doctorId, name: nameEn, department: existing?.department ?? deptEn(doc.department), hospitalId, hospitalName: hospital?.name ?? '', image: findDoctorImage(hospitalId, doc.name, existing?.image), expertLabel: existing?.expertLabel ?? expertLabelEn(doc.department, nameEn) } if (infoZh) record.expertInfo = expertInfoEn[doctorId] ?? infoZh return record }) const removedDoctors = oldDoctors.filter((d) => !excelDoctorIds.includes(d.id)).map((d) => d.id) const addedDoctors = excelDoctorIds.filter((id) => !oldDoctorById[id]) writeRawJson(rows) writeHospitalsContent(hospitalsOut, doctorsOut) writeDoctorsZh(doctorZh) writeHospitalNamesZh(hospitalNamesZh) writeHospitalIntrosZh(hospitalIntrosZh) console.log(`Excel: ${path.basename(excelPath)}`) console.log(`Hospitals: ${hospitalsOut.length}, Doctors: ${doctorsOut.length}`) console.log(`Removed doctors: ${removedDoctors.length}${removedDoctors.length ? ` (${removedDoctors.join(', ')})` : ''}`) console.log(`Added doctors: ${addedDoctors.length}${addedDoctors.length ? ` (${addedDoctors.join(', ')})` : ''}`) console.log(`Expert bios: ${doctorsOut.filter((d) => d.expertInfo).length}`) } main()